source: mainline/uspace/lib/c/generic/devman.c@ 622dea8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 622dea8 was 622dea8, checked in by Jakub Jermar <jakub@…>, 14 years ago

Protect devman_get_phone() by a mutex.

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