source: mainline/uspace/lib/c/generic/devman.c@ 58898d1d

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

Revisit a few uses of list_count, list_nth w.r.t. type used for object count.

  • 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(iface_t iface)
80{
81 switch (iface) {
82 case INTERFACE_DDF_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(SERVICE_DEVMAN,
92 INTERFACE_DDF_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 INTERFACE_DDF_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(SERVICE_DEVMAN,
111 INTERFACE_DDF_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(iface_t iface)
133{
134 switch (iface) {
135 case INTERFACE_DDF_DRIVER:
136 fibril_mutex_lock(&devman_driver_mutex);
137
138 if (devman_driver_sess == NULL)
139 devman_driver_sess =
140 service_connect(SERVICE_DEVMAN,
141 INTERFACE_DDF_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 INTERFACE_DDF_CLIENT:
150 fibril_mutex_lock(&devman_client_mutex);
151
152 if (devman_client_sess == NULL)
153 devman_client_sess =
154 service_connect(SERVICE_DEVMAN,
155 INTERFACE_DDF_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(INTERFACE_DDF_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(INTERFACE_DDF_DRIVER);
195 async_connect_to_me(exch, 0, 0, 0);
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 unsigned long match_count = list_count(&match_ids->ids);
220 async_exch_t *exch = devman_exchange_begin_blocking(INTERFACE_DDF_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(INTERFACE_DDF_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(devman_handle_t handle, unsigned int flags)
290{
291 async_sess_t *sess;
292
293 if (flags & IPC_FLAG_BLOCKING)
294 sess = service_connect_blocking(SERVICE_DEVMAN,
295 INTERFACE_DEVMAN_DEVICE, handle);
296 else
297 sess = service_connect(SERVICE_DEVMAN,
298 INTERFACE_DEVMAN_DEVICE, handle);
299
300 return sess;
301}
302
303/** Remove function from device.
304 *
305 * Request devman to remove function owned by this driver task.
306 * @param funh Devman handle of the function
307 *
308 * @return EOK on success or negative error code.
309 */
310int devman_remove_function(devman_handle_t funh)
311{
312 async_exch_t *exch;
313 sysarg_t retval;
314
315 exch = devman_exchange_begin_blocking(INTERFACE_DDF_DRIVER);
316 retval = async_req_1_0(exch, DEVMAN_REMOVE_FUNCTION, (sysarg_t) funh);
317 devman_exchange_end(exch);
318
319 return (int) retval;
320}
321
322int devman_drv_fun_online(devman_handle_t funh)
323{
324 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_DRIVER);
325 if (exch == NULL)
326 return ENOMEM;
327
328 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_ONLINE, funh);
329
330 devman_exchange_end(exch);
331 return (int) retval;
332}
333
334int devman_drv_fun_offline(devman_handle_t funh)
335{
336 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_DRIVER);
337 if (exch == NULL)
338 return ENOMEM;
339
340 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_OFFLINE, funh);
341
342 devman_exchange_end(exch);
343 return (int) retval;
344}
345
346async_sess_t *devman_parent_device_connect(devman_handle_t handle,
347 unsigned int flags)
348{
349 async_sess_t *sess;
350
351 if (flags & IPC_FLAG_BLOCKING)
352 sess = service_connect_blocking(SERVICE_DEVMAN,
353 INTERFACE_DEVMAN_PARENT, handle);
354 else
355 sess = service_connect_blocking(SERVICE_DEVMAN,
356 INTERFACE_DEVMAN_PARENT, handle);
357
358 return sess;
359}
360
361int devman_fun_get_handle(const char *pathname, devman_handle_t *handle,
362 unsigned int flags)
363{
364 async_exch_t *exch;
365
366 if (flags & IPC_FLAG_BLOCKING)
367 exch = devman_exchange_begin_blocking(INTERFACE_DDF_CLIENT);
368 else {
369 exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
370 if (exch == NULL)
371 return ENOMEM;
372 }
373
374 ipc_call_t answer;
375 aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
376 &answer);
377 sysarg_t retval = async_data_write_start(exch, pathname,
378 str_size(pathname));
379
380 devman_exchange_end(exch);
381
382 if (retval != EOK) {
383 async_forget(req);
384 return retval;
385 }
386
387 async_wait_for(req, &retval);
388
389 if (retval != EOK) {
390 if (handle != NULL)
391 *handle = (devman_handle_t) -1;
392
393 return retval;
394 }
395
396 if (handle != NULL)
397 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
398
399 return retval;
400}
401
402static int devman_get_str_internal(sysarg_t method, sysarg_t arg1,
403 sysarg_t arg2, sysarg_t *r1, char *buf, size_t buf_size)
404{
405 async_exch_t *exch;
406 ipc_call_t dreply;
407 size_t act_size;
408 sysarg_t dretval;
409
410 exch = devman_exchange_begin_blocking(INTERFACE_DDF_CLIENT);
411
412 ipc_call_t answer;
413 aid_t req = async_send_2(exch, method, arg1, arg2, &answer);
414 aid_t dreq = async_data_read(exch, buf, buf_size - 1, &dreply);
415 async_wait_for(dreq, &dretval);
416
417 devman_exchange_end(exch);
418
419 if (dretval != EOK) {
420 async_forget(req);
421 return dretval;
422 }
423
424 sysarg_t retval;
425 async_wait_for(req, &retval);
426
427 if (retval != EOK) {
428 return retval;
429 }
430
431 if (r1 != NULL)
432 *r1 = IPC_GET_ARG1(answer);
433 act_size = IPC_GET_ARG2(dreply);
434 assert(act_size <= buf_size - 1);
435 buf[act_size] = '\0';
436
437 return EOK;
438}
439
440int devman_fun_get_path(devman_handle_t handle, char *buf, size_t buf_size)
441{
442 return devman_get_str_internal(DEVMAN_FUN_GET_PATH, handle, 0, NULL,
443 buf, buf_size);
444}
445
446int devman_fun_get_match_id(devman_handle_t handle, size_t index, char *buf,
447 size_t buf_size, unsigned int *rscore)
448{
449 int rc;
450 sysarg_t score = 0;
451
452 rc = devman_get_str_internal(DEVMAN_FUN_GET_MATCH_ID, handle, index,
453 &score, buf, buf_size);
454 if (rc != EOK)
455 return rc;
456
457 *rscore = score;
458 return rc;
459}
460
461int devman_fun_get_name(devman_handle_t handle, char *buf, size_t buf_size)
462{
463 return devman_get_str_internal(DEVMAN_FUN_GET_NAME, handle, 0, NULL,
464 buf, buf_size);
465}
466
467int devman_fun_get_driver_name(devman_handle_t handle, char *buf, size_t buf_size)
468{
469 return devman_get_str_internal(DEVMAN_FUN_GET_DRIVER_NAME, handle, 0,
470 NULL, buf, buf_size);
471}
472
473int devman_fun_online(devman_handle_t funh)
474{
475 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
476 if (exch == NULL)
477 return ENOMEM;
478
479 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_ONLINE, funh);
480
481 devman_exchange_end(exch);
482 return (int) retval;
483}
484
485int devman_fun_offline(devman_handle_t funh)
486{
487 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
488 if (exch == NULL)
489 return ENOMEM;
490
491 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_OFFLINE, funh);
492
493 devman_exchange_end(exch);
494 return (int) retval;
495}
496
497static int devman_get_handles_once(sysarg_t method, sysarg_t arg1,
498 devman_handle_t *handle_buf, size_t buf_size, size_t *act_size)
499{
500 async_exch_t *exch = devman_exchange_begin_blocking(INTERFACE_DDF_CLIENT);
501
502 ipc_call_t answer;
503 aid_t req = async_send_1(exch, method, arg1, &answer);
504 int rc = async_data_read_start(exch, handle_buf, buf_size);
505
506 devman_exchange_end(exch);
507
508 if (rc != EOK) {
509 async_forget(req);
510 return rc;
511 }
512
513 sysarg_t retval;
514 async_wait_for(req, &retval);
515
516 if (retval != EOK) {
517 return retval;
518 }
519
520 *act_size = IPC_GET_ARG1(answer);
521 return EOK;
522}
523
524/** Get list of handles.
525 *
526 * Returns an allocated array of handles.
527 *
528 * @param method IPC method
529 * @param arg1 IPC argument 1
530 * @param data Place to store pointer to array of handles
531 * @param count Place to store number of handles
532 * @return EOK on success or negative error code
533 */
534static int devman_get_handles_internal(sysarg_t method, sysarg_t arg1,
535 devman_handle_t **data, size_t *count)
536{
537 devman_handle_t *handles;
538 size_t act_size;
539 size_t alloc_size;
540 int rc;
541
542 *data = NULL;
543 act_size = 0; /* silence warning */
544
545 rc = devman_get_handles_once(method, arg1, NULL, 0,
546 &act_size);
547 if (rc != EOK)
548 return rc;
549
550 alloc_size = act_size;
551 handles = malloc(alloc_size);
552 if (handles == NULL)
553 return ENOMEM;
554
555 while (true) {
556 rc = devman_get_handles_once(method, arg1, handles, alloc_size,
557 &act_size);
558 if (rc != EOK)
559 return rc;
560
561 if (act_size <= alloc_size)
562 break;
563
564 alloc_size *= 2;
565 free(handles);
566
567 handles = malloc(alloc_size);
568 if (handles == NULL)
569 return ENOMEM;
570 }
571
572 *count = act_size / sizeof(devman_handle_t);
573 *data = handles;
574 return EOK;
575}
576
577int devman_fun_get_child(devman_handle_t funh, devman_handle_t *devh)
578{
579 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
580 if (exch == NULL)
581 return ENOMEM;
582
583 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_GET_CHILD,
584 funh, devh);
585
586 devman_exchange_end(exch);
587 return (int) retval;
588}
589
590int devman_dev_get_functions(devman_handle_t devh, devman_handle_t **funcs,
591 size_t *count)
592{
593 return devman_get_handles_internal(DEVMAN_DEV_GET_FUNCTIONS,
594 devh, funcs, count);
595}
596
597int devman_dev_get_parent(devman_handle_t devh, devman_handle_t *funh)
598{
599 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
600 if (exch == NULL)
601 return ENOMEM;
602
603 sysarg_t retval = async_req_1_1(exch, DEVMAN_DEV_GET_PARENT,
604 devh, funh);
605
606 devman_exchange_end(exch);
607 return (int) retval;
608}
609
610int devman_fun_sid_to_handle(service_id_t sid, devman_handle_t *handle)
611{
612 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
613 if (exch == NULL)
614 return ENOMEM;
615
616 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_SID_TO_HANDLE,
617 sid, handle);
618
619 devman_exchange_end(exch);
620 return (int) retval;
621}
622
623int devman_get_drivers(devman_handle_t **drvs,
624 size_t *count)
625{
626 return devman_get_handles_internal(DEVMAN_GET_DRIVERS, 0, drvs, count);
627}
628
629int devman_driver_get_devices(devman_handle_t drvh, devman_handle_t **devs,
630 size_t *count)
631{
632 return devman_get_handles_internal(DEVMAN_DRIVER_GET_DEVICES,
633 drvh, devs, count);
634}
635
636int devman_driver_get_handle(const char *drvname, devman_handle_t *handle)
637{
638 async_exch_t *exch;
639
640 exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
641 if (exch == NULL)
642 return ENOMEM;
643
644 ipc_call_t answer;
645 aid_t req = async_send_0(exch, DEVMAN_DRIVER_GET_HANDLE, &answer);
646 sysarg_t retval = async_data_write_start(exch, drvname,
647 str_size(drvname));
648
649 devman_exchange_end(exch);
650
651 if (retval != EOK) {
652 async_forget(req);
653 return retval;
654 }
655
656 async_wait_for(req, &retval);
657
658 if (retval != EOK) {
659 if (handle != NULL)
660 *handle = (devman_handle_t) -1;
661
662 return retval;
663 }
664
665 if (handle != NULL)
666 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
667
668 return retval;
669}
670
671int devman_driver_get_match_id(devman_handle_t handle, size_t index, char *buf,
672 size_t buf_size, unsigned int *rscore)
673{
674 int rc;
675 sysarg_t score = 0;
676
677 rc = devman_get_str_internal(DEVMAN_DRIVER_GET_MATCH_ID, handle, index,
678 &score, buf, buf_size);
679 if (rc != EOK)
680 return rc;
681
682 *rscore = score;
683 return rc;
684}
685
686int devman_driver_get_name(devman_handle_t handle, char *buf, size_t buf_size)
687{
688 return devman_get_str_internal(DEVMAN_DRIVER_GET_NAME, handle, 0, NULL,
689 buf, buf_size);
690}
691
692int devman_driver_get_state(devman_handle_t drvh, driver_state_t *rstate)
693{
694 sysarg_t state;
695 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
696 if (exch == NULL)
697 return ENOMEM;
698
699 int rc = async_req_1_1(exch, DEVMAN_DRIVER_GET_STATE, drvh,
700 &state);
701
702 devman_exchange_end(exch);
703 if (rc != EOK)
704 return rc;
705
706 *rstate = state;
707 return rc;
708}
709
710int devman_driver_load(devman_handle_t drvh)
711{
712 async_exch_t *exch = devman_exchange_begin(INTERFACE_DDF_CLIENT);
713 if (exch == NULL)
714 return ENOMEM;
715
716 int rc = async_req_1_0(exch, DEVMAN_DRIVER_LOAD, drvh);
717
718 devman_exchange_end(exch);
719 return rc;
720}
721
722/** @}
723 */
Note: See TracBrowser for help on using the repository browser.