source: mainline/uspace/lib/c/generic/devman.c@ 554debd

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

More conservative locking in devman

Add mutex guard when accessing driver_t structure during driver assigning.

Added missing async_wait_for and removed extra mutex_unlock.

To speed-up answer time, driver start is done in separate fibril to avoid
blocking IPC connection fibril.

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