source: mainline/uspace/lib/c/generic/devman.c@ 15e54f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15e54f5 was 15e54f5, checked in by Martin Decky <martin@…>, 14 years ago

fix deadlock in client part of devman protocol

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[729fa2d6]1/*
2 * Copyright (c) 2007 Josef Cejka
3 * Copyright (c) 2009 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 */
[64d2b10]30
31/** @addtogroup libc
[5cd136ab]32 * @{
33 */
34/** @file
35 */
[729fa2d6]36
[c47e1a8]37#include <str.h>
[729fa2d6]38#include <ipc/services.h>
[79ae36dd]39#include <ns.h>
[729fa2d6]40#include <ipc/devman.h>
41#include <devman.h>
[622dea8]42#include <fibril_synch.h>
[79ae36dd]43#include <async.h>
[729fa2d6]44#include <errno.h>
45#include <malloc.h>
46#include <bool.h>
47
[79ae36dd]48static FIBRIL_MUTEX_INITIALIZE(devman_driver_block_mutex);
49static FIBRIL_MUTEX_INITIALIZE(devman_client_block_mutex);
50
51static FIBRIL_MUTEX_INITIALIZE(devman_driver_mutex);
52static FIBRIL_MUTEX_INITIALIZE(devman_client_mutex);
[729fa2d6]53
[79ae36dd]54static async_sess_t *devman_driver_block_sess = NULL;
55static async_sess_t *devman_client_block_sess = NULL;
[622dea8]56
[79ae36dd]57static async_sess_t *devman_driver_sess = NULL;
58static async_sess_t *devman_client_sess = NULL;
59
60static void clone_session(fibril_mutex_t *mtx, async_sess_t *src,
61 async_sess_t **dst)
62{
63 fibril_mutex_lock(mtx);
64
65 if ((*dst == NULL) && (src != NULL))
66 *dst = src;
67
68 fibril_mutex_unlock(mtx);
69}
70
71/** Start an async exchange on the devman session (blocking).
72 *
73 * @param iface Device manager interface to choose
74 *
75 * @return New exchange.
76 *
77 */
78async_exch_t *devman_exchange_begin_blocking(devman_interface_t iface)
[729fa2d6]79{
80 switch (iface) {
81 case DEVMAN_DRIVER:
[79ae36dd]82 fibril_mutex_lock(&devman_driver_block_mutex);
83
84 while (devman_driver_block_sess == NULL) {
85 clone_session(&devman_driver_mutex, devman_driver_sess,
86 &devman_driver_block_sess);
87
88 if (devman_driver_block_sess == NULL)
89 devman_driver_block_sess =
90 service_connect_blocking(EXCHANGE_SERIALIZE,
91 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
[622dea8]92 }
[729fa2d6]93
[79ae36dd]94 fibril_mutex_unlock(&devman_driver_block_mutex);
[729fa2d6]95
[79ae36dd]96 clone_session(&devman_driver_mutex, devman_driver_block_sess,
97 &devman_driver_sess);
98
99 return async_exchange_begin(devman_driver_block_sess);
[729fa2d6]100 case DEVMAN_CLIENT:
[79ae36dd]101 fibril_mutex_lock(&devman_client_block_mutex);
[729fa2d6]102
[79ae36dd]103 while (devman_client_block_sess == NULL) {
104 clone_session(&devman_client_mutex, devman_client_sess,
105 &devman_client_block_sess);
106
107 if (devman_client_block_sess == NULL)
108 devman_client_block_sess =
109 service_connect_blocking(EXCHANGE_SERIALIZE,
110 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
[4db43721]111 }
[729fa2d6]112
[79ae36dd]113 fibril_mutex_unlock(&devman_client_block_mutex);
114
115 clone_session(&devman_client_mutex, devman_client_block_sess,
116 &devman_client_sess);
117
118 return async_exchange_begin(devman_client_block_sess);
[729fa2d6]119 default:
[79ae36dd]120 return NULL;
[729fa2d6]121 }
122}
123
[79ae36dd]124/** Start an async exchange on the devman session.
125 *
126 * @param iface Device manager interface to choose
127 *
128 * @return New exchange.
129 *
130 */
131async_exch_t *devman_exchange_begin(devman_interface_t iface)
132{
133 switch (iface) {
134 case DEVMAN_DRIVER:
135 fibril_mutex_lock(&devman_driver_mutex);
136
137 if (devman_driver_sess == NULL)
138 devman_driver_sess =
139 service_connect(EXCHANGE_SERIALIZE, SERVICE_DEVMAN,
140 DEVMAN_DRIVER, 0);
141
142 fibril_mutex_unlock(&devman_driver_mutex);
143
144 if (devman_driver_sess == NULL)
145 return NULL;
146
147 return async_exchange_begin(devman_driver_sess);
148 case DEVMAN_CLIENT:
149 fibril_mutex_lock(&devman_client_mutex);
150
151 if (devman_client_sess == NULL)
152 devman_client_sess =
153 service_connect(EXCHANGE_SERIALIZE, SERVICE_DEVMAN,
154 DEVMAN_CLIENT, 0);
155
156 fibril_mutex_unlock(&devman_client_mutex);
157
158 if (devman_client_sess == NULL)
159 return NULL;
160
161 return async_exchange_begin(devman_client_sess);
162 default:
163 return NULL;
164 }
165}
166
167/** Finish an async exchange on the devman session.
168 *
169 * @param exch Exchange to be finished.
170 *
171 */
172void devman_exchange_end(async_exch_t *exch)
173{
174 async_exchange_end(exch);
175}
176
[729fa2d6]177/** Register running driver with device manager. */
178int devman_driver_register(const char *name, async_client_conn_t conn)
179{
[79ae36dd]180 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
[729fa2d6]181
182 ipc_call_t answer;
[79ae36dd]183 aid_t req = async_send_2(exch, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
184 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
185
186 devman_exchange_end(exch);
[729fa2d6]187
188 if (retval != EOK) {
189 async_wait_for(req, NULL);
[79ae36dd]190 return retval;
[729fa2d6]191 }
192
193 async_set_client_connection(conn);
194
[79ae36dd]195 exch = devman_exchange_begin(DEVMAN_DRIVER);
196 async_connect_to_me(exch, 0, 0, 0, NULL);
197 devman_exchange_end(exch);
[729fa2d6]198
[79ae36dd]199 async_wait_for(req, &retval);
[729fa2d6]200 return retval;
201}
202
[8b1e15ac]203/** Add function to a device.
204 *
205 * Request devman to add a new function to the specified device owned by
206 * this driver task.
207 *
[79ae36dd]208 * @param name Name of the new function
209 * @param ftype Function type, fun_inner or fun_exposed
210 * @param match_ids Match IDs (should be empty for fun_exposed)
211 * @param devh Devman handle of the device
212 * @param funh Place to store handle of the new function
213 *
214 * @return EOK on success or negative error code.
[8b1e15ac]215 *
216 */
217int devman_add_function(const char *name, fun_type_t ftype,
218 match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
[1b367b4]219{
[8b1e15ac]220 int match_count = list_count(&match_ids->ids);
[79ae36dd]221 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
222
[7707954]223 ipc_call_t answer;
[79ae36dd]224 aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
[8b1e15ac]225 devh, match_count, &answer);
[79ae36dd]226 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
[7707954]227 if (retval != EOK) {
[79ae36dd]228 devman_exchange_end(exch);
[7707954]229 async_wait_for(req, NULL);
230 return retval;
231 }
232
[79ae36dd]233 link_t *link = match_ids->ids.next;
234 match_id_t *match_id = NULL;
235
236 while (link != &match_ids->ids) {
237 match_id = list_get_instance(link, match_id_t, link);
238
[15e54f5]239 ipc_call_t answer2;
240 aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
241 match_id->score, &answer2);
[79ae36dd]242 retval = async_data_write_start(exch, match_id->id,
243 str_size(match_id->id));
244 if (retval != EOK) {
245 devman_exchange_end(exch);
[15e54f5]246 async_wait_for(req2, NULL);
[79ae36dd]247 async_wait_for(req, NULL);
248 return retval;
249 }
250
[15e54f5]251 async_wait_for(req2, &retval);
[79ae36dd]252 if (retval != EOK) {
253 devman_exchange_end(exch);
[15e54f5]254 async_wait_for(req, NULL);
[79ae36dd]255 return retval;
256 }
257
258 link = link->next;
[4265fd4]259 }
[7707954]260
[79ae36dd]261 devman_exchange_end(exch);
[15e54f5]262
263 async_wait_for(req, &retval);
264 if (retval != EOK) {
265 if (funh != NULL)
266 *funh = (int) IPC_GET_ARG1(answer);
267 } else {
268 if (funh != NULL)
269 *funh = -1;
270 }
271
272 return retval;
[7707954]273}
274
[1b367b4]275int devman_add_device_to_class(devman_handle_t devman_handle,
276 const char *class_name)
[692c40cb]277{
[79ae36dd]278 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
[692c40cb]279
280 ipc_call_t answer;
[79ae36dd]281 aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CLASS,
[1b367b4]282 devman_handle, &answer);
[79ae36dd]283 sysarg_t retval = async_data_write_start(exch, class_name,
[1b367b4]284 str_size(class_name));
[79ae36dd]285
286 devman_exchange_end(exch);
287
[692c40cb]288 if (retval != EOK) {
289 async_wait_for(req, NULL);
290 return retval;
291 }
292
293 async_wait_for(req, &retval);
[1b367b4]294 return retval;
[692c40cb]295}
296
[79ae36dd]297async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
298 unsigned int flags)
[5cd136ab]299{
[79ae36dd]300 async_sess_t *sess;
[5cd136ab]301
[79ae36dd]302 if (flags & IPC_FLAG_BLOCKING)
303 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
304 DEVMAN_CONNECT_TO_DEVICE, handle);
305 else
306 sess = service_connect(mgmt, SERVICE_DEVMAN,
307 DEVMAN_CONNECT_TO_DEVICE, handle);
308
309 return sess;
[5cd136ab]310}
311
[79ae36dd]312async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
313 devman_handle_t handle, unsigned int flags)
[5cd136ab]314{
[79ae36dd]315 async_sess_t *sess;
[5cd136ab]316
[79ae36dd]317 if (flags & IPC_FLAG_BLOCKING)
318 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
319 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
320 else
321 sess = service_connect(mgmt, SERVICE_DEVMAN,
322 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
323
324 return sess;
[5cd136ab]325}
326
[1b367b4]327int devman_device_get_handle(const char *pathname, devman_handle_t *handle,
328 unsigned int flags)
[f658458]329{
[79ae36dd]330 async_exch_t *exch;
331
332 if (flags & IPC_FLAG_BLOCKING)
333 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
334 else {
335 exch = devman_exchange_begin(DEVMAN_CLIENT);
336 if (exch == NULL)
337 return errno;
338 }
[f658458]339
340 ipc_call_t answer;
[79ae36dd]341 aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
[f658458]342 &answer);
[79ae36dd]343 sysarg_t retval = async_data_write_start(exch, pathname,
[1b367b4]344 str_size(pathname));
[79ae36dd]345
346 devman_exchange_end(exch);
347
[f658458]348 if (retval != EOK) {
349 async_wait_for(req, NULL);
350 return retval;
351 }
352
353 async_wait_for(req, &retval);
354
355 if (retval != EOK) {
356 if (handle != NULL)
[0b5a4131]357 *handle = (devman_handle_t) -1;
[79ae36dd]358
[f658458]359 return retval;
360 }
361
362 if (handle != NULL)
[0b5a4131]363 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
[f658458]364
365 return retval;
366}
367
[e72fb34]368int devman_device_get_handle_by_class(const char *classname,
369 const char *devname, devman_handle_t *handle, unsigned int flags)
370{
[79ae36dd]371 async_exch_t *exch;
372
373 if (flags & IPC_FLAG_BLOCKING)
374 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
375 else {
376 exch = devman_exchange_begin(DEVMAN_CLIENT);
377 if (exch == NULL)
378 return errno;
379 }
380
[e72fb34]381 ipc_call_t answer;
[79ae36dd]382 aid_t req = async_send_1(exch, DEVMAN_DEVICE_GET_HANDLE_BY_CLASS,
[e72fb34]383 flags, &answer);
[79ae36dd]384 sysarg_t retval = async_data_write_start(exch, classname,
[e72fb34]385 str_size(classname));
[79ae36dd]386
[e72fb34]387 if (retval != EOK) {
[79ae36dd]388 devman_exchange_end(exch);
[e72fb34]389 async_wait_for(req, NULL);
390 return retval;
391 }
[79ae36dd]392
393 retval = async_data_write_start(exch, devname,
[e72fb34]394 str_size(devname));
[79ae36dd]395
396 devman_exchange_end(exch);
397
[e72fb34]398 if (retval != EOK) {
399 async_wait_for(req, NULL);
400 return retval;
401 }
[79ae36dd]402
[e72fb34]403 async_wait_for(req, &retval);
[79ae36dd]404
[e72fb34]405 if (retval != EOK) {
406 if (handle != NULL)
407 *handle = (devman_handle_t) -1;
[79ae36dd]408
[e72fb34]409 return retval;
410 }
[79ae36dd]411
[e72fb34]412 if (handle != NULL)
413 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
[79ae36dd]414
[e72fb34]415 return retval;
416}
417
[431d6d6]418int devman_get_device_path(devman_handle_t handle, char *path, size_t path_size)
419{
[79ae36dd]420 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
421 if (exch == NULL)
422 return errno;
423
[431d6d6]424 ipc_call_t answer;
[79ae36dd]425 aid_t req = async_send_1(exch, DEVMAN_DEVICE_GET_DEVICE_PATH,
[431d6d6]426 handle, &answer);
[79ae36dd]427
[431d6d6]428 ipc_call_t data_request_call;
[79ae36dd]429 aid_t data_request = async_data_read(exch, path, path_size,
[431d6d6]430 &data_request_call);
[79ae36dd]431
432 devman_exchange_end(exch);
433
[431d6d6]434 if (data_request == 0) {
435 async_wait_for(req, NULL);
436 return ENOMEM;
437 }
[79ae36dd]438
[431d6d6]439 sysarg_t data_request_rc;
440 async_wait_for(data_request, &data_request_rc);
[79ae36dd]441
442 sysarg_t opening_request_rc;
[431d6d6]443 async_wait_for(req, &opening_request_rc);
[79ae36dd]444
[431d6d6]445 if (data_request_rc != EOK) {
446 /* Prefer the return code of the opening request. */
[79ae36dd]447 if (opening_request_rc != EOK)
[431d6d6]448 return (int) opening_request_rc;
[79ae36dd]449 else
[431d6d6]450 return (int) data_request_rc;
451 }
[79ae36dd]452
453 if (opening_request_rc != EOK)
[431d6d6]454 return (int) opening_request_rc;
[79ae36dd]455
[852803a]456 /* To be on the safe-side. */
[431d6d6]457 path[path_size - 1] = 0;
[852803a]458 size_t transferred_size = IPC_GET_ARG2(data_request_call);
[79ae36dd]459 if (transferred_size >= path_size)
[431d6d6]460 return ELIMIT;
[79ae36dd]461
[852803a]462 /* Terminate the string (trailing 0 not send over IPC). */
463 path[transferred_size] = 0;
[431d6d6]464 return EOK;
465}
466
[5cd136ab]467/** @}
[398c4d7]468 */
Note: See TracBrowser for help on using the repository browser.