| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010-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 libdrv
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <async.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "usbhc_iface.h"
|
|---|
| 40 | #include "ddf/driver.h"
|
|---|
| 41 |
|
|---|
| 42 | #define USB_MAX_PAYLOAD_SIZE 1020
|
|---|
| 43 |
|
|---|
| 44 | static void remote_usbhc_request_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 45 | static void remote_usbhc_bind_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 46 | static void remote_usbhc_find_by_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 47 | static void remote_usbhc_release_address(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 48 | static void remote_usbhc_register_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 49 | static void remote_usbhc_unregister_endpoint(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 50 | static void remote_usbhc_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 51 | static void remote_usbhc_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 52 | //static void remote_usbhc(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 53 |
|
|---|
| 54 | /** Remote USB host controller interface operations. */
|
|---|
| 55 | static remote_iface_func_ptr_t remote_usbhc_iface_ops[] = {
|
|---|
| 56 | [IPC_M_USBHC_REQUEST_ADDRESS] = remote_usbhc_request_address,
|
|---|
| 57 | [IPC_M_USBHC_RELEASE_ADDRESS] = remote_usbhc_release_address,
|
|---|
| 58 | [IPC_M_USBHC_BIND_ADDRESS] = remote_usbhc_bind_address,
|
|---|
| 59 | [IPC_M_USBHC_GET_HANDLE_BY_ADDRESS] = remote_usbhc_find_by_address,
|
|---|
| 60 |
|
|---|
| 61 | [IPC_M_USBHC_REGISTER_ENDPOINT] = remote_usbhc_register_endpoint,
|
|---|
| 62 | [IPC_M_USBHC_UNREGISTER_ENDPOINT] = remote_usbhc_unregister_endpoint,
|
|---|
| 63 |
|
|---|
| 64 | [IPC_M_USBHC_READ] = remote_usbhc_read,
|
|---|
| 65 | [IPC_M_USBHC_WRITE] = remote_usbhc_write,
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | /** Remote USB host controller interface structure.
|
|---|
| 69 | */
|
|---|
| 70 | remote_iface_t remote_usbhc_iface = {
|
|---|
| 71 | .method_count = sizeof(remote_usbhc_iface_ops) /
|
|---|
| 72 | sizeof(remote_usbhc_iface_ops[0]),
|
|---|
| 73 | .methods = remote_usbhc_iface_ops
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | typedef struct {
|
|---|
| 77 | ipc_callid_t caller;
|
|---|
| 78 | ipc_callid_t data_caller;
|
|---|
| 79 | void *buffer;
|
|---|
| 80 | size_t size;
|
|---|
| 81 | } async_transaction_t;
|
|---|
| 82 |
|
|---|
| 83 | static void async_transaction_destroy(async_transaction_t *trans)
|
|---|
| 84 | {
|
|---|
| 85 | if (trans == NULL) {
|
|---|
| 86 | return;
|
|---|
| 87 | }
|
|---|
| 88 | if (trans->buffer != NULL) {
|
|---|
| 89 | free(trans->buffer);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | free(trans);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static async_transaction_t *async_transaction_create(ipc_callid_t caller)
|
|---|
| 96 | {
|
|---|
| 97 | async_transaction_t *trans = malloc(sizeof(async_transaction_t));
|
|---|
| 98 | if (trans == NULL) {
|
|---|
| 99 | return NULL;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | trans->caller = caller;
|
|---|
| 103 | trans->data_caller = 0;
|
|---|
| 104 | trans->buffer = NULL;
|
|---|
| 105 | trans->size = 0;
|
|---|
| 106 |
|
|---|
| 107 | return trans;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | void remote_usbhc_request_address(ddf_fun_t *fun, void *iface,
|
|---|
| 111 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 112 | {
|
|---|
| 113 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 114 |
|
|---|
| 115 | if (!usb_iface->request_address) {
|
|---|
| 116 | async_answer_0(callid, ENOTSUP);
|
|---|
| 117 | return;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | usb_address_t address = DEV_IPC_GET_ARG1(*call);
|
|---|
| 121 | const bool strict = DEV_IPC_GET_ARG2(*call);
|
|---|
| 122 | const usb_speed_t speed = DEV_IPC_GET_ARG3(*call);
|
|---|
| 123 |
|
|---|
| 124 | const int rc = usb_iface->request_address(fun, &address, strict, speed);
|
|---|
| 125 | if (rc != EOK) {
|
|---|
| 126 | async_answer_0(callid, rc);
|
|---|
| 127 | } else {
|
|---|
| 128 | async_answer_1(callid, EOK, (sysarg_t) address);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void remote_usbhc_bind_address(ddf_fun_t *fun, void *iface,
|
|---|
| 133 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 134 | {
|
|---|
| 135 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 136 |
|
|---|
| 137 | if (!usb_iface->bind_address) {
|
|---|
| 138 | async_answer_0(callid, ENOTSUP);
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 143 | devman_handle_t handle = (devman_handle_t) DEV_IPC_GET_ARG2(*call);
|
|---|
| 144 |
|
|---|
| 145 | int rc = usb_iface->bind_address(fun, address, handle);
|
|---|
| 146 |
|
|---|
| 147 | async_answer_0(callid, rc);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | void remote_usbhc_find_by_address(ddf_fun_t *fun, void *iface,
|
|---|
| 151 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 152 | {
|
|---|
| 153 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 154 |
|
|---|
| 155 | if (!usb_iface->find_by_address) {
|
|---|
| 156 | async_answer_0(callid, ENOTSUP);
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 161 | devman_handle_t handle;
|
|---|
| 162 | int rc = usb_iface->find_by_address(fun, address, &handle);
|
|---|
| 163 |
|
|---|
| 164 | if (rc == EOK) {
|
|---|
| 165 | async_answer_1(callid, EOK, handle);
|
|---|
| 166 | } else {
|
|---|
| 167 | async_answer_0(callid, rc);
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | void remote_usbhc_release_address(ddf_fun_t *fun, void *iface,
|
|---|
| 172 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 173 | {
|
|---|
| 174 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 175 |
|
|---|
| 176 | if (!usb_iface->release_address) {
|
|---|
| 177 | async_answer_0(callid, ENOTSUP);
|
|---|
| 178 | return;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 182 |
|
|---|
| 183 | int rc = usb_iface->release_address(fun, address);
|
|---|
| 184 |
|
|---|
| 185 | async_answer_0(callid, rc);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | static void callback_out(ddf_fun_t *fun,
|
|---|
| 190 | int outcome, void *arg)
|
|---|
| 191 | {
|
|---|
| 192 | async_transaction_t *trans = (async_transaction_t *)arg;
|
|---|
| 193 |
|
|---|
| 194 | async_answer_0(trans->caller, outcome);
|
|---|
| 195 |
|
|---|
| 196 | async_transaction_destroy(trans);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | static void callback_in(ddf_fun_t *fun,
|
|---|
| 200 | int outcome, size_t actual_size, void *arg)
|
|---|
| 201 | {
|
|---|
| 202 | async_transaction_t *trans = (async_transaction_t *)arg;
|
|---|
| 203 |
|
|---|
| 204 | if (outcome != EOK) {
|
|---|
| 205 | async_answer_0(trans->caller, outcome);
|
|---|
| 206 | if (trans->data_caller) {
|
|---|
| 207 | async_answer_0(trans->data_caller, EINTR);
|
|---|
| 208 | }
|
|---|
| 209 | async_transaction_destroy(trans);
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | trans->size = actual_size;
|
|---|
| 214 |
|
|---|
| 215 | if (trans->data_caller) {
|
|---|
| 216 | async_data_read_finalize(trans->data_caller,
|
|---|
| 217 | trans->buffer, actual_size);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | async_answer_0(trans->caller, EOK);
|
|---|
| 221 |
|
|---|
| 222 | async_transaction_destroy(trans);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | void remote_usbhc_register_endpoint(ddf_fun_t *fun, void *iface,
|
|---|
| 226 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 227 | {
|
|---|
| 228 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 229 |
|
|---|
| 230 | if (!usb_iface->register_endpoint) {
|
|---|
| 231 | async_answer_0(callid, ENOTSUP);
|
|---|
| 232 | return;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | #define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
|
|---|
| 236 | type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) >> 16)
|
|---|
| 237 | #define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
|
|---|
| 238 | type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) & 0xffff)
|
|---|
| 239 |
|
|---|
| 240 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 241 |
|
|---|
| 242 | _INIT_FROM_HIGH_DATA2(usb_transfer_type_t, transfer_type, 2);
|
|---|
| 243 | _INIT_FROM_LOW_DATA2(usb_direction_t, direction, 2);
|
|---|
| 244 |
|
|---|
| 245 | _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
|
|---|
| 246 | _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
|
|---|
| 247 |
|
|---|
| 248 | #undef _INIT_FROM_HIGH_DATA2
|
|---|
| 249 | #undef _INIT_FROM_LOW_DATA2
|
|---|
| 250 |
|
|---|
| 251 | int rc = usb_iface->register_endpoint(fun, target.address,
|
|---|
| 252 | target.endpoint, transfer_type, direction, max_packet_size, interval);
|
|---|
| 253 |
|
|---|
| 254 | async_answer_0(callid, rc);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | void remote_usbhc_unregister_endpoint(ddf_fun_t *fun, void *iface,
|
|---|
| 258 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 259 | {
|
|---|
| 260 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 261 |
|
|---|
| 262 | if (!usb_iface->unregister_endpoint) {
|
|---|
| 263 | async_answer_0(callid, ENOTSUP);
|
|---|
| 264 | return;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 268 | usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG2(*call);
|
|---|
| 269 | usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG3(*call);
|
|---|
| 270 |
|
|---|
| 271 | int rc = usb_iface->unregister_endpoint(fun,
|
|---|
| 272 | address, endpoint, direction);
|
|---|
| 273 |
|
|---|
| 274 | async_answer_0(callid, rc);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | void remote_usbhc_read(
|
|---|
| 278 | ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 279 | {
|
|---|
| 280 | assert(fun);
|
|---|
| 281 | assert(iface);
|
|---|
| 282 | assert(call);
|
|---|
| 283 |
|
|---|
| 284 | const usbhc_iface_t *hc_iface = iface;
|
|---|
| 285 |
|
|---|
| 286 | if (!hc_iface->read) {
|
|---|
| 287 | async_answer_0(callid, ENOTSUP);
|
|---|
| 288 | return;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 292 | const uint64_t setup =
|
|---|
| 293 | ((uint64_t)DEV_IPC_GET_ARG2(*call)) |
|
|---|
| 294 | (((uint64_t)DEV_IPC_GET_ARG3(*call)) << 32);
|
|---|
| 295 |
|
|---|
| 296 | async_transaction_t *trans = async_transaction_create(callid);
|
|---|
| 297 | if (trans == NULL) {
|
|---|
| 298 | async_answer_0(callid, ENOMEM);
|
|---|
| 299 | return;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | if (!async_data_read_receive(&trans->data_caller, &trans->size)) {
|
|---|
| 303 | async_answer_0(callid, EPARTY);
|
|---|
| 304 | return;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | trans->buffer = malloc(trans->size);
|
|---|
| 308 | if (trans->buffer == NULL) {
|
|---|
| 309 | async_answer_0(trans->data_caller, ENOMEM);
|
|---|
| 310 | async_answer_0(callid, ENOMEM);
|
|---|
| 311 | async_transaction_destroy(trans);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | const int rc = hc_iface->read(
|
|---|
| 315 | fun, target, setup, trans->buffer, trans->size, callback_in, trans);
|
|---|
| 316 |
|
|---|
| 317 | if (rc != EOK) {
|
|---|
| 318 | async_answer_0(trans->data_caller, rc);
|
|---|
| 319 | async_answer_0(callid, rc);
|
|---|
| 320 | async_transaction_destroy(trans);
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | void remote_usbhc_write(
|
|---|
| 325 | ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 326 | {
|
|---|
| 327 | assert(fun);
|
|---|
| 328 | assert(iface);
|
|---|
| 329 | assert(call);
|
|---|
| 330 |
|
|---|
| 331 | const usbhc_iface_t *hc_iface = iface;
|
|---|
| 332 |
|
|---|
| 333 | if (!hc_iface->write) {
|
|---|
| 334 | async_answer_0(callid, ENOTSUP);
|
|---|
| 335 | return;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 339 | const size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
|
|---|
| 340 | const uint64_t setup =
|
|---|
| 341 | ((uint64_t)DEV_IPC_GET_ARG3(*call)) |
|
|---|
| 342 | (((uint64_t)DEV_IPC_GET_ARG4(*call)) << 32);
|
|---|
| 343 |
|
|---|
| 344 | async_transaction_t *trans = async_transaction_create(callid);
|
|---|
| 345 | if (trans == NULL) {
|
|---|
| 346 | async_answer_0(callid, ENOMEM);
|
|---|
| 347 | return;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | if (data_buffer_len > 0) {
|
|---|
| 351 | int rc = async_data_write_accept(&trans->buffer, false,
|
|---|
| 352 | 1, USB_MAX_PAYLOAD_SIZE,
|
|---|
| 353 | 0, &trans->size);
|
|---|
| 354 |
|
|---|
| 355 | if (rc != EOK) {
|
|---|
| 356 | async_answer_0(callid, rc);
|
|---|
| 357 | async_transaction_destroy(trans);
|
|---|
| 358 | return;
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | int rc = hc_iface->write(
|
|---|
| 363 | fun, target, setup, trans->buffer, trans->size, callback_out, trans);
|
|---|
| 364 |
|
|---|
| 365 | if (rc != EOK) {
|
|---|
| 366 | async_answer_0(callid, rc);
|
|---|
| 367 | async_transaction_destroy(trans);
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 | /**
|
|---|
| 373 | * @}
|
|---|
| 374 | */
|
|---|