| 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 |
|
|---|
| 29 | /** @addtogroup drvusbuhcihc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief UHCI driver USB transfer structure
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <macros.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <usb/usb.h>
|
|---|
| 43 | #include <usb/debug.h>
|
|---|
| 44 | #include <usb/host/endpoint.h>
|
|---|
| 45 | #include <usb/host/utils/malloc32.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "uhci_batch.h"
|
|---|
| 48 | #include "hw_struct/transfer_descriptor.h"
|
|---|
| 49 |
|
|---|
| 50 | #define DEFAULT_ERROR_COUNT 3
|
|---|
| 51 |
|
|---|
| 52 | /** Safely destructs uhci_transfer_batch_t structure.
|
|---|
| 53 | *
|
|---|
| 54 | * @param[in] uhci_batch Instance to destroy.
|
|---|
| 55 | */
|
|---|
| 56 | static void uhci_transfer_batch_dispose(uhci_transfer_batch_t *uhci_batch)
|
|---|
| 57 | {
|
|---|
| 58 | if (uhci_batch) {
|
|---|
| 59 | usb_transfer_batch_destroy(uhci_batch->usb_batch);
|
|---|
| 60 | free32(uhci_batch->device_buffer);
|
|---|
| 61 | free(uhci_batch);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /** Finishes usb_transfer_batch and destroys the structure.
|
|---|
| 66 | *
|
|---|
| 67 | * @param[in] uhci_batch Instance to finish and destroy.
|
|---|
| 68 | */
|
|---|
| 69 | void uhci_transfer_batch_finish_dispose(uhci_transfer_batch_t *uhci_batch)
|
|---|
| 70 | {
|
|---|
| 71 | assert(uhci_batch);
|
|---|
| 72 | assert(uhci_batch->usb_batch);
|
|---|
| 73 | assert(!link_in_use(&uhci_batch->link));
|
|---|
| 74 | usb_transfer_batch_finish(uhci_batch->usb_batch,
|
|---|
| 75 | uhci_transfer_batch_data_buffer(uhci_batch));
|
|---|
| 76 | uhci_transfer_batch_dispose(uhci_batch);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Transfer batch setup table. */
|
|---|
| 80 | static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t);
|
|---|
| 81 |
|
|---|
| 82 | /** Allocate memory and initialize internal data structure.
|
|---|
| 83 | *
|
|---|
| 84 | * @param[in] usb_batch Pointer to generic USB batch structure.
|
|---|
| 85 | * @return Valid pointer if all structures were successfully created,
|
|---|
| 86 | * NULL otherwise.
|
|---|
| 87 | *
|
|---|
| 88 | * Determines the number of needed transfer descriptors (TDs).
|
|---|
| 89 | * Prepares a transport buffer (that is accessible by the hardware).
|
|---|
| 90 | * Initializes parameters needed for the transfer and callback.
|
|---|
| 91 | */
|
|---|
| 92 | uhci_transfer_batch_t * uhci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
|
|---|
| 93 | {
|
|---|
| 94 | static_assert((sizeof(td_t) % 16) == 0);
|
|---|
| 95 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
|---|
| 96 | if (ptr == NULL) { \
|
|---|
| 97 | usb_log_error(message); \
|
|---|
| 98 | uhci_transfer_batch_dispose(uhci_batch); \
|
|---|
| 99 | return NULL; \
|
|---|
| 100 | } else (void)0
|
|---|
| 101 |
|
|---|
| 102 | uhci_transfer_batch_t *uhci_batch =
|
|---|
| 103 | calloc(1, sizeof(uhci_transfer_batch_t));
|
|---|
| 104 | CHECK_NULL_DISPOSE_RETURN(uhci_batch,
|
|---|
| 105 | "Failed to allocate UHCI batch.\n");
|
|---|
| 106 | link_initialize(&uhci_batch->link);
|
|---|
| 107 | uhci_batch->td_count =
|
|---|
| 108 | (usb_batch->buffer_size + usb_batch->ep->max_packet_size - 1)
|
|---|
| 109 | / usb_batch->ep->max_packet_size;
|
|---|
| 110 | if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 111 | uhci_batch->td_count += 2;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | const size_t total_size = (sizeof(td_t) * uhci_batch->td_count)
|
|---|
| 115 | + sizeof(qh_t) + usb_batch->setup_size + usb_batch->buffer_size;
|
|---|
| 116 | uhci_batch->device_buffer = malloc32(total_size);
|
|---|
| 117 | CHECK_NULL_DISPOSE_RETURN(uhci_batch->device_buffer,
|
|---|
| 118 | "Failed to allocate UHCI buffer.\n");
|
|---|
| 119 | memset(uhci_batch->device_buffer, 0, total_size);
|
|---|
| 120 |
|
|---|
| 121 | uhci_batch->tds = uhci_batch->device_buffer;
|
|---|
| 122 | uhci_batch->qh =
|
|---|
| 123 | (uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count));
|
|---|
| 124 |
|
|---|
| 125 | qh_init(uhci_batch->qh);
|
|---|
| 126 | qh_set_element_td(uhci_batch->qh, &uhci_batch->tds[0]);
|
|---|
| 127 |
|
|---|
| 128 | void *dest =
|
|---|
| 129 | uhci_batch->device_buffer + (sizeof(td_t) * uhci_batch->td_count)
|
|---|
| 130 | + sizeof(qh_t);
|
|---|
| 131 | /* Copy SETUP packet data to the device buffer */
|
|---|
| 132 | memcpy(dest, usb_batch->setup_buffer, usb_batch->setup_size);
|
|---|
| 133 | dest += usb_batch->setup_size;
|
|---|
| 134 | /* Copy generic data unless they are provided by the device */
|
|---|
| 135 | if (usb_batch->ep->direction != USB_DIRECTION_IN) {
|
|---|
| 136 | memcpy(dest, usb_batch->buffer, usb_batch->buffer_size);
|
|---|
| 137 | }
|
|---|
| 138 | uhci_batch->usb_batch = usb_batch;
|
|---|
| 139 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
|
|---|
| 140 | " memory structures ready.\n", usb_batch,
|
|---|
| 141 | USB_TRANSFER_BATCH_ARGS(*usb_batch));
|
|---|
| 142 |
|
|---|
| 143 | const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
|
|---|
| 144 |
|
|---|
| 145 | assert(batch_setup[usb_batch->ep->transfer_type]);
|
|---|
| 146 | batch_setup[usb_batch->ep->transfer_type](uhci_batch, dir);
|
|---|
| 147 |
|
|---|
| 148 | return uhci_batch;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Check batch TDs for activity.
|
|---|
| 152 | *
|
|---|
| 153 | * @param[in] uhci_batch Batch structure to use.
|
|---|
| 154 | * @return False, if there is an active TD, true otherwise.
|
|---|
| 155 | *
|
|---|
| 156 | * Walk all TDs. Stop with false if there is an active one (it is to be
|
|---|
| 157 | * processed). Stop with true if an error is found. Return true if the last TD
|
|---|
| 158 | * is reached.
|
|---|
| 159 | */
|
|---|
| 160 | bool uhci_transfer_batch_is_complete(const uhci_transfer_batch_t *uhci_batch)
|
|---|
| 161 | {
|
|---|
| 162 | assert(uhci_batch);
|
|---|
| 163 | assert(uhci_batch->usb_batch);
|
|---|
| 164 |
|
|---|
| 165 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT
|
|---|
| 166 | " checking %zu transfer(s) for completion.\n",
|
|---|
| 167 | uhci_batch->usb_batch,
|
|---|
| 168 | USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch),
|
|---|
| 169 | uhci_batch->td_count);
|
|---|
| 170 | uhci_batch->usb_batch->transfered_size = 0;
|
|---|
| 171 |
|
|---|
| 172 | for (size_t i = 0;i < uhci_batch->td_count; ++i) {
|
|---|
| 173 | if (td_is_active(&uhci_batch->tds[i])) {
|
|---|
| 174 | return false;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | uhci_batch->usb_batch->error = td_status(&uhci_batch->tds[i]);
|
|---|
| 178 | if (uhci_batch->usb_batch->error != EOK) {
|
|---|
| 179 | assert(uhci_batch->usb_batch->ep != NULL);
|
|---|
| 180 |
|
|---|
| 181 | usb_log_debug("Batch %p found error TD(%zu->%p):%"
|
|---|
| 182 | PRIx32 ".\n", uhci_batch->usb_batch, i,
|
|---|
| 183 | &uhci_batch->tds[i], uhci_batch->tds[i].status);
|
|---|
| 184 | td_print_status(&uhci_batch->tds[i]);
|
|---|
| 185 |
|
|---|
| 186 | endpoint_toggle_set(uhci_batch->usb_batch->ep,
|
|---|
| 187 | td_toggle(&uhci_batch->tds[i]));
|
|---|
| 188 | if (i > 0)
|
|---|
| 189 | goto substract_ret;
|
|---|
| 190 | return true;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | uhci_batch->usb_batch->transfered_size
|
|---|
| 194 | += td_act_size(&uhci_batch->tds[i]);
|
|---|
| 195 | if (td_is_short(&uhci_batch->tds[i]))
|
|---|
| 196 | goto substract_ret;
|
|---|
| 197 | }
|
|---|
| 198 | substract_ret:
|
|---|
| 199 | uhci_batch->usb_batch->transfered_size
|
|---|
| 200 | -= uhci_batch->usb_batch->setup_size;
|
|---|
| 201 | return true;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /** Direction to pid conversion table */
|
|---|
| 205 | static const usb_packet_id direction_pids[] = {
|
|---|
| 206 | [USB_DIRECTION_IN] = USB_PID_IN,
|
|---|
| 207 | [USB_DIRECTION_OUT] = USB_PID_OUT,
|
|---|
| 208 | };
|
|---|
| 209 |
|
|---|
| 210 | /** Prepare generic data transfer
|
|---|
| 211 | *
|
|---|
| 212 | * @param[in] uhci_batch Batch structure to use.
|
|---|
| 213 | * @param[in] dir Communication direction.
|
|---|
| 214 | *
|
|---|
| 215 | * Transactions with alternating toggle bit and supplied pid value.
|
|---|
| 216 | * The last transfer is marked with IOC flag.
|
|---|
| 217 | */
|
|---|
| 218 | static void batch_data(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
|
|---|
| 219 | {
|
|---|
| 220 | assert(uhci_batch);
|
|---|
| 221 | assert(uhci_batch->usb_batch);
|
|---|
| 222 | assert(uhci_batch->usb_batch->ep);
|
|---|
| 223 | assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 | const usb_packet_id pid = direction_pids[dir];
|
|---|
| 227 | const bool low_speed =
|
|---|
| 228 | uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
|
|---|
| 229 | const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
|
|---|
| 230 | const usb_target_t target = {{
|
|---|
| 231 | uhci_batch->usb_batch->ep->address,
|
|---|
| 232 | uhci_batch->usb_batch->ep->endpoint }};
|
|---|
| 233 |
|
|---|
| 234 | int toggle = endpoint_toggle_get(uhci_batch->usb_batch->ep);
|
|---|
| 235 | assert(toggle == 0 || toggle == 1);
|
|---|
| 236 |
|
|---|
| 237 | size_t td = 0;
|
|---|
| 238 | size_t remain_size = uhci_batch->usb_batch->buffer_size;
|
|---|
| 239 | char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
|
|---|
| 240 |
|
|---|
| 241 | while (remain_size > 0) {
|
|---|
| 242 | const size_t packet_size = min(remain_size, mps);
|
|---|
| 243 |
|
|---|
| 244 | const td_t *next_td = (td + 1 < uhci_batch->td_count)
|
|---|
| 245 | ? &uhci_batch->tds[td + 1] : NULL;
|
|---|
| 246 |
|
|---|
| 247 | assert(td < uhci_batch->td_count);
|
|---|
| 248 | td_init(
|
|---|
| 249 | &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
|
|---|
| 250 | toggle, false, low_speed, target, pid, buffer, next_td);
|
|---|
| 251 |
|
|---|
| 252 | ++td;
|
|---|
| 253 | toggle = 1 - toggle;
|
|---|
| 254 | buffer += packet_size;
|
|---|
| 255 | remain_size -= packet_size;
|
|---|
| 256 | }
|
|---|
| 257 | td_set_ioc(&uhci_batch->tds[td - 1]);
|
|---|
| 258 | endpoint_toggle_set(uhci_batch->usb_batch->ep, toggle);
|
|---|
| 259 | usb_log_debug2(
|
|---|
| 260 | "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
|
|---|
| 261 | uhci_batch->usb_batch,
|
|---|
| 262 | usb_str_transfer_type(uhci_batch->usb_batch->ep->transfer_type),
|
|---|
| 263 | usb_str_direction(uhci_batch->usb_batch->ep->direction),
|
|---|
| 264 | USB_TRANSFER_BATCH_ARGS(*uhci_batch->usb_batch));
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Prepare generic control transfer
|
|---|
| 268 | *
|
|---|
| 269 | * @param[in] uhci_batch Batch structure to use.
|
|---|
| 270 | * @param[in] dir Communication direction.
|
|---|
| 271 | *
|
|---|
| 272 | * Setup stage with toggle 0 and USB_PID_SETUP.
|
|---|
| 273 | * Data stage with alternating toggle and pid determined by the communication
|
|---|
| 274 | * direction.
|
|---|
| 275 | * Status stage with toggle 1 and pid determined by the communication direction.
|
|---|
| 276 | * The last transfer is marked with IOC.
|
|---|
| 277 | */
|
|---|
| 278 | static void batch_control(uhci_transfer_batch_t *uhci_batch, usb_direction_t dir)
|
|---|
| 279 | {
|
|---|
| 280 | assert(uhci_batch);
|
|---|
| 281 | assert(uhci_batch->usb_batch);
|
|---|
| 282 | assert(uhci_batch->usb_batch->ep);
|
|---|
| 283 | assert(dir == USB_DIRECTION_OUT || dir == USB_DIRECTION_IN);
|
|---|
| 284 | assert(uhci_batch->td_count >= 2);
|
|---|
| 285 | static const usb_packet_id status_stage_pids[] = {
|
|---|
| 286 | [USB_DIRECTION_IN] = USB_PID_OUT,
|
|---|
| 287 | [USB_DIRECTION_OUT] = USB_PID_IN,
|
|---|
| 288 | };
|
|---|
| 289 |
|
|---|
| 290 | const usb_packet_id data_stage_pid = direction_pids[dir];
|
|---|
| 291 | const usb_packet_id status_stage_pid = status_stage_pids[dir];
|
|---|
| 292 | const bool low_speed =
|
|---|
| 293 | uhci_batch->usb_batch->ep->speed == USB_SPEED_LOW;
|
|---|
| 294 | const size_t mps = uhci_batch->usb_batch->ep->max_packet_size;
|
|---|
| 295 | const usb_target_t target = {{
|
|---|
| 296 | uhci_batch->usb_batch->ep->address,
|
|---|
| 297 | uhci_batch->usb_batch->ep->endpoint }};
|
|---|
| 298 |
|
|---|
| 299 | /* setup stage */
|
|---|
| 300 | td_init(
|
|---|
| 301 | &uhci_batch->tds[0], DEFAULT_ERROR_COUNT,
|
|---|
| 302 | uhci_batch->usb_batch->setup_size, 0, false,
|
|---|
| 303 | low_speed, target, USB_PID_SETUP,
|
|---|
| 304 | uhci_transfer_batch_setup_buffer(uhci_batch), &uhci_batch->tds[1]);
|
|---|
| 305 |
|
|---|
| 306 | /* data stage */
|
|---|
| 307 | size_t td = 1;
|
|---|
| 308 | unsigned toggle = 1;
|
|---|
| 309 | size_t remain_size = uhci_batch->usb_batch->buffer_size;
|
|---|
| 310 | char *buffer = uhci_transfer_batch_data_buffer(uhci_batch);
|
|---|
| 311 |
|
|---|
| 312 | while (remain_size > 0) {
|
|---|
| 313 | const size_t packet_size = min(remain_size, mps);
|
|---|
| 314 |
|
|---|
| 315 | td_init(
|
|---|
| 316 | &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, packet_size,
|
|---|
| 317 | toggle, false, low_speed, target, data_stage_pid,
|
|---|
| 318 | buffer, &uhci_batch->tds[td + 1]);
|
|---|
| 319 |
|
|---|
| 320 | ++td;
|
|---|
| 321 | toggle = 1 - toggle;
|
|---|
| 322 | buffer += packet_size;
|
|---|
| 323 | remain_size -= packet_size;
|
|---|
| 324 | assert(td < uhci_batch->td_count);
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /* status stage */
|
|---|
| 328 | assert(td == uhci_batch->td_count - 1);
|
|---|
| 329 |
|
|---|
| 330 | td_init(
|
|---|
| 331 | &uhci_batch->tds[td], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
|
|---|
| 332 | target, status_stage_pid, NULL, NULL);
|
|---|
| 333 | td_set_ioc(&uhci_batch->tds[td]);
|
|---|
| 334 |
|
|---|
| 335 | usb_log_debug2("Control last TD status: %x.\n",
|
|---|
| 336 | uhci_batch->tds[td].status);
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | static void (*const batch_setup[])(uhci_transfer_batch_t*, usb_direction_t) =
|
|---|
| 340 | {
|
|---|
| 341 | [USB_TRANSFER_CONTROL] = batch_control,
|
|---|
| 342 | [USB_TRANSFER_BULK] = batch_data,
|
|---|
| 343 | [USB_TRANSFER_INTERRUPT] = batch_data,
|
|---|
| 344 | [USB_TRANSFER_ISOCHRONOUS] = NULL,
|
|---|
| 345 | };
|
|---|
| 346 | /**
|
|---|
| 347 | * @}
|
|---|
| 348 | */
|
|---|