[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 <devman.h>
|
---|
| 41 | #include <usbvirt/device.h>
|
---|
| 42 | #include <usbvirt/ipc.h>
|
---|
| 43 | #include <usb/debug.h>
|
---|
| 44 |
|
---|
[42e2172] | 45 | /** Send control read transfer to virtual USB device.
|
---|
| 46 | *
|
---|
[79ae36dd] | 47 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 48 | * @param ep Target endpoint number.
|
---|
| 49 | * @param setup_buffer Setup buffer.
|
---|
| 50 | * @param setup_buffer_size Setup buffer size in bytes.
|
---|
| 51 | * @param data_buffer Data buffer (DATA stage of control transfer).
|
---|
| 52 | * @param data_buffer_size Size of data buffer in bytes.
|
---|
| 53 | * @param data_transfered_size Number of actually transferred bytes.
|
---|
[79ae36dd] | 54 | *
|
---|
[42e2172] | 55 | * @return Error code.
|
---|
[79ae36dd] | 56 | *
|
---|
[42e2172] | 57 | */
|
---|
[79ae36dd] | 58 | int usbvirt_ipc_send_control_read(async_sess_t *sess, void *setup_buffer,
|
---|
| 59 | size_t setup_buffer_size, void *data_buffer, size_t data_buffer_size,
|
---|
| 60 | size_t *data_transfered_size)
|
---|
[e25a849] | 61 | {
|
---|
[79ae36dd] | 62 | if (!sess)
|
---|
[beee81a] | 63 | return EINVAL;
|
---|
[79ae36dd] | 64 |
|
---|
| 65 | if ((setup_buffer == NULL) || (setup_buffer_size == 0))
|
---|
[beee81a] | 66 | return EINVAL;
|
---|
[79ae36dd] | 67 |
|
---|
| 68 | if ((data_buffer == NULL) || (data_buffer_size == 0))
|
---|
[beee81a] | 69 | return EINVAL;
|
---|
[79ae36dd] | 70 |
|
---|
| 71 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 72 |
|
---|
| 73 | aid_t opening_request = async_send_0(exch, IPC_M_USBVIRT_CONTROL_READ,
|
---|
| 74 | NULL);
|
---|
[e25a849] | 75 | if (opening_request == 0) {
|
---|
[79ae36dd] | 76 | async_exchange_end(exch);
|
---|
[e25a849] | 77 | return ENOMEM;
|
---|
| 78 | }
|
---|
[79ae36dd] | 79 |
|
---|
| 80 | int rc = async_data_write_start(exch, setup_buffer, setup_buffer_size);
|
---|
[e25a849] | 81 | if (rc != EOK) {
|
---|
[79ae36dd] | 82 | async_exchange_end(exch);
|
---|
[e25a849] | 83 | async_wait_for(opening_request, NULL);
|
---|
| 84 | return rc;
|
---|
| 85 | }
|
---|
[79ae36dd] | 86 |
|
---|
[e25a849] | 87 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 88 | aid_t data_request = async_data_read(exch, data_buffer, data_buffer_size,
|
---|
[e25a849] | 89 | &data_request_call);
|
---|
[79ae36dd] | 90 |
|
---|
| 91 | async_exchange_end(exch);
|
---|
| 92 |
|
---|
[e25a849] | 93 | if (data_request == 0) {
|
---|
| 94 | async_wait_for(opening_request, NULL);
|
---|
| 95 | return ENOMEM;
|
---|
| 96 | }
|
---|
[79ae36dd] | 97 |
|
---|
[e25a849] | 98 | sysarg_t data_request_rc;
|
---|
| 99 | sysarg_t opening_request_rc;
|
---|
| 100 | async_wait_for(data_request, &data_request_rc);
|
---|
| 101 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 102 |
|
---|
[e25a849] | 103 | if (data_request_rc != EOK) {
|
---|
| 104 | /* Prefer the return code of the opening request. */
|
---|
[79ae36dd] | 105 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 106 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 107 | else
|
---|
[e25a849] | 108 | return (int) data_request_rc;
|
---|
| 109 | }
|
---|
[79ae36dd] | 110 |
|
---|
| 111 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 112 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 113 |
|
---|
| 114 | if (data_transfered_size != NULL)
|
---|
[beee81a] | 115 | *data_transfered_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 116 |
|
---|
[e25a849] | 117 | return EOK;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[42e2172] | 120 | /** Send control write transfer to virtual USB device.
|
---|
| 121 | *
|
---|
[79ae36dd] | 122 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 123 | * @param ep Target endpoint number.
|
---|
| 124 | * @param setup_buffer Setup buffer.
|
---|
| 125 | * @param setup_buffer_size Setup buffer size in bytes.
|
---|
| 126 | * @param data_buffer Data buffer (DATA stage of control transfer).
|
---|
| 127 | * @param data_buffer_size Size of data buffer in bytes.
|
---|
[79ae36dd] | 128 | *
|
---|
[42e2172] | 129 | * @return Error code.
|
---|
[79ae36dd] | 130 | *
|
---|
[42e2172] | 131 | */
|
---|
[79ae36dd] | 132 | int usbvirt_ipc_send_control_write(async_sess_t *sess, void *setup_buffer,
|
---|
| 133 | size_t setup_buffer_size, void *data_buffer, size_t data_buffer_size)
|
---|
[e25a849] | 134 | {
|
---|
[79ae36dd] | 135 | if (!sess)
|
---|
[beee81a] | 136 | return EINVAL;
|
---|
[79ae36dd] | 137 |
|
---|
| 138 | if ((setup_buffer == NULL) || (setup_buffer_size == 0))
|
---|
[beee81a] | 139 | return EINVAL;
|
---|
[79ae36dd] | 140 |
|
---|
| 141 | if ((data_buffer_size > 0) && (data_buffer == NULL))
|
---|
[beee81a] | 142 | return EINVAL;
|
---|
[79ae36dd] | 143 |
|
---|
| 144 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 145 |
|
---|
| 146 | aid_t opening_request = async_send_1(exch, IPC_M_USBVIRT_CONTROL_WRITE,
|
---|
| 147 | data_buffer_size, NULL);
|
---|
[e25a849] | 148 | if (opening_request == 0) {
|
---|
[79ae36dd] | 149 | async_exchange_end(exch);
|
---|
[e25a849] | 150 | return ENOMEM;
|
---|
| 151 | }
|
---|
[79ae36dd] | 152 |
|
---|
| 153 | int rc = async_data_write_start(exch, setup_buffer, setup_buffer_size);
|
---|
[e25a849] | 154 | if (rc != EOK) {
|
---|
[79ae36dd] | 155 | async_exchange_end(exch);
|
---|
[e25a849] | 156 | async_wait_for(opening_request, NULL);
|
---|
| 157 | return rc;
|
---|
| 158 | }
|
---|
[79ae36dd] | 159 |
|
---|
[e25a849] | 160 | if (data_buffer_size > 0) {
|
---|
[79ae36dd] | 161 | rc = async_data_write_start(exch, data_buffer, data_buffer_size);
|
---|
[e25a849] | 162 | if (rc != EOK) {
|
---|
[79ae36dd] | 163 | async_exchange_end(exch);
|
---|
[e25a849] | 164 | async_wait_for(opening_request, NULL);
|
---|
| 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
[79ae36dd] | 168 |
|
---|
| 169 | async_exchange_end(exch);
|
---|
| 170 |
|
---|
[e25a849] | 171 | sysarg_t opening_request_rc;
|
---|
| 172 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 173 |
|
---|
[e25a849] | 174 | return (int) opening_request_rc;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[42e2172] | 177 | /** Request data transfer from virtual USB device.
|
---|
| 178 | *
|
---|
[79ae36dd] | 179 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 180 | * @param ep Target endpoint number.
|
---|
| 181 | * @param tr_type Transfer type (interrupt or bulk).
|
---|
| 182 | * @param data Data buffer.
|
---|
| 183 | * @param data_size Size of the data buffer in bytes.
|
---|
| 184 | * @param act_size Number of actually returned bytes.
|
---|
[79ae36dd] | 185 | *
|
---|
[42e2172] | 186 | * @return Error code.
|
---|
[79ae36dd] | 187 | *
|
---|
[42e2172] | 188 | */
|
---|
[79ae36dd] | 189 | int usbvirt_ipc_send_data_in(async_sess_t *sess, usb_endpoint_t ep,
|
---|
[e25a849] | 190 | usb_transfer_type_t tr_type, void *data, size_t data_size, size_t *act_size)
|
---|
| 191 | {
|
---|
[79ae36dd] | 192 | if (!sess)
|
---|
[beee81a] | 193 | return EINVAL;
|
---|
[79ae36dd] | 194 |
|
---|
[beee81a] | 195 | usbvirt_hc_to_device_method_t method;
|
---|
[79ae36dd] | 196 |
|
---|
[beee81a] | 197 | switch (tr_type) {
|
---|
| 198 | case USB_TRANSFER_INTERRUPT:
|
---|
| 199 | method = IPC_M_USBVIRT_INTERRUPT_IN;
|
---|
| 200 | break;
|
---|
| 201 | case USB_TRANSFER_BULK:
|
---|
| 202 | method = IPC_M_USBVIRT_BULK_IN;
|
---|
| 203 | break;
|
---|
| 204 | default:
|
---|
| 205 | return EINVAL;
|
---|
| 206 | }
|
---|
[79ae36dd] | 207 |
|
---|
| 208 | if ((ep <= 0) || (ep >= USBVIRT_ENDPOINT_MAX))
|
---|
[beee81a] | 209 | return EINVAL;
|
---|
[79ae36dd] | 210 |
|
---|
| 211 | if ((data == NULL) || (data_size == 0))
|
---|
[beee81a] | 212 | return EINVAL;
|
---|
[79ae36dd] | 213 |
|
---|
| 214 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 215 |
|
---|
| 216 | aid_t opening_request = async_send_2(exch, method, ep, tr_type, NULL);
|
---|
[e25a849] | 217 | if (opening_request == 0) {
|
---|
[79ae36dd] | 218 | async_exchange_end(exch);
|
---|
[e25a849] | 219 | return ENOMEM;
|
---|
| 220 | }
|
---|
[79ae36dd] | 221 |
|
---|
[e25a849] | 222 | ipc_call_t data_request_call;
|
---|
[79ae36dd] | 223 | aid_t data_request = async_data_read(exch, data, data_size,
|
---|
| 224 | &data_request_call);
|
---|
| 225 |
|
---|
| 226 | async_exchange_end(exch);
|
---|
| 227 |
|
---|
[e25a849] | 228 | if (data_request == 0) {
|
---|
| 229 | async_wait_for(opening_request, NULL);
|
---|
| 230 | return ENOMEM;
|
---|
| 231 | }
|
---|
[79ae36dd] | 232 |
|
---|
[e25a849] | 233 | sysarg_t data_request_rc;
|
---|
| 234 | sysarg_t opening_request_rc;
|
---|
| 235 | async_wait_for(data_request, &data_request_rc);
|
---|
| 236 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 237 |
|
---|
[e25a849] | 238 | if (data_request_rc != EOK) {
|
---|
| 239 | /* Prefer the return code of the opening request. */
|
---|
[79ae36dd] | 240 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 241 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 242 | else
|
---|
[e25a849] | 243 | return (int) data_request_rc;
|
---|
| 244 | }
|
---|
[79ae36dd] | 245 |
|
---|
| 246 | if (opening_request_rc != EOK)
|
---|
[e25a849] | 247 | return (int) opening_request_rc;
|
---|
[79ae36dd] | 248 |
|
---|
| 249 | if (act_size != NULL)
|
---|
[e25a849] | 250 | *act_size = IPC_GET_ARG2(data_request_call);
|
---|
[79ae36dd] | 251 |
|
---|
[e25a849] | 252 | return EOK;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[42e2172] | 255 | /** Send data to virtual USB device.
|
---|
| 256 | *
|
---|
[79ae36dd] | 257 | * @param sess Session to the virtual device.
|
---|
[42e2172] | 258 | * @param ep Target endpoint number.
|
---|
| 259 | * @param tr_type Transfer type (interrupt or bulk).
|
---|
| 260 | * @param data Data buffer.
|
---|
| 261 | * @param data_size Size of the data buffer in bytes.
|
---|
[79ae36dd] | 262 | *
|
---|
[42e2172] | 263 | * @return Error code.
|
---|
[79ae36dd] | 264 | *
|
---|
[42e2172] | 265 | */
|
---|
[79ae36dd] | 266 | int usbvirt_ipc_send_data_out(async_sess_t *sess, usb_endpoint_t ep,
|
---|
[e25a849] | 267 | usb_transfer_type_t tr_type, void *data, size_t data_size)
|
---|
| 268 | {
|
---|
[79ae36dd] | 269 | if (!sess)
|
---|
[beee81a] | 270 | return EINVAL;
|
---|
[79ae36dd] | 271 |
|
---|
[beee81a] | 272 | usbvirt_hc_to_device_method_t method;
|
---|
[79ae36dd] | 273 |
|
---|
[beee81a] | 274 | switch (tr_type) {
|
---|
| 275 | case USB_TRANSFER_INTERRUPT:
|
---|
| 276 | method = IPC_M_USBVIRT_INTERRUPT_OUT;
|
---|
| 277 | break;
|
---|
| 278 | case USB_TRANSFER_BULK:
|
---|
| 279 | method = IPC_M_USBVIRT_BULK_OUT;
|
---|
| 280 | break;
|
---|
| 281 | default:
|
---|
| 282 | return EINVAL;
|
---|
| 283 | }
|
---|
[79ae36dd] | 284 |
|
---|
| 285 | if ((ep <= 0) || (ep >= USBVIRT_ENDPOINT_MAX))
|
---|
[beee81a] | 286 | return EINVAL;
|
---|
[79ae36dd] | 287 |
|
---|
| 288 | if ((data == NULL) || (data_size == 0))
|
---|
[beee81a] | 289 | return EINVAL;
|
---|
[79ae36dd] | 290 |
|
---|
| 291 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 292 |
|
---|
| 293 | aid_t opening_request = async_send_1(exch, method, ep, NULL);
|
---|
[e25a849] | 294 | if (opening_request == 0) {
|
---|
[79ae36dd] | 295 | async_exchange_end(exch);
|
---|
[e25a849] | 296 | return ENOMEM;
|
---|
| 297 | }
|
---|
[79ae36dd] | 298 |
|
---|
| 299 | int rc = async_data_write_start(exch, data, data_size);
|
---|
| 300 |
|
---|
| 301 | async_exchange_end(exch);
|
---|
| 302 |
|
---|
[e25a849] | 303 | if (rc != EOK) {
|
---|
| 304 | async_wait_for(opening_request, NULL);
|
---|
| 305 | return rc;
|
---|
| 306 | }
|
---|
[79ae36dd] | 307 |
|
---|
[e25a849] | 308 | sysarg_t opening_request_rc;
|
---|
| 309 | async_wait_for(opening_request, &opening_request_rc);
|
---|
[79ae36dd] | 310 |
|
---|
[e25a849] | 311 | return (int) opening_request_rc;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /**
|
---|
| 315 | * @}
|
---|
| 316 | */
|
---|