| 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 drvusbohci
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI driver USB transaction structure
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 |
|
|---|
| 37 | #include <usb/usb.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "batch.h"
|
|---|
| 41 | #include "utils/malloc32.h"
|
|---|
| 42 | #include "hw_struct/endpoint_descriptor.h"
|
|---|
| 43 | #include "hw_struct/transfer_descriptor.h"
|
|---|
| 44 |
|
|---|
| 45 | typedef struct ohci_batch {
|
|---|
| 46 | ed_t *ed;
|
|---|
| 47 | td_t *tds;
|
|---|
| 48 | size_t td_count;
|
|---|
| 49 | } ohci_batch_t;
|
|---|
| 50 |
|
|---|
| 51 | static void batch_control(usb_transfer_batch_t *instance,
|
|---|
| 52 | usb_direction_t data_dir, usb_direction_t status_dir);
|
|---|
| 53 | static void batch_data(usb_transfer_batch_t *instance);
|
|---|
| 54 | static void batch_call_in_and_dispose(usb_transfer_batch_t *instance);
|
|---|
| 55 | static void batch_call_out_and_dispose(usb_transfer_batch_t *instance);
|
|---|
| 56 |
|
|---|
| 57 | #define DEFAULT_ERROR_COUNT 3
|
|---|
| 58 | usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
|
|---|
| 59 | char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
|
|---|
| 60 | usbhc_iface_transfer_in_callback_t func_in,
|
|---|
| 61 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
|---|
| 62 | {
|
|---|
| 63 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
|---|
| 64 | if (ptr == NULL) { \
|
|---|
| 65 | usb_log_error(message); \
|
|---|
| 66 | if (instance) { \
|
|---|
| 67 | batch_dispose(instance); \
|
|---|
| 68 | } \
|
|---|
| 69 | return NULL; \
|
|---|
| 70 | } else (void)0
|
|---|
| 71 |
|
|---|
| 72 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
|---|
| 73 | CHECK_NULL_DISPOSE_RETURN(instance,
|
|---|
| 74 | "Failed to allocate batch instance.\n");
|
|---|
| 75 | usb_target_t target =
|
|---|
| 76 | { .address = ep->address, .endpoint = ep->endpoint };
|
|---|
| 77 | usb_transfer_batch_init(instance, target, ep->transfer_type, ep->speed,
|
|---|
| 78 | ep->max_packet_size, buffer, NULL, buffer_size, NULL, setup_size,
|
|---|
| 79 | func_in, func_out, arg, fun, ep, NULL);
|
|---|
| 80 |
|
|---|
| 81 | ohci_batch_t *data = malloc(sizeof(ohci_batch_t));
|
|---|
| 82 | CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
|
|---|
| 83 | bzero(data, sizeof(ohci_batch_t));
|
|---|
| 84 | instance->private_data = data;
|
|---|
| 85 |
|
|---|
| 86 | /* we needs + 1 transfer descriptor as the last one won't be executed */
|
|---|
| 87 | data->td_count = 1 +
|
|---|
| 88 | ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
|
|---|
| 89 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 90 | data->td_count += 2;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | data->tds = malloc32(sizeof(td_t) * data->td_count);
|
|---|
| 94 | CHECK_NULL_DISPOSE_RETURN(data->tds,
|
|---|
| 95 | "Failed to allocate transfer descriptors.\n");
|
|---|
| 96 | bzero(data->tds, sizeof(td_t) * data->td_count);
|
|---|
| 97 |
|
|---|
| 98 | data->ed = malloc32(sizeof(ed_t));
|
|---|
| 99 | CHECK_NULL_DISPOSE_RETURN(data->ed,
|
|---|
| 100 | "Failed to allocate endpoint descriptor.\n");
|
|---|
| 101 |
|
|---|
| 102 | if (buffer_size > 0) {
|
|---|
| 103 | instance->transport_buffer = malloc32(buffer_size);
|
|---|
| 104 | CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
|
|---|
| 105 | "Failed to allocate device accessible buffer.\n");
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (setup_size > 0) {
|
|---|
| 109 | instance->setup_buffer = malloc32(setup_size);
|
|---|
| 110 | CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
|
|---|
| 111 | "Failed to allocate device accessible setup buffer.\n");
|
|---|
| 112 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return instance;
|
|---|
| 116 | }
|
|---|
| 117 | /*----------------------------------------------------------------------------*/
|
|---|
| 118 | void batch_dispose(usb_transfer_batch_t *instance)
|
|---|
| 119 | {
|
|---|
| 120 | assert(instance);
|
|---|
| 121 | ohci_batch_t *data = instance->private_data;
|
|---|
| 122 | assert(data);
|
|---|
| 123 | free32(data->ed);
|
|---|
| 124 | free32(data->tds);
|
|---|
| 125 | free32(instance->setup_buffer);
|
|---|
| 126 | free32(instance->transport_buffer);
|
|---|
| 127 | free(data);
|
|---|
| 128 | free(instance);
|
|---|
| 129 | }
|
|---|
| 130 | /*----------------------------------------------------------------------------*/
|
|---|
| 131 | bool batch_is_complete(usb_transfer_batch_t *instance)
|
|---|
| 132 | {
|
|---|
| 133 | assert(instance);
|
|---|
| 134 | ohci_batch_t *data = instance->private_data;
|
|---|
| 135 | assert(data);
|
|---|
| 136 | size_t tds = data->td_count - 1;
|
|---|
| 137 | usb_log_debug("Batch(%p) checking %d td(s) for completion.\n",
|
|---|
| 138 | instance, tds);
|
|---|
| 139 | usb_log_debug("ED: %x:%x:%x:%x.\n",
|
|---|
| 140 | data->ed->status, data->ed->td_head, data->ed->td_tail,
|
|---|
| 141 | data->ed->next);
|
|---|
| 142 | size_t i = 0;
|
|---|
| 143 | for (; i < tds; ++i) {
|
|---|
| 144 | usb_log_debug("TD %d: %x:%x:%x:%x.\n", i,
|
|---|
| 145 | data->tds[i].status, data->tds[i].cbp, data->tds[i].next,
|
|---|
| 146 | data->tds[i].be);
|
|---|
| 147 | if (!td_is_finished(&data->tds[i])) {
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 | instance->error = td_error(&data->tds[i]);
|
|---|
| 151 | /* FIXME: calculate real transfered size */
|
|---|
| 152 | instance->transfered_size = instance->buffer_size;
|
|---|
| 153 | if (instance->error != EOK) {
|
|---|
| 154 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
|---|
| 155 | instance, i, data->tds[i].status);
|
|---|
| 156 | return true;
|
|---|
| 157 | // endpoint_toggle_set(instance->ep,
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | return true;
|
|---|
| 161 | }
|
|---|
| 162 | /*----------------------------------------------------------------------------*/
|
|---|
| 163 | void batch_control_write(usb_transfer_batch_t *instance)
|
|---|
| 164 | {
|
|---|
| 165 | assert(instance);
|
|---|
| 166 | /* We are data out, we are supposed to provide data */
|
|---|
| 167 | memcpy(instance->transport_buffer, instance->buffer,
|
|---|
| 168 | instance->buffer_size);
|
|---|
| 169 | instance->next_step = batch_call_out_and_dispose;
|
|---|
| 170 | batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
|
|---|
| 171 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
|---|
| 172 | }
|
|---|
| 173 | /*----------------------------------------------------------------------------*/
|
|---|
| 174 | void batch_control_read(usb_transfer_batch_t *instance)
|
|---|
| 175 | {
|
|---|
| 176 | assert(instance);
|
|---|
| 177 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 178 | batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
|
|---|
| 179 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
|---|
| 180 | }
|
|---|
| 181 | /*----------------------------------------------------------------------------*/
|
|---|
| 182 | void batch_interrupt_in(usb_transfer_batch_t *instance)
|
|---|
| 183 | {
|
|---|
| 184 | assert(instance);
|
|---|
| 185 | assert(instance->direction == USB_DIRECTION_IN);
|
|---|
| 186 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 187 | batch_data(instance);
|
|---|
| 188 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
|---|
| 189 | }
|
|---|
| 190 | /*----------------------------------------------------------------------------*/
|
|---|
| 191 | void batch_interrupt_out(usb_transfer_batch_t *instance)
|
|---|
| 192 | {
|
|---|
| 193 | assert(instance);
|
|---|
| 194 | assert(instance->direction == USB_DIRECTION_OUT);
|
|---|
| 195 | /* We are data out, we are supposed to provide data */
|
|---|
| 196 | memcpy(instance->transport_buffer, instance->buffer,
|
|---|
| 197 | instance->buffer_size);
|
|---|
| 198 | instance->next_step = batch_call_out_and_dispose;
|
|---|
| 199 | batch_data(instance);
|
|---|
| 200 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
|---|
| 201 | }
|
|---|
| 202 | /*----------------------------------------------------------------------------*/
|
|---|
| 203 | void batch_bulk_in(usb_transfer_batch_t *instance)
|
|---|
| 204 | {
|
|---|
| 205 | assert(instance);
|
|---|
| 206 | instance->direction = USB_DIRECTION_IN;
|
|---|
| 207 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 208 | batch_data(instance);
|
|---|
| 209 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
|---|
| 210 | }
|
|---|
| 211 | /*----------------------------------------------------------------------------*/
|
|---|
| 212 | void batch_bulk_out(usb_transfer_batch_t *instance)
|
|---|
| 213 | {
|
|---|
| 214 | assert(instance);
|
|---|
| 215 | instance->direction = USB_DIRECTION_IN;
|
|---|
| 216 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 217 | batch_data(instance);
|
|---|
| 218 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
|---|
| 219 | }
|
|---|
| 220 | /*----------------------------------------------------------------------------*/
|
|---|
| 221 | ed_t * batch_ed(usb_transfer_batch_t *instance)
|
|---|
| 222 | {
|
|---|
| 223 | assert(instance);
|
|---|
| 224 | ohci_batch_t *data = instance->private_data;
|
|---|
| 225 | assert(data);
|
|---|
| 226 | return data->ed;
|
|---|
| 227 | }
|
|---|
| 228 | /*----------------------------------------------------------------------------*/
|
|---|
| 229 | void batch_control(usb_transfer_batch_t *instance,
|
|---|
| 230 | usb_direction_t data_dir, usb_direction_t status_dir)
|
|---|
| 231 | {
|
|---|
| 232 | assert(instance);
|
|---|
| 233 | ohci_batch_t *data = instance->private_data;
|
|---|
| 234 | assert(data);
|
|---|
| 235 | ed_init(data->ed, instance->ep);
|
|---|
| 236 | ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]);
|
|---|
| 237 | usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed,
|
|---|
| 238 | data->ed->status, data->ed->td_tail, data->ed->td_head,
|
|---|
| 239 | data->ed->next);
|
|---|
| 240 | int toggle = 0;
|
|---|
| 241 | /* setup stage */
|
|---|
| 242 | td_init(&data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
|
|---|
| 243 | instance->setup_size, toggle);
|
|---|
| 244 | td_set_next(&data->tds[0], &data->tds[1]);
|
|---|
| 245 | usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0].status,
|
|---|
| 246 | data->tds[0].cbp, data->tds[0].next, data->tds[0].be);
|
|---|
| 247 |
|
|---|
| 248 | /* data stage */
|
|---|
| 249 | size_t td_current = 1;
|
|---|
| 250 | size_t remain_size = instance->buffer_size;
|
|---|
| 251 | char *transfer_buffer = instance->transport_buffer;
|
|---|
| 252 | while (remain_size > 0) {
|
|---|
| 253 | size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
|
|---|
| 254 | OHCI_TD_MAX_TRANSFER : remain_size;
|
|---|
| 255 | toggle = 1 - toggle;
|
|---|
| 256 |
|
|---|
| 257 | td_init(&data->tds[td_current], data_dir, transfer_buffer,
|
|---|
| 258 | transfer_size, toggle);
|
|---|
| 259 | td_set_next(&data->tds[td_current], &data->tds[td_current + 1]);
|
|---|
| 260 | usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
|
|---|
| 261 | data->tds[td_current].status, data->tds[td_current].cbp,
|
|---|
| 262 | data->tds[td_current].next, data->tds[td_current].be);
|
|---|
| 263 |
|
|---|
| 264 | transfer_buffer += transfer_size;
|
|---|
| 265 | remain_size -= transfer_size;
|
|---|
| 266 | assert(td_current < data->td_count - 2);
|
|---|
| 267 | ++td_current;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /* status stage */
|
|---|
| 271 | assert(td_current == data->td_count - 2);
|
|---|
| 272 | td_init(&data->tds[td_current], status_dir, NULL, 0, 1);
|
|---|
| 273 | usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
|
|---|
| 274 | data->tds[td_current].status, data->tds[td_current].cbp,
|
|---|
| 275 | data->tds[td_current].next, data->tds[td_current].be);
|
|---|
| 276 | }
|
|---|
| 277 | /*----------------------------------------------------------------------------*/
|
|---|
| 278 | void batch_data(usb_transfer_batch_t *instance)
|
|---|
| 279 | {
|
|---|
| 280 | assert(instance);
|
|---|
| 281 | ohci_batch_t *data = instance->private_data;
|
|---|
| 282 | assert(data);
|
|---|
| 283 | ed_init(data->ed, instance->ep);
|
|---|
| 284 | ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]);
|
|---|
| 285 | usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed,
|
|---|
| 286 | data->ed->status, data->ed->td_tail, data->ed->td_head,
|
|---|
| 287 | data->ed->next);
|
|---|
| 288 |
|
|---|
| 289 | /* data stage */
|
|---|
| 290 | size_t td_current = 0;
|
|---|
| 291 | size_t remain_size = instance->buffer_size;
|
|---|
| 292 | char *transfer_buffer = instance->transport_buffer;
|
|---|
| 293 | while (remain_size > 0) {
|
|---|
| 294 | size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
|
|---|
| 295 | OHCI_TD_MAX_TRANSFER : remain_size;
|
|---|
| 296 |
|
|---|
| 297 | td_init(&data->tds[td_current], instance->ep->direction,
|
|---|
| 298 | transfer_buffer, transfer_size, -1);
|
|---|
| 299 | td_set_next(&data->tds[td_current], &data->tds[td_current + 1]);
|
|---|
| 300 | usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
|
|---|
| 301 | data->tds[td_current].status, data->tds[td_current].cbp,
|
|---|
| 302 | data->tds[td_current].next, data->tds[td_current].be);
|
|---|
| 303 |
|
|---|
| 304 | transfer_buffer += transfer_size;
|
|---|
| 305 | remain_size -= transfer_size;
|
|---|
| 306 | assert(td_current < data->td_count);
|
|---|
| 307 | ++td_current;
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 | /*----------------------------------------------------------------------------*/
|
|---|
| 311 | /** Helper function calls callback and correctly disposes of batch structure.
|
|---|
| 312 | *
|
|---|
| 313 | * @param[in] instance Batch structure to use.
|
|---|
| 314 | */
|
|---|
| 315 | void batch_call_in_and_dispose(usb_transfer_batch_t *instance)
|
|---|
| 316 | {
|
|---|
| 317 | assert(instance);
|
|---|
| 318 | usb_transfer_batch_call_in(instance);
|
|---|
| 319 | batch_dispose(instance);
|
|---|
| 320 | }
|
|---|
| 321 | /*----------------------------------------------------------------------------*/
|
|---|
| 322 | /** Helper function calls callback and correctly disposes of batch structure.
|
|---|
| 323 | *
|
|---|
| 324 | * @param[in] instance Batch structure to use.
|
|---|
| 325 | */
|
|---|
| 326 | void batch_call_out_and_dispose(usb_transfer_batch_t *instance)
|
|---|
| 327 | {
|
|---|
| 328 | assert(instance);
|
|---|
| 329 | usb_transfer_batch_call_out(instance);
|
|---|
| 330 | batch_dispose(instance);
|
|---|
| 331 | }
|
|---|
| 332 | /**
|
|---|
| 333 | * @}
|
|---|
| 334 | */
|
|---|