source: mainline/uspace/lib/c/generic/devman.c@ 21cb3ac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 21cb3ac was 8b1e15ac, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Finish splitting device node: devman client in C library, drv library. Update device drivers accordingly.

  • Property mode set to 100644
File size: 8.1 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/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
49static int devman_phone_driver = -1;
50static int devman_phone_client = -1;
51
52static FIBRIL_MUTEX_INITIALIZE(devman_phone_mutex);
53
54int 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. */
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
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
125static int devman_send_match_id(int phone, match_id_t *match_id)
126{
127 ipc_call_t answer;
128
129 aid_t req = async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score,
130 &answer);
131 int retval = async_data_write_start(phone, match_id->id,
132 str_size(match_id->id));
133
134 async_wait_for(req, NULL);
135 return retval;
136}
137
138
139static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
140{
141 link_t *link = match_ids->ids.next;
142 match_id_t *match_id = NULL;
143 int ret = EOK;
144
145 while (link != &match_ids->ids) {
146 match_id = list_get_instance(link, match_id_t, link);
147 ret = devman_send_match_id(phone, match_id);
148 if (ret != EOK) {
149 printf("Driver failed to send match id, error %d\n",
150 ret);
151 return ret;
152 }
153
154 link = link->next;
155 }
156
157 return ret;
158}
159
160/** Add function to a device.
161 *
162 * Request devman to add a new function to the specified device owned by
163 * this driver task.
164 *
165 * @param name Name of the new function
166 * @param ftype Function type, fun_inner or fun_exposed
167 * @param match_ids Match IDs (should be empty for fun_exposed)
168 * @param devh Devman handle of the device
169 * @param funh Place to store handle of the new function
170 *
171 * @return EOK on success or negative error code.
172 */
173int devman_add_function(const char *name, fun_type_t ftype,
174 match_id_list_t *match_ids, devman_handle_t devh, devman_handle_t *funh)
175{
176 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
177 int fun_handle;
178
179 if (phone < 0)
180 return phone;
181
182 async_serialize_start();
183
184 int match_count = list_count(&match_ids->ids);
185 ipc_call_t answer;
186
187 aid_t req = async_send_3(phone, DEVMAN_ADD_FUNCTION, (sysarg_t) ftype,
188 devh, match_count, &answer);
189
190 sysarg_t retval = async_data_write_start(phone, name, str_size(name));
191 if (retval != EOK) {
192 async_wait_for(req, NULL);
193 async_serialize_end();
194 return retval;
195 }
196
197 devman_send_match_ids(phone, match_ids);
198
199 async_wait_for(req, &retval);
200
201 async_serialize_end();
202
203 if (retval == EOK)
204 fun_handle = (int) IPC_GET_ARG1(answer);
205 else
206 fun_handle = -1;
207
208 *funh = fun_handle;
209
210 return retval;
211}
212
213int devman_add_device_to_class(devman_handle_t devman_handle,
214 const char *class_name)
215{
216 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
217
218 if (phone < 0)
219 return phone;
220
221 async_serialize_start();
222 ipc_call_t answer;
223 aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS,
224 devman_handle, &answer);
225
226 sysarg_t retval = async_data_write_start(phone, class_name,
227 str_size(class_name));
228 if (retval != EOK) {
229 async_wait_for(req, NULL);
230 async_serialize_end();
231 return retval;
232 }
233
234 async_wait_for(req, &retval);
235 async_serialize_end();
236
237 return retval;
238}
239
240void devman_hangup_phone(devman_interface_t iface)
241{
242 switch (iface) {
243 case DEVMAN_DRIVER:
244 if (devman_phone_driver >= 0) {
245 async_hangup(devman_phone_driver);
246 devman_phone_driver = -1;
247 }
248 break;
249 case DEVMAN_CLIENT:
250 if (devman_phone_client >= 0) {
251 async_hangup(devman_phone_client);
252 devman_phone_client = -1;
253 }
254 break;
255 default:
256 break;
257 }
258}
259
260int devman_device_connect(devman_handle_t handle, unsigned int flags)
261{
262 int phone;
263
264 if (flags & IPC_FLAG_BLOCKING) {
265 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
266 DEVMAN_CONNECT_TO_DEVICE, handle);
267 } else {
268 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
269 DEVMAN_CONNECT_TO_DEVICE, handle);
270 }
271
272 return phone;
273}
274
275int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
276{
277 int phone;
278
279 if (flags & IPC_FLAG_BLOCKING) {
280 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
281 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
282 } else {
283 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
284 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
285 }
286
287 return phone;
288}
289
290int devman_device_get_handle(const char *pathname, devman_handle_t *handle,
291 unsigned int flags)
292{
293 int phone = devman_get_phone(DEVMAN_CLIENT, flags);
294
295 if (phone < 0)
296 return phone;
297
298 async_serialize_start();
299
300 ipc_call_t answer;
301 aid_t req = async_send_2(phone, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
302 &answer);
303
304 sysarg_t retval = async_data_write_start(phone, pathname,
305 str_size(pathname));
306 if (retval != EOK) {
307 async_wait_for(req, NULL);
308 async_serialize_end();
309 return retval;
310 }
311
312 async_wait_for(req, &retval);
313
314 async_serialize_end();
315
316 if (retval != EOK) {
317 if (handle != NULL)
318 *handle = (devman_handle_t) -1;
319 return retval;
320 }
321
322 if (handle != NULL)
323 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
324
325 return retval;
326}
327
328
329/** @}
330 */
Note: See TracBrowser for help on using the repository browser.