[4192d3d6] | 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 | */
|
---|
[17ceb72] | 28 | /** @addtogroup drvusbuhcihc
|
---|
[4192d3d6] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
[17ceb72] | 32 | * @brief UHCI driver USB transaction structure
|
---|
[4192d3d6] | 33 | */
|
---|
| 34 | #include <errno.h>
|
---|
[86c2ccd] | 35 | #include <str_error.h>
|
---|
[4192d3d6] | 36 |
|
---|
[bdc8ab1] | 37 | #include <usb/usb.h>
|
---|
[4192d3d6] | 38 | #include <usb/debug.h>
|
---|
| 39 |
|
---|
[83c439c] | 40 | #include "batch.h"
|
---|
[53338bda] | 41 | #include "transfer_list.h"
|
---|
[8850690] | 42 | #include "uhci_hc.h"
|
---|
[9a818a9] | 43 | #include "utils/malloc32.h"
|
---|
[4192d3d6] | 44 |
|
---|
| 45 | #define DEFAULT_ERROR_COUNT 3
|
---|
| 46 |
|
---|
[eae83aa] | 47 | static void batch_control(batch_t *instance,
|
---|
| 48 | usb_packet_id data_stage, usb_packet_id status_stage);
|
---|
[5620bd4] | 49 | static void batch_data(batch_t *instance, usb_packet_id pid);
|
---|
[83c439c] | 50 | static void batch_call_in(batch_t *instance);
|
---|
| 51 | static void batch_call_out(batch_t *instance);
|
---|
| 52 | static void batch_call_in_and_dispose(batch_t *instance);
|
---|
| 53 | static void batch_call_out_and_dispose(batch_t *instance);
|
---|
[4192d3d6] | 54 |
|
---|
| 55 |
|
---|
[17ceb72] | 56 | /** Allocate memory and initialize internal data structure.
|
---|
[a7e2f0d] | 57 | *
|
---|
| 58 | * @param[in] fun DDF function to pass to callback.
|
---|
| 59 | * @param[in] target Device and endpoint target of the transaction.
|
---|
| 60 | * @param[in] transfer_type Interrupt, Control or Bulk.
|
---|
| 61 | * @param[in] max_packet_size maximum allowed size of data packets.
|
---|
| 62 | * @param[in] speed Speed of the transaction.
|
---|
| 63 | * @param[in] buffer Data source/destination.
|
---|
| 64 | * @param[in] size Size of the buffer.
|
---|
| 65 | * @param[in] setup_buffer Setup data source (if not NULL)
|
---|
| 66 | * @param[in] setup_size Size of setup_buffer (should be always 8)
|
---|
| 67 | * @param[in] func_in function to call on inbound transaction completion
|
---|
| 68 | * @param[in] func_out function to call on outbound transaction completion
|
---|
| 69 | * @param[in] arg additional parameter to func_in or func_out
|
---|
| 70 | * @param[in] manager Pointer to toggle management structure.
|
---|
[17ceb72] | 71 | * @return Valid pointer if all substructures were successfully created,
|
---|
| 72 | * NULL otherwise.
|
---|
| 73 | *
|
---|
| 74 | * Determines the number of needed packets (TDs). Prepares a transport buffer
|
---|
| 75 | * (that is accessible by the hardware). Initializes parameters needed for the
|
---|
| 76 | * transaction and callback.
|
---|
[a7e2f0d] | 77 | */
|
---|
[eb1a2f4] | 78 | batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
|
---|
[9a818a9] | 79 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
[1a93bb0] | 80 | usb_speed_t speed, char *buffer, size_t size,
|
---|
[7dd3318] | 81 | char* setup_buffer, size_t setup_size,
|
---|
[4192d3d6] | 82 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
[5620bd4] | 83 | usbhc_iface_transfer_out_callback_t func_out, void *arg,
|
---|
| 84 | device_keeper_t *manager
|
---|
| 85 | )
|
---|
[4192d3d6] | 86 | {
|
---|
| 87 | assert(func_in == NULL || func_out == NULL);
|
---|
| 88 | assert(func_in != NULL || func_out != NULL);
|
---|
| 89 |
|
---|
[2ab6875] | 90 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
| 91 | if (ptr == NULL) { \
|
---|
| 92 | usb_log_error(message); \
|
---|
| 93 | if (instance) { \
|
---|
| 94 | batch_dispose(instance); \
|
---|
| 95 | } \
|
---|
| 96 | return NULL; \
|
---|
| 97 | } else (void)0
|
---|
| 98 |
|
---|
[83c439c] | 99 | batch_t *instance = malloc(sizeof(batch_t));
|
---|
[2ab6875] | 100 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
| 101 | "Failed to allocate batch instance.\n");
|
---|
| 102 | bzero(instance, sizeof(batch_t));
|
---|
[7dd3318] | 103 |
|
---|
[6143ce3] | 104 | instance->qh = malloc32(sizeof(qh_t));
|
---|
[2ab6875] | 105 | CHECK_NULL_DISPOSE_RETURN(instance->qh,
|
---|
| 106 | "Failed to allocate batch queue head.\n");
|
---|
[6143ce3] | 107 | qh_init(instance->qh);
|
---|
[7dd3318] | 108 |
|
---|
| 109 | instance->packets = (size + max_packet_size - 1) / max_packet_size;
|
---|
| 110 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 111 | instance->packets += 2;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[c5b93dc] | 114 | instance->tds = malloc32(sizeof(td_t) * instance->packets);
|
---|
[2ab6875] | 115 | CHECK_NULL_DISPOSE_RETURN(
|
---|
| 116 | instance->tds, "Failed to allocate transfer descriptors.\n");
|
---|
[c5b93dc] | 117 | bzero(instance->tds, sizeof(td_t) * instance->packets);
|
---|
[9a818a9] | 118 |
|
---|
[2ab6875] | 119 | if (size > 0) {
|
---|
[bcaefe3] | 120 | instance->transport_buffer = malloc32(size);
|
---|
[2ab6875] | 121 | CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
|
---|
| 122 | "Failed to allocate device accessible buffer.\n");
|
---|
[4192d3d6] | 123 | }
|
---|
| 124 |
|
---|
[2ab6875] | 125 | if (setup_size > 0) {
|
---|
| 126 | instance->setup_buffer = malloc32(setup_size);
|
---|
| 127 | CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
|
---|
| 128 | "Failed to allocate device accessible setup buffer.\n");
|
---|
[7dd3318] | 129 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[4192d3d6] | 132 |
|
---|
| 133 | link_initialize(&instance->link);
|
---|
[7dd3318] | 134 |
|
---|
[2ab6875] | 135 | instance->max_packet_size = max_packet_size;
|
---|
[4192d3d6] | 136 | instance->target = target;
|
---|
[9a818a9] | 137 | instance->transfer_type = transfer_type;
|
---|
[4192d3d6] | 138 | instance->buffer = buffer;
|
---|
| 139 | instance->buffer_size = size;
|
---|
[7dd3318] | 140 | instance->setup_size = setup_size;
|
---|
[eb1a2f4] | 141 | instance->fun = fun;
|
---|
[4192d3d6] | 142 | instance->arg = arg;
|
---|
[014d5033] | 143 | instance->speed = speed;
|
---|
[5620bd4] | 144 | instance->manager = manager;
|
---|
[6143ce3] | 145 | instance->callback_out = func_out;
|
---|
| 146 | instance->callback_in = func_in;
|
---|
[9a818a9] | 147 |
|
---|
[6143ce3] | 148 | qh_set_element_td(instance->qh, addr_to_phys(instance->tds));
|
---|
[2ab6875] | 149 |
|
---|
[4abc304] | 150 | usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
|
---|
| 151 | instance, target.address, target.endpoint);
|
---|
[9a818a9] | 152 | return instance;
|
---|
[4192d3d6] | 153 | }
|
---|
| 154 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 155 | /** Check batch TDs for activity.
|
---|
[a7e2f0d] | 156 | *
|
---|
| 157 | * @param[in] instance Batch structure to use.
|
---|
| 158 | * @return False, if there is an active TD, true otherwise.
|
---|
[17ceb72] | 159 | *
|
---|
| 160 | * Walk all TDs. Stop with false if there is an active one (it is to be
|
---|
| 161 | * processed). Stop with true if an error is found. Return true if the last TS
|
---|
| 162 | * is reached.
|
---|
[a7e2f0d] | 163 | */
|
---|
[83c439c] | 164 | bool batch_is_complete(batch_t *instance)
|
---|
[4192d3d6] | 165 | {
|
---|
| 166 | assert(instance);
|
---|
[48563a3] | 167 | usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
|
---|
[7dd3318] | 168 | instance, instance->packets);
|
---|
[600733e] | 169 | instance->transfered_size = 0;
|
---|
[7dd3318] | 170 | size_t i = 0;
|
---|
| 171 | for (;i < instance->packets; ++i) {
|
---|
[c5b93dc] | 172 | if (td_is_active(&instance->tds[i])) {
|
---|
[7dd3318] | 173 | return false;
|
---|
[600733e] | 174 | }
|
---|
[c5b93dc] | 175 |
|
---|
| 176 | instance->error = td_status(&instance->tds[i]);
|
---|
[7dd3318] | 177 | if (instance->error != EOK) {
|
---|
[48563a3] | 178 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
---|
[c5b93dc] | 179 | instance, i, instance->tds[i].status);
|
---|
[67352d2] | 180 | td_print_status(&instance->tds[i]);
|
---|
[a60150a] | 181 |
|
---|
[bcaefe3] | 182 | device_keeper_set_toggle(instance->manager,
|
---|
| 183 | instance->target, td_toggle(&instance->tds[i]));
|
---|
[c5b93dc] | 184 | if (i > 0)
|
---|
| 185 | goto substract_ret;
|
---|
[7dd3318] | 186 | return true;
|
---|
| 187 | }
|
---|
[c5b93dc] | 188 |
|
---|
| 189 | instance->transfered_size += td_act_size(&instance->tds[i]);
|
---|
| 190 | if (td_is_short(&instance->tds[i]))
|
---|
| 191 | goto substract_ret;
|
---|
[4192d3d6] | 192 | }
|
---|
[c5b93dc] | 193 | substract_ret:
|
---|
[600733e] | 194 | instance->transfered_size -= instance->setup_size;
|
---|
[7dd3318] | 195 | return true;
|
---|
[4192d3d6] | 196 | }
|
---|
| 197 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 198 | /** Prepares control write transaction.
|
---|
| 199 | *
|
---|
| 200 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 201 | *
|
---|
| 202 | * Uses genercir control function with pids OUT and IN.
|
---|
[a7e2f0d] | 203 | */
|
---|
[83c439c] | 204 | void batch_control_write(batch_t *instance)
|
---|
[4192d3d6] | 205 | {
|
---|
| 206 | assert(instance);
|
---|
[17ceb72] | 207 | /* We are data out, we are supposed to provide data */
|
---|
[a7e2f0d] | 208 | memcpy(instance->transport_buffer, instance->buffer,
|
---|
| 209 | instance->buffer_size);
|
---|
[bdc8ab1] | 210 | batch_control(instance, USB_PID_OUT, USB_PID_IN);
|
---|
[83c439c] | 211 | instance->next_step = batch_call_out_and_dispose;
|
---|
[4abc304] | 212 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
[4192d3d6] | 213 | }
|
---|
| 214 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 215 | /** Prepares control read transaction.
|
---|
| 216 | *
|
---|
| 217 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 218 | *
|
---|
| 219 | * Uses generic control with pids IN and OUT.
|
---|
[a7e2f0d] | 220 | */
|
---|
[83c439c] | 221 | void batch_control_read(batch_t *instance)
|
---|
[4192d3d6] | 222 | {
|
---|
| 223 | assert(instance);
|
---|
[bdc8ab1] | 224 | batch_control(instance, USB_PID_IN, USB_PID_OUT);
|
---|
[83c439c] | 225 | instance->next_step = batch_call_in_and_dispose;
|
---|
[4abc304] | 226 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
[4192d3d6] | 227 | }
|
---|
| 228 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 229 | /** Prepare interrupt in transaction.
|
---|
[a7e2f0d] | 230 | *
|
---|
| 231 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 232 | *
|
---|
| 233 | * Data transaction with PID_IN.
|
---|
[a7e2f0d] | 234 | */
|
---|
[5620bd4] | 235 | void batch_interrupt_in(batch_t *instance)
|
---|
[4192d3d6] | 236 | {
|
---|
[c8dd5b1] | 237 | assert(instance);
|
---|
[5620bd4] | 238 | batch_data(instance, USB_PID_IN);
|
---|
[83c439c] | 239 | instance->next_step = batch_call_in_and_dispose;
|
---|
[4abc304] | 240 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
[4192d3d6] | 241 | }
|
---|
| 242 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 243 | /** Prepare interrupt out transaction.
|
---|
[a7e2f0d] | 244 | *
|
---|
| 245 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 246 | *
|
---|
| 247 | * Data transaction with PID_OUT.
|
---|
[a7e2f0d] | 248 | */
|
---|
[5620bd4] | 249 | void batch_interrupt_out(batch_t *instance)
|
---|
[4192d3d6] | 250 | {
|
---|
[c8dd5b1] | 251 | assert(instance);
|
---|
[17ceb72] | 252 | /* We are data out, we are supposed to provide data */
|
---|
[7dd3318] | 253 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
---|
[5620bd4] | 254 | batch_data(instance, USB_PID_OUT);
|
---|
[0e06a14] | 255 | instance->next_step = batch_call_out_and_dispose;
|
---|
| 256 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
| 257 | }
|
---|
| 258 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 259 | /** Prepare bulk in transaction.
|
---|
[a7e2f0d] | 260 | *
|
---|
| 261 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 262 | *
|
---|
| 263 | * Data transaction with PID_IN.
|
---|
[a7e2f0d] | 264 | */
|
---|
[5620bd4] | 265 | void batch_bulk_in(batch_t *instance)
|
---|
[0e06a14] | 266 | {
|
---|
| 267 | assert(instance);
|
---|
[5620bd4] | 268 | batch_data(instance, USB_PID_IN);
|
---|
[0e06a14] | 269 | instance->next_step = batch_call_in_and_dispose;
|
---|
| 270 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
| 271 | }
|
---|
| 272 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 273 | /** Prepare bulk out transaction.
|
---|
[a7e2f0d] | 274 | *
|
---|
| 275 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 276 | *
|
---|
| 277 | * Data transaction with PID_OUT.
|
---|
[a7e2f0d] | 278 | */
|
---|
[5620bd4] | 279 | void batch_bulk_out(batch_t *instance)
|
---|
[0e06a14] | 280 | {
|
---|
| 281 | assert(instance);
|
---|
[17ceb72] | 282 | /* We are data out, we are supposed to provide data */
|
---|
[0e06a14] | 283 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
---|
[5620bd4] | 284 | batch_data(instance, USB_PID_OUT);
|
---|
[0e06a14] | 285 | instance->next_step = batch_call_out_and_dispose;
|
---|
| 286 | usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
|
---|
| 287 | }
|
---|
| 288 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 289 | /** Prepare generic data transaction
|
---|
[a7e2f0d] | 290 | *
|
---|
| 291 | * @param[in] instance Batch structure to use.
|
---|
| 292 | * @param[in] pid to use for data packets.
|
---|
[17ceb72] | 293 | *
|
---|
| 294 | * Packets with alternating toggle bit and supplied pid value.
|
---|
| 295 | * The last packet is marked with IOC flag.
|
---|
[a7e2f0d] | 296 | */
|
---|
[5620bd4] | 297 | void batch_data(batch_t *instance, usb_packet_id pid)
|
---|
[0e06a14] | 298 | {
|
---|
| 299 | assert(instance);
|
---|
[c3ae877] | 300 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
[bcaefe3] | 301 | int toggle =
|
---|
| 302 | device_keeper_get_toggle(instance->manager, instance->target);
|
---|
[3e37964] | 303 | assert(toggle == 0 || toggle == 1);
|
---|
[0e06a14] | 304 |
|
---|
| 305 | size_t packet = 0;
|
---|
| 306 | size_t remain_size = instance->buffer_size;
|
---|
| 307 | while (remain_size > 0) {
|
---|
[7dd3318] | 308 | char *data =
|
---|
[0e06a14] | 309 | instance->transport_buffer + instance->buffer_size
|
---|
| 310 | - remain_size;
|
---|
| 311 |
|
---|
| 312 | const size_t packet_size =
|
---|
| 313 | (instance->max_packet_size > remain_size) ?
|
---|
| 314 | remain_size : instance->max_packet_size;
|
---|
[c8dd5b1] | 315 |
|
---|
[bcaefe3] | 316 | td_t *next_packet = (packet + 1 < instance->packets)
|
---|
| 317 | ? &instance->tds[packet + 1] : NULL;
|
---|
[30a4301] | 318 |
|
---|
[bcaefe3] | 319 | assert(packet < instance->packets);
|
---|
[0e06a14] | 320 | assert(packet_size <= remain_size);
|
---|
[bcaefe3] | 321 |
|
---|
| 322 | td_init(
|
---|
| 323 | &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
|
---|
| 324 | toggle, false, low_speed, instance->target, pid, data,
|
---|
| 325 | next_packet);
|
---|
| 326 |
|
---|
| 327 |
|
---|
| 328 | toggle = 1 - toggle;
|
---|
[0e06a14] | 329 | remain_size -= packet_size;
|
---|
[bcaefe3] | 330 | ++packet;
|
---|
[0e06a14] | 331 | }
|
---|
[eb0dc58] | 332 | td_set_ioc(&instance->tds[packet - 1]);
|
---|
[5620bd4] | 333 | device_keeper_set_toggle(instance->manager, instance->target, toggle);
|
---|
[4192d3d6] | 334 | }
|
---|
| 335 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 336 | /** Prepare generic control transaction
|
---|
[a7e2f0d] | 337 | *
|
---|
| 338 | * @param[in] instance Batch structure to use.
|
---|
| 339 | * @param[in] data_stage to use for data packets.
|
---|
| 340 | * @param[in] status_stage to use for data packets.
|
---|
[17ceb72] | 341 | *
|
---|
| 342 | * Setup stage with toggle 0 and USB_PID_SETUP.
|
---|
| 343 | * Data stage with alternating toggle and pid supplied by parameter.
|
---|
| 344 | * Status stage with toggle 1 and pid supplied by parameter.
|
---|
| 345 | * The last packet is marked with IOC.
|
---|
[a7e2f0d] | 346 | */
|
---|
[3e37964] | 347 | void batch_control(batch_t *instance,
|
---|
[eae83aa] | 348 | usb_packet_id data_stage, usb_packet_id status_stage)
|
---|
[bdc8ab1] | 349 | {
|
---|
| 350 | assert(instance);
|
---|
| 351 |
|
---|
| 352 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
| 353 | int toggle = 0;
|
---|
| 354 | /* setup stage */
|
---|
[c5b93dc] | 355 | td_init(instance->tds, DEFAULT_ERROR_COUNT,
|
---|
[bdc8ab1] | 356 | instance->setup_size, toggle, false, low_speed, instance->target,
|
---|
| 357 | USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
|
---|
| 358 |
|
---|
| 359 | /* data stage */
|
---|
| 360 | size_t packet = 1;
|
---|
| 361 | size_t remain_size = instance->buffer_size;
|
---|
| 362 | while (remain_size > 0) {
|
---|
| 363 | char *data =
|
---|
| 364 | instance->transport_buffer + instance->buffer_size
|
---|
| 365 | - remain_size;
|
---|
| 366 |
|
---|
| 367 | toggle = 1 - toggle;
|
---|
| 368 |
|
---|
| 369 | const size_t packet_size =
|
---|
| 370 | (instance->max_packet_size > remain_size) ?
|
---|
| 371 | remain_size : instance->max_packet_size;
|
---|
| 372 |
|
---|
[bcaefe3] | 373 | td_init(
|
---|
| 374 | &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
|
---|
| 375 | toggle, false, low_speed, instance->target, data_stage,
|
---|
| 376 | data, &instance->tds[packet + 1]);
|
---|
[bdc8ab1] | 377 |
|
---|
| 378 | ++packet;
|
---|
| 379 | assert(packet < instance->packets);
|
---|
| 380 | assert(packet_size <= remain_size);
|
---|
| 381 | remain_size -= packet_size;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /* status stage */
|
---|
| 385 | assert(packet == instance->packets - 1);
|
---|
[c5b93dc] | 386 | td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
|
---|
[bdc8ab1] | 387 | 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
|
---|
| 388 |
|
---|
[eb0dc58] | 389 | td_set_ioc(&instance->tds[packet]);
|
---|
[bdc8ab1] | 390 | usb_log_debug2("Control last TD status: %x.\n",
|
---|
| 391 | instance->tds[packet].status);
|
---|
| 392 | }
|
---|
| 393 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 394 | /** Prepare data, get error status and call callback in.
|
---|
[a7e2f0d] | 395 | *
|
---|
| 396 | * @param[in] instance Batch structure to use.
|
---|
[17ceb72] | 397 | * Copies data from transport buffer, and calls callback with appropriate
|
---|
| 398 | * parameters.
|
---|
[a7e2f0d] | 399 | */
|
---|
[83c439c] | 400 | void batch_call_in(batch_t *instance)
|
---|
[4192d3d6] | 401 | {
|
---|
| 402 | assert(instance);
|
---|
| 403 | assert(instance->callback_in);
|
---|
| 404 |
|
---|
[17ceb72] | 405 | /* We are data in, we need data */
|
---|
[a7e2f0d] | 406 | memcpy(instance->buffer, instance->transport_buffer,
|
---|
| 407 | instance->buffer_size);
|
---|
[7dd3318] | 408 |
|
---|
| 409 | int err = instance->error;
|
---|
[4abc304] | 410 | usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
|
---|
[48563a3] | 411 | instance, instance->transfer_type, str_error(err), err,
|
---|
| 412 | instance->transfered_size);
|
---|
[7dd3318] | 413 |
|
---|
[bcaefe3] | 414 | instance->callback_in(
|
---|
| 415 | instance->fun, err, instance->transfered_size, instance->arg);
|
---|
[4192d3d6] | 416 | }
|
---|
| 417 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 418 | /** Get error status and call callback out.
|
---|
[a7e2f0d] | 419 | *
|
---|
| 420 | * @param[in] instance Batch structure to use.
|
---|
| 421 | */
|
---|
[83c439c] | 422 | void batch_call_out(batch_t *instance)
|
---|
[4192d3d6] | 423 | {
|
---|
| 424 | assert(instance);
|
---|
| 425 | assert(instance->callback_out);
|
---|
| 426 |
|
---|
[7dd3318] | 427 | int err = instance->error;
|
---|
[4abc304] | 428 | usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
|
---|
[48563a3] | 429 | instance, instance->transfer_type, str_error(err), err);
|
---|
[eb1a2f4] | 430 | instance->callback_out(instance->fun,
|
---|
[7dd3318] | 431 | err, instance->arg);
|
---|
[4192d3d6] | 432 | }
|
---|
| 433 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 434 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
[a7e2f0d] | 435 | *
|
---|
| 436 | * @param[in] instance Batch structure to use.
|
---|
| 437 | */
|
---|
[83c439c] | 438 | void batch_call_in_and_dispose(batch_t *instance)
|
---|
[4192d3d6] | 439 | {
|
---|
| 440 | assert(instance);
|
---|
[83c439c] | 441 | batch_call_in(instance);
|
---|
[a60150a] | 442 | batch_dispose(instance);
|
---|
[4192d3d6] | 443 | }
|
---|
| 444 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 445 | /** Helper function calls callback and correctly disposes of batch structure.
|
---|
[a7e2f0d] | 446 | *
|
---|
| 447 | * @param[in] instance Batch structure to use.
|
---|
| 448 | */
|
---|
[83c439c] | 449 | void batch_call_out_and_dispose(batch_t *instance)
|
---|
[4192d3d6] | 450 | {
|
---|
| 451 | assert(instance);
|
---|
[83c439c] | 452 | batch_call_out(instance);
|
---|
[a60150a] | 453 | batch_dispose(instance);
|
---|
| 454 | }
|
---|
| 455 | /*----------------------------------------------------------------------------*/
|
---|
[17ceb72] | 456 | /** Correctly dispose all used data structures.
|
---|
[a7e2f0d] | 457 | *
|
---|
| 458 | * @param[in] instance Batch structure to use.
|
---|
| 459 | */
|
---|
[a60150a] | 460 | void batch_dispose(batch_t *instance)
|
---|
| 461 | {
|
---|
| 462 | assert(instance);
|
---|
[48563a3] | 463 | usb_log_debug("Batch(%p) disposing.\n", instance);
|
---|
[bcaefe3] | 464 | /* free32 is NULL safe */
|
---|
[7dd3318] | 465 | free32(instance->tds);
|
---|
| 466 | free32(instance->qh);
|
---|
| 467 | free32(instance->setup_buffer);
|
---|
| 468 | free32(instance->transport_buffer);
|
---|
[4192d3d6] | 469 | free(instance);
|
---|
| 470 | }
|
---|
| 471 | /**
|
---|
| 472 | * @}
|
---|
| 473 | */
|
---|