source: mainline/uspace/lib/c/generic/devman.c@ 7ea7db31

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

Protect devman_get_phone() by a mutex.

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