[e25a849] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libusbvirt
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * IPC wrappers, host controller side.
|
---|
| 34 | */
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <str.h>
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <async.h>
|
---|
| 40 | #include <usbvirt/device.h>
|
---|
| 41 | #include <usbvirt/ipc.h>
|
---|
| 42 | #include <usb/debug.h>
|
---|
| 43 |
|
---|
[42e2172] | 44 | /** Send control read transfer to virtual USB device.
|
---|
| 45 | *
|
---|
[79ae36dd] | 46 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 47 | * @param ep Target endpoint number.
|
---|
| 48 | * @param setup_buffer Setup buffer.
|
---|
| 49 | * @param setup_buffer_size Setup buffer size in bytes.
|
---|
| 50 | * @param data_buffer Data buffer (DATA stage of control transfer).
|
---|
| 51 | * @param data_buffer_size Size of data buffer in bytes.
|
---|
| 52 | * @param data_transfered_size Number of actually transferred bytes.
|
---|
[79ae36dd] | 53 | *
|
---|
[42e2172] | 54 | * @return Error code.
|
---|
[79ae36dd] | 55 | *
|
---|
[42e2172] | 56 | */
|
---|
[79ae36dd] | 57 | int usbvirt_ipc_send_control_read(async_sess_t *sess, void *setup_buffer,
|
---|
| 58 | size_t setup_buffer_size, void *data_buffer, size_t data_buffer_size,
|
---|
| 59 | size_t *data_transfered_size)
|
---|
[e25a849] | 60 | {
|
---|
[79ae36dd] | 61 | if (!sess)
|
---|
[beee81a] | 62 | return EINVAL;
|
---|
[79ae36dd] | 63 |
|
---|
| 64 | if ((setup_buffer == NULL) || (setup_buffer_size == 0))
|
---|
[beee81a] | 65 | return EINVAL;
|
---|
[79ae36dd] | 66 |
|
---|
| 67 | if ((data_buffer == NULL) || (data_buffer_size == 0))
|
---|
[beee81a] | 68 | return EINVAL;
|
---|
[79ae36dd] | 69 |
|
---|
| 70 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 71 |
|
---|
| 72 | aid_t opening_request = async_send_0(exch, IPC_M_USBVIRT_CONTROL_READ,
|
---|
| 73 | NULL);
|
---|
[e25a849] | 74 | if (opening_request == 0) {
|
---|
[79ae36dd] | 75 | async_exchange_end(exch);
|
---|
[e25a849] | 76 | return ENOMEM;
|
---|
| 77 | }
|
---|
[79ae36dd] | 78 |
|
---|
| 79 | int rc = async_data_write_start(exch, setup_buffer, setup_buffer_size);
|
---|
[e25a849] | 80 | if (rc != EOK) {
|
---|
[79ae36dd] | 81 | async_exchange_end(exch);
|
---|
[50b581d] | 82 | async_forget(opening_request);
|
---|
[e25a849] | 83 | return rc;
|
---|
| 84 | }
|
---|
[79ae36dd] | 85 |
|
---|
[e25a849] | 86 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 87 | aid_t data_request = async_data_read(exch, data_buffer, data_buffer_size,
|
---|
[e25a849] | 88 | &data_request_call);
|
---|
[79ae36dd] | 89 |
|
---|
| 90 | async_exchange_end(exch);
|
---|
| 91 |
|
---|
[e25a849] | 92 | if (data_request == 0) {
|
---|
[50b581d] | 93 | async_forget(opening_request);
|
---|
[e25a849] | 94 | return ENOMEM;
|
---|
| 95 | }
|
---|
[79ae36dd] | 96 |
|
---|
[e25a849] | 97 | sysarg_t data_request_rc;
|
---|
| 98 | sysarg_t opening_request_rc;
|
---|
| 99 | async_wait_for(data_request, &data_request_rc);
|
---|
| 100 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 101 |
|
---|
[e25a849] | 102 | if (data_request_rc != EOK) {
|
---|
| 103 | /* Prefer the return code of the opening request. */
|
---|
[79ae36dd] | 104 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 105 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 106 | else
|
---|
[e25a849] | 107 | return (int) data_request_rc;
|
---|
| 108 | }
|
---|
[79ae36dd] | 109 |
|
---|
| 110 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 111 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 112 |
|
---|
| 113 | if (data_transfered_size != NULL)
|
---|
[beee81a] | 114 | *data_transfered_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 115 |
|
---|
[e25a849] | 116 | return EOK;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[42e2172] | 119 | /** Send control write transfer to virtual USB device.
|
---|
| 120 | *
|
---|
[79ae36dd] | 121 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 122 | * @param ep Target endpoint number.
|
---|
| 123 | * @param setup_buffer Setup buffer.
|
---|
| 124 | * @param setup_buffer_size Setup buffer size in bytes.
|
---|
| 125 | * @param data_buffer Data buffer (DATA stage of control transfer).
|
---|
| 126 | * @param data_buffer_size Size of data buffer in bytes.
|
---|
[79ae36dd] | 127 | *
|
---|
[42e2172] | 128 | * @return Error code.
|
---|
[79ae36dd] | 129 | *
|
---|
[42e2172] | 130 | */
|
---|
[79ae36dd] | 131 | int usbvirt_ipc_send_control_write(async_sess_t *sess, void *setup_buffer,
|
---|
| 132 | size_t setup_buffer_size, void *data_buffer, size_t data_buffer_size)
|
---|
[e25a849] | 133 | {
|
---|
[79ae36dd] | 134 | if (!sess)
|
---|
[beee81a] | 135 | return EINVAL;
|
---|
[79ae36dd] | 136 |
|
---|
| 137 | if ((setup_buffer == NULL) || (setup_buffer_size == 0))
|
---|
[beee81a] | 138 | return EINVAL;
|
---|
[79ae36dd] | 139 |
|
---|
| 140 | if ((data_buffer_size > 0) && (data_buffer == NULL))
|
---|
[beee81a] | 141 | return EINVAL;
|
---|
[79ae36dd] | 142 |
|
---|
| 143 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 144 |
|
---|
| 145 | aid_t opening_request = async_send_1(exch, IPC_M_USBVIRT_CONTROL_WRITE,
|
---|
| 146 | data_buffer_size, NULL);
|
---|
[e25a849] | 147 | if (opening_request == 0) {
|
---|
[79ae36dd] | 148 | async_exchange_end(exch);
|
---|
[e25a849] | 149 | return ENOMEM;
|
---|
| 150 | }
|
---|
[79ae36dd] | 151 |
|
---|
| 152 | int rc = async_data_write_start(exch, setup_buffer, setup_buffer_size);
|
---|
[e25a849] | 153 | if (rc != EOK) {
|
---|
[79ae36dd] | 154 | async_exchange_end(exch);
|
---|
[50b581d] | 155 | async_forget(opening_request);
|
---|
[e25a849] | 156 | return rc;
|
---|
| 157 | }
|
---|
[79ae36dd] | 158 |
|
---|
[e25a849] | 159 | if (data_buffer_size > 0) {
|
---|
[79ae36dd] | 160 | rc = async_data_write_start(exch, data_buffer, data_buffer_size);
|
---|
[e25a849] | 161 | if (rc != EOK) {
|
---|
[79ae36dd] | 162 | async_exchange_end(exch);
|
---|
[50b581d] | 163 | async_forget(opening_request);
|
---|
[e25a849] | 164 | return rc;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
[79ae36dd] | 167 |
|
---|
| 168 | async_exchange_end(exch);
|
---|
| 169 |
|
---|
[e25a849] | 170 | sysarg_t opening_request_rc;
|
---|
| 171 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 172 |
|
---|
[e25a849] | 173 | return (int) opening_request_rc;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[42e2172] | 176 | /** Request data transfer from virtual USB device.
|
---|
| 177 | *
|
---|
[79ae36dd] | 178 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 179 | * @param ep Target endpoint number.
|
---|
| 180 | * @param tr_type Transfer type (interrupt or bulk).
|
---|
| 181 | * @param data Data buffer.
|
---|
| 182 | * @param data_size Size of the data buffer in bytes.
|
---|
| 183 | * @param act_size Number of actually returned bytes.
|
---|
[79ae36dd] | 184 | *
|
---|
[42e2172] | 185 | * @return Error code.
|
---|
[79ae36dd] | 186 | *
|
---|
[42e2172] | 187 | */
|
---|
[79ae36dd] | 188 | int usbvirt_ipc_send_data_in(async_sess_t *sess, usb_endpoint_t ep,
|
---|
[e25a849] | 189 | usb_transfer_type_t tr_type, void *data, size_t data_size, size_t *act_size)
|
---|
| 190 | {
|
---|
[79ae36dd] | 191 | if (!sess)
|
---|
[beee81a] | 192 | return EINVAL;
|
---|
[79ae36dd] | 193 |
|
---|
[beee81a] | 194 | usbvirt_hc_to_device_method_t method;
|
---|
[79ae36dd] | 195 |
|
---|
[beee81a] | 196 | switch (tr_type) {
|
---|
| 197 | case USB_TRANSFER_INTERRUPT:
|
---|
| 198 | method = IPC_M_USBVIRT_INTERRUPT_IN;
|
---|
| 199 | break;
|
---|
| 200 | case USB_TRANSFER_BULK:
|
---|
| 201 | method = IPC_M_USBVIRT_BULK_IN;
|
---|
| 202 | break;
|
---|
| 203 | default:
|
---|
| 204 | return EINVAL;
|
---|
| 205 | }
|
---|
[79ae36dd] | 206 |
|
---|
| 207 | if ((ep <= 0) || (ep >= USBVIRT_ENDPOINT_MAX))
|
---|
[beee81a] | 208 | return EINVAL;
|
---|
[79ae36dd] | 209 |
|
---|
| 210 | if ((data == NULL) || (data_size == 0))
|
---|
[beee81a] | 211 | return EINVAL;
|
---|
[79ae36dd] | 212 |
|
---|
| 213 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 214 |
|
---|
| 215 | aid_t opening_request = async_send_2(exch, method, ep, tr_type, NULL);
|
---|
[e25a849] | 216 | if (opening_request == 0) {
|
---|
[79ae36dd] | 217 | async_exchange_end(exch);
|
---|
[e25a849] | 218 | return ENOMEM;
|
---|
| 219 | }
|
---|
[79ae36dd] | 220 |
|
---|
[e25a849] | 221 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 222 | aid_t data_request = async_data_read(exch, data, data_size,
|
---|
| 223 | &data_request_call);
|
---|
| 224 |
|
---|
| 225 | async_exchange_end(exch);
|
---|
| 226 |
|
---|
[e25a849] | 227 | if (data_request == 0) {
|
---|
[50b581d] | 228 | async_forget(opening_request);
|
---|
[e25a849] | 229 | return ENOMEM;
|
---|
| 230 | }
|
---|
[79ae36dd] | 231 |
|
---|
[e25a849] | 232 | sysarg_t data_request_rc;
|
---|
| 233 | sysarg_t opening_request_rc;
|
---|
| 234 | async_wait_for(data_request, &data_request_rc);
|
---|
| 235 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 236 |
|
---|
[e25a849] | 237 | if (data_request_rc != EOK) {
|
---|
| 238 | /* Prefer the return code of the opening request. */
|
---|
[79ae36dd] | 239 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 240 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 241 | else
|
---|
[e25a849] | 242 | return (int) data_request_rc;
|
---|
| 243 | }
|
---|
[79ae36dd] | 244 |
|
---|
| 245 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 246 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 247 |
|
---|
| 248 | if (act_size != NULL)
|
---|
[e25a849] | 249 | *act_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 250 |
|
---|
[e25a849] | 251 | return EOK;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[42e2172] | 254 | /** Send data to virtual USB device.
|
---|
| 255 | *
|
---|
[79ae36dd] | 256 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 257 | * @param ep Target endpoint number.
|
---|
| 258 | * @param tr_type Transfer type (interrupt or bulk).
|
---|
| 259 | * @param data Data buffer.
|
---|
| 260 | * @param data_size Size of the data buffer in bytes.
|
---|
[79ae36dd] | 261 | *
|
---|
[42e2172] | 262 | * @return Error code.
|
---|
[79ae36dd] | 263 | *
|
---|
[42e2172] | 264 | */
|
---|
[79ae36dd] | 265 | int usbvirt_ipc_send_data_out(async_sess_t *sess, usb_endpoint_t ep,
|
---|
[e25a849] | 266 | usb_transfer_type_t tr_type, void *data, size_t data_size)
|
---|
| 267 | {
|
---|
[79ae36dd] | 268 | if (!sess)
|
---|
[beee81a] | 269 | return EINVAL;
|
---|
[79ae36dd] | 270 |
|
---|
[beee81a] | 271 | usbvirt_hc_to_device_method_t method;
|
---|
[79ae36dd] | 272 |
|
---|
[beee81a] | 273 | switch (tr_type) {
|
---|
| 274 | case USB_TRANSFER_INTERRUPT:
|
---|
| 275 | method = IPC_M_USBVIRT_INTERRUPT_OUT;
|
---|
| 276 | break;
|
---|
| 277 | case USB_TRANSFER_BULK:
|
---|
| 278 | method = IPC_M_USBVIRT_BULK_OUT;
|
---|
| 279 | break;
|
---|
| 280 | default:
|
---|
| 281 | return EINVAL;
|
---|
| 282 | }
|
---|
[79ae36dd] | 283 |
|
---|
| 284 | if ((ep <= 0) || (ep >= USBVIRT_ENDPOINT_MAX))
|
---|
[beee81a] | 285 | return EINVAL;
|
---|
[79ae36dd] | 286 |
|
---|
| 287 | if ((data == NULL) || (data_size == 0))
|
---|
[beee81a] | 288 | return EINVAL;
|
---|
[79ae36dd] | 289 |
|
---|
| 290 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 291 |
|
---|
| 292 | aid_t opening_request = async_send_1(exch, method, ep, NULL);
|
---|
[e25a849] | 293 | if (opening_request == 0) {
|
---|
[79ae36dd] | 294 | async_exchange_end(exch);
|
---|
[e25a849] | 295 | return ENOMEM;
|
---|
| 296 | }
|
---|
[79ae36dd] | 297 |
|
---|
| 298 | int rc = async_data_write_start(exch, data, data_size);
|
---|
| 299 |
|
---|
| 300 | async_exchange_end(exch);
|
---|
| 301 |
|
---|
[e25a849] | 302 | if (rc != EOK) {
|
---|
[50b581d] | 303 | async_forget(opening_request);
|
---|
[e25a849] | 304 | return rc;
|
---|
| 305 | }
|
---|
[79ae36dd] | 306 |
|
---|
[e25a849] | 307 | sysarg_t opening_request_rc;
|
---|
| 308 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 309 |
|
---|
[e25a849] | 310 | return (int) opening_request_rc;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | * @}
|
---|
| 315 | */
|
---|