[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>
|
---|
[3f162ab] | 35 | #include <macros.h>
|
---|
[81dce9f] | 36 |
|
---|
| 37 | #include <usb/usb.h>
|
---|
| 38 | #include <usb/debug.h>
|
---|
[549ff23] | 39 |
|
---|
[f58ef61] | 40 | #include <usb/host/usb_transfer_batch.h>
|
---|
[df8f3fa] | 41 | #include <usb/host/hcd.h>
|
---|
[81dce9f] | 42 |
|
---|
[549ff23] | 43 | /** Allocate and initialize usb_transfer_batch structure.
|
---|
| 44 | * @param ep endpoint used by the transfer batch.
|
---|
| 45 | * @param buffer data to send/recieve.
|
---|
| 46 | * @param buffer_size Size of data buffer.
|
---|
| 47 | * @param setup_buffer Data to send in SETUP stage of control transfer.
|
---|
| 48 | * @param func_in callback on IN transfer completion.
|
---|
| 49 | * @param func_out callback on OUT transfer completion.
|
---|
[cbd568b] | 50 | * @param fun DDF function (passed to callback function).
|
---|
[549ff23] | 51 | * @param arg Argument to pass to the callback function.
|
---|
| 52 | * @param private_data driver specific per batch data.
|
---|
| 53 | * @param private_data_dtor Function to properly destroy private_data.
|
---|
| 54 | * @return Pointer to valid usb_transfer_batch_t structure, NULL on failure.
|
---|
| 55 | */
|
---|
| 56 | usb_transfer_batch_t * usb_transfer_batch_create(
|
---|
[2cc6e97] | 57 | endpoint_t *ep,
|
---|
[81dce9f] | 58 | char *buffer,
|
---|
| 59 | size_t buffer_size,
|
---|
[a837544] | 60 | uint64_t setup_buffer,
|
---|
[81dce9f] | 61 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
| 62 | usbhc_iface_transfer_out_callback_t func_out,
|
---|
| 63 | void *arg,
|
---|
| 64 | ddf_fun_t *fun,
|
---|
[2cc6e97] | 65 | void *private_data,
|
---|
[70fb822] | 66 | void (*private_data_dtor)(void *)
|
---|
[81dce9f] | 67 | )
|
---|
| 68 | {
|
---|
[549ff23] | 69 | if (func_in == NULL && func_out == NULL)
|
---|
| 70 | return NULL;
|
---|
| 71 | if (func_in != NULL && func_out != NULL)
|
---|
| 72 | return NULL;
|
---|
| 73 |
|
---|
[70fb822] | 74 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
| 75 | if (instance) {
|
---|
| 76 | instance->ep = ep;
|
---|
| 77 | instance->callback_in = func_in;
|
---|
| 78 | instance->callback_out = func_out;
|
---|
| 79 | instance->arg = arg;
|
---|
| 80 | instance->buffer = buffer;
|
---|
| 81 | instance->buffer_size = buffer_size;
|
---|
[a00ac07] | 82 | instance->setup_size = 0;
|
---|
[70fb822] | 83 | instance->fun = fun;
|
---|
| 84 | instance->private_data = private_data;
|
---|
| 85 | instance->private_data_dtor = private_data_dtor;
|
---|
| 86 | instance->transfered_size = 0;
|
---|
| 87 | instance->error = EOK;
|
---|
[a00ac07] | 88 | if (ep && ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
[a837544] | 89 | memcpy(instance->setup_buffer, &setup_buffer,
|
---|
[a00ac07] | 90 | USB_SETUP_PACKET_SIZE);
|
---|
| 91 | instance->setup_size = USB_SETUP_PACKET_SIZE;
|
---|
| 92 | }
|
---|
[70fb822] | 93 | if (instance->ep)
|
---|
| 94 | endpoint_use(instance->ep);
|
---|
| 95 | }
|
---|
| 96 | return instance;
|
---|
[2cc6e97] | 97 | }
|
---|
[a76b01b4] | 98 |
|
---|
[2cc6e97] | 99 | /** Correctly dispose all used data structures.
|
---|
| 100 | *
|
---|
| 101 | * @param[in] instance Batch structure to use.
|
---|
| 102 | */
|
---|
[549ff23] | 103 | void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance)
|
---|
[2cc6e97] | 104 | {
|
---|
[96e2d01] | 105 | if (!instance)
|
---|
| 106 | return;
|
---|
[c4fb5ecd] | 107 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
---|
| 108 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
[70fb822] | 109 | if (instance->ep) {
|
---|
| 110 | endpoint_release(instance->ep);
|
---|
| 111 | }
|
---|
[2cc6e97] | 112 | if (instance->private_data) {
|
---|
| 113 | assert(instance->private_data_dtor);
|
---|
| 114 | instance->private_data_dtor(instance->private_data);
|
---|
| 115 | }
|
---|
| 116 | free(instance);
|
---|
| 117 | }
|
---|
[a76b01b4] | 118 |
|
---|
[549ff23] | 119 | /** Prepare data and call the right callback.
|
---|
| 120 | *
|
---|
| 121 | * @param[in] instance Batch structure to use.
|
---|
| 122 | * @param[in] data Data to copy to the output buffer.
|
---|
| 123 | * @param[in] size Size of @p data.
|
---|
[f167f55b] | 124 | * @param[in] error Error value to use.
|
---|
[549ff23] | 125 | */
|
---|
[f167f55b] | 126 | void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
|
---|
| 127 | const void *data, size_t size, int error)
|
---|
[549ff23] | 128 | {
|
---|
| 129 | assert(instance);
|
---|
| 130 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
|
---|
| 131 | instance, USB_TRANSFER_BATCH_ARGS(*instance));
|
---|
| 132 |
|
---|
| 133 | /* NOTE: Only one of these pointers should be set. */
|
---|
| 134 | if (instance->callback_out) {
|
---|
| 135 | /* Check for commands that reset toggle bit */
|
---|
| 136 | if (instance->ep->transfer_type == USB_TRANSFER_CONTROL
|
---|
[f167f55b] | 137 | && error == EOK) {
|
---|
[549ff23] | 138 | const usb_target_t target =
|
---|
| 139 | {{ instance->ep->address, instance->ep->endpoint }};
|
---|
| 140 | reset_ep_if_need(fun_to_hcd(instance->fun), target,
|
---|
| 141 | instance->setup_buffer);
|
---|
| 142 | }
|
---|
[f167f55b] | 143 | instance->callback_out(instance->fun, error, instance->arg);
|
---|
[549ff23] | 144 | }
|
---|
| 145 |
|
---|
| 146 | if (instance->callback_in) {
|
---|
| 147 | /* We care about the data and there are some to copy */
|
---|
[2582d8f] | 148 | const size_t safe_size = min(size, instance->buffer_size);
|
---|
[549ff23] | 149 | if (data) {
|
---|
[2582d8f] | 150 | memcpy(instance->buffer, data, safe_size);
|
---|
[549ff23] | 151 | }
|
---|
[f167f55b] | 152 | instance->callback_in(instance->fun, error,
|
---|
[2582d8f] | 153 | safe_size, instance->arg);
|
---|
[549ff23] | 154 | }
|
---|
| 155 | }
|
---|
[81dce9f] | 156 | /**
|
---|
| 157 | * @}
|
---|
| 158 | */
|
---|