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