1 | /*
|
---|
2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
3 | * Copyright (c) 2011 Vojtech Horky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libusbvirt
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * IPC wrappers, device side.
|
---|
35 | */
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <assert.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <usbvirt/device.h>
|
---|
42 | #include <usbvirt/ipc.h>
|
---|
43 | #include <usb/debug.h>
|
---|
44 |
|
---|
45 | /** Handle VHC request for device name.
|
---|
46 | *
|
---|
47 | * @param dev Target virtual device.
|
---|
48 | * @param icall The call with the request.
|
---|
49 | *
|
---|
50 | */
|
---|
51 | static void ipc_get_name(usbvirt_device_t *dev, ipc_call_t *icall)
|
---|
52 | {
|
---|
53 | if (dev->name == NULL) {
|
---|
54 | async_answer_0(icall, ENOENT);
|
---|
55 | }
|
---|
56 |
|
---|
57 | size_t size = str_size(dev->name);
|
---|
58 |
|
---|
59 | ipc_call_t call;
|
---|
60 | size_t accepted_size;
|
---|
61 | if (!async_data_read_receive(&call, &accepted_size)) {
|
---|
62 | async_answer_0(&call, EINVAL);
|
---|
63 | async_answer_0(icall, EINVAL);
|
---|
64 | return;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (accepted_size > size) {
|
---|
68 | accepted_size = size;
|
---|
69 | }
|
---|
70 | async_data_read_finalize(&call, dev->name, accepted_size);
|
---|
71 |
|
---|
72 | async_answer_1(icall, EOK, accepted_size);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Handle VHC request for control read from the device.
|
---|
76 | *
|
---|
77 | * @param dev Target virtual device.
|
---|
78 | * @param icall The call with the request.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | static void ipc_control_read(usbvirt_device_t *dev, ipc_call_t *icall)
|
---|
82 | {
|
---|
83 | errno_t rc;
|
---|
84 |
|
---|
85 | void *setup_packet = NULL;
|
---|
86 | size_t setup_packet_len = 0;
|
---|
87 | size_t data_len = 0;
|
---|
88 |
|
---|
89 | rc = async_data_write_accept(&setup_packet, false,
|
---|
90 | 1, 1024, 0, &setup_packet_len);
|
---|
91 | if (rc != EOK) {
|
---|
92 | async_answer_0(icall, rc);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | ipc_call_t data;
|
---|
97 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
98 | async_answer_0(&data, EPARTY);
|
---|
99 | async_answer_0(icall, EPARTY);
|
---|
100 | free(setup_packet);
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | void *buffer = malloc(data_len);
|
---|
105 | if (buffer == NULL) {
|
---|
106 | async_answer_0(&data, EPARTY);
|
---|
107 | async_answer_0(icall, ENOMEM);
|
---|
108 | free(setup_packet);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | size_t actual_len;
|
---|
113 | rc = usbvirt_control_read(dev, setup_packet, setup_packet_len,
|
---|
114 | buffer, data_len, &actual_len);
|
---|
115 |
|
---|
116 | if (rc != EOK) {
|
---|
117 | async_answer_0(&data, rc);
|
---|
118 | async_answer_0(icall, rc);
|
---|
119 | free(setup_packet);
|
---|
120 | free(buffer);
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | async_data_read_finalize(&data, buffer, actual_len);
|
---|
125 | async_answer_0(icall, EOK);
|
---|
126 |
|
---|
127 | free(setup_packet);
|
---|
128 | free(buffer);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Handle VHC request for control write to the device.
|
---|
132 | *
|
---|
133 | * @param dev Target virtual device.
|
---|
134 | * @param icall The call with the request.
|
---|
135 | *
|
---|
136 | */
|
---|
137 | static void ipc_control_write(usbvirt_device_t *dev, ipc_call_t *icall)
|
---|
138 | {
|
---|
139 | size_t data_buffer_len = ipc_get_arg1(icall);
|
---|
140 | errno_t rc;
|
---|
141 |
|
---|
142 | void *setup_packet = NULL;
|
---|
143 | void *data_buffer = NULL;
|
---|
144 | size_t setup_packet_len = 0;
|
---|
145 |
|
---|
146 | rc = async_data_write_accept(&setup_packet, false,
|
---|
147 | 1, 0, 0, &setup_packet_len);
|
---|
148 | if (rc != EOK) {
|
---|
149 | async_answer_0(icall, rc);
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (data_buffer_len > 0) {
|
---|
154 | rc = async_data_write_accept(&data_buffer, false,
|
---|
155 | 1, 0, 0, &data_buffer_len);
|
---|
156 | if (rc != EOK) {
|
---|
157 | async_answer_0(icall, rc);
|
---|
158 | free(setup_packet);
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | rc = usbvirt_control_write(dev, setup_packet, setup_packet_len,
|
---|
164 | data_buffer, data_buffer_len);
|
---|
165 |
|
---|
166 | async_answer_0(icall, rc);
|
---|
167 |
|
---|
168 | free(setup_packet);
|
---|
169 | if (data_buffer != NULL) {
|
---|
170 | free(data_buffer);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Handle VHC request for data read from the device (in transfer).
|
---|
175 | *
|
---|
176 | * @param dev Target virtual device.
|
---|
177 | * @param icall The call with the request.
|
---|
178 | *
|
---|
179 | */
|
---|
180 | static void ipc_data_in(usbvirt_device_t *dev,
|
---|
181 | usb_transfer_type_t transfer_type, ipc_call_t *icall)
|
---|
182 | {
|
---|
183 | usb_endpoint_t endpoint = ipc_get_arg1(icall);
|
---|
184 |
|
---|
185 | errno_t rc;
|
---|
186 |
|
---|
187 | size_t data_len = 0;
|
---|
188 | ipc_call_t data;
|
---|
189 | if (!async_data_read_receive(&data, &data_len)) {
|
---|
190 | async_answer_0(&data, EPARTY);
|
---|
191 | async_answer_0(icall, EPARTY);
|
---|
192 | return;
|
---|
193 | }
|
---|
194 |
|
---|
195 | void *buffer = malloc(data_len);
|
---|
196 | if (buffer == NULL) {
|
---|
197 | async_answer_0(&data, EPARTY);
|
---|
198 | async_answer_0(icall, ENOMEM);
|
---|
199 | return;
|
---|
200 | }
|
---|
201 |
|
---|
202 | size_t actual_len;
|
---|
203 | rc = usbvirt_data_in(dev, transfer_type, endpoint,
|
---|
204 | buffer, data_len, &actual_len);
|
---|
205 |
|
---|
206 | if (rc != EOK) {
|
---|
207 | async_answer_0(&data, rc);
|
---|
208 | async_answer_0(icall, rc);
|
---|
209 | free(buffer);
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | async_data_read_finalize(&data, buffer, actual_len);
|
---|
214 | async_answer_0(icall, EOK);
|
---|
215 |
|
---|
216 | free(buffer);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Handle VHC request for data write to the device (out transfer).
|
---|
220 | *
|
---|
221 | * @param dev Target virtual device.
|
---|
222 | * @param icall The call with the request.
|
---|
223 | *
|
---|
224 | */
|
---|
225 | static void ipc_data_out(usbvirt_device_t *dev,
|
---|
226 | usb_transfer_type_t transfer_type, ipc_call_t *icall)
|
---|
227 | {
|
---|
228 | usb_endpoint_t endpoint = ipc_get_arg1(icall);
|
---|
229 |
|
---|
230 | void *data_buffer = NULL;
|
---|
231 | size_t data_buffer_size = 0;
|
---|
232 |
|
---|
233 | errno_t rc = async_data_write_accept(&data_buffer, false,
|
---|
234 | 1, 0, 0, &data_buffer_size);
|
---|
235 | if (rc != EOK) {
|
---|
236 | async_answer_0(icall, rc);
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | rc = usbvirt_data_out(dev, transfer_type, endpoint,
|
---|
241 | data_buffer, data_buffer_size);
|
---|
242 |
|
---|
243 | async_answer_0(icall, rc);
|
---|
244 |
|
---|
245 | free(data_buffer);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** Handle incoming IPC call for virtual USB device.
|
---|
249 | *
|
---|
250 | * @param dev Target USB device.
|
---|
251 | * @param call Incoming call.
|
---|
252 | *
|
---|
253 | * @return Whether the call was handled.
|
---|
254 | *
|
---|
255 | */
|
---|
256 | bool usbvirt_ipc_handle_call(usbvirt_device_t *dev, ipc_call_t *call)
|
---|
257 | {
|
---|
258 | switch (ipc_get_imethod(call)) {
|
---|
259 | case IPC_M_USBVIRT_GET_NAME:
|
---|
260 | ipc_get_name(dev, call);
|
---|
261 | break;
|
---|
262 |
|
---|
263 | case IPC_M_USBVIRT_CONTROL_READ:
|
---|
264 | ipc_control_read(dev, call);
|
---|
265 | break;
|
---|
266 |
|
---|
267 | case IPC_M_USBVIRT_CONTROL_WRITE:
|
---|
268 | ipc_control_write(dev, call);
|
---|
269 | break;
|
---|
270 |
|
---|
271 | case IPC_M_USBVIRT_INTERRUPT_IN:
|
---|
272 | ipc_data_in(dev, USB_TRANSFER_INTERRUPT, call);
|
---|
273 | break;
|
---|
274 |
|
---|
275 | case IPC_M_USBVIRT_BULK_IN:
|
---|
276 | ipc_data_in(dev, USB_TRANSFER_BULK, call);
|
---|
277 | break;
|
---|
278 |
|
---|
279 | case IPC_M_USBVIRT_INTERRUPT_OUT:
|
---|
280 | ipc_data_out(dev, USB_TRANSFER_INTERRUPT, call);
|
---|
281 | break;
|
---|
282 |
|
---|
283 | case IPC_M_USBVIRT_BULK_OUT:
|
---|
284 | ipc_data_out(dev, USB_TRANSFER_BULK, call);
|
---|
285 | break;
|
---|
286 |
|
---|
287 | default:
|
---|
288 | return false;
|
---|
289 | }
|
---|
290 |
|
---|
291 | return true;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @}
|
---|
296 | */
|
---|