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 | #include <string.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <ipc/ipc.h>
|
---|
34 | #include <ipc/services.h>
|
---|
35 | #include <ipc/devman.h>
|
---|
36 | #include <devman.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <malloc.h>
|
---|
40 | #include <bool.h>
|
---|
41 | #include <adt/list.h>
|
---|
42 |
|
---|
43 | static int devman_phone_driver = -1;
|
---|
44 | static int devman_phone_client = -1;
|
---|
45 |
|
---|
46 | int devman_get_phone(devman_interface_t iface, unsigned int flags)
|
---|
47 | {
|
---|
48 | switch (iface) {
|
---|
49 | case DEVMAN_DRIVER:
|
---|
50 | if (devman_phone_driver >= 0)
|
---|
51 | return devman_phone_driver;
|
---|
52 |
|
---|
53 | if (flags & IPC_FLAG_BLOCKING)
|
---|
54 | devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
|
---|
55 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
56 | else
|
---|
57 | devman_phone_driver = ipc_connect_me_to(PHONE_NS,
|
---|
58 | SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
|
---|
59 |
|
---|
60 | return devman_phone_driver;
|
---|
61 | case DEVMAN_CLIENT:
|
---|
62 | if (devman_phone_client >= 0)
|
---|
63 | return devman_phone_client;
|
---|
64 |
|
---|
65 | if (flags & IPC_FLAG_BLOCKING)
|
---|
66 | devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
|
---|
67 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
68 | else
|
---|
69 | devman_phone_client = ipc_connect_me_to(PHONE_NS,
|
---|
70 | SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
|
---|
71 |
|
---|
72 | return devman_phone_client;
|
---|
73 | default:
|
---|
74 | return -1;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Register running driver with device manager. */
|
---|
79 | int devman_driver_register(const char *name, async_client_conn_t conn)
|
---|
80 | {
|
---|
81 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
---|
82 |
|
---|
83 | if (phone < 0)
|
---|
84 | return phone;
|
---|
85 |
|
---|
86 | async_serialize_start();
|
---|
87 |
|
---|
88 | ipc_call_t answer;
|
---|
89 | aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
|
---|
90 |
|
---|
91 | ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
92 | if (retval != EOK) {
|
---|
93 | async_wait_for(req, NULL);
|
---|
94 | async_serialize_end();
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 |
|
---|
98 | async_set_client_connection(conn);
|
---|
99 |
|
---|
100 | ipcarg_t callback_phonehash;
|
---|
101 | ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
|
---|
102 | async_wait_for(req, &retval);
|
---|
103 |
|
---|
104 | async_serialize_end();
|
---|
105 |
|
---|
106 | return retval;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static int devman_send_match_id(int phone, match_id_t *match_id) \
|
---|
110 | {
|
---|
111 | ipc_call_t answer;
|
---|
112 | async_send_1(phone, DEVMAN_ADD_MATCH_ID, match_id->score, &answer);
|
---|
113 | int retval = async_data_write_start(phone, match_id->id, str_size(match_id->id));
|
---|
114 | return retval;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | static int devman_send_match_ids(int phone, match_id_list_t *match_ids)
|
---|
119 | {
|
---|
120 | link_t *link = match_ids->ids.next;
|
---|
121 | match_id_t *match_id = NULL;
|
---|
122 | int ret = EOK;
|
---|
123 |
|
---|
124 | while (link != &match_ids->ids) {
|
---|
125 | match_id = list_get_instance(link, match_id_t, link);
|
---|
126 | if (EOK != (ret = devman_send_match_id(phone, match_id)))
|
---|
127 | {
|
---|
128 | printf("Driver failed to send match id, error number = %d\n", ret);
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 | link = link->next;
|
---|
132 | }
|
---|
133 | return ret;
|
---|
134 | }
|
---|
135 |
|
---|
136 | int devman_child_device_register(
|
---|
137 | const char *name, match_id_list_t *match_ids, device_handle_t parent_handle, device_handle_t *handle)
|
---|
138 | {
|
---|
139 | int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
|
---|
140 |
|
---|
141 | if (phone < 0)
|
---|
142 | return phone;
|
---|
143 |
|
---|
144 | async_serialize_start();
|
---|
145 |
|
---|
146 | int match_count = list_count(&match_ids->ids);
|
---|
147 | ipc_call_t answer;
|
---|
148 | aid_t req = async_send_2(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, match_count, &answer);
|
---|
149 |
|
---|
150 | ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
|
---|
151 | if (retval != EOK) {
|
---|
152 | async_wait_for(req, NULL);
|
---|
153 | async_serialize_end();
|
---|
154 | return retval;
|
---|
155 | }
|
---|
156 |
|
---|
157 | devman_send_match_ids(phone, match_ids);
|
---|
158 |
|
---|
159 | async_wait_for(req, &retval);
|
---|
160 |
|
---|
161 | async_serialize_end();
|
---|
162 |
|
---|
163 | if (retval != EOK) {
|
---|
164 | if (handle != NULL) {
|
---|
165 | *handle = -1;
|
---|
166 | }
|
---|
167 | return retval;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (handle != NULL)
|
---|
171 | *handle = (int) IPC_GET_ARG1(answer);
|
---|
172 |
|
---|
173 | return retval;
|
---|
174 | }
|
---|
175 |
|
---|
176 | void devman_hangup_phone(devman_interface_t iface)
|
---|
177 | {
|
---|
178 | switch (iface) {
|
---|
179 | case DEVMAN_DRIVER:
|
---|
180 | if (devman_phone_driver >= 0) {
|
---|
181 | ipc_hangup(devman_phone_driver);
|
---|
182 | devman_phone_driver = -1;
|
---|
183 | }
|
---|
184 | break;
|
---|
185 | case DEVMAN_CLIENT:
|
---|
186 | if (devman_phone_client >= 0) {
|
---|
187 | ipc_hangup(devman_phone_client);
|
---|
188 | devman_phone_client = -1;
|
---|
189 | }
|
---|
190 | break;
|
---|
191 | default:
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|