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

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

libc: do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 7.6 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 <stdio.h>
39#include <ipc/services.h>
40#include <ipc/devman.h>
41#include <devman.h>
42#include <async.h>
[622dea8]43#include <fibril_synch.h>
[729fa2d6]44#include <errno.h>
45#include <malloc.h>
46#include <bool.h>
[66babbd]47#include <adt/list.h>
[729fa2d6]48
49static int devman_phone_driver = -1;
50static int devman_phone_client = -1;
51
[622dea8]52static FIBRIL_MUTEX_INITIALIZE(devman_phone_mutex);
53
[729fa2d6]54int devman_get_phone(devman_interface_t iface, unsigned int flags)
55{
56 switch (iface) {
57 case DEVMAN_DRIVER:
[622dea8]58 fibril_mutex_lock(&devman_phone_mutex);
59 if (devman_phone_driver >= 0) {
60 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]61 return devman_phone_driver;
[622dea8]62 }
[729fa2d6]63
64 if (flags & IPC_FLAG_BLOCKING)
[8983273]65 devman_phone_driver = async_connect_me_to_blocking(
66 PHONE_NS, SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
[729fa2d6]67 else
[8983273]68 devman_phone_driver = async_connect_me_to(PHONE_NS,
[729fa2d6]69 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
70
[622dea8]71 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]72 return devman_phone_driver;
73 case DEVMAN_CLIENT:
[622dea8]74 fibril_mutex_lock(&devman_phone_mutex);
75 if (devman_phone_client >= 0) {
76 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]77 return devman_phone_client;
[622dea8]78 }
[729fa2d6]79
[4db43721]80 if (flags & IPC_FLAG_BLOCKING) {
[8983273]81 devman_phone_client = async_connect_me_to_blocking(
82 PHONE_NS, SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
[4db43721]83 } else {
[8983273]84 devman_phone_client = async_connect_me_to(PHONE_NS,
[729fa2d6]85 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
[4db43721]86 }
[729fa2d6]87
[622dea8]88 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]89 return devman_phone_client;
90 default:
91 return -1;
92 }
93}
94
95/** Register running driver with device manager. */
96int devman_driver_register(const char *name, async_client_conn_t conn)
97{
98 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
99
100 if (phone < 0)
101 return phone;
102
103 async_serialize_start();
104
105 ipc_call_t answer;
106 aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
107
[96b02eb9]108 sysarg_t retval = async_data_write_start(phone, name, str_size(name));
[729fa2d6]109 if (retval != EOK) {
110 async_wait_for(req, NULL);
111 async_serialize_end();
112 return -1;
113 }
114
115 async_set_client_connection(conn);
116
[64d2b10]117 async_connect_to_me(phone, 0, 0, 0, NULL);
[729fa2d6]118 async_wait_for(req, &retval);
119
120 async_serialize_end();
121
122 return retval;
123}
124
[66babbd]125static int devman_send_match_id(int phone, match_id_t *match_id) \
126{
127 ipc_call_t answer;
[398c4d7]128 aid_t req = async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score, &answer);
[66babbd]129 int retval = async_data_write_start(phone, match_id->id, str_size(match_id->id));
[398c4d7]130 async_wait_for(req, NULL);
131 return retval;
[66babbd]132}
133
134
135static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
136{
137 link_t *link = match_ids->ids.next;
138 match_id_t *match_id = NULL;
139 int ret = EOK;
140
141 while (link != &match_ids->ids) {
142 match_id = list_get_instance(link, match_id_t, link);
143 if (EOK != (ret = devman_send_match_id(phone, match_id)))
144 {
145 printf("Driver failed to send match id, error number = %d\n", ret);
146 return ret;
147 }
148 link = link->next;
149 }
150 return ret;
151}
152
[bda60d9]153int devman_child_device_register(
[0b5a4131]154 const char *name, match_id_list_t *match_ids, devman_handle_t parent_handle, devman_handle_t *handle)
[d347b53]155{
[7707954]156 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
157
158 if (phone < 0)
159 return phone;
160
161 async_serialize_start();
162
[66babbd]163 int match_count = list_count(&match_ids->ids);
[7707954]164 ipc_call_t answer;
[66babbd]165 aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
[7707954]166
[96b02eb9]167 sysarg_t retval = async_data_write_start(phone, name, str_size(name));
[7707954]168 if (retval != EOK) {
169 async_wait_for(req, NULL);
170 async_serialize_end();
171 return retval;
172 }
173
[66babbd]174 devman_send_match_ids(phone, match_ids);
[bda60d9]175
[7707954]176 async_wait_for(req, &retval);
177
178 async_serialize_end();
179
180 if (retval != EOK) {
181 if (handle != NULL) {
182 *handle = -1;
183 }
184 return retval;
185 }
186
187 if (handle != NULL)
188 *handle = (int) IPC_GET_ARG1(answer);
[66babbd]189
190 return retval;
[7707954]191}
192
[0b5a4131]193int devman_add_device_to_class(devman_handle_t devman_handle, const char *class_name)
[692c40cb]194{
195 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
196
197 if (phone < 0)
198 return phone;
199
200 async_serialize_start();
201 ipc_call_t answer;
[0b5a4131]202 aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS, devman_handle, &answer);
[692c40cb]203
[96b02eb9]204 sysarg_t retval = async_data_write_start(phone, class_name, str_size(class_name));
[692c40cb]205 if (retval != EOK) {
206 async_wait_for(req, NULL);
207 async_serialize_end();
208 return retval;
209 }
210
211 async_wait_for(req, &retval);
212 async_serialize_end();
213
214 return retval;
215}
216
[729fa2d6]217void devman_hangup_phone(devman_interface_t iface)
218{
219 switch (iface) {
220 case DEVMAN_DRIVER:
221 if (devman_phone_driver >= 0) {
[64d2b10]222 async_hangup(devman_phone_driver);
[729fa2d6]223 devman_phone_driver = -1;
224 }
225 break;
226 case DEVMAN_CLIENT:
227 if (devman_phone_client >= 0) {
[64d2b10]228 async_hangup(devman_phone_client);
[729fa2d6]229 devman_phone_client = -1;
230 }
231 break;
232 default:
233 break;
234 }
235}
[5cd136ab]236
[0b5a4131]237int devman_device_connect(devman_handle_t handle, unsigned int flags)
[5cd136ab]238{
239 int phone;
240
241 if (flags & IPC_FLAG_BLOCKING) {
[8983273]242 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]243 DEVMAN_CONNECT_TO_DEVICE, handle);
244 } else {
[8983273]245 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]246 DEVMAN_CONNECT_TO_DEVICE, handle);
247 }
248
249 return phone;
250}
251
[0b5a4131]252int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
[5cd136ab]253{
254 int phone;
255
256 if (flags & IPC_FLAG_BLOCKING) {
[8983273]257 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]258 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
259 } else {
[8983273]260 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]261 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
262 }
263
264 return phone;
265}
266
[0b5a4131]267int devman_device_get_handle(const char *pathname, devman_handle_t *handle, unsigned int flags)
[f658458]268{
269 int phone = devman_get_phone(DEVMAN_CLIENT, flags);
270
271 if (phone < 0)
272 return phone;
273
274 async_serialize_start();
275
276 ipc_call_t answer;
277 aid_t req = async_send_2(phone, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
278 &answer);
279
[96b02eb9]280 sysarg_t retval = async_data_write_start(phone, pathname, str_size(pathname));
[f658458]281 if (retval != EOK) {
282 async_wait_for(req, NULL);
283 async_serialize_end();
284 return retval;
285 }
286
287 async_wait_for(req, &retval);
288
289 async_serialize_end();
290
291 if (retval != EOK) {
292 if (handle != NULL)
[0b5a4131]293 *handle = (devman_handle_t) -1;
[f658458]294 return retval;
295 }
296
297 if (handle != NULL)
[0b5a4131]298 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
[f658458]299
300 return retval;
301}
302
303
[5cd136ab]304/** @}
[398c4d7]305 */
Note: See TracBrowser for help on using the repository browser.