source: mainline/uspace/lib/c/generic/devman.c@ 1b367b4

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

Small C-style fixes in devman client code.

  • Property mode set to 100644
File size: 7.7 KB
RevLine 
[729fa2d6]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 */
[64d2b10]30
31/** @addtogroup libc
[5cd136ab]32 * @{
33 */
34/** @file
35 */
[729fa2d6]36
[c47e1a8]37#include <str.h>
[729fa2d6]38#include <stdio.h>
39#include <ipc/services.h>
40#include <ipc/devman.h>
41#include <devman.h>
42#include <async.h>
[622dea8]43#include <fibril_synch.h>
[729fa2d6]44#include <errno.h>
45#include <malloc.h>
46#include <bool.h>
[66babbd]47#include <adt/list.h>
[729fa2d6]48
49static int devman_phone_driver = -1;
50static int devman_phone_client = -1;
51
[622dea8]52static FIBRIL_MUTEX_INITIALIZE(devman_phone_mutex);
53
[729fa2d6]54int devman_get_phone(devman_interface_t iface, unsigned int flags)
55{
56 switch (iface) {
57 case DEVMAN_DRIVER:
[622dea8]58 fibril_mutex_lock(&devman_phone_mutex);
59 if (devman_phone_driver >= 0) {
60 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]61 return devman_phone_driver;
[622dea8]62 }
[729fa2d6]63
64 if (flags & IPC_FLAG_BLOCKING)
[8983273]65 devman_phone_driver = async_connect_me_to_blocking(
66 PHONE_NS, SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
[729fa2d6]67 else
[8983273]68 devman_phone_driver = async_connect_me_to(PHONE_NS,
[729fa2d6]69 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
70
[622dea8]71 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]72 return devman_phone_driver;
73 case DEVMAN_CLIENT:
[622dea8]74 fibril_mutex_lock(&devman_phone_mutex);
75 if (devman_phone_client >= 0) {
76 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]77 return devman_phone_client;
[622dea8]78 }
[729fa2d6]79
[4db43721]80 if (flags & IPC_FLAG_BLOCKING) {
[8983273]81 devman_phone_client = async_connect_me_to_blocking(
82 PHONE_NS, SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
[4db43721]83 } else {
[8983273]84 devman_phone_client = async_connect_me_to(PHONE_NS,
[729fa2d6]85 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
[4db43721]86 }
[729fa2d6]87
[622dea8]88 fibril_mutex_unlock(&devman_phone_mutex);
[729fa2d6]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
[96b02eb9]108 sysarg_t retval = async_data_write_start(phone, name, str_size(name));
[729fa2d6]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
[64d2b10]117 async_connect_to_me(phone, 0, 0, 0, NULL);
[729fa2d6]118 async_wait_for(req, &retval);
119
120 async_serialize_end();
121
122 return retval;
123}
124
[1b367b4]125static int devman_send_match_id(int phone, match_id_t *match_id)
[66babbd]126{
127 ipc_call_t answer;
[1b367b4]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
[398c4d7]134 async_wait_for(req, NULL);
135 return retval;
[66babbd]136}
137
138
[1b367b4]139static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
[66babbd]140{
141 link_t *link = match_ids->ids.next;
142 match_id_t *match_id = NULL;
143 int ret = EOK;
[1b367b4]144
[66babbd]145 while (link != &match_ids->ids) {
146 match_id = list_get_instance(link, match_id_t, link);
[1b367b4]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;
[66babbd]152 }
[1b367b4]153
[66babbd]154 link = link->next;
155 }
[1b367b4]156
157 return ret;
[66babbd]158}
159
[1b367b4]160int devman_child_device_register(const char *name, match_id_list_t *match_ids,
161 devman_handle_t parent_handle, devman_handle_t *handle)
162{
[7707954]163 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
164
165 if (phone < 0)
166 return phone;
167
168 async_serialize_start();
169
[66babbd]170 int match_count = list_count(&match_ids->ids);
[7707954]171 ipc_call_t answer;
[1b367b4]172 aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count,
173 &answer);
[7707954]174
[96b02eb9]175 sysarg_t retval = async_data_write_start(phone, name, str_size(name));
[7707954]176 if (retval != EOK) {
177 async_wait_for(req, NULL);
178 async_serialize_end();
179 return retval;
180 }
181
[66babbd]182 devman_send_match_ids(phone, match_ids);
[bda60d9]183
[7707954]184 async_wait_for(req, &retval);
185
186 async_serialize_end();
187
188 if (retval != EOK) {
[1b367b4]189 if (handle != NULL)
[7707954]190 *handle = -1;
[1b367b4]191
[7707954]192 return retval;
[1b367b4]193 }
[7707954]194
195 if (handle != NULL)
[1b367b4]196 *handle = (int) IPC_GET_ARG1(answer);
[66babbd]197
198 return retval;
[7707954]199}
200
[1b367b4]201int devman_add_device_to_class(devman_handle_t devman_handle,
202 const char *class_name)
[692c40cb]203{
204 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
205
206 if (phone < 0)
207 return phone;
208
209 async_serialize_start();
210 ipc_call_t answer;
[1b367b4]211 aid_t req = async_send_1(phone, DEVMAN_ADD_DEVICE_TO_CLASS,
212 devman_handle, &answer);
[692c40cb]213
[1b367b4]214 sysarg_t retval = async_data_write_start(phone, class_name,
215 str_size(class_name));
[692c40cb]216 if (retval != EOK) {
217 async_wait_for(req, NULL);
218 async_serialize_end();
219 return retval;
220 }
221
222 async_wait_for(req, &retval);
223 async_serialize_end();
224
[1b367b4]225 return retval;
[692c40cb]226}
227
[729fa2d6]228void devman_hangup_phone(devman_interface_t iface)
229{
230 switch (iface) {
231 case DEVMAN_DRIVER:
232 if (devman_phone_driver >= 0) {
[64d2b10]233 async_hangup(devman_phone_driver);
[729fa2d6]234 devman_phone_driver = -1;
235 }
236 break;
237 case DEVMAN_CLIENT:
238 if (devman_phone_client >= 0) {
[64d2b10]239 async_hangup(devman_phone_client);
[729fa2d6]240 devman_phone_client = -1;
241 }
242 break;
243 default:
244 break;
245 }
246}
[5cd136ab]247
[0b5a4131]248int devman_device_connect(devman_handle_t handle, unsigned int flags)
[5cd136ab]249{
250 int phone;
251
252 if (flags & IPC_FLAG_BLOCKING) {
[8983273]253 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]254 DEVMAN_CONNECT_TO_DEVICE, handle);
255 } else {
[8983273]256 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]257 DEVMAN_CONNECT_TO_DEVICE, handle);
258 }
259
260 return phone;
261}
262
[0b5a4131]263int devman_parent_device_connect(devman_handle_t handle, unsigned int flags)
[5cd136ab]264{
265 int phone;
266
267 if (flags & IPC_FLAG_BLOCKING) {
[8983273]268 phone = async_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]269 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
270 } else {
[8983273]271 phone = async_connect_me_to(PHONE_NS, SERVICE_DEVMAN,
[5cd136ab]272 DEVMAN_CONNECT_TO_PARENTS_DEVICE, handle);
273 }
274
275 return phone;
276}
277
[1b367b4]278int devman_device_get_handle(const char *pathname, devman_handle_t *handle,
279 unsigned int flags)
[f658458]280{
281 int phone = devman_get_phone(DEVMAN_CLIENT, flags);
282
283 if (phone < 0)
284 return phone;
285
286 async_serialize_start();
287
288 ipc_call_t answer;
289 aid_t req = async_send_2(phone, DEVMAN_DEVICE_GET_HANDLE, flags, 0,
290 &answer);
291
[1b367b4]292 sysarg_t retval = async_data_write_start(phone, pathname,
293 str_size(pathname));
[f658458]294 if (retval != EOK) {
295 async_wait_for(req, NULL);
296 async_serialize_end();
297 return retval;
298 }
299
300 async_wait_for(req, &retval);
301
302 async_serialize_end();
303
304 if (retval != EOK) {
305 if (handle != NULL)
[0b5a4131]306 *handle = (devman_handle_t) -1;
[f658458]307 return retval;
308 }
309
310 if (handle != NULL)
[0b5a4131]311 *handle = (devman_handle_t) IPC_GET_ARG1(answer);
[f658458]312
313 return retval;
314}
315
316
[5cd136ab]317/** @}
[398c4d7]318 */
Note: See TracBrowser for help on using the repository browser.