| 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_BIND_ADDRESS] = remote_usbhc_bind_address,
|
|---|
| 58 | [IPC_M_USBHC_GET_HANDLE_BY_ADDRESS] = remote_usbhc_find_by_address,
|
|---|
| 59 | [IPC_M_USBHC_RELEASE_ADDRESS] = remote_usbhc_release_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_speed_t speed = DEV_IPC_GET_ARG1(*call);
|
|---|
| 121 |
|
|---|
| 122 | usb_address_t address;
|
|---|
| 123 | int rc = usb_iface->request_address(fun, speed, &address);
|
|---|
| 124 | if (rc != EOK) {
|
|---|
| 125 | async_answer_0(callid, rc);
|
|---|
| 126 | } else {
|
|---|
| 127 | async_answer_1(callid, EOK, (sysarg_t) address);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void remote_usbhc_bind_address(ddf_fun_t *fun, void *iface,
|
|---|
| 132 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 133 | {
|
|---|
| 134 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 135 |
|
|---|
| 136 | if (!usb_iface->bind_address) {
|
|---|
| 137 | async_answer_0(callid, ENOTSUP);
|
|---|
| 138 | return;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 142 | devman_handle_t handle = (devman_handle_t) DEV_IPC_GET_ARG2(*call);
|
|---|
| 143 |
|
|---|
| 144 | int rc = usb_iface->bind_address(fun, address, handle);
|
|---|
| 145 |
|
|---|
| 146 | async_answer_0(callid, rc);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void remote_usbhc_find_by_address(ddf_fun_t *fun, void *iface,
|
|---|
| 150 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 151 | {
|
|---|
| 152 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 153 |
|
|---|
| 154 | if (!usb_iface->find_by_address) {
|
|---|
| 155 | async_answer_0(callid, ENOTSUP);
|
|---|
| 156 | return;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 160 | devman_handle_t handle;
|
|---|
| 161 | int rc = usb_iface->find_by_address(fun, address, &handle);
|
|---|
| 162 |
|
|---|
| 163 | if (rc == EOK) {
|
|---|
| 164 | async_answer_1(callid, EOK, handle);
|
|---|
| 165 | } else {
|
|---|
| 166 | async_answer_0(callid, rc);
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | void remote_usbhc_release_address(ddf_fun_t *fun, void *iface,
|
|---|
| 171 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 172 | {
|
|---|
| 173 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 174 |
|
|---|
| 175 | if (!usb_iface->release_address) {
|
|---|
| 176 | async_answer_0(callid, ENOTSUP);
|
|---|
| 177 | return;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 181 |
|
|---|
| 182 | int rc = usb_iface->release_address(fun, address);
|
|---|
| 183 |
|
|---|
| 184 | async_answer_0(callid, rc);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | static void callback_out(ddf_fun_t *fun,
|
|---|
| 189 | int outcome, void *arg)
|
|---|
| 190 | {
|
|---|
| 191 | async_transaction_t *trans = (async_transaction_t *)arg;
|
|---|
| 192 |
|
|---|
| 193 | async_answer_0(trans->caller, outcome);
|
|---|
| 194 |
|
|---|
| 195 | async_transaction_destroy(trans);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static void callback_in(ddf_fun_t *fun,
|
|---|
| 199 | int outcome, size_t actual_size, void *arg)
|
|---|
| 200 | {
|
|---|
| 201 | async_transaction_t *trans = (async_transaction_t *)arg;
|
|---|
| 202 |
|
|---|
| 203 | if (outcome != EOK) {
|
|---|
| 204 | async_answer_0(trans->caller, outcome);
|
|---|
| 205 | if (trans->data_caller) {
|
|---|
| 206 | async_answer_0(trans->data_caller, EINTR);
|
|---|
| 207 | }
|
|---|
| 208 | async_transaction_destroy(trans);
|
|---|
| 209 | return;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | trans->size = actual_size;
|
|---|
| 213 |
|
|---|
| 214 | if (trans->data_caller) {
|
|---|
| 215 | async_data_read_finalize(trans->data_caller,
|
|---|
| 216 | trans->buffer, actual_size);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | async_answer_0(trans->caller, EOK);
|
|---|
| 220 |
|
|---|
| 221 | async_transaction_destroy(trans);
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | void remote_usbhc_register_endpoint(ddf_fun_t *fun, void *iface,
|
|---|
| 225 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 226 | {
|
|---|
| 227 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 228 |
|
|---|
| 229 | if (!usb_iface->register_endpoint) {
|
|---|
| 230 | async_answer_0(callid, ENOTSUP);
|
|---|
| 231 | return;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | #define _INIT_FROM_HIGH_DATA2(type, var, arg_no) \
|
|---|
| 235 | type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
|
|---|
| 236 | #define _INIT_FROM_LOW_DATA2(type, var, arg_no) \
|
|---|
| 237 | type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 16)
|
|---|
| 238 | #define _INIT_FROM_HIGH_DATA3(type, var, arg_no) \
|
|---|
| 239 | type var = (type) DEV_IPC_GET_ARG##arg_no(*call) / (1 << 16)
|
|---|
| 240 | #define _INIT_FROM_MIDDLE_DATA3(type, var, arg_no) \
|
|---|
| 241 | type var = (type) (DEV_IPC_GET_ARG##arg_no(*call) / (1 << 8)) % (1 << 8)
|
|---|
| 242 | #define _INIT_FROM_LOW_DATA3(type, var, arg_no) \
|
|---|
| 243 | type var = (type) DEV_IPC_GET_ARG##arg_no(*call) % (1 << 8)
|
|---|
| 244 |
|
|---|
| 245 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 246 |
|
|---|
| 247 | _INIT_FROM_HIGH_DATA3(usb_speed_t, speed, 2);
|
|---|
| 248 | _INIT_FROM_MIDDLE_DATA3(usb_transfer_type_t, transfer_type, 2);
|
|---|
| 249 | _INIT_FROM_LOW_DATA3(usb_direction_t, direction, 2);
|
|---|
| 250 |
|
|---|
| 251 | _INIT_FROM_HIGH_DATA2(size_t, max_packet_size, 3);
|
|---|
| 252 | _INIT_FROM_LOW_DATA2(unsigned int, interval, 3);
|
|---|
| 253 |
|
|---|
| 254 | #undef _INIT_FROM_HIGH_DATA2
|
|---|
| 255 | #undef _INIT_FROM_LOW_DATA2
|
|---|
| 256 | #undef _INIT_FROM_HIGH_DATA3
|
|---|
| 257 | #undef _INIT_FROM_MIDDLE_DATA3
|
|---|
| 258 | #undef _INIT_FROM_LOW_DATA3
|
|---|
| 259 |
|
|---|
| 260 | int rc = usb_iface->register_endpoint(fun, target.address, speed,
|
|---|
| 261 | target.endpoint, transfer_type, direction, max_packet_size, interval);
|
|---|
| 262 |
|
|---|
| 263 | async_answer_0(callid, rc);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | void remote_usbhc_unregister_endpoint(ddf_fun_t *fun, void *iface,
|
|---|
| 267 | ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 268 | {
|
|---|
| 269 | usbhc_iface_t *usb_iface = (usbhc_iface_t *) iface;
|
|---|
| 270 |
|
|---|
| 271 | if (!usb_iface->unregister_endpoint) {
|
|---|
| 272 | async_answer_0(callid, ENOTSUP);
|
|---|
| 273 | return;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | usb_address_t address = (usb_address_t) DEV_IPC_GET_ARG1(*call);
|
|---|
| 277 | usb_endpoint_t endpoint = (usb_endpoint_t) DEV_IPC_GET_ARG2(*call);
|
|---|
| 278 | usb_direction_t direction = (usb_direction_t) DEV_IPC_GET_ARG3(*call);
|
|---|
| 279 |
|
|---|
| 280 | int rc = usb_iface->unregister_endpoint(fun,
|
|---|
| 281 | address, endpoint, direction);
|
|---|
| 282 |
|
|---|
| 283 | async_answer_0(callid, rc);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | void remote_usbhc_read(
|
|---|
| 287 | ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 288 | {
|
|---|
| 289 | assert(fun);
|
|---|
| 290 | assert(iface);
|
|---|
| 291 | assert(call);
|
|---|
| 292 |
|
|---|
| 293 | const usbhc_iface_t *hc_iface = iface;
|
|---|
| 294 |
|
|---|
| 295 | if (!hc_iface->read) {
|
|---|
| 296 | async_answer_0(callid, ENOTSUP);
|
|---|
| 297 | return;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 301 | const uint64_t setup =
|
|---|
| 302 | ((uint64_t)DEV_IPC_GET_ARG2(*call)) |
|
|---|
| 303 | (((uint64_t)DEV_IPC_GET_ARG3(*call)) << 32);
|
|---|
| 304 |
|
|---|
| 305 | async_transaction_t *trans = async_transaction_create(callid);
|
|---|
| 306 | if (trans == NULL) {
|
|---|
| 307 | async_answer_0(callid, ENOMEM);
|
|---|
| 308 | return;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | if (!async_data_read_receive(&trans->data_caller, &trans->size)) {
|
|---|
| 312 | async_answer_0(callid, EPARTY);
|
|---|
| 313 | return;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | trans->buffer = malloc(trans->size);
|
|---|
| 317 | if (trans->buffer == NULL) {
|
|---|
| 318 | async_answer_0(trans->data_caller, ENOMEM);
|
|---|
| 319 | async_answer_0(callid, ENOMEM);
|
|---|
| 320 | async_transaction_destroy(trans);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | const int rc = hc_iface->read(
|
|---|
| 324 | fun, target, setup, trans->buffer, trans->size, callback_in, trans);
|
|---|
| 325 |
|
|---|
| 326 | if (rc != EOK) {
|
|---|
| 327 | async_answer_0(trans->data_caller, rc);
|
|---|
| 328 | async_answer_0(callid, rc);
|
|---|
| 329 | async_transaction_destroy(trans);
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | void remote_usbhc_write(
|
|---|
| 334 | ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 335 | {
|
|---|
| 336 | assert(fun);
|
|---|
| 337 | assert(iface);
|
|---|
| 338 | assert(call);
|
|---|
| 339 |
|
|---|
| 340 | const usbhc_iface_t *hc_iface = iface;
|
|---|
| 341 |
|
|---|
| 342 | if (!hc_iface->write) {
|
|---|
| 343 | async_answer_0(callid, ENOTSUP);
|
|---|
| 344 | return;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | const usb_target_t target = { .packed = DEV_IPC_GET_ARG1(*call) };
|
|---|
| 348 | const size_t data_buffer_len = DEV_IPC_GET_ARG2(*call);
|
|---|
| 349 | const uint64_t setup =
|
|---|
| 350 | ((uint64_t)DEV_IPC_GET_ARG3(*call)) |
|
|---|
| 351 | (((uint64_t)DEV_IPC_GET_ARG4(*call)) << 32);
|
|---|
| 352 |
|
|---|
| 353 | async_transaction_t *trans = async_transaction_create(callid);
|
|---|
| 354 | if (trans == NULL) {
|
|---|
| 355 | async_answer_0(callid, ENOMEM);
|
|---|
| 356 | return;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (data_buffer_len > 0) {
|
|---|
| 360 | int rc = async_data_write_accept(&trans->buffer, false,
|
|---|
| 361 | 1, USB_MAX_PAYLOAD_SIZE,
|
|---|
| 362 | 0, &trans->size);
|
|---|
| 363 |
|
|---|
| 364 | if (rc != EOK) {
|
|---|
| 365 | async_answer_0(callid, rc);
|
|---|
| 366 | async_transaction_destroy(trans);
|
|---|
| 367 | return;
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | int rc = hc_iface->write(
|
|---|
| 372 | fun, target, setup, trans->buffer, trans->size, callback_out, trans);
|
|---|
| 373 |
|
|---|
| 374 | if (rc != EOK) {
|
|---|
| 375 | async_answer_0(callid, rc);
|
|---|
| 376 | async_transaction_destroy(trans);
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 | /**
|
|---|
| 382 | * @}
|
|---|
| 383 | */
|
|---|