| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 | /** @addtogroup libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * USB transfer transaction structures (implementation).
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 |
|
|---|
| 37 | #include <usb/usb.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 | #include <usb/host/usb_transfer_batch.h>
|
|---|
| 40 | #include <usb/host/hcd.h>
|
|---|
| 41 |
|
|---|
| 42 | usb_transfer_batch_t * usb_transfer_batch_get(
|
|---|
| 43 | endpoint_t *ep,
|
|---|
| 44 | char *buffer,
|
|---|
| 45 | size_t buffer_size,
|
|---|
| 46 | uint64_t setup_buffer,
|
|---|
| 47 | usbhc_iface_transfer_in_callback_t func_in,
|
|---|
| 48 | usbhc_iface_transfer_out_callback_t func_out,
|
|---|
| 49 | void *arg,
|
|---|
| 50 | ddf_fun_t *fun,
|
|---|
| 51 | void *private_data,
|
|---|
| 52 | void (*private_data_dtor)(void *)
|
|---|
| 53 | )
|
|---|
| 54 | {
|
|---|
| 55 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
|---|
| 56 | if (instance) {
|
|---|
| 57 | instance->ep = ep;
|
|---|
| 58 | instance->callback_in = func_in;
|
|---|
| 59 | instance->callback_out = func_out;
|
|---|
| 60 | instance->arg = arg;
|
|---|
| 61 | instance->buffer = buffer;
|
|---|
| 62 | instance->buffer_size = buffer_size;
|
|---|
| 63 | instance->setup_size = 0;
|
|---|
| 64 | instance->fun = fun;
|
|---|
| 65 | instance->private_data = private_data;
|
|---|
| 66 | instance->private_data_dtor = private_data_dtor;
|
|---|
| 67 | instance->transfered_size = 0;
|
|---|
| 68 | instance->error = EOK;
|
|---|
| 69 | if (ep && ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 70 | memcpy(instance->setup_buffer, &setup_buffer,
|
|---|
| 71 | USB_SETUP_PACKET_SIZE);
|
|---|
| 72 | instance->setup_size = USB_SETUP_PACKET_SIZE;
|
|---|
| 73 | }
|
|---|
| 74 | if (instance->ep)
|
|---|
| 75 | endpoint_use(instance->ep);
|
|---|
| 76 | }
|
|---|
| 77 | return instance;
|
|---|
| 78 | }
|
|---|
| 79 | /*----------------------------------------------------------------------------*/
|
|---|
| 80 | /** Mark batch as finished and run callback.
|
|---|
| 81 | *
|
|---|
| 82 | * @param[in] instance Batch structure to use.
|
|---|
| 83 | * @param[in] data Data to copy to the output buffer.
|
|---|
| 84 | * @param[in] size Size of @p data.
|
|---|
| 85 | */
|
|---|
| 86 | void usb_transfer_batch_finish(
|
|---|
| 87 | usb_transfer_batch_t *instance, const void *data, size_t size)
|
|---|
| 88 | {
|
|---|
| 89 | assert(instance);
|
|---|
| 90 | assert(instance->ep);
|
|---|
| 91 | /* we care about the data and there are some to copy */
|
|---|
| 92 | if (instance->ep->direction != USB_DIRECTION_OUT
|
|---|
| 93 | && data) {
|
|---|
| 94 | const size_t min_size =
|
|---|
| 95 | size < instance->buffer_size ? size : instance->buffer_size;
|
|---|
| 96 | memcpy(instance->buffer, data, min_size);
|
|---|
| 97 | }
|
|---|
| 98 | if (instance->callback_out)
|
|---|
| 99 | usb_transfer_batch_call_out(instance);
|
|---|
| 100 | if (instance->callback_in)
|
|---|
| 101 | usb_transfer_batch_call_in(instance);
|
|---|
| 102 |
|
|---|
| 103 | }
|
|---|
| 104 | /*----------------------------------------------------------------------------*/
|
|---|
| 105 | /** Prepare data, get error status and call callback in.
|
|---|
| 106 | *
|
|---|
| 107 | * @param[in] instance Batch structure to use.
|
|---|
| 108 | * Copies data from transport buffer, and calls callback with appropriate
|
|---|
| 109 | * parameters.
|
|---|
| 110 | */
|
|---|
| 111 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance)
|
|---|
| 112 | {
|
|---|
| 113 | assert(instance);
|
|---|
| 114 | assert(instance->callback_in);
|
|---|
| 115 |
|
|---|
| 116 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed (%zuB): %s.\n",
|
|---|
| 117 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
|---|
| 118 | instance->transfered_size, str_error(instance->error));
|
|---|
| 119 |
|
|---|
| 120 | instance->callback_in(instance->fun, instance->error,
|
|---|
| 121 | instance->transfered_size, instance->arg);
|
|---|
| 122 | }
|
|---|
| 123 | /*----------------------------------------------------------------------------*/
|
|---|
| 124 | /** Get error status and call callback out.
|
|---|
| 125 | *
|
|---|
| 126 | * @param[in] instance Batch structure to use.
|
|---|
| 127 | */
|
|---|
| 128 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance)
|
|---|
| 129 | {
|
|---|
| 130 | assert(instance);
|
|---|
| 131 | assert(instance->callback_out);
|
|---|
| 132 |
|
|---|
| 133 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed: %s.\n",
|
|---|
| 134 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
|---|
| 135 | str_error(instance->error));
|
|---|
| 136 |
|
|---|
| 137 | if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
|---|
| 138 | && instance->error == EOK) {
|
|---|
| 139 | const usb_target_t target =
|
|---|
| 140 | {{ instance->ep->address, instance->ep->endpoint }};
|
|---|
| 141 | reset_ep_if_need(
|
|---|
| 142 | fun_to_hcd(instance->fun), target, instance->setup_buffer);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | instance->callback_out(instance->fun,
|
|---|
| 146 | instance->error, instance->arg);
|
|---|
| 147 | }
|
|---|
| 148 | /*----------------------------------------------------------------------------*/
|
|---|
| 149 | /** Correctly dispose all used data structures.
|
|---|
| 150 | *
|
|---|
| 151 | * @param[in] instance Batch structure to use.
|
|---|
| 152 | */
|
|---|
| 153 | void usb_transfer_batch_dispose(usb_transfer_batch_t *instance)
|
|---|
| 154 | {
|
|---|
| 155 | if (!instance)
|
|---|
| 156 | return;
|
|---|
| 157 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
|---|
| 158 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
|---|
| 159 | if (instance->ep) {
|
|---|
| 160 | endpoint_release(instance->ep);
|
|---|
| 161 | }
|
|---|
| 162 | if (instance->private_data) {
|
|---|
| 163 | assert(instance->private_data_dtor);
|
|---|
| 164 | instance->private_data_dtor(instance->private_data);
|
|---|
| 165 | }
|
|---|
| 166 | free(instance);
|
|---|
| 167 | }
|
|---|
| 168 | /**
|
|---|
| 169 | * @}
|
|---|
| 170 | */
|
|---|