[81dce9f] | 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 | */
|
---|
[160b75e] | 28 | /** @addtogroup libusbhost
|
---|
[81dce9f] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
[c92c13f] | 32 | * USB transfer transaction structures (implementation).
|
---|
[81dce9f] | 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/batch.h>
|
---|
[df8f3fa] | 40 | #include <usb/host/hcd.h>
|
---|
[81dce9f] | 41 |
|
---|
[70fb822] | 42 | usb_transfer_batch_t * usb_transfer_batch_get(
|
---|
[2cc6e97] | 43 | endpoint_t *ep,
|
---|
[81dce9f] | 44 | char *buffer,
|
---|
[d017cea] | 45 | char *data_buffer,
|
---|
[81dce9f] | 46 | size_t buffer_size,
|
---|
| 47 | char *setup_buffer,
|
---|
| 48 | size_t setup_size,
|
---|
| 49 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 50 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
| 51 | void *arg,
|
---|
| 52 | ddf_fun_t *fun,
|
---|
[2cc6e97] | 53 | void *private_data,
|
---|
[70fb822] | 54 | void (*private_data_dtor)(void *)
|
---|
[81dce9f] | 55 | )
|
---|
| 56 | {
|
---|
[70fb822] | 57 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
| 58 | if (instance) {
|
---|
| 59 | link_initialize(&instance->link);
|
---|
| 60 | instance->ep = ep;
|
---|
| 61 | instance->callback_in = func_in;
|
---|
| 62 | instance->callback_out = func_out;
|
---|
| 63 | instance->arg = arg;
|
---|
| 64 | instance->buffer = buffer;
|
---|
| 65 | instance->data_buffer = data_buffer;
|
---|
| 66 | instance->buffer_size = buffer_size;
|
---|
| 67 | instance->setup_buffer = setup_buffer;
|
---|
| 68 | instance->setup_size = setup_size;
|
---|
| 69 | instance->fun = fun;
|
---|
| 70 | instance->private_data = private_data;
|
---|
| 71 | instance->private_data_dtor = private_data_dtor;
|
---|
| 72 | instance->transfered_size = 0;
|
---|
| 73 | instance->next_step = NULL;
|
---|
| 74 | instance->error = EOK;
|
---|
| 75 | if (instance->ep)
|
---|
| 76 | endpoint_use(instance->ep);
|
---|
| 77 | }
|
---|
| 78 | return instance;
|
---|
[2cc6e97] | 79 | }
|
---|
| 80 | /*----------------------------------------------------------------------------*/
|
---|
[81dce9f] | 81 | /** Mark batch as finished and continue with next step.
|
---|
| 82 | *
|
---|
| 83 | * @param[in] instance Batch structure to use.
|
---|
| 84 | *
|
---|
| 85 | */
|
---|
[f18d82f0] | 86 | void usb_transfer_batch_finish(
|
---|
| 87 | usb_transfer_batch_t *instance, const void *data, size_t size)
|
---|
[81dce9f] | 88 | {
|
---|
| 89 | assert(instance);
|
---|
[f18d82f0] | 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 |
|
---|
[81dce9f] | 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 | */
|
---|
[1387692] | 111 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 112 | {
|
---|
| 113 | assert(instance);
|
---|
| 114 | assert(instance->callback_in);
|
---|
| 115 |
|
---|
| 116 | /* We are data in, we need data */
|
---|
[b5cfeab4] | 117 | if (instance->data_buffer && (instance->buffer != instance->data_buffer))
|
---|
| 118 | memcpy(instance->buffer,
|
---|
| 119 | instance->data_buffer, instance->buffer_size);
|
---|
[81dce9f] | 120 |
|
---|
[026793d] | 121 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed (%zuB): %s.\n",
|
---|
[c4fb5ecd] | 122 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 123 | instance->transfered_size, str_error(instance->error));
|
---|
[81dce9f] | 124 |
|
---|
[d7186cd] | 125 | instance->callback_in(instance->fun, instance->error,
|
---|
| 126 | instance->transfered_size, instance->arg);
|
---|
[81dce9f] | 127 | }
|
---|
| 128 | /*----------------------------------------------------------------------------*/
|
---|
| 129 | /** Get error status and call callback out.
|
---|
| 130 | *
|
---|
| 131 | * @param[in] instance Batch structure to use.
|
---|
| 132 | */
|
---|
[1387692] | 133 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 134 | {
|
---|
| 135 | assert(instance);
|
---|
| 136 | assert(instance->callback_out);
|
---|
| 137 |
|
---|
[026793d] | 138 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed: %s.\n",
|
---|
[c4fb5ecd] | 139 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 140 | str_error(instance->error));
|
---|
[d7186cd] | 141 |
|
---|
[df8f3fa] | 142 | if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
---|
| 143 | && instance->error == EOK) {
|
---|
| 144 | usb_target_t target =
|
---|
| 145 | {instance->ep->address, instance->ep->endpoint};
|
---|
| 146 | reset_ep_if_need(
|
---|
| 147 | fun_to_hcd(instance->fun), target, instance->setup_buffer);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[81dce9f] | 150 | instance->callback_out(instance->fun,
|
---|
[d7186cd] | 151 | instance->error, instance->arg);
|
---|
[81dce9f] | 152 | }
|
---|
[2cc6e97] | 153 | /*----------------------------------------------------------------------------*/
|
---|
| 154 | /** Correctly dispose all used data structures.
|
---|
| 155 | *
|
---|
| 156 | * @param[in] instance Batch structure to use.
|
---|
| 157 | */
|
---|
| 158 | void usb_transfer_batch_dispose(usb_transfer_batch_t *instance)
|
---|
| 159 | {
|
---|
[96e2d01] | 160 | if (!instance)
|
---|
| 161 | return;
|
---|
[c4fb5ecd] | 162 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
| 163 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
[70fb822] | 164 | if (instance->ep) {
|
---|
| 165 | endpoint_release(instance->ep);
|
---|
| 166 | }
|
---|
[2cc6e97] | 167 | if (instance->private_data) {
|
---|
| 168 | assert(instance->private_data_dtor);
|
---|
| 169 | instance->private_data_dtor(instance->private_data);
|
---|
| 170 | }
|
---|
| 171 | free(instance);
|
---|
| 172 | }
|
---|
[81dce9f] | 173 | /**
|
---|
| 174 | * @}
|
---|
| 175 | */
|
---|