source: mainline/uspace/lib/c/generic/devman.c@ 9696b01

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

Printing match IDs of drivers and functions.

  • Property mode set to 100644
File size: 17.2 KB
Line 
1/*
2 * Copyright (c) 2007 Josef Cejka
3 * Copyright (c) 2013 Jiri Svoboda
4 * Copyright (c) 2010 Lenka Trochtova
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup libc
32 * @{
33 */
34/** @file
35 */
36
37#include <adt/list.h>
38#include <str.h>
39#include <ipc/services.h>
40#include <ns.h>
41#include <ipc/devman.h>
42#include <devman.h>
43#include <fibril_synch.h>
44#include <async.h>
45#include <errno.h>
46#include <malloc.h>
47#include <stdbool.h>
48
49static FIBRIL_MUTEX_INITIALIZE(devman_driver_block_mutex);
50static FIBRIL_MUTEX_INITIALIZE(devman_client_block_mutex);
51
52static FIBRIL_MUTEX_INITIALIZE(devman_driver_mutex);
53static FIBRIL_MUTEX_INITIALIZE(devman_client_mutex);
54
55static async_sess_t *devman_driver_block_sess = NULL;
56static async_sess_t *devman_client_block_sess = NULL;
57
58static async_sess_t *devman_driver_sess = NULL;
59static async_sess_t *devman_client_sess = NULL;
60
61static void clone_session(fibril_mutex_t *mtx, async_sess_t *src,
62 async_sess_t **dst)
63{
64 fibril_mutex_lock(mtx);
65
66 if ((*dst == NULL) && (src != NULL))
67 *dst = src;
68
69 fibril_mutex_unlock(mtx);
70}
71
72/** Start an async exchange on the devman session (blocking).
73 *
74 * @param iface Device manager interface to choose
75 *
76 * @return New exchange.
77 *
78 */
79async_exch_t *devman_exchange_begin_blocking(devman_interface_t iface)
80{
81 switch (iface) {
82 case DEVMAN_DRIVER:
83 fibril_mutex_lock(&devman_driver_block_mutex);
84
85 while (devman_driver_block_sess == NULL) {
86 clone_session(&devman_driver_mutex, devman_driver_sess,
87 &devman_driver_block_sess);
88
89 if (devman_driver_block_sess == NULL)
90 devman_driver_block_sess =
91 service_connect_blocking(EXCHANGE_PARALLEL,
92 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
93 }
94
95 fibril_mutex_unlock(&devman_driver_block_mutex);
96
97 clone_session(&devman_driver_mutex, devman_driver_block_sess,
98 &devman_driver_sess);
99
100 return async_exchange_begin(devman_driver_block_sess);
101 case DEVMAN_CLIENT:
102 fibril_mutex_lock(&devman_client_block_mutex);
103
104 while (devman_client_block_sess == NULL) {
105 clone_session(&devman_client_mutex, devman_client_sess,
106 &devman_client_block_sess);
107
108 if (devman_client_block_sess == NULL)
109 devman_client_block_sess =
110 service_connect_blocking(EXCHANGE_SERIALIZE,
111 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
112 }
113
114 fibril_mutex_unlock(&devman_client_block_mutex);
115
116 clone_session(&devman_client_mutex, devman_client_block_sess,
117 &devman_client_sess);
118
119 return async_exchange_begin(devman_client_block_sess);
120 default:
121 return NULL;
122 }
123}
124
125/** Start an async exchange on the devman session.
126 *
127 * @param iface Device manager interface to choose
128 *
129 * @return New exchange.
130 *
131 */
132async_exch_t *devman_exchange_begin(devman_interface_t iface)
133{
134 switch (iface) {
135 case DEVMAN_DRIVER:
136 fibril_mutex_lock(&devman_driver_mutex);
137
138 if (devman_driver_sess == NULL)
139 devman_driver_sess =
140 service_connect(EXCHANGE_PARALLEL, SERVICE_DEVMAN,
141 DEVMAN_DRIVER, 0);
142
143 fibril_mutex_unlock(&devman_driver_mutex);
144
145 if (devman_driver_sess == NULL)
146 return NULL;
147
148 return async_exchange_begin(devman_driver_sess);
149 case DEVMAN_CLIENT:
150 fibril_mutex_lock(&devman_client_mutex);
151
152 if (devman_client_sess == NULL)
153 devman_client_sess =
154 service_connect(EXCHANGE_SERIALIZE, SERVICE_DEVMAN,
155 DEVMAN_CLIENT, 0);
156
157 fibril_mutex_unlock(&devman_client_mutex);
158
159 if (devman_client_sess == NULL)
160 return NULL;
161
162 return async_exchange_begin(devman_client_sess);
163 default:
164 return NULL;
165 }
166}
167
168/** Finish an async exchange on the devman session.
169 *
170 * @param exch Exchange to be finished.
171 *
172 */
173void devman_exchange_end(async_exch_t *exch)
174{
175 async_exchange_end(exch);
176}
177
178/** Register running driver with device manager. */
179int devman_driver_register(const char *name)
180{
181 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
182
183 ipc_call_t answer;
184 aid_t req = async_send_2(exch, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
185 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
186
187 devman_exchange_end(exch);
188
189 if (retval != EOK) {
190 async_forget(req);
191 return retval;
192 }
193
194 exch = devman_exchange_begin(DEVMAN_DRIVER);
195 async_connect_to_me(exch, 0, 0, 0, NULL, NULL);
196 devman_exchange_end(exch);
197
198 async_wait_for(req, &retval);
199 return retval;
200}
201
202/** Add function to a device.
203 *
204 * Request devman to add a new function to the specified device owned by
205 * this driver task.
206 *
207 * @param name Name of the new function
208 * @param ftype Function type, fun_inner or fun_exposed
209 * @param match_ids Match IDs (should be empty for fun_exposed)
210 * @param devh Devman handle of the device
211 * @param funh Place to store handle of the new function
212 *
213 * @return EOK on success or negative error code.
214 *
215 */
216int devman_add_function(const char *name, fun_type_t ftype,
217 match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
218{
219 int match_count = list_count(&match_ids->ids);
220 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
221
222 ipc_call_t answer;
223 aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
224 devh, match_count, &answer);
225 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
226 if (retval != EOK) {
227 devman_exchange_end(exch);
228 async_forget(req);
229 return retval;
230 }
231
232 list_foreach(match_ids->ids, link, match_id_t, match_id) {
233 ipc_call_t answer2;
234 aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
235 match_id->score, &answer2);
236 retval = async_data_write_start(exch, match_id->id,
237 str_size(match_id->id));
238 if (retval != EOK) {
239 devman_exchange_end(exch);
240 async_forget(req2);
241 async_forget(req);
242 return retval;
243 }
244
245 async_wait_for(req2, &retval);
246 if (retval != EOK) {
247 devman_exchange_end(exch);
248 async_forget(req);
249 return retval;
250 }
251 }
252
253 devman_exchange_end(exch);
254
255 async_wait_for(req, &retval);
256 if (retval == EOK) {
257 if (funh != NULL)
258 *funh = (int) IPC_GET_ARG1(answer);
259 } else {
260 if (funh != NULL)
261 *funh = -1;
262 }
263
264 return retval;
265}
266
267int devman_add_device_to_category(devman_handle_t devman_handle,
268 const char *cat_name)
269{
270 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
271
272 ipc_call_t answer;
273 aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CATEGORY,
274 devman_handle, &answer);
275 sysarg_t retval = async_data_write_start(exch, cat_name,
276 str_size(cat_name));
277
278 devman_exchange_end(exch);
279
280 if (retval != EOK) {
281 async_forget(req);
282 return retval;
283 }
284
285 async_wait_for(req, &retval);
286 return retval;
287}
288
289async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
290 unsigned int flags)
291{
292 async_sess_t *sess;
293
294 if (flags & IPC_FLAG_BLOCKING)
295 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
296 DEVMAN_CONNECT_TO_DEVICE, handle);
297 else
298 sess = service_connect(mgmt, SERVICE_DEVMAN,
299 DEVMAN_CONNECT_TO_DEVICE, handle);
300
301 return sess;
302}
303
304/** Remove function from device.
305 *
306 * Request devman to remove function owned by this driver task.
307 * @param funh Devman handle of the function
308 *
309 * @return EOK on success or negative error code.
310 */
311int devman_remove_function(devman_handle_t funh)
312{
313 async_exch_t *exch;
314 sysarg_t retval;
315
316 exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
317 retval = async_req_1_0(exch, DEVMAN_REMOVE_FUNCTION, (sysarg_t) funh);
318 devman_exchange_end(exch);
319
320 return (int) retval;
321}
322
323int devman_drv_fun_online(devman_handle_t funh)
324{
325 async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
326 if (exch == NULL)
327 return ENOMEM;
328
329 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_ONLINE, funh);
330
331 devman_exchange_end(exch);
332 return (int) retval;
333}
334
335int devman_drv_fun_offline(devman_handle_t funh)
336{
337 async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
338 if (exch == NULL)
339 return ENOMEM;
340
341 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_OFFLINE, funh);
342
343 devman_exchange_end(exch);
344 return (int) retval;
345}
346
347async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
348 devman_handle_t handle, unsigned int flags)
349{
350 async_sess_t *sess;
351
352 if (flags & IPC_FLAG_BLOCKING)
353 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
354 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
355 else
356 sess = service_connect(mgmt, SERVICE_DEVMAN,
357 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
358
359 return sess;
360}
361
362int devman_fun_get_handle(const char *pathname, devman_handle_t *handle,
363 unsigned int flags)
364{
365 async_exch_t *exch;
366
367 if (flags & IPC_FLAG_BLOCKING)
368 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
369 else {
370 exch = devman_exchange_begin(DEVMAN_CLIENT);
371 if (exch == NULL)
372 return ENOMEM;
373 }
374
375 ipc_call_t answer;
376 aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
377 &answer);
378 sysarg_t retval = async_data_write_start(exch, pathname,
379 str_size(pathname));
380
381 devman_exchange_end(exch);
382
383 if (retval != EOK) {
384 async_forget(req);
385 return retval;
386 }
387
388 async_wait_for(req, &retval);
389
390 if (retval != EOK) {
391 if (handle != NULL)
392 *handle = (devman_handle_t) -1;
393
394 return retval;
395 }
396
397 if (handle != NULL)
398 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
399
400 return retval;
401}
402
403static int devman_get_str_internal(sysarg_t method, sysarg_t arg1,
404 sysarg_t arg2, sysarg_t *r1, char *buf, size_t buf_size)
405{
406 async_exch_t *exch;
407 ipc_call_t dreply;
408 size_t act_size;
409 sysarg_t dretval;
410
411 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
412
413 ipc_call_t answer;
414 aid_t req = async_send_2(exch, method, arg1, arg2, &answer);
415 aid_t dreq = async_data_read(exch, buf, buf_size - 1, &dreply);
416 async_wait_for(dreq, &dretval);
417
418 devman_exchange_end(exch);
419
420 if (dretval != EOK) {
421 async_forget(req);
422 return dretval;
423 }
424
425 sysarg_t retval;
426 async_wait_for(req, &retval);
427
428 if (retval != EOK) {
429 return retval;
430 }
431
432 if (r1 != NULL)
433 *r1 = IPC_GET_ARG1(answer);
434 act_size = IPC_GET_ARG2(dreply);
435 assert(act_size <= buf_size - 1);
436 buf[act_size] = '\0';
437
438 return EOK;
439}
440
441int devman_fun_get_path(devman_handle_t handle, char *buf, size_t buf_size)
442{
443 return devman_get_str_internal(DEVMAN_FUN_GET_PATH, handle, 0, NULL,
444 buf, buf_size);
445}
446
447int devman_fun_get_match_id(devman_handle_t handle, size_t index, char *buf,
448 size_t buf_size, unsigned int *rscore)
449{
450 int rc;
451 sysarg_t score = 0;
452
453 rc = devman_get_str_internal(DEVMAN_FUN_GET_MATCH_ID, handle, index,
454 &score, buf, buf_size);
455 if (rc != EOK)
456 return rc;
457
458 *rscore = score;
459 return rc;
460}
461
462int devman_fun_get_name(devman_handle_t handle, char *buf, size_t buf_size)
463{
464 return devman_get_str_internal(DEVMAN_FUN_GET_NAME, handle, 0, NULL,
465 buf, buf_size);
466}
467
468int devman_fun_get_driver_name(devman_handle_t handle, char *buf, size_t buf_size)
469{
470 return devman_get_str_internal(DEVMAN_FUN_GET_DRIVER_NAME, handle, 0,
471 NULL, buf, buf_size);
472}
473
474int devman_fun_online(devman_handle_t funh)
475{
476 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
477 if (exch == NULL)
478 return ENOMEM;
479
480 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_ONLINE, funh);
481
482 devman_exchange_end(exch);
483 return (int) retval;
484}
485
486int devman_fun_offline(devman_handle_t funh)
487{
488 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
489 if (exch == NULL)
490 return ENOMEM;
491
492 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_OFFLINE, funh);
493
494 devman_exchange_end(exch);
495 return (int) retval;
496}
497
498static int devman_get_handles_once(sysarg_t method, sysarg_t arg1,
499 devman_handle_t *handle_buf, size_t buf_size, size_t *act_size)
500{
501 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
502
503 ipc_call_t answer;
504 aid_t req = async_send_1(exch, method, arg1, &answer);
505 int rc = async_data_read_start(exch, handle_buf, buf_size);
506
507 devman_exchange_end(exch);
508
509 if (rc != EOK) {
510 async_forget(req);
511 return rc;
512 }
513
514 sysarg_t retval;
515 async_wait_for(req, &retval);
516
517 if (retval != EOK) {
518 return retval;
519 }
520
521 *act_size = IPC_GET_ARG1(answer);
522 return EOK;
523}
524
525/** Get list of handles.
526 *
527 * Returns an allocated array of handles.
528 *
529 * @param method IPC method
530 * @param arg1 IPC argument 1
531 * @param data Place to store pointer to array of handles
532 * @param count Place to store number of handles
533 * @return EOK on success or negative error code
534 */
535static int devman_get_handles_internal(sysarg_t method, sysarg_t arg1,
536 devman_handle_t **data, size_t *count)
537{
538 devman_handle_t *handles;
539 size_t act_size;
540 size_t alloc_size;
541 int rc;
542
543 *data = NULL;
544 act_size = 0; /* silence warning */
545
546 rc = devman_get_handles_once(method, arg1, NULL, 0,
547 &act_size);
548 if (rc != EOK)
549 return rc;
550
551 alloc_size = act_size;
552 handles = malloc(alloc_size);
553 if (handles == NULL)
554 return ENOMEM;
555
556 while (true) {
557 rc = devman_get_handles_once(method, arg1, handles, alloc_size,
558 &act_size);
559 if (rc != EOK)
560 return rc;
561
562 if (act_size <= alloc_size)
563 break;
564
565 alloc_size *= 2;
566 free(handles);
567
568 handles = malloc(alloc_size);
569 if (handles == NULL)
570 return ENOMEM;
571 }
572
573 *count = act_size / sizeof(devman_handle_t);
574 *data = handles;
575 return EOK;
576}
577
578int devman_fun_get_child(devman_handle_t funh, devman_handle_t *devh)
579{
580 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
581 if (exch == NULL)
582 return ENOMEM;
583
584 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_GET_CHILD,
585 funh, devh);
586
587 devman_exchange_end(exch);
588 return (int) retval;
589}
590
591int devman_dev_get_functions(devman_handle_t devh, devman_handle_t **funcs,
592 size_t *count)
593{
594 return devman_get_handles_internal(DEVMAN_DEV_GET_FUNCTIONS,
595 devh, funcs, count);
596}
597
598int devman_dev_get_parent(devman_handle_t devh, devman_handle_t *funh)
599{
600 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
601 if (exch == NULL)
602 return ENOMEM;
603
604 sysarg_t retval = async_req_1_1(exch, DEVMAN_DEV_GET_PARENT,
605 devh, funh);
606
607 devman_exchange_end(exch);
608 return (int) retval;
609}
610
611int devman_fun_sid_to_handle(service_id_t sid, devman_handle_t *handle)
612{
613 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
614 if (exch == NULL)
615 return ENOMEM;
616
617 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_SID_TO_HANDLE,
618 sid, handle);
619
620 devman_exchange_end(exch);
621 return (int) retval;
622}
623
624int devman_get_drivers(devman_handle_t **drvs,
625 size_t *count)
626{
627 return devman_get_handles_internal(DEVMAN_GET_DRIVERS, 0, drvs, count);
628}
629
630int devman_driver_get_devices(devman_handle_t drvh, devman_handle_t **devs,
631 size_t *count)
632{
633 return devman_get_handles_internal(DEVMAN_DRIVER_GET_DEVICES,
634 drvh, devs, count);
635}
636
637int devman_driver_get_handle(const char *drvname, devman_handle_t *handle)
638{
639 async_exch_t *exch;
640
641 exch = devman_exchange_begin(DEVMAN_CLIENT);
642 if (exch == NULL)
643 return ENOMEM;
644
645 ipc_call_t answer;
646 aid_t req = async_send_0(exch, DEVMAN_DRIVER_GET_HANDLE, &answer);
647 sysarg_t retval = async_data_write_start(exch, drvname,
648 str_size(drvname));
649
650 devman_exchange_end(exch);
651
652 if (retval != EOK) {
653 async_forget(req);
654 return retval;
655 }
656
657 async_wait_for(req, &retval);
658
659 if (retval != EOK) {
660 if (handle != NULL)
661 *handle = (devman_handle_t) -1;
662
663 return retval;
664 }
665
666 if (handle != NULL)
667 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
668
669 return retval;
670}
671
672int devman_driver_get_match_id(devman_handle_t handle, size_t index, char *buf,
673 size_t buf_size, unsigned int *rscore)
674{
675 int rc;
676 sysarg_t score = 0;
677
678 rc = devman_get_str_internal(DEVMAN_DRIVER_GET_MATCH_ID, handle, index,
679 &score, buf, buf_size);
680 if (rc != EOK)
681 return rc;
682
683 *rscore = score;
684 return rc;
685}
686
687int devman_driver_get_name(devman_handle_t handle, char *buf, size_t buf_size)
688{
689 return devman_get_str_internal(DEVMAN_DRIVER_GET_NAME, handle, 0, NULL,
690 buf, buf_size);
691}
692
693int devman_driver_get_state(devman_handle_t drvh, driver_state_t *rstate)
694{
695 sysarg_t state;
696 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
697 if (exch == NULL)
698 return ENOMEM;
699
700 int rc = async_req_1_1(exch, DEVMAN_DRIVER_GET_STATE, drvh,
701 &state);
702
703 devman_exchange_end(exch);
704 if (rc != EOK)
705 return rc;
706
707 *rstate = state;
708 return rc;
709}
710
711int devman_driver_load(devman_handle_t drvh)
712{
713 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
714 if (exch == NULL)
715 return ENOMEM;
716
717 int rc = async_req_1_0(exch, DEVMAN_DRIVER_LOAD, drvh);
718
719 devman_exchange_end(exch);
720 return rc;
721}
722
723/** @}
724 */
Note: See TracBrowser for help on using the repository browser.