[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,
|
---|
| 45 | size_t buffer_size,
|
---|
| 46 | char *setup_buffer,
|
---|
| 47 | size_t setup_size,
|
---|
| 48 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 49 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
| 50 | void *arg,
|
---|
| 51 | ddf_fun_t *fun,
|
---|
[2cc6e97] | 52 | void *private_data,
|
---|
[70fb822] | 53 | void (*private_data_dtor)(void *)
|
---|
[81dce9f] | 54 | )
|
---|
| 55 | {
|
---|
[70fb822] | 56 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
| 57 | if (instance) {
|
---|
| 58 | instance->ep = ep;
|
---|
| 59 | instance->callback_in = func_in;
|
---|
| 60 | instance->callback_out = func_out;
|
---|
| 61 | instance->arg = arg;
|
---|
| 62 | instance->buffer = buffer;
|
---|
| 63 | instance->buffer_size = buffer_size;
|
---|
| 64 | instance->setup_buffer = setup_buffer;
|
---|
| 65 | instance->setup_size = setup_size;
|
---|
| 66 | instance->fun = fun;
|
---|
| 67 | instance->private_data = private_data;
|
---|
| 68 | instance->private_data_dtor = private_data_dtor;
|
---|
| 69 | instance->transfered_size = 0;
|
---|
| 70 | instance->error = EOK;
|
---|
| 71 | if (instance->ep)
|
---|
| 72 | endpoint_use(instance->ep);
|
---|
| 73 | }
|
---|
| 74 | return instance;
|
---|
[2cc6e97] | 75 | }
|
---|
| 76 | /*----------------------------------------------------------------------------*/
|
---|
[81dce9f] | 77 | /** Mark batch as finished and continue with next step.
|
---|
| 78 | *
|
---|
| 79 | * @param[in] instance Batch structure to use.
|
---|
| 80 | *
|
---|
| 81 | */
|
---|
[f18d82f0] | 82 | void usb_transfer_batch_finish(
|
---|
| 83 | usb_transfer_batch_t *instance, const void *data, size_t size)
|
---|
[81dce9f] | 84 | {
|
---|
| 85 | assert(instance);
|
---|
[f18d82f0] | 86 | assert(instance->ep);
|
---|
| 87 | /* we care about the data and there are some to copy */
|
---|
| 88 | if (instance->ep->direction != USB_DIRECTION_OUT
|
---|
| 89 | && data) {
|
---|
| 90 | const size_t min_size =
|
---|
| 91 | size < instance->buffer_size ? size : instance->buffer_size;
|
---|
| 92 | memcpy(instance->buffer, data, min_size);
|
---|
| 93 | }
|
---|
| 94 | if (instance->callback_out)
|
---|
| 95 | usb_transfer_batch_call_out(instance);
|
---|
| 96 | if (instance->callback_in)
|
---|
| 97 | usb_transfer_batch_call_in(instance);
|
---|
| 98 |
|
---|
[81dce9f] | 99 | }
|
---|
| 100 | /*----------------------------------------------------------------------------*/
|
---|
| 101 | /** Prepare data, get error status and call callback in.
|
---|
| 102 | *
|
---|
| 103 | * @param[in] instance Batch structure to use.
|
---|
| 104 | * Copies data from transport buffer, and calls callback with appropriate
|
---|
| 105 | * parameters.
|
---|
| 106 | */
|
---|
[1387692] | 107 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 108 | {
|
---|
| 109 | assert(instance);
|
---|
| 110 | assert(instance->callback_in);
|
---|
| 111 |
|
---|
[026793d] | 112 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed (%zuB): %s.\n",
|
---|
[c4fb5ecd] | 113 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 114 | instance->transfered_size, str_error(instance->error));
|
---|
[81dce9f] | 115 |
|
---|
[d7186cd] | 116 | instance->callback_in(instance->fun, instance->error,
|
---|
| 117 | instance->transfered_size, instance->arg);
|
---|
[81dce9f] | 118 | }
|
---|
| 119 | /*----------------------------------------------------------------------------*/
|
---|
| 120 | /** Get error status and call callback out.
|
---|
| 121 | *
|
---|
| 122 | * @param[in] instance Batch structure to use.
|
---|
| 123 | */
|
---|
[1387692] | 124 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 125 | {
|
---|
| 126 | assert(instance);
|
---|
| 127 | assert(instance->callback_out);
|
---|
| 128 |
|
---|
[026793d] | 129 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed: %s.\n",
|
---|
[c4fb5ecd] | 130 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 131 | str_error(instance->error));
|
---|
[d7186cd] | 132 |
|
---|
[df8f3fa] | 133 | if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
---|
| 134 | && instance->error == EOK) {
|
---|
| 135 | usb_target_t target =
|
---|
| 136 | {instance->ep->address, instance->ep->endpoint};
|
---|
| 137 | reset_ep_if_need(
|
---|
| 138 | fun_to_hcd(instance->fun), target, instance->setup_buffer);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[81dce9f] | 141 | instance->callback_out(instance->fun,
|
---|
[d7186cd] | 142 | instance->error, instance->arg);
|
---|
[81dce9f] | 143 | }
|
---|
[2cc6e97] | 144 | /*----------------------------------------------------------------------------*/
|
---|
| 145 | /** Correctly dispose all used data structures.
|
---|
| 146 | *
|
---|
| 147 | * @param[in] instance Batch structure to use.
|
---|
| 148 | */
|
---|
| 149 | void usb_transfer_batch_dispose(usb_transfer_batch_t *instance)
|
---|
| 150 | {
|
---|
[96e2d01] | 151 | if (!instance)
|
---|
| 152 | return;
|
---|
[c4fb5ecd] | 153 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
| 154 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
[70fb822] | 155 | if (instance->ep) {
|
---|
| 156 | endpoint_release(instance->ep);
|
---|
| 157 | }
|
---|
[2cc6e97] | 158 | if (instance->private_data) {
|
---|
| 159 | assert(instance->private_data_dtor);
|
---|
| 160 | instance->private_data_dtor(instance->private_data);
|
---|
| 161 | }
|
---|
| 162 | free(instance);
|
---|
| 163 | }
|
---|
[81dce9f] | 164 | /**
|
---|
| 165 | * @}
|
---|
| 166 | */
|
---|