source: mainline/uspace/lib/c/generic/devman.c@ 0b5a4131

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

Rename device_handle_t to devman_handle_t and make it explicitly clear that
device_handle_t is a handle understood by devman.

  • 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 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 return retval;
121}
122
123
124static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
125{
126 link_t *link = match_ids->ids.next;
127 match_id_t *match_id = NULL;
128 int ret = EOK;
129
130 while (link != &match_ids->ids) {
131 match_id = list_get_instance(link, match_id_t, link);
132 if (EOK != (ret = devman_send_match_id(phone, match_id)))
133 {
134 printf("Driver failed to send match id, error number = %d\n", ret);
135 return ret;
136 }
137 link = link->next;
138 }
139 return ret;
140}
141
142int devman_child_device_register(
143 const char *name, match_id_list_t *match_ids, devman_handle_t parent_handle, devman_handle_t *handle)
144{
145 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
146
147 if (phone < 0)
148 return phone;
149
150 async_serialize_start();
151
152 int match_count = list_count(&match_ids->ids);
153 ipc_call_t answer;
154 aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
155
156 ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
157 if (retval != EOK) {
158 async_wait_for(req, NULL);
159 async_serialize_end();
160 return retval;
161 }
162
163 devman_send_match_ids(phone, match_ids);
164
165 async_wait_for(req, &retval);
166
167 async_serialize_end();
168
169 if (retval != EOK) {
170 if (handle != NULL) {
171 *handle = -1;
172 }
173 return retval;
174 }
175
176 if (handle != NULL)
177 *handle = (int) IPC_GET_ARG1(answer);
178
179 return retval;
180}
181
182int devman_add_device_to_class(devman_handle_t devman_handle, const char *class_name)
183{
184 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
185
186 if (phone < 0)
187 return phone;
188
189 async_serialize_start();
190 ipc_call_t answer;
191 aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS, devman_handle, &answer);
192
193 ipcarg_t retval = async_data_write_start(phone, class_name, str_size(class_name));
194 if (retval != EOK) {
195 async_wait_for(req, NULL);
196 async_serialize_end();
197 return retval;
198 }
199
200 async_wait_for(req, &retval);
201 async_serialize_end();
202
203 return retval;
204}
205
206void devman_hangup_phone(devman_interface_t iface)
207{
208 switch (iface) {
209 case DEVMAN_DRIVER:
210 if (devman_phone_driver >= 0) {
211 ipc_hangup(devman_phone_driver);
212 devman_phone_driver = -1;
213 }
214 break;
215 case DEVMAN_CLIENT:
216 if (devman_phone_client >= 0) {
217 ipc_hangup(devman_phone_client);
218 devman_phone_client = -1;
219 }
220 break;
221 default:
222 break;
223 }
224}
225
226int devman_device_connect(devman_handle_t handle, unsigned int flags)
227{
228 int phone;
229
230 if (flags & IPC_FLAG_BLOCKING) {
231 phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
232 DEVMAN_CONNECT_TO_DEVICE, handle);
233 } else {
234 phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
235 DEVMAN_CONNECT_TO_DEVICE, handle);
236 }
237
238 return phone;
239}
240
241int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
242{
243 int phone;
244
245 if (flags & IPC_FLAG_BLOCKING) {
246 phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
247 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
248 } else {
249 phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
250 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
251 }
252
253 return phone;
254}
255
256int devman_device_get_handle(const char *pathname, devman_handle_t *handle, unsigned int flags)
257{
258 int phone = devman_get_phone(DEVMAN_CLIENT, flags);
259
260 if (phone < 0)
261 return phone;
262
263 async_serialize_start();
264
265 ipc_call_t answer;
266 aid_t req = async_send_2(phone, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
267 &answer);
268
269 ipcarg_t retval = async_data_write_start(phone, pathname, str_size(pathname));
270 if (retval != EOK) {
271 async_wait_for(req, NULL);
272 async_serialize_end();
273 return retval;
274 }
275
276 async_wait_for(req, &retval);
277
278 async_serialize_end();
279
280 if (retval != EOK) {
281 if (handle != NULL)
282 *handle = (devman_handle_t) -1;
283 return retval;
284 }
285
286 if (handle != NULL)
287 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
288
289 return retval;
290}
291
292
293/** @}
294 */
Note: See TracBrowser for help on using the repository browser.