source: mainline/uspace/srv/devman/client_conn.c@ 1db5669

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

devctl show-drv lists attached devices, add device count to list-drv.

  • Property mode set to 100644
File size: 16.0 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * Copyright (c) 2013 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @defgroup devman Device manager.
32 * @brief HelenOS device manager.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <inttypes.h>
40#include <assert.h>
41#include <ns.h>
42#include <async.h>
43#include <stdio.h>
44#include <errno.h>
45#include <str_error.h>
46#include <stdbool.h>
47#include <fibril_synch.h>
48#include <stdlib.h>
49#include <str.h>
50#include <ctype.h>
51#include <ipc/devman.h>
52
53#include "client_conn.h"
54#include "dev.h"
55#include "devman.h"
56#include "driver.h"
57#include "fun.h"
58#include "loc.h"
59#include "main.h"
60
61/** Find handle for the device instance identified by the device's path in the
62 * device tree. */
63static void devman_function_get_handle(ipc_callid_t iid, ipc_call_t *icall)
64{
65 char *pathname;
66 devman_handle_t handle;
67
68 int rc = async_data_write_accept((void **) &pathname, true, 0, 0, 0, 0);
69 if (rc != EOK) {
70 async_answer_0(iid, rc);
71 return;
72 }
73
74 fun_node_t *fun = find_fun_node_by_path(&device_tree, pathname);
75
76 free(pathname);
77
78 if (fun == NULL) {
79 async_answer_0(iid, ENOENT);
80 return;
81 }
82
83 fibril_rwlock_read_lock(&device_tree.rwlock);
84
85 /* Check function state */
86 if (fun->state == FUN_REMOVED) {
87 fibril_rwlock_read_unlock(&device_tree.rwlock);
88 async_answer_0(iid, ENOENT);
89 return;
90 }
91 handle = fun->handle;
92
93 fibril_rwlock_read_unlock(&device_tree.rwlock);
94
95 /* Delete reference created above by find_fun_node_by_path() */
96 fun_del_ref(fun);
97
98 async_answer_1(iid, EOK, handle);
99}
100
101/** Get device name. */
102static void devman_fun_get_name(ipc_callid_t iid, ipc_call_t *icall)
103{
104 devman_handle_t handle = IPC_GET_ARG1(*icall);
105
106 fun_node_t *fun = find_fun_node(&device_tree, handle);
107 if (fun == NULL) {
108 async_answer_0(iid, ENOMEM);
109 return;
110 }
111
112 ipc_callid_t data_callid;
113 size_t data_len;
114 if (!async_data_read_receive(&data_callid, &data_len)) {
115 async_answer_0(iid, EINVAL);
116 fun_del_ref(fun);
117 return;
118 }
119
120 void *buffer = malloc(data_len);
121 if (buffer == NULL) {
122 async_answer_0(data_callid, ENOMEM);
123 async_answer_0(iid, ENOMEM);
124 fun_del_ref(fun);
125 return;
126 }
127
128 fibril_rwlock_read_lock(&device_tree.rwlock);
129
130 /* Check function state */
131 if (fun->state == FUN_REMOVED) {
132 fibril_rwlock_read_unlock(&device_tree.rwlock);
133 free(buffer);
134
135 async_answer_0(data_callid, ENOENT);
136 async_answer_0(iid, ENOENT);
137 fun_del_ref(fun);
138 return;
139 }
140
141 size_t sent_length = str_size(fun->name);
142 if (sent_length > data_len) {
143 sent_length = data_len;
144 }
145
146 async_data_read_finalize(data_callid, fun->name, sent_length);
147 async_answer_0(iid, EOK);
148
149 fibril_rwlock_read_unlock(&device_tree.rwlock);
150 fun_del_ref(fun);
151 free(buffer);
152}
153
154/** Get function driver name. */
155static void devman_fun_get_driver_name(ipc_callid_t iid, ipc_call_t *icall)
156{
157 devman_handle_t handle = IPC_GET_ARG1(*icall);
158
159 fun_node_t *fun = find_fun_node(&device_tree, handle);
160 if (fun == NULL) {
161 async_answer_0(iid, ENOMEM);
162 return;
163 }
164
165 ipc_callid_t data_callid;
166 size_t data_len;
167 if (!async_data_read_receive(&data_callid, &data_len)) {
168 async_answer_0(iid, EINVAL);
169 fun_del_ref(fun);
170 return;
171 }
172
173 void *buffer = malloc(data_len);
174 if (buffer == NULL) {
175 async_answer_0(data_callid, ENOMEM);
176 async_answer_0(iid, ENOMEM);
177 fun_del_ref(fun);
178 return;
179 }
180
181 fibril_rwlock_read_lock(&device_tree.rwlock);
182
183 /* Check function state */
184 if (fun->state == FUN_REMOVED) {
185 fibril_rwlock_read_unlock(&device_tree.rwlock);
186 free(buffer);
187
188 async_answer_0(data_callid, ENOENT);
189 async_answer_0(iid, ENOENT);
190 fun_del_ref(fun);
191 return;
192 }
193
194 /* Check whether function has a driver */
195 if (fun->child == NULL || fun->child->drv == NULL) {
196 fibril_rwlock_read_unlock(&device_tree.rwlock);
197 free(buffer);
198
199 async_answer_0(data_callid, EINVAL);
200 async_answer_0(iid, EINVAL);
201 fun_del_ref(fun);
202 return;
203 }
204
205 size_t sent_length = str_size(fun->child->drv->name);
206 if (sent_length > data_len) {
207 sent_length = data_len;
208 }
209
210 async_data_read_finalize(data_callid, fun->child->drv->name,
211 sent_length);
212 async_answer_0(iid, EOK);
213
214 fibril_rwlock_read_unlock(&device_tree.rwlock);
215 fun_del_ref(fun);
216 free(buffer);
217}
218
219/** Get device path. */
220static void devman_fun_get_path(ipc_callid_t iid, ipc_call_t *icall)
221{
222 devman_handle_t handle = IPC_GET_ARG1(*icall);
223
224 fun_node_t *fun = find_fun_node(&device_tree, handle);
225 if (fun == NULL) {
226 async_answer_0(iid, ENOMEM);
227 return;
228 }
229
230 ipc_callid_t data_callid;
231 size_t data_len;
232 if (!async_data_read_receive(&data_callid, &data_len)) {
233 async_answer_0(iid, EINVAL);
234 fun_del_ref(fun);
235 return;
236 }
237
238 void *buffer = malloc(data_len);
239 if (buffer == NULL) {
240 async_answer_0(data_callid, ENOMEM);
241 async_answer_0(iid, ENOMEM);
242 fun_del_ref(fun);
243 return;
244 }
245
246 fibril_rwlock_read_lock(&device_tree.rwlock);
247
248 /* Check function state */
249 if (fun->state == FUN_REMOVED) {
250 fibril_rwlock_read_unlock(&device_tree.rwlock);
251 free(buffer);
252
253 async_answer_0(data_callid, ENOENT);
254 async_answer_0(iid, ENOENT);
255 fun_del_ref(fun);
256 return;
257 }
258
259 size_t sent_length = str_size(fun->pathname);
260 if (sent_length > data_len) {
261 sent_length = data_len;
262 }
263
264 async_data_read_finalize(data_callid, fun->pathname, sent_length);
265 async_answer_0(iid, EOK);
266
267 fibril_rwlock_read_unlock(&device_tree.rwlock);
268 fun_del_ref(fun);
269 free(buffer);
270}
271
272/** Get handle for parent function of a device. */
273static void devman_dev_get_parent(ipc_callid_t iid, ipc_call_t *icall)
274{
275 dev_node_t *dev;
276
277 fibril_rwlock_read_lock(&device_tree.rwlock);
278
279 dev = find_dev_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
280 if (dev == NULL || dev->state == DEVICE_REMOVED) {
281 fibril_rwlock_read_unlock(&device_tree.rwlock);
282 async_answer_0(iid, ENOENT);
283 return;
284 }
285
286 if (dev->pfun == NULL) {
287 fibril_rwlock_read_unlock(&device_tree.rwlock);
288 async_answer_0(iid, ENOENT);
289 return;
290 }
291
292 async_answer_1(iid, EOK, dev->pfun->handle);
293
294 fibril_rwlock_read_unlock(&device_tree.rwlock);
295}
296
297static void devman_dev_get_functions(ipc_callid_t iid, ipc_call_t *icall)
298{
299 ipc_callid_t callid;
300 size_t size;
301 size_t act_size;
302 int rc;
303
304 if (!async_data_read_receive(&callid, &size)) {
305 async_answer_0(callid, EREFUSED);
306 async_answer_0(iid, EREFUSED);
307 return;
308 }
309
310 fibril_rwlock_read_lock(&device_tree.rwlock);
311
312 dev_node_t *dev = find_dev_node_no_lock(&device_tree,
313 IPC_GET_ARG1(*icall));
314 if (dev == NULL || dev->state == DEVICE_REMOVED) {
315 fibril_rwlock_read_unlock(&device_tree.rwlock);
316 async_answer_0(callid, ENOENT);
317 async_answer_0(iid, ENOENT);
318 return;
319 }
320
321 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
322 if (hdl_buf == NULL) {
323 fibril_rwlock_read_unlock(&device_tree.rwlock);
324 async_answer_0(callid, ENOMEM);
325 async_answer_0(iid, ENOMEM);
326 return;
327 }
328
329 rc = dev_get_functions(&device_tree, dev, hdl_buf, size, &act_size);
330 if (rc != EOK) {
331 fibril_rwlock_read_unlock(&device_tree.rwlock);
332 async_answer_0(callid, rc);
333 async_answer_0(iid, rc);
334 return;
335 }
336
337 fibril_rwlock_read_unlock(&device_tree.rwlock);
338
339 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
340 free(hdl_buf);
341
342 async_answer_1(iid, retval, act_size);
343}
344
345/** Get handle for child device of a function. */
346static void devman_fun_get_child(ipc_callid_t iid, ipc_call_t *icall)
347{
348 fun_node_t *fun;
349
350 fibril_rwlock_read_lock(&device_tree.rwlock);
351
352 fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
353 if (fun == NULL || fun->state == FUN_REMOVED) {
354 fibril_rwlock_read_unlock(&device_tree.rwlock);
355 async_answer_0(iid, ENOENT);
356 return;
357 }
358
359 if (fun->child == NULL) {
360 fibril_rwlock_read_unlock(&device_tree.rwlock);
361 async_answer_0(iid, ENOENT);
362 return;
363 }
364
365 async_answer_1(iid, EOK, fun->child->handle);
366
367 fibril_rwlock_read_unlock(&device_tree.rwlock);
368}
369
370/** Online function.
371 *
372 * Send a request to online a function to the responsible driver.
373 * The driver may offline other functions if necessary (i.e. if the state
374 * of this function is linked to state of another function somehow).
375 */
376static void devman_fun_online(ipc_callid_t iid, ipc_call_t *icall)
377{
378 fun_node_t *fun;
379 int rc;
380
381 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
382 if (fun == NULL) {
383 async_answer_0(iid, ENOENT);
384 return;
385 }
386
387 rc = driver_fun_online(&device_tree, fun);
388 fun_del_ref(fun);
389
390 async_answer_0(iid, (sysarg_t) rc);
391}
392
393/** Offline function.
394 *
395 * Send a request to offline a function to the responsible driver. As
396 * a result the subtree rooted at that function should be cleanly
397 * detatched. The driver may offline other functions if necessary
398 * (i.e. if the state of this function is linked to state of another
399 * function somehow).
400 */
401static void devman_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
402{
403 fun_node_t *fun;
404 int rc;
405
406 fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
407 if (fun == NULL) {
408 async_answer_0(iid, ENOENT);
409 return;
410 }
411
412 rc = driver_fun_offline(&device_tree, fun);
413 fun_del_ref(fun);
414
415 async_answer_0(iid, (sysarg_t) rc);
416}
417
418/** Find handle for the function instance identified by its service ID. */
419static void devman_fun_sid_to_handle(ipc_callid_t iid, ipc_call_t *icall)
420{
421 fun_node_t *fun;
422
423 fun = find_loc_tree_function(&device_tree, IPC_GET_ARG1(*icall));
424
425 if (fun == NULL) {
426 async_answer_0(iid, ENOENT);
427 return;
428 }
429
430 fibril_rwlock_read_lock(&device_tree.rwlock);
431
432 /* Check function state */
433 if (fun->state == FUN_REMOVED) {
434 fibril_rwlock_read_unlock(&device_tree.rwlock);
435 async_answer_0(iid, ENOENT);
436 return;
437 }
438
439 async_answer_1(iid, EOK, fun->handle);
440 fibril_rwlock_read_unlock(&device_tree.rwlock);
441 fun_del_ref(fun);
442}
443
444/** Get list of all registered drivers. */
445static void devman_get_drivers(ipc_callid_t iid, ipc_call_t *icall)
446{
447 ipc_callid_t callid;
448 size_t size;
449 size_t act_size;
450 int rc;
451
452 if (!async_data_read_receive(&callid, &size)) {
453 async_answer_0(iid, EREFUSED);
454 return;
455 }
456
457 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
458 if (hdl_buf == NULL) {
459 async_answer_0(callid, ENOMEM);
460 async_answer_0(iid, ENOMEM);
461 return;
462 }
463
464 rc = driver_get_list(&drivers_list, hdl_buf, size, &act_size);
465 if (rc != EOK) {
466 async_answer_0(callid, rc);
467 async_answer_0(iid, rc);
468 return;
469 }
470
471 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
472 free(hdl_buf);
473
474 async_answer_1(iid, retval, act_size);
475}
476
477static void devman_driver_get_devices(ipc_callid_t iid, ipc_call_t *icall)
478{
479 ipc_callid_t callid;
480 size_t size;
481 size_t act_size;
482 int rc;
483
484 if (!async_data_read_receive(&callid, &size)) {
485 async_answer_0(iid, EREFUSED);
486 return;
487 }
488
489 driver_t *drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
490 if (drv == NULL) {
491 async_answer_0(callid, ENOENT);
492 async_answer_0(iid, ENOENT);
493 return;
494 }
495
496 devman_handle_t *hdl_buf = (devman_handle_t *) malloc(size);
497 if (hdl_buf == NULL) {
498 async_answer_0(callid, ENOMEM);
499 async_answer_0(iid, ENOMEM);
500 return;
501 }
502
503 rc = driver_get_devices(drv, hdl_buf, size, &act_size);
504 if (rc != EOK) {
505 fibril_rwlock_read_unlock(&device_tree.rwlock);
506 async_answer_0(callid, rc);
507 async_answer_0(iid, rc);
508 return;
509 }
510
511 sysarg_t retval = async_data_read_finalize(callid, hdl_buf, size);
512 free(hdl_buf);
513
514 async_answer_1(iid, retval, act_size);
515}
516
517
518/** Find driver by name. */
519static void devman_driver_get_handle(ipc_callid_t iid, ipc_call_t *icall)
520{
521 char *drvname;
522
523 int rc = async_data_write_accept((void **) &drvname, true, 0, 0, 0, 0);
524 if (rc != EOK) {
525 async_answer_0(iid, rc);
526 return;
527 }
528
529 driver_t *driver = driver_find_by_name(&drivers_list, drvname);
530
531 free(drvname);
532
533 if (driver == NULL) {
534 async_answer_0(iid, ENOENT);
535 return;
536 }
537
538 async_answer_1(iid, EOK, driver->handle);
539}
540
541/** Get driver name. */
542static void devman_driver_get_name(ipc_callid_t iid, ipc_call_t *icall)
543{
544 devman_handle_t handle = IPC_GET_ARG1(*icall);
545
546 driver_t *drv = driver_find(&drivers_list, handle);
547 if (drv == NULL) {
548 async_answer_0(iid, ENOMEM);
549 return;
550 }
551
552 ipc_callid_t data_callid;
553 size_t data_len;
554 if (!async_data_read_receive(&data_callid, &data_len)) {
555 async_answer_0(iid, EINVAL);
556 return;
557 }
558
559 void *buffer = malloc(data_len);
560 if (buffer == NULL) {
561 async_answer_0(data_callid, ENOMEM);
562 async_answer_0(iid, ENOMEM);
563 return;
564 }
565
566 fibril_mutex_lock(&drv->driver_mutex);
567
568 size_t sent_length = str_size(drv->name);
569 if (sent_length > data_len) {
570 sent_length = data_len;
571 }
572
573 async_data_read_finalize(data_callid, drv->name, sent_length);
574 async_answer_0(iid, EOK);
575
576 fibril_mutex_unlock(&drv->driver_mutex);
577
578 free(buffer);
579}
580
581/** Get driver state. */
582static void devman_driver_get_state(ipc_callid_t iid, ipc_call_t *icall)
583{
584 driver_t *drv;
585
586 drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
587 if (drv == NULL) {
588 async_answer_0(iid, ENOENT);
589 return;
590 }
591
592 async_answer_1(iid, EOK, (sysarg_t) drv->state);
593}
594
595/** Forcibly load a driver. */
596static void devman_driver_load(ipc_callid_t iid, ipc_call_t *icall)
597{
598 driver_t *drv;
599 int rc;
600
601 drv = driver_find(&drivers_list, IPC_GET_ARG1(*icall));
602 if (drv == NULL) {
603 async_answer_0(iid, ENOENT);
604 return;
605 }
606
607 fibril_mutex_lock(&drv->driver_mutex);
608 rc = start_driver(drv) ? EOK : EIO;
609 fibril_mutex_unlock(&drv->driver_mutex);
610
611 async_answer_0(iid, rc);
612}
613
614/** Function for handling connections from a client to the device manager. */
615void devman_connection_client(ipc_callid_t iid, ipc_call_t *icall)
616{
617 /* Accept connection. */
618 async_answer_0(iid, EOK);
619
620 while (true) {
621 ipc_call_t call;
622 ipc_callid_t callid = async_get_call(&call);
623
624 if (!IPC_GET_IMETHOD(call))
625 break;
626
627 switch (IPC_GET_IMETHOD(call)) {
628 case DEVMAN_DEVICE_GET_HANDLE:
629 devman_function_get_handle(callid, &call);
630 break;
631 case DEVMAN_DEV_GET_PARENT:
632 devman_dev_get_parent(callid, &call);
633 break;
634 case DEVMAN_DEV_GET_FUNCTIONS:
635 devman_dev_get_functions(callid, &call);
636 break;
637 case DEVMAN_FUN_GET_CHILD:
638 devman_fun_get_child(callid, &call);
639 break;
640 case DEVMAN_FUN_GET_NAME:
641 devman_fun_get_name(callid, &call);
642 break;
643 case DEVMAN_FUN_GET_DRIVER_NAME:
644 devman_fun_get_driver_name(callid, &call);
645 break;
646 case DEVMAN_FUN_GET_PATH:
647 devman_fun_get_path(callid, &call);
648 break;
649 case DEVMAN_FUN_ONLINE:
650 devman_fun_online(callid, &call);
651 break;
652 case DEVMAN_FUN_OFFLINE:
653 devman_fun_offline(callid, &call);
654 break;
655 case DEVMAN_FUN_SID_TO_HANDLE:
656 devman_fun_sid_to_handle(callid, &call);
657 break;
658 case DEVMAN_GET_DRIVERS:
659 devman_get_drivers(callid, &call);
660 break;
661 case DEVMAN_DRIVER_GET_DEVICES:
662 devman_driver_get_devices(callid, &call);
663 break;
664 case DEVMAN_DRIVER_GET_HANDLE:
665 devman_driver_get_handle(callid, &call);
666 break;
667 case DEVMAN_DRIVER_GET_NAME:
668 devman_driver_get_name(callid, &call);
669 break;
670 case DEVMAN_DRIVER_GET_STATE:
671 devman_driver_get_state(callid, &call);
672 break;
673 case DEVMAN_DRIVER_LOAD:
674 devman_driver_load(callid, &call);
675 break;
676 default:
677 async_answer_0(callid, ENOENT);
678 }
679 }
680}
681
682
683/** @}
684 */
Note: See TracBrowser for help on using the repository browser.