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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba38f72c was ba38f72c, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Split device tree node into dev_node_t (device node) and fun_node_t (function node).

  • Property mode set to 100644
File size: 16.0 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 <inttypes.h>
39#include <assert.h>
40#include <ipc/services.h>
41#include <ipc/ns.h>
42#include <async.h>
43#include <stdio.h>
44#include <errno.h>
45#include <bool.h>
46#include <fibril_synch.h>
47#include <stdlib.h>
48#include <str.h>
49#include <dirent.h>
50#include <fcntl.h>
51#include <sys/stat.h>
52#include <ctype.h>
53#include <ipc/devman.h>
54#include <ipc/driver.h>
55#include <thread.h>
56#include <devmap.h>
57
58#include "devman.h"
59
60#define DRIVER_DEFAULT_STORE "/drv"
61
62static driver_list_t drivers_list;
63static dev_tree_t device_tree;
64static class_list_t class_list;
65
66/** Register running driver. */
67static driver_t *devman_driver_register(void)
68{
69 ipc_call_t icall;
70 ipc_callid_t iid;
71 driver_t *driver = NULL;
72
73 printf(NAME ": devman_driver_register \n");
74
75 iid = async_get_call(&icall);
76 if (IPC_GET_IMETHOD(icall) != DEVMAN_DRIVER_REGISTER) {
77 async_answer_0(iid, EREFUSED);
78 return NULL;
79 }
80
81 char *drv_name = NULL;
82
83 /* Get driver name. */
84 int rc = async_data_write_accept((void **) &drv_name, true, 0, 0, 0, 0);
85 if (rc != EOK) {
86 async_answer_0(iid, rc);
87 return NULL;
88 }
89
90 printf(NAME ": the %s driver is trying to register by the service.\n",
91 drv_name);
92
93 /* Find driver structure. */
94 driver = find_driver(&drivers_list, drv_name);
95
96 if (driver == NULL) {
97 printf(NAME ": no driver named %s was found.\n", drv_name);
98 free(drv_name);
99 drv_name = NULL;
100 async_answer_0(iid, ENOENT);
101 return NULL;
102 }
103
104 free(drv_name);
105 drv_name = NULL;
106
107 /* Create connection to the driver. */
108 printf(NAME ": creating connection to the %s driver.\n", driver->name);
109 ipc_call_t call;
110 ipc_callid_t callid = async_get_call(&call);
111 if (IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) {
112 async_answer_0(callid, ENOTSUP);
113 async_answer_0(iid, ENOTSUP);
114 return NULL;
115 }
116
117 /* Remember driver's phone. */
118 set_driver_phone(driver, IPC_GET_ARG5(call));
119
120 printf(NAME ": the %s driver was successfully registered as running.\n",
121 driver->name);
122
123 async_answer_0(callid, EOK);
124 async_answer_0(iid, EOK);
125
126 return driver;
127}
128
129/** Receive device match ID from the device's parent driver and add it to the
130 * list of devices match ids.
131 *
132 * @param match_ids The list of the device's match ids.
133 * @return Zero on success, negative error code otherwise.
134 */
135static int devman_receive_match_id(match_id_list_t *match_ids)
136{
137 match_id_t *match_id = create_match_id();
138 ipc_callid_t callid;
139 ipc_call_t call;
140 int rc = 0;
141
142 callid = async_get_call(&call);
143 if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
144 printf(NAME ": ERROR: devman_receive_match_id - invalid "
145 "protocol.\n");
146 async_answer_0(callid, EINVAL);
147 delete_match_id(match_id);
148 return EINVAL;
149 }
150
151 if (match_id == NULL) {
152 printf(NAME ": ERROR: devman_receive_match_id - failed to "
153 "allocate match id.\n");
154 async_answer_0(callid, ENOMEM);
155 return ENOMEM;
156 }
157
158 async_answer_0(callid, EOK);
159
160 match_id->score = IPC_GET_ARG1(call);
161
162 char *match_id_str;
163 rc = async_data_write_accept((void **) &match_id_str, true, 0, 0, 0, 0);
164 match_id->id = match_id_str;
165 if (rc != EOK) {
166 delete_match_id(match_id);
167 printf(NAME ": devman_receive_match_id - failed to receive "
168 "match id string.\n");
169 return rc;
170 }
171
172 list_append(&match_id->link, &match_ids->ids);
173
174 printf(NAME ": received match id '%s', score = %d \n",
175 match_id->id, match_id->score);
176 return rc;
177}
178
179/** Receive device match IDs from the device's parent driver and add them to the
180 * list of devices match ids.
181 *
182 * @param match_count The number of device's match ids to be received.
183 * @param match_ids The list of the device's match ids.
184 * @return Zero on success, negative error code otherwise.
185 */
186static int devman_receive_match_ids(sysarg_t match_count,
187 match_id_list_t *match_ids)
188{
189 int ret = EOK;
190 size_t i;
191
192 for (i = 0; i < match_count; i++) {
193 if (EOK != (ret = devman_receive_match_id(match_ids)))
194 return ret;
195 }
196 return ret;
197}
198
199static int assign_driver_fibril(void *arg)
200{
201 dev_node_t *dev_node = (dev_node_t *) arg;
202 assign_driver(dev_node, &drivers_list, &device_tree);
203 return EOK;
204}
205
206/** Handle child device registration.
207 *
208 * Child devices are registered by their parent's device driver.
209 */
210static void devman_add_child(ipc_callid_t callid, ipc_call_t *call)
211{
212 devman_handle_t parent_handle = IPC_GET_ARG1(*call);
213 sysarg_t match_count = IPC_GET_ARG2(*call);
214 dev_tree_t *tree = &device_tree;
215
216 fibril_rwlock_write_lock(&tree->rwlock);
217 dev_node_t *pdev = find_dev_node_no_lock(&device_tree, parent_handle);
218
219 if (pdev == NULL) {
220 fibril_rwlock_write_unlock(&tree->rwlock);
221 async_answer_0(callid, ENOENT);
222 return;
223 }
224
225 char *fun_name = NULL;
226 int rc = async_data_write_accept((void **)&fun_name, true, 0, 0, 0, 0);
227 if (rc != EOK) {
228 fibril_rwlock_write_unlock(&tree->rwlock);
229 async_answer_0(callid, rc);
230 return;
231 }
232
233 fun_node_t *fun = create_fun_node();
234 if (!insert_fun_node(&device_tree, fun, fun_name, pdev)) {
235 fibril_rwlock_write_unlock(&tree->rwlock);
236 delete_fun_node(fun);
237 async_answer_0(callid, ENOMEM);
238 return;
239 }
240
241 dev_node_t *dev;
242
243 dev = create_dev_node();
244 if (dev == NULL) {
245 fibril_rwlock_write_unlock(&tree->rwlock);
246 delete_fun_node(fun);
247 async_answer_0(callid, ENOMEM);
248 return;
249 }
250
251 insert_dev_node(tree, dev, fun);
252
253 fibril_rwlock_write_unlock(&tree->rwlock);
254
255 printf(NAME ": devman_add_child %s\n", fun->pathname);
256
257 devman_receive_match_ids(match_count, &fun->match_ids);
258
259 /*
260 * Try to find a suitable driver and assign it to the device. We do
261 * not want to block the current fibril that is used for processing
262 * incoming calls: we will launch a separate fibril to handle the
263 * driver assigning. That is because assign_driver can actually include
264 * task spawning which could take some time.
265 */
266 fid_t assign_fibril = fibril_create(assign_driver_fibril, dev);
267 if (assign_fibril == 0) {
268 /*
269 * Fallback in case we are out of memory.
270 * Probably not needed as we will die soon anyway ;-).
271 */
272 (void) assign_driver_fibril(fun);
273 } else {
274 fibril_add_ready(assign_fibril);
275 }
276
277 /* Return device handle to parent's driver. */
278 async_answer_1(callid, EOK, fun->handle);
279}
280
281static void devmap_register_class_dev(dev_class_info_t *cli)
282{
283 /* Create devmap path and name for the device. */
284 char *devmap_pathname = NULL;
285
286 asprintf(&devmap_pathname, "%s/%s%c%s", DEVMAP_CLASS_NAMESPACE,
287 cli->dev_class->name, DEVMAP_SEPARATOR, cli->dev_name);
288 if (devmap_pathname == NULL)
289 return;
290
291 /*
292 * Register the device by the device mapper and remember its devmap
293 * handle.
294 */
295 devmap_device_register_with_iface(devmap_pathname,
296 &cli->devmap_handle, DEVMAN_CONNECT_FROM_DEVMAP);
297
298 /*
299 * Add device to the hash map of class devices registered by device
300 * mapper.
301 */
302 class_add_devmap_function(&class_list, cli);
303
304 free(devmap_pathname);
305}
306
307static void devman_add_function_to_class(ipc_callid_t callid, ipc_call_t *call)
308{
309 devman_handle_t handle = IPC_GET_ARG1(*call);
310
311 /* Get class name. */
312 char *class_name;
313 int rc = async_data_write_accept((void **) &class_name, true,
314 0, 0, 0, 0);
315 if (rc != EOK) {
316 async_answer_0(callid, rc);
317 return;
318 }
319
320 fun_node_t *fun = find_fun_node(&device_tree, handle);
321 if (fun == NULL) {
322 async_answer_0(callid, ENOENT);
323 return;
324 }
325
326 dev_class_t *cl = get_dev_class(&class_list, class_name);
327 dev_class_info_t *class_info = add_function_to_class(fun, cl, NULL);
328
329 /* Register the device's class alias by devmapper. */
330 devmap_register_class_dev(class_info);
331
332 printf(NAME ": function'%s' added to class '%s', class name '%s' was "
333 "asigned to it\n", fun->pathname, class_name, class_info->dev_name);
334
335 async_answer_0(callid, EOK);
336}
337
338/** Initialize driver which has registered itself as running and ready.
339 *
340 * The initialization is done in a separate fibril to avoid deadlocks (if the
341 * driver needed to be served by devman during the driver's initialization).
342 */
343static int init_running_drv(void *drv)
344{
345 driver_t *driver = (driver_t *) drv;
346
347 initialize_running_driver(driver, &device_tree);
348 printf(NAME ": the %s driver was successfully initialized. \n",
349 driver->name);
350 return 0;
351}
352
353/** Function for handling connections from a driver to the device manager. */
354static void devman_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
355{
356 /* Accept the connection. */
357 async_answer_0(iid, EOK);
358
359 driver_t *driver = devman_driver_register();
360 if (driver == NULL)
361 return;
362
363 /*
364 * Initialize the driver as running (e.g. pass assigned devices to it)
365 * in a separate fibril; the separate fibril is used to enable the
366 * driver to use devman service during the driver's initialization.
367 */
368 fid_t fid = fibril_create(init_running_drv, driver);
369 if (fid == 0) {
370 printf(NAME ": Error creating fibril for the initialization of "
371 "the newly registered running driver.\n");
372 return;
373 }
374 fibril_add_ready(fid);
375
376 ipc_callid_t callid;
377 ipc_call_t call;
378 bool cont = true;
379 while (cont) {
380 callid = async_get_call(&call);
381
382 switch (IPC_GET_IMETHOD(call)) {
383 case IPC_M_PHONE_HUNGUP:
384 cont = false;
385 continue;
386 case DEVMAN_ADD_CHILD_DEVICE:
387 devman_add_child(callid, &call);
388 break;
389 case DEVMAN_ADD_DEVICE_TO_CLASS:
390 devman_add_function_to_class(callid, &call);
391 break;
392 default:
393 async_answer_0(callid, EINVAL);
394 break;
395 }
396 }
397}
398
399/** Find handle for the device instance identified by the device's path in the
400 * device tree. */
401static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
402{
403 char *pathname;
404
405 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
406 if (rc != EOK) {
407 async_answer_0(iid, rc);
408 return;
409 }
410
411 fun_node_t * fun = find_fun_node_by_path(&device_tree, pathname);
412
413 free(pathname);
414
415 if (fun == NULL) {
416 async_answer_0(iid, ENOENT);
417 return;
418 }
419
420 async_answer_1(iid, EOK, fun->handle);
421}
422
423
424/** Function for handling connections from a client to the device manager. */
425static void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
426{
427 /* Accept connection. */
428 async_answer_0(iid, EOK);
429
430 bool cont = true;
431 while (cont) {
432 ipc_call_t call;
433 ipc_callid_t callid = async_get_call(&call);
434
435 switch (IPC_GET_IMETHOD(call)) {
436 case IPC_M_PHONE_HUNGUP:
437 cont = false;
438 continue;
439 case DEVMAN_DEVICE_GET_HANDLE:
440 devman_function_get_handle(callid, &call);
441 break;
442 default:
443 async_answer_0(callid, ENOENT);
444 }
445 }
446}
447
448static void devman_forward(ipc_callid_t iid, ipc_call_t *icall,
449 bool drv_to_parent)
450{
451 devman_handle_t handle = IPC_GET_ARG2(*icall);
452
453 fun_node_t *fun = find_fun_node(&device_tree, handle);
454 if (fun == NULL) {
455 printf(NAME ": devman_forward error - no device function with "
456 "handle %" PRIun " was found.\n", handle);
457 async_answer_0(iid, ENOENT);
458 return;
459 }
460
461 driver_t *driver = NULL;
462
463 if (drv_to_parent) {
464 if (fun->dev->pfun != NULL)
465 driver = fun->dev->pfun->dev->drv;
466 } else if (fun->dev->state == DEVICE_USABLE) {
467 driver = fun->dev->drv;
468 assert(driver != NULL);
469 }
470
471 if (driver == NULL) {
472 printf(NAME ": devman_forward error - the device is not in %" PRIun
473 " usable state.\n", handle);
474 async_answer_0(iid, ENOENT);
475 return;
476 }
477
478 int method;
479 if (drv_to_parent)
480 method = DRIVER_DRIVER;
481 else
482 method = DRIVER_CLIENT;
483
484 if (driver->phone <= 0) {
485 printf(NAME ": devman_forward: cound not forward to driver %s ",
486 driver->name);
487 printf("the driver's phone is %" PRIun ").\n", driver->phone);
488 async_answer_0(iid, EINVAL);
489 return;
490 }
491
492 printf(NAME ": devman_forward: forward connection to function %s to "
493 "driver %s.\n", fun->pathname, driver->name);
494 async_forward_fast(iid, driver->phone, method, fun->handle, 0, IPC_FF_NONE);
495}
496
497/** Function for handling connections from a client forwarded by the device
498 * mapper to the device manager. */
499static void devman_connection_devmapper(ipc_callid_t iid, ipc_call_t *icall)
500{
501 devmap_handle_t devmap_handle = IPC_GET_ARG2(*icall);
502 fun_node_t *fun;
503 dev_node_t *dev;
504
505 fun = find_devmap_tree_function(&device_tree, devmap_handle);
506 if (fun == NULL)
507 fun = find_devmap_class_function(&class_list, devmap_handle);
508
509 if (fun == NULL || fun->dev->drv == NULL) {
510 async_answer_0(iid, ENOENT);
511 return;
512 }
513
514 dev = fun->dev;
515
516 if (dev->state != DEVICE_USABLE || dev->drv->phone <= 0) {
517 async_answer_0(iid, EINVAL);
518 return;
519 }
520
521 async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, dev->handle, 0,
522 IPC_FF_NONE);
523 printf(NAME ": devman_connection_devmapper: forwarded connection to "
524 "device %s to driver %s.\n", fun->pathname, dev->drv->name);
525}
526
527/** Function for handling connections to device manager. */
528static void devman_connection(ipc_callid_t iid, ipc_call_t *icall)
529{
530 /* Select interface. */
531 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
532 case DEVMAN_DRIVER:
533 devman_connection_driver(iid, icall);
534 break;
535 case DEVMAN_CLIENT:
536 devman_connection_client(iid, icall);
537 break;
538 case DEVMAN_CONNECT_TO_DEVICE:
539 /* Connect client to selected device. */
540 devman_forward(iid, icall, false);
541 break;
542 case DEVMAN_CONNECT_FROM_DEVMAP:
543 /* Someone connected through devmap node. */
544 devman_connection_devmapper(iid, icall);
545 break;
546 case DEVMAN_CONNECT_TO_PARENTS_DEVICE:
547 /* Connect client to selected device. */
548 devman_forward(iid, icall, true);
549 break;
550 default:
551 /* No such interface */
552 async_answer_0(iid, ENOENT);
553 }
554}
555
556/** Initialize device manager internal structures. */
557static bool devman_init(void)
558{
559 printf(NAME ": devman_init - looking for available drivers.\n");
560
561 /* Initialize list of available drivers. */
562 init_driver_list(&drivers_list);
563 if (lookup_available_drivers(&drivers_list,
564 DRIVER_DEFAULT_STORE) == 0) {
565 printf(NAME " no drivers found.");
566 return false;
567 }
568
569 printf(NAME ": devman_init - list of drivers has been initialized.\n");
570
571 /* Create root device node. */
572 if (!init_device_tree(&device_tree, &drivers_list)) {
573 printf(NAME " failed to initialize device tree.");
574 return false;
575 }
576
577 init_class_list(&class_list);
578
579 /*
580 * !!! devman_connection ... as the device manager is not a real devmap
581 * driver (it uses a completely different ipc protocol than an ordinary
582 * devmap driver) forwarding a connection from client to the devman by
583 * devmapper would not work.
584 */
585 devmap_driver_register(NAME, devman_connection);
586
587 return true;
588}
589
590int main(int argc, char *argv[])
591{
592 printf(NAME ": HelenOS Device Manager\n");
593
594 if (!devman_init()) {
595 printf(NAME ": Error while initializing service\n");
596 return -1;
597 }
598
599 /* Set a handler of incomming connections. */
600 async_set_client_connection(devman_connection);
601
602 /* Register device manager at naming service. */
603 if (service_register(SERVICE_DEVMAN) != EOK)
604 return -1;
605
606 printf(NAME ": Accepting connections\n");
607 async_manager();
608
609 /* Never reached. */
610 return 0;
611}
612
613/** @}
614 */
Note: See TracBrowser for help on using the repository browser.