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