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