source: mainline/uspace/lib/c/generic/devman.c@ d87561c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d87561c was d87561c, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

C style: type-casting etc.

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[729fa2d6]1/*
2 * Copyright (c) 2007 Josef Cejka
[7beb220]3 * Copyright (c) 2011 Jiri Svoboda
[729fa2d6]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 */
[64d2b10]30
31/** @addtogroup libc
[5cd136ab]32 * @{
33 */
34/** @file
35 */
[729fa2d6]36
[b72efe8]37#include <adt/list.h>
[c47e1a8]38#include <str.h>
[729fa2d6]39#include <ipc/services.h>
[79ae36dd]40#include <ns.h>
[729fa2d6]41#include <ipc/devman.h>
42#include <devman.h>
[622dea8]43#include <fibril_synch.h>
[79ae36dd]44#include <async.h>
[729fa2d6]45#include <errno.h>
46#include <malloc.h>
[3e6a98c5]47#include <stdbool.h>
[729fa2d6]48
[79ae36dd]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);
[729fa2d6]54
[79ae36dd]55static async_sess_t *devman_driver_block_sess = NULL;
56static async_sess_t *devman_client_block_sess = NULL;
[622dea8]57
[79ae36dd]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)
[729fa2d6]80{
81 switch (iface) {
82 case DEVMAN_DRIVER:
[79ae36dd]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 =
[422722e]91 service_connect_blocking(EXCHANGE_PARALLEL,
[79ae36dd]92 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
[622dea8]93 }
[729fa2d6]94
[79ae36dd]95 fibril_mutex_unlock(&devman_driver_block_mutex);
[729fa2d6]96
[79ae36dd]97 clone_session(&devman_driver_mutex, devman_driver_block_sess,
98 &devman_driver_sess);
99
100 return async_exchange_begin(devman_driver_block_sess);
[729fa2d6]101 case DEVMAN_CLIENT:
[79ae36dd]102 fibril_mutex_lock(&devman_client_block_mutex);
[729fa2d6]103
[79ae36dd]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);
[4db43721]112 }
[729fa2d6]113
[79ae36dd]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);
[729fa2d6]120 default:
[79ae36dd]121 return NULL;
[729fa2d6]122 }
123}
124
[79ae36dd]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 =
[422722e]140 service_connect(EXCHANGE_PARALLEL, SERVICE_DEVMAN,
[79ae36dd]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
[729fa2d6]178/** Register running driver with device manager. */
[f302586]179int devman_driver_register(const char *name)
[729fa2d6]180{
[79ae36dd]181 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
[729fa2d6]182
183 ipc_call_t answer;
[79ae36dd]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);
[729fa2d6]188
189 if (retval != EOK) {
[50b581d]190 async_forget(req);
[79ae36dd]191 return retval;
[729fa2d6]192 }
193
[79ae36dd]194 exch = devman_exchange_begin(DEVMAN_DRIVER);
[f302586]195 async_connect_to_me(exch, 0, 0, 0, NULL, NULL);
[79ae36dd]196 devman_exchange_end(exch);
[729fa2d6]197
[79ae36dd]198 async_wait_for(req, &retval);
[729fa2d6]199 return retval;
200}
201
[8b1e15ac]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 *
[79ae36dd]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.
[8b1e15ac]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)
[1b367b4]218{
[8b1e15ac]219 int match_count = list_count(&match_ids->ids);
[79ae36dd]220 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
221
[7707954]222 ipc_call_t answer;
[79ae36dd]223 aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
[8b1e15ac]224 devh, match_count, &answer);
[79ae36dd]225 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
[7707954]226 if (retval != EOK) {
[79ae36dd]227 devman_exchange_end(exch);
[50b581d]228 async_forget(req);
[7707954]229 return retval;
230 }
231
[79ae36dd]232 match_id_t *match_id = NULL;
233
[b72efe8]234 list_foreach(match_ids->ids, link) {
[79ae36dd]235 match_id = list_get_instance(link, match_id_t, link);
236
[15e54f5]237 ipc_call_t answer2;
238 aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
239 match_id->score, &answer2);
[79ae36dd]240 retval = async_data_write_start(exch, match_id->id,
241 str_size(match_id->id));
242 if (retval != EOK) {
243 devman_exchange_end(exch);
[50b581d]244 async_forget(req2);
245 async_forget(req);
[79ae36dd]246 return retval;
247 }
248
[15e54f5]249 async_wait_for(req2, &retval);
[79ae36dd]250 if (retval != EOK) {
251 devman_exchange_end(exch);
[50b581d]252 async_forget(req);
[79ae36dd]253 return retval;
254 }
[4265fd4]255 }
[7707954]256
[79ae36dd]257 devman_exchange_end(exch);
[15e54f5]258
259 async_wait_for(req, &retval);
[e6211f8]260 if (retval == EOK) {
[15e54f5]261 if (funh != NULL)
262 *funh = (int) IPC_GET_ARG1(answer);
263 } else {
264 if (funh != NULL)
265 *funh = -1;
266 }
267
268 return retval;
[7707954]269}
270
[1dc4a5e]271int devman_add_device_to_category(devman_handle_t devman_handle,
272 const char *cat_name)
[692c40cb]273{
[79ae36dd]274 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
[692c40cb]275
276 ipc_call_t answer;
[1dc4a5e]277 aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CATEGORY,
[1b367b4]278 devman_handle, &answer);
[1dc4a5e]279 sysarg_t retval = async_data_write_start(exch, cat_name,
280 str_size(cat_name));
[79ae36dd]281
282 devman_exchange_end(exch);
283
[692c40cb]284 if (retval != EOK) {
[50b581d]285 async_forget(req);
[692c40cb]286 return retval;
287 }
288
289 async_wait_for(req, &retval);
[1b367b4]290 return retval;
[692c40cb]291}
292
[79ae36dd]293async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
294 unsigned int flags)
[5cd136ab]295{
[79ae36dd]296 async_sess_t *sess;
[5cd136ab]297
[79ae36dd]298 if (flags & IPC_FLAG_BLOCKING)
299 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
300 DEVMAN_CONNECT_TO_DEVICE, handle);
301 else
302 sess = service_connect(mgmt, SERVICE_DEVMAN,
303 DEVMAN_CONNECT_TO_DEVICE, handle);
304
305 return sess;
[5cd136ab]306}
307
[d0dd7b5]308/** Remove function from device.
309 *
310 * Request devman to remove function owned by this driver task.
311 * @param funh Devman handle of the function
312 *
313 * @return EOK on success or negative error code.
314 */
315int devman_remove_function(devman_handle_t funh)
316{
317 async_exch_t *exch;
318 sysarg_t retval;
319
320 exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
321 retval = async_req_1_0(exch, DEVMAN_REMOVE_FUNCTION, (sysarg_t) funh);
322 devman_exchange_end(exch);
323
324 return (int) retval;
325}
326
[1a5b252]327int devman_drv_fun_online(devman_handle_t funh)
328{
329 async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
330 if (exch == NULL)
331 return ENOMEM;
332
333 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_ONLINE, funh);
334
335 devman_exchange_end(exch);
336 return (int) retval;
337}
338
339int devman_drv_fun_offline(devman_handle_t funh)
340{
341 async_exch_t *exch = devman_exchange_begin(DEVMAN_DRIVER);
342 if (exch == NULL)
343 return ENOMEM;
344
345 sysarg_t retval = async_req_1_0(exch, DEVMAN_DRV_FUN_OFFLINE, funh);
346
347 devman_exchange_end(exch);
348 return (int) retval;
349}
350
[79ae36dd]351async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
352 devman_handle_t handle, unsigned int flags)
[5cd136ab]353{
[79ae36dd]354 async_sess_t *sess;
[5cd136ab]355
[79ae36dd]356 if (flags & IPC_FLAG_BLOCKING)
357 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
358 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
359 else
360 sess = service_connect(mgmt, SERVICE_DEVMAN,
361 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
362
363 return sess;
[5cd136ab]364}
365
[7beb220]366int devman_fun_get_handle(const char *pathname, devman_handle_t *handle,
[1b367b4]367 unsigned int flags)
[f658458]368{
[79ae36dd]369 async_exch_t *exch;
370
371 if (flags & IPC_FLAG_BLOCKING)
372 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
373 else {
374 exch = devman_exchange_begin(DEVMAN_CLIENT);
375 if (exch == NULL)
[e280857]376 return ENOMEM;
[79ae36dd]377 }
[f658458]378
379 ipc_call_t answer;
[79ae36dd]380 aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
[f658458]381 &answer);
[79ae36dd]382 sysarg_t retval = async_data_write_start(exch, pathname,
[1b367b4]383 str_size(pathname));
[79ae36dd]384
385 devman_exchange_end(exch);
386
[f658458]387 if (retval != EOK) {
[50b581d]388 async_forget(req);
[f658458]389 return retval;
390 }
391
392 async_wait_for(req, &retval);
393
394 if (retval != EOK) {
395 if (handle != NULL)
[0b5a4131]396 *handle = (devman_handle_t) -1;
[79ae36dd]397
[f658458]398 return retval;
399 }
400
401 if (handle != NULL)
[0b5a4131]402 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
[f658458]403
404 return retval;
405}
406
[7beb220]407static int devman_get_str_internal(sysarg_t method, sysarg_t arg1, char *buf,
408 size_t buf_size)
[431d6d6]409{
[7beb220]410 async_exch_t *exch;
411 ipc_call_t dreply;
412 size_t act_size;
413 sysarg_t dretval;
[79ae36dd]414
[d87561c]415 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
[79ae36dd]416
[7beb220]417 ipc_call_t answer;
418 aid_t req = async_send_1(exch, method, arg1, &answer);
419 aid_t dreq = async_data_read(exch, buf, buf_size - 1, &dreply);
420 async_wait_for(dreq, &dretval);
[79ae36dd]421
422 devman_exchange_end(exch);
423
[7beb220]424 if (dretval != EOK) {
[50b581d]425 async_forget(req);
[7beb220]426 return dretval;
[431d6d6]427 }
[79ae36dd]428
[7beb220]429 sysarg_t retval;
430 async_wait_for(req, &retval);
431
[3f57fb7]432 if (retval != EOK) {
[7beb220]433 return retval;
[3f57fb7]434 }
[7beb220]435
436 act_size = IPC_GET_ARG2(dreply);
437 assert(act_size <= buf_size - 1);
438 buf[act_size] = '\0';
439
440 return EOK;
441}
442
443int devman_fun_get_path(devman_handle_t handle, char *buf, size_t buf_size)
444{
445 return devman_get_str_internal(DEVMAN_FUN_GET_PATH, handle, buf,
446 buf_size);
447}
448
449int devman_fun_get_name(devman_handle_t handle, char *buf, size_t buf_size)
450{
451 return devman_get_str_internal(DEVMAN_FUN_GET_NAME, handle, buf,
452 buf_size);
453}
454
[3f57fb7]455int devman_fun_get_driver_name(devman_handle_t handle, char *buf, size_t buf_size)
456{
457 return devman_get_str_internal(DEVMAN_FUN_GET_DRIVER_NAME, handle, buf,
458 buf_size);
459}
460
[1a5b252]461int devman_fun_online(devman_handle_t funh)
462{
463 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
464 if (exch == NULL)
465 return ENOMEM;
466
467 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_ONLINE, funh);
468
469 devman_exchange_end(exch);
470 return (int) retval;
471}
472
473int devman_fun_offline(devman_handle_t funh)
474{
475 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
476 if (exch == NULL)
477 return ENOMEM;
478
479 sysarg_t retval = async_req_1_0(exch, DEVMAN_FUN_OFFLINE, funh);
480
481 devman_exchange_end(exch);
482 return (int) retval;
483}
484
[7beb220]485static int devman_get_handles_once(sysarg_t method, sysarg_t arg1,
486 devman_handle_t *handle_buf, size_t buf_size, size_t *act_size)
487{
488 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
489
490 ipc_call_t answer;
491 aid_t req = async_send_1(exch, method, arg1, &answer);
492 int rc = async_data_read_start(exch, handle_buf, buf_size);
[79ae36dd]493
[7beb220]494 devman_exchange_end(exch);
[79ae36dd]495
[7beb220]496 if (rc != EOK) {
[50b581d]497 async_forget(req);
[7beb220]498 return rc;
[431d6d6]499 }
[79ae36dd]500
[7beb220]501 sysarg_t retval;
502 async_wait_for(req, &retval);
[79ae36dd]503
[7beb220]504 if (retval != EOK) {
505 return retval;
506 }
[79ae36dd]507
[7beb220]508 *act_size = IPC_GET_ARG1(answer);
[431d6d6]509 return EOK;
510}
511
[7beb220]512/** Get list of handles.
513 *
514 * Returns an allocated array of handles.
515 *
516 * @param method IPC method
517 * @param arg1 IPC argument 1
518 * @param data Place to store pointer to array of handles
519 * @param count Place to store number of handles
520 * @return EOK on success or negative error code
521 */
522static int devman_get_handles_internal(sysarg_t method, sysarg_t arg1,
523 devman_handle_t **data, size_t *count)
524{
525 devman_handle_t *handles;
526 size_t act_size;
527 size_t alloc_size;
528 int rc;
529
530 *data = NULL;
531 act_size = 0; /* silence warning */
532
533 rc = devman_get_handles_once(method, arg1, NULL, 0,
534 &act_size);
535 if (rc != EOK)
536 return rc;
537
538 alloc_size = act_size;
539 handles = malloc(alloc_size);
540 if (handles == NULL)
541 return ENOMEM;
542
543 while (true) {
544 rc = devman_get_handles_once(method, arg1, handles, alloc_size,
545 &act_size);
546 if (rc != EOK)
547 return rc;
548
549 if (act_size <= alloc_size)
550 break;
551
552 alloc_size *= 2;
553 free(handles);
554
555 handles = malloc(alloc_size);
556 if (handles == NULL)
557 return ENOMEM;
558 }
559
560 *count = act_size / sizeof(devman_handle_t);
561 *data = handles;
562 return EOK;
563}
564
565int devman_fun_get_child(devman_handle_t funh, devman_handle_t *devh)
566{
567 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
568 if (exch == NULL)
569 return ENOMEM;
570
571 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_GET_CHILD,
572 funh, devh);
573
574 devman_exchange_end(exch);
575 return (int) retval;
576}
577
578int devman_dev_get_functions(devman_handle_t devh, devman_handle_t **funcs,
579 size_t *count)
580{
581 return devman_get_handles_internal(DEVMAN_DEV_GET_FUNCTIONS,
582 devh, funcs, count);
583}
584
[e280857]585int devman_fun_sid_to_handle(service_id_t sid, devman_handle_t *handle)
586{
587 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
588 if (exch == NULL)
589 return ENOMEM;
590
591 sysarg_t retval = async_req_1_1(exch, DEVMAN_FUN_SID_TO_HANDLE,
592 sid, handle);
593
594 devman_exchange_end(exch);
595 return (int) retval;
596}
597
[5cd136ab]598/** @}
[398c4d7]599 */
Note: See TracBrowser for help on using the repository browser.