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/services.h>
|
---|
40 | #include <ipc/devman.h>
|
---|
41 | #include <devman.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <fibril_synch.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <malloc.h>
|
---|
46 | #include <bool.h>
|
---|
47 | #include <adt/list.h>
|
---|
48 |
|
---|
49 | static int devman_phone_driver = -1;
|
---|
50 | static int devman_phone_client = -1;
|
---|
51 |
|
---|
52 | static FIBRIL_MUTEX_INITIALIZE(devman_phone_mutex);
|
---|
53 |
|
---|
54 | int devman_get_phone(devman_interface_t iface, unsigned int flags)
|
---|
55 | {
|
---|
56 | switch (iface) {
|
---|
57 | case DEVMAN_DRIVER:
|
---|
58 | fibril_mutex_lock(&devman_phone_mutex);
|
---|
59 | if (devman_phone_driver >= 0) {
|
---|
60 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
61 | return devman_phone_driver;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (flags & IPC_FLAG_BLOCKING)
|
---|
65 | devman_phone_driver = async_connect_me_to_blocking(
|
---|
66 | PHONE_NS, SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
67 | else
|
---|
68 | devman_phone_driver = async_connect_me_to(PHONE_NS,
|
---|
69 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
70 |
|
---|
71 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
72 | return devman_phone_driver;
|
---|
73 | case DEVMAN_CLIENT:
|
---|
74 | fibril_mutex_lock(&devman_phone_mutex);
|
---|
75 | if (devman_phone_client >= 0) {
|
---|
76 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
77 | return devman_phone_client;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
81 | devman_phone_client = async_connect_me_to_blocking(
|
---|
82 | PHONE_NS, SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
83 | } else {
|
---|
84 | devman_phone_client = async_connect_me_to(PHONE_NS,
|
---|
85 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | fibril_mutex_unlock(&devman_phone_mutex);
|
---|
89 | return devman_phone_client;
|
---|
90 | default:
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Register running driver with device manager. */
|
---|
96 | int 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 |
|
---|
108 | sysarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
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 |
|
---|
117 | async_connect_to_me(phone, 0, 0, 0, NULL);
|
---|
118 | async_wait_for(req, &retval);
|
---|
119 |
|
---|
120 | async_serialize_end();
|
---|
121 |
|
---|
122 | return retval;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int devman_send_match_id(int phone, match_id_t *match_id) \
|
---|
126 | {
|
---|
127 | ipc_call_t answer;
|
---|
128 | aid_t req = async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score, &answer);
|
---|
129 | int retval = async_data_write_start(phone, match_id->id, str_size(match_id->id));
|
---|
130 | async_wait_for(req, NULL);
|
---|
131 | return retval;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | static 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 |
|
---|
153 | int devman_child_device_register(
|
---|
154 | const char *name, match_id_list_t *match_ids, devman_handle_t parent_handle, devman_handle_t *handle)
|
---|
155 | {
|
---|
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 |
|
---|
163 | int match_count = list_count(&match_ids->ids);
|
---|
164 | ipc_call_t answer;
|
---|
165 | aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
|
---|
166 |
|
---|
167 | sysarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
168 | if (retval != EOK) {
|
---|
169 | async_wait_for(req, NULL);
|
---|
170 | async_serialize_end();
|
---|
171 | return retval;
|
---|
172 | }
|
---|
173 |
|
---|
174 | devman_send_match_ids(phone, match_ids);
|
---|
175 |
|
---|
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);
|
---|
189 |
|
---|
190 | return retval;
|
---|
191 | }
|
---|
192 |
|
---|
193 | int devman_add_device_to_class(devman_handle_t devman_handle, const char *class_name)
|
---|
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;
|
---|
202 | aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS, devman_handle, &answer);
|
---|
203 |
|
---|
204 | sysarg_t retval = async_data_write_start(phone, class_name, str_size(class_name));
|
---|
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 |
|
---|
217 | void devman_hangup_phone(devman_interface_t iface)
|
---|
218 | {
|
---|
219 | switch (iface) {
|
---|
220 | case DEVMAN_DRIVER:
|
---|
221 | if (devman_phone_driver >= 0) {
|
---|
222 | async_hangup(devman_phone_driver);
|
---|
223 | devman_phone_driver = -1;
|
---|
224 | }
|
---|
225 | break;
|
---|
226 | case DEVMAN_CLIENT:
|
---|
227 | if (devman_phone_client >= 0) {
|
---|
228 | async_hangup(devman_phone_client);
|
---|
229 | devman_phone_client = -1;
|
---|
230 | }
|
---|
231 | break;
|
---|
232 | default:
|
---|
233 | break;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | int devman_device_connect(devman_handle_t handle, unsigned int flags)
|
---|
238 | {
|
---|
239 | int phone;
|
---|
240 |
|
---|
241 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
242 | phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
---|
243 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
244 | } else {
|
---|
245 | phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
---|
246 | DEVMAN_CONNECT_TO_DEVICE, handle);
|
---|
247 | }
|
---|
248 |
|
---|
249 | return phone;
|
---|
250 | }
|
---|
251 |
|
---|
252 | int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
|
---|
253 | {
|
---|
254 | int phone;
|
---|
255 |
|
---|
256 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
257 | phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
|
---|
258 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
259 | } else {
|
---|
260 | phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
|
---|
261 | DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
|
---|
262 | }
|
---|
263 |
|
---|
264 | return phone;
|
---|
265 | }
|
---|
266 |
|
---|
267 | int devman_device_get_handle(const char *pathname, devman_handle_t *handle, unsigned int flags)
|
---|
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 |
|
---|
280 | sysarg_t retval = async_data_write_start(phone, pathname, str_size(pathname));
|
---|
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)
|
---|
293 | *handle = (devman_handle_t) -1;
|
---|
294 | return retval;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (handle != NULL)
|
---|
298 | *handle = (devman_handle_t) IPC_GET_ARG1(answer);
|
---|
299 |
|
---|
300 | return retval;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /** @}
|
---|
305 | */
|
---|