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

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

Separate list_t typedef from link_t (user-space part).

  • list_t represents lists
  • Use list_first(), list_last(), list_empty() where appropriate
  • Use list_foreach() where possible
  • assert_link_not_used()
  • usb_hid_report_path_free() shall not unlink the path, caller must do it
  • Property mode set to 100644
File size: 11.7 KB
Line 
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 */
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 <bool.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_SERIALIZE,
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_SERIALIZE, 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, async_client_conn_t conn)
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_wait_for(req, NULL);
191 return retval;
192 }
193
194 async_set_client_connection(conn);
195
196 exch = devman_exchange_begin(DEVMAN_DRIVER);
197 async_connect_to_me(exch, 0, 0, 0, NULL, NULL);
198 devman_exchange_end(exch);
199
200 async_wait_for(req, &retval);
201 return retval;
202}
203
204/** Add function to a device.
205 *
206 * Request devman to add a new function to the specified device owned by
207 * this driver task.
208 *
209 * @param name Name of the new function
210 * @param ftype Function type, fun_inner or fun_exposed
211 * @param match_ids Match IDs (should be empty for fun_exposed)
212 * @param devh Devman handle of the device
213 * @param funh Place to store handle of the new function
214 *
215 * @return EOK on success or negative error code.
216 *
217 */
218int devman_add_function(const char *name, fun_type_t ftype,
219 match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
220{
221 int match_count = list_count(&match_ids->ids);
222 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
223
224 ipc_call_t answer;
225 aid_t req = async_send_3(exch, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
226 devh, match_count, &answer);
227 sysarg_t retval = async_data_write_start(exch, name, str_size(name));
228 if (retval != EOK) {
229 devman_exchange_end(exch);
230 async_wait_for(req, NULL);
231 return retval;
232 }
233
234 match_id_t *match_id = NULL;
235
236 list_foreach(match_ids->ids, link) {
237 match_id = list_get_instance(link, match_id_t, link);
238
239 ipc_call_t answer2;
240 aid_t req2 = async_send_1(exch, DEVMAN_ADD_MATCH_ID,
241 match_id->score, &answer2);
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);
246 async_wait_for(req2, NULL);
247 async_wait_for(req, NULL);
248 return retval;
249 }
250
251 async_wait_for(req2, &retval);
252 if (retval != EOK) {
253 devman_exchange_end(exch);
254 async_wait_for(req, NULL);
255 return retval;
256 }
257 }
258
259 devman_exchange_end(exch);
260
261 async_wait_for(req, &retval);
262 if (retval == EOK) {
263 if (funh != NULL)
264 *funh = (int) IPC_GET_ARG1(answer);
265 } else {
266 if (funh != NULL)
267 *funh = -1;
268 }
269
270 return retval;
271}
272
273int devman_add_device_to_class(devman_handle_t devman_handle,
274 const char *class_name)
275{
276 async_exch_t *exch = devman_exchange_begin_blocking(DEVMAN_DRIVER);
277
278 ipc_call_t answer;
279 aid_t req = async_send_1(exch, DEVMAN_ADD_DEVICE_TO_CLASS,
280 devman_handle, &answer);
281 sysarg_t retval = async_data_write_start(exch, class_name,
282 str_size(class_name));
283
284 devman_exchange_end(exch);
285
286 if (retval != EOK) {
287 async_wait_for(req, NULL);
288 return retval;
289 }
290
291 async_wait_for(req, &retval);
292 return retval;
293}
294
295async_sess_t *devman_device_connect(exch_mgmt_t mgmt, devman_handle_t handle,
296 unsigned int flags)
297{
298 async_sess_t *sess;
299
300 if (flags & IPC_FLAG_BLOCKING)
301 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
302 DEVMAN_CONNECT_TO_DEVICE, handle);
303 else
304 sess = service_connect(mgmt, SERVICE_DEVMAN,
305 DEVMAN_CONNECT_TO_DEVICE, handle);
306
307 return sess;
308}
309
310async_sess_t *devman_parent_device_connect(exch_mgmt_t mgmt,
311 devman_handle_t handle, unsigned int flags)
312{
313 async_sess_t *sess;
314
315 if (flags & IPC_FLAG_BLOCKING)
316 sess = service_connect_blocking(mgmt, SERVICE_DEVMAN,
317 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
318 else
319 sess = service_connect(mgmt, SERVICE_DEVMAN,
320 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
321
322 return sess;
323}
324
325int devman_device_get_handle(const char *pathname, devman_handle_t *handle,
326 unsigned int flags)
327{
328 async_exch_t *exch;
329
330 if (flags & IPC_FLAG_BLOCKING)
331 exch = devman_exchange_begin_blocking(DEVMAN_CLIENT);
332 else {
333 exch = devman_exchange_begin(DEVMAN_CLIENT);
334 if (exch == NULL)
335 return errno;
336 }
337
338 ipc_call_t answer;
339 aid_t req = async_send_2(exch, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
340 &answer);
341 sysarg_t retval = async_data_write_start(exch, pathname,
342 str_size(pathname));
343
344 devman_exchange_end(exch);
345
346 if (retval != EOK) {
347 async_wait_for(req, NULL);
348 return retval;
349 }
350
351 async_wait_for(req, &retval);
352
353 if (retval != EOK) {
354 if (handle != NULL)
355 *handle = (devman_handle_t) -1;
356
357 return retval;
358 }
359
360 if (handle != NULL)
361 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
362
363 return retval;
364}
365
366int devman_device_get_handle_by_class(const char *classname,
367 const char *devname, devman_handle_t *handle, unsigned int flags)
368{
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)
376 return errno;
377 }
378
379 ipc_call_t answer;
380 aid_t req = async_send_1(exch, DEVMAN_DEVICE_GET_HANDLE_BY_CLASS,
381 flags, &answer);
382 sysarg_t retval = async_data_write_start(exch, classname,
383 str_size(classname));
384
385 if (retval != EOK) {
386 devman_exchange_end(exch);
387 async_wait_for(req, NULL);
388 return retval;
389 }
390
391 retval = async_data_write_start(exch, devname,
392 str_size(devname));
393
394 devman_exchange_end(exch);
395
396 if (retval != EOK) {
397 async_wait_for(req, NULL);
398 return retval;
399 }
400
401 async_wait_for(req, &retval);
402
403 if (retval != EOK) {
404 if (handle != NULL)
405 *handle = (devman_handle_t) -1;
406
407 return retval;
408 }
409
410 if (handle != NULL)
411 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
412
413 return retval;
414}
415
416int devman_get_device_path(devman_handle_t handle, char *path, size_t path_size)
417{
418 async_exch_t *exch = devman_exchange_begin(DEVMAN_CLIENT);
419 if (exch == NULL)
420 return errno;
421
422 ipc_call_t answer;
423 aid_t req = async_send_1(exch, DEVMAN_DEVICE_GET_DEVICE_PATH,
424 handle, &answer);
425
426 ipc_call_t data_request_call;
427 aid_t data_request = async_data_read(exch, path, path_size,
428 &data_request_call);
429
430 devman_exchange_end(exch);
431
432 if (data_request == 0) {
433 async_wait_for(req, NULL);
434 return ENOMEM;
435 }
436
437 sysarg_t data_request_rc;
438 async_wait_for(data_request, &data_request_rc);
439
440 sysarg_t opening_request_rc;
441 async_wait_for(req, &opening_request_rc);
442
443 if (data_request_rc != EOK) {
444 /* Prefer the return code of the opening request. */
445 if (opening_request_rc != EOK)
446 return (int) opening_request_rc;
447 else
448 return (int) data_request_rc;
449 }
450
451 if (opening_request_rc != EOK)
452 return (int) opening_request_rc;
453
454 /* To be on the safe-side. */
455 path[path_size - 1] = 0;
456 size_t transferred_size = IPC_GET_ARG2(data_request_call);
457 if (transferred_size >= path_size)
458 return ELIMIT;
459
460 /* Terminate the string (trailing 0 not send over IPC). */
461 path[transferred_size] = 0;
462 return EOK;
463}
464
465/** @}
466 */
Note: See TracBrowser for help on using the repository browser.