[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 |
|
---|
[2cc6e97] | 42 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance);
|
---|
| 43 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance);
|
---|
| 44 |
|
---|
[1387692] | 45 | void usb_transfer_batch_init(
|
---|
| 46 | usb_transfer_batch_t *instance,
|
---|
[2cc6e97] | 47 | endpoint_t *ep,
|
---|
[81dce9f] | 48 | char *buffer,
|
---|
[d017cea] | 49 | char *data_buffer,
|
---|
[81dce9f] | 50 | size_t buffer_size,
|
---|
| 51 | char *setup_buffer,
|
---|
| 52 | size_t setup_size,
|
---|
| 53 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 54 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
| 55 | void *arg,
|
---|
| 56 | ddf_fun_t *fun,
|
---|
[2cc6e97] | 57 | void *private_data,
|
---|
| 58 | void (*private_data_dtor)(void *p_data)
|
---|
[81dce9f] | 59 | )
|
---|
| 60 | {
|
---|
| 61 | assert(instance);
|
---|
| 62 | link_initialize(&instance->link);
|
---|
[d017cea] | 63 | instance->ep = ep;
|
---|
[81dce9f] | 64 | instance->callback_in = func_in;
|
---|
| 65 | instance->callback_out = func_out;
|
---|
| 66 | instance->arg = arg;
|
---|
| 67 | instance->buffer = buffer;
|
---|
[d017cea] | 68 | instance->data_buffer = data_buffer;
|
---|
[81dce9f] | 69 | instance->buffer_size = buffer_size;
|
---|
| 70 | instance->setup_buffer = setup_buffer;
|
---|
| 71 | instance->setup_size = setup_size;
|
---|
| 72 | instance->fun = fun;
|
---|
| 73 | instance->private_data = private_data;
|
---|
[2cc6e97] | 74 | instance->private_data_dtor = private_data_dtor;
|
---|
[81dce9f] | 75 | instance->transfered_size = 0;
|
---|
| 76 | instance->next_step = NULL;
|
---|
| 77 | instance->error = EOK;
|
---|
[4fd3faf] | 78 | endpoint_use(instance->ep);
|
---|
[81dce9f] | 79 | }
|
---|
| 80 | /*----------------------------------------------------------------------------*/
|
---|
[2cc6e97] | 81 | /** Helper function, calls callback and correctly destroys batch structure.
|
---|
| 82 | *
|
---|
| 83 | * @param[in] instance Batch structure to use.
|
---|
| 84 | */
|
---|
| 85 | void usb_transfer_batch_call_in_and_dispose(usb_transfer_batch_t *instance)
|
---|
| 86 | {
|
---|
| 87 | assert(instance);
|
---|
| 88 | usb_transfer_batch_call_in(instance);
|
---|
| 89 | usb_transfer_batch_dispose(instance);
|
---|
| 90 | }
|
---|
| 91 | /*----------------------------------------------------------------------------*/
|
---|
| 92 | /** Helper function calls callback and correctly destroys batch structure.
|
---|
| 93 | *
|
---|
| 94 | * @param[in] instance Batch structure to use.
|
---|
| 95 | */
|
---|
| 96 | void usb_transfer_batch_call_out_and_dispose(usb_transfer_batch_t *instance)
|
---|
| 97 | {
|
---|
| 98 | assert(instance);
|
---|
| 99 | usb_transfer_batch_call_out(instance);
|
---|
| 100 | usb_transfer_batch_dispose(instance);
|
---|
| 101 | }
|
---|
| 102 | /*----------------------------------------------------------------------------*/
|
---|
[81dce9f] | 103 | /** Mark batch as finished and continue with next step.
|
---|
| 104 | *
|
---|
| 105 | * @param[in] instance Batch structure to use.
|
---|
| 106 | *
|
---|
| 107 | */
|
---|
[0c224b2] | 108 | void usb_transfer_batch_finish(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 109 | {
|
---|
| 110 | assert(instance);
|
---|
[4fd3faf] | 111 | assert(instance->ep);
|
---|
[062b25f] | 112 | assert(instance->next_step);
|
---|
[4fd3faf] | 113 | endpoint_release(instance->ep);
|
---|
[81dce9f] | 114 | instance->next_step(instance);
|
---|
| 115 | }
|
---|
| 116 | /*----------------------------------------------------------------------------*/
|
---|
| 117 | /** Prepare data, get error status and call callback in.
|
---|
| 118 | *
|
---|
| 119 | * @param[in] instance Batch structure to use.
|
---|
| 120 | * Copies data from transport buffer, and calls callback with appropriate
|
---|
| 121 | * parameters.
|
---|
| 122 | */
|
---|
[1387692] | 123 | void usb_transfer_batch_call_in(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 124 | {
|
---|
| 125 | assert(instance);
|
---|
| 126 | assert(instance->callback_in);
|
---|
[d017cea] | 127 | assert(instance->ep);
|
---|
[81dce9f] | 128 |
|
---|
| 129 | /* We are data in, we need data */
|
---|
[b5cfeab4] | 130 | if (instance->data_buffer && (instance->buffer != instance->data_buffer))
|
---|
| 131 | memcpy(instance->buffer,
|
---|
| 132 | instance->data_buffer, instance->buffer_size);
|
---|
[81dce9f] | 133 |
|
---|
[026793d] | 134 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed (%zuB): %s.\n",
|
---|
[c4fb5ecd] | 135 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 136 | instance->transfered_size, str_error(instance->error));
|
---|
[81dce9f] | 137 |
|
---|
[d7186cd] | 138 | instance->callback_in(instance->fun, instance->error,
|
---|
| 139 | instance->transfered_size, instance->arg);
|
---|
[81dce9f] | 140 | }
|
---|
| 141 | /*----------------------------------------------------------------------------*/
|
---|
| 142 | /** Get error status and call callback out.
|
---|
| 143 | *
|
---|
| 144 | * @param[in] instance Batch structure to use.
|
---|
| 145 | */
|
---|
[1387692] | 146 | void usb_transfer_batch_call_out(usb_transfer_batch_t *instance)
|
---|
[81dce9f] | 147 | {
|
---|
| 148 | assert(instance);
|
---|
| 149 | assert(instance->callback_out);
|
---|
| 150 |
|
---|
[026793d] | 151 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed: %s.\n",
|
---|
[c4fb5ecd] | 152 | instance, USB_TRANSFER_BATCH_ARGS(*instance),
|
---|
| 153 | str_error(instance->error));
|
---|
[d7186cd] | 154 |
|
---|
[df8f3fa] | 155 | if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
---|
| 156 | && instance->error == EOK) {
|
---|
| 157 | usb_target_t target =
|
---|
| 158 | {instance->ep->address, instance->ep->endpoint};
|
---|
| 159 | reset_ep_if_need(
|
---|
| 160 | fun_to_hcd(instance->fun), target, instance->setup_buffer);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[81dce9f] | 163 | instance->callback_out(instance->fun,
|
---|
[d7186cd] | 164 | instance->error, instance->arg);
|
---|
[81dce9f] | 165 | }
|
---|
[2cc6e97] | 166 | /*----------------------------------------------------------------------------*/
|
---|
| 167 | /** Correctly dispose all used data structures.
|
---|
| 168 | *
|
---|
| 169 | * @param[in] instance Batch structure to use.
|
---|
| 170 | */
|
---|
| 171 | void usb_transfer_batch_dispose(usb_transfer_batch_t *instance)
|
---|
| 172 | {
|
---|
| 173 | assert(instance);
|
---|
[c4fb5ecd] | 174 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
| 175 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
[2cc6e97] | 176 | if (instance->private_data) {
|
---|
| 177 | assert(instance->private_data_dtor);
|
---|
| 178 | instance->private_data_dtor(instance->private_data);
|
---|
| 179 | }
|
---|
| 180 | free(instance);
|
---|
| 181 | }
|
---|
[81dce9f] | 182 | /**
|
---|
| 183 | * @}
|
---|
| 184 | */
|
---|