[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"
|
---|
[9a6fde4] | 41 | #include "hcd_endpoint.h"
|
---|
[2bf8f8c] | 42 | #include "utils/malloc32.h"
|
---|
[06c552c] | 43 | #include "hw_struct/endpoint_descriptor.h"
|
---|
| 44 | #include "hw_struct/transfer_descriptor.h"
|
---|
| 45 |
|
---|
[2cc6e97] | 46 | typedef struct ohci_transfer_batch {
|
---|
[06c552c] | 47 | ed_t *ed;
|
---|
[9a6fde4] | 48 | td_t **tds;
|
---|
[06c552c] | 49 | size_t td_count;
|
---|
[9a6fde4] | 50 | size_t leave_td;
|
---|
| 51 | char *device_buffer;
|
---|
[2cc6e97] | 52 | } ohci_transfer_batch_t;
|
---|
| 53 |
|
---|
| 54 | static void ohci_transfer_batch_dispose(void *ohci_batch)
|
---|
| 55 | {
|
---|
| 56 | ohci_transfer_batch_t *instance = ohci_batch;
|
---|
[9a6fde4] | 57 | if (!instance)
|
---|
| 58 | return;
|
---|
| 59 | free32(instance->device_buffer);
|
---|
| 60 | unsigned i = 0;
|
---|
| 61 | if (instance->tds) {
|
---|
| 62 | for (; i< instance->td_count; ++i) {
|
---|
| 63 | if (i != instance->leave_td)
|
---|
| 64 | free32(instance->tds[i]);
|
---|
| 65 | }
|
---|
| 66 | free(instance->tds);
|
---|
| 67 | }
|
---|
| 68 | free(instance);
|
---|
[2cc6e97] | 69 | }
|
---|
[9a6fde4] | 70 | /*----------------------------------------------------------------------------*/
|
---|
[e42dd32] | 71 | static void batch_control(usb_transfer_batch_t *instance,
|
---|
| 72 | usb_direction_t data_dir, usb_direction_t status_dir);
|
---|
[c6fe469] | 73 | static void batch_data(usb_transfer_batch_t *instance);
|
---|
[9a6fde4] | 74 | /*----------------------------------------------------------------------------*/
|
---|
[6bec59b] | 75 | usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
|
---|
| 76 | char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
|
---|
[be7950e8] | 77 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
[6bec59b] | 78 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
---|
[be7950e8] | 79 | {
|
---|
[2bf8f8c] | 80 | #define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
|
---|
| 81 | if (ptr == NULL) { \
|
---|
| 82 | usb_log_error(message); \
|
---|
| 83 | if (instance) { \
|
---|
[2cc6e97] | 84 | usb_transfer_batch_dispose(instance); \
|
---|
[2bf8f8c] | 85 | } \
|
---|
| 86 | return NULL; \
|
---|
| 87 | } else (void)0
|
---|
| 88 |
|
---|
[1387692] | 89 | usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
|
---|
[2bf8f8c] | 90 | CHECK_NULL_DISPOSE_RETURN(instance,
|
---|
| 91 | "Failed to allocate batch instance.\n");
|
---|
[d017cea] | 92 | usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
|
---|
[2cc6e97] | 93 | NULL, setup_size, func_in, func_out, arg, fun, NULL,
|
---|
| 94 | ohci_transfer_batch_dispose);
|
---|
[2bf8f8c] | 95 |
|
---|
[9a6fde4] | 96 | hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
|
---|
| 97 | assert(hcd_ep);
|
---|
| 98 |
|
---|
| 99 | ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
|
---|
[06c552c] | 100 | CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
|
---|
| 101 | instance->private_data = data;
|
---|
| 102 |
|
---|
[9a6fde4] | 103 | data->td_count =
|
---|
[b854e56] | 104 | ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
|
---|
[06c552c] | 105 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 106 | data->td_count += 2;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[9a6fde4] | 109 | /* we need one extra place for td that is currently assigned to hcd_ep*/
|
---|
| 110 | data->tds = calloc(sizeof(td_t*), data->td_count + 1);
|
---|
[06c552c] | 111 | CHECK_NULL_DISPOSE_RETURN(data->tds,
|
---|
| 112 | "Failed to allocate transfer descriptors.\n");
|
---|
| 113 |
|
---|
[9a6fde4] | 114 | data->tds[0] = hcd_ep->td;
|
---|
| 115 | data->leave_td = 0;
|
---|
| 116 | unsigned i = 1;
|
---|
| 117 | for (; i <= data->td_count; ++i) {
|
---|
| 118 | data->tds[i] = malloc32(sizeof(td_t));
|
---|
| 119 | CHECK_NULL_DISPOSE_RETURN(data->tds[i],
|
---|
| 120 | "Failed to allocate TD %d.\n", i );
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | data->ed = hcd_ep->ed;
|
---|
[06c552c] | 124 |
|
---|
[9a6fde4] | 125 | if (setup_size + buffer_size > 0) {
|
---|
| 126 | data->device_buffer = malloc32(setup_size + buffer_size);
|
---|
| 127 | CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
|
---|
[2bf8f8c] | 128 | "Failed to allocate device accessible buffer.\n");
|
---|
[9a6fde4] | 129 | instance->setup_buffer = data->device_buffer;
|
---|
| 130 | instance->data_buffer = data->device_buffer + setup_size;
|
---|
[2bf8f8c] | 131 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | return instance;
|
---|
[be7950e8] | 135 | }
|
---|
| 136 | /*----------------------------------------------------------------------------*/
|
---|
[f98b8269] | 137 | bool batch_is_complete(usb_transfer_batch_t *instance)
|
---|
| 138 | {
|
---|
[f1be95c8] | 139 | assert(instance);
|
---|
[2cc6e97] | 140 | ohci_transfer_batch_t *data = instance->private_data;
|
---|
[f1be95c8] | 141 | assert(data);
|
---|
[9a6fde4] | 142 | size_t tds = data->td_count;
|
---|
[78d4e1f] | 143 | usb_log_debug("Batch(%p) checking %d td(s) for completion.\n",
|
---|
[f1be95c8] | 144 | instance, tds);
|
---|
[78d4e1f] | 145 | usb_log_debug("ED: %x:%x:%x:%x.\n",
|
---|
| 146 | data->ed->status, data->ed->td_head, data->ed->td_tail,
|
---|
| 147 | data->ed->next);
|
---|
[f1be95c8] | 148 | size_t i = 0;
|
---|
| 149 | for (; i < tds; ++i) {
|
---|
[9a6fde4] | 150 | assert(data->tds[i] != NULL);
|
---|
[78d4e1f] | 151 | usb_log_debug("TD %d: %x:%x:%x:%x.\n", i,
|
---|
[9a6fde4] | 152 | data->tds[i]->status, data->tds[i]->cbp, data->tds[i]->next,
|
---|
| 153 | data->tds[i]->be);
|
---|
| 154 | if (!td_is_finished(data->tds[i])) {
|
---|
[f1be95c8] | 155 | return false;
|
---|
[aa9ccf7] | 156 | }
|
---|
[9a6fde4] | 157 | instance->error = td_error(data->tds[i]);
|
---|
[f1be95c8] | 158 | /* FIXME: calculate real transfered size */
|
---|
| 159 | instance->transfered_size = instance->buffer_size;
|
---|
| 160 | if (instance->error != EOK) {
|
---|
| 161 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
---|
[9a6fde4] | 162 | instance, i, data->tds[i]->status);
|
---|
| 163 | break;
|
---|
[f1be95c8] | 164 | }
|
---|
| 165 | }
|
---|
[d6522dd] | 166 | data->leave_td = i;
|
---|
[9a6fde4] | 167 | assert(data->leave_td <= data->td_count);
|
---|
[d6522dd] | 168 | hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
|
---|
| 169 | assert(hcd_ep);
|
---|
| 170 | hcd_ep->td = data->tds[i];
|
---|
| 171 |
|
---|
[f1be95c8] | 172 | return true;
|
---|
[f98b8269] | 173 | }
|
---|
| 174 | /*----------------------------------------------------------------------------*/
|
---|
[7013b14] | 175 | void batch_commit(usb_transfer_batch_t *instance)
|
---|
| 176 | {
|
---|
| 177 | assert(instance);
|
---|
| 178 | ohci_transfer_batch_t *data = instance->private_data;
|
---|
| 179 | assert(data);
|
---|
| 180 | ed_set_end_td(data->ed, data->tds[data->td_count]);
|
---|
| 181 | }
|
---|
| 182 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 183 | void batch_control_write(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 184 | {
|
---|
[2bf8f8c] | 185 | assert(instance);
|
---|
[d328172] | 186 | /* We are data out, we are supposed to provide data */
|
---|
[d017cea] | 187 | memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
|
---|
[2cc6e97] | 188 | instance->next_step = usb_transfer_batch_call_out_and_dispose;
|
---|
[e42dd32] | 189 | batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
|
---|
[d328172] | 190 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
[be7950e8] | 191 | }
|
---|
| 192 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 193 | void batch_control_read(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 194 | {
|
---|
[2bf8f8c] | 195 | assert(instance);
|
---|
[2cc6e97] | 196 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
[e42dd32] | 197 | batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
|
---|
[81001f6] | 198 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
[be7950e8] | 199 | }
|
---|
| 200 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 201 | void batch_interrupt_in(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 202 | {
|
---|
[2bf8f8c] | 203 | assert(instance);
|
---|
[2cc6e97] | 204 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
[c6fe469] | 205 | batch_data(instance);
|
---|
[d328172] | 206 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
[be7950e8] | 207 | }
|
---|
| 208 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 209 | void batch_interrupt_out(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 210 | {
|
---|
[2bf8f8c] | 211 | assert(instance);
|
---|
[d328172] | 212 | /* We are data out, we are supposed to provide data */
|
---|
[d017cea] | 213 | memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
|
---|
[2cc6e97] | 214 | instance->next_step = usb_transfer_batch_call_out_and_dispose;
|
---|
[c6fe469] | 215 | batch_data(instance);
|
---|
[d328172] | 216 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
[be7950e8] | 217 | }
|
---|
| 218 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 219 | void batch_bulk_in(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 220 | {
|
---|
[2bf8f8c] | 221 | assert(instance);
|
---|
[2cc6e97] | 222 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
[c6fe469] | 223 | batch_data(instance);
|
---|
[d328172] | 224 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
[be7950e8] | 225 | }
|
---|
| 226 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 227 | void batch_bulk_out(usb_transfer_batch_t *instance)
|
---|
[be7950e8] | 228 | {
|
---|
[2bf8f8c] | 229 | assert(instance);
|
---|
[2cc6e97] | 230 | instance->next_step = usb_transfer_batch_call_in_and_dispose;
|
---|
[c6fe469] | 231 | batch_data(instance);
|
---|
[d328172] | 232 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
| 233 | }
|
---|
| 234 | /*----------------------------------------------------------------------------*/
|
---|
[f98b8269] | 235 | ed_t * batch_ed(usb_transfer_batch_t *instance)
|
---|
| 236 | {
|
---|
[7786cea] | 237 | assert(instance);
|
---|
[2cc6e97] | 238 | ohci_transfer_batch_t *data = instance->private_data;
|
---|
[7786cea] | 239 | assert(data);
|
---|
| 240 | return data->ed;
|
---|
| 241 | }
|
---|
| 242 | /*----------------------------------------------------------------------------*/
|
---|
[e42dd32] | 243 | void batch_control(usb_transfer_batch_t *instance,
|
---|
| 244 | usb_direction_t data_dir, usb_direction_t status_dir)
|
---|
[7786cea] | 245 | {
|
---|
| 246 | assert(instance);
|
---|
[2cc6e97] | 247 | ohci_transfer_batch_t *data = instance->private_data;
|
---|
[7786cea] | 248 | assert(data);
|
---|
[d6522dd] | 249 | usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
|
---|
[8790650] | 250 | data->ed->status, data->ed->td_tail, data->ed->td_head,
|
---|
| 251 | data->ed->next);
|
---|
[e42dd32] | 252 | int toggle = 0;
|
---|
| 253 | /* setup stage */
|
---|
[9a6fde4] | 254 | td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
|
---|
[e42dd32] | 255 | instance->setup_size, toggle);
|
---|
[9a6fde4] | 256 | td_set_next(data->tds[0], data->tds[1]);
|
---|
| 257 | usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0]->status,
|
---|
| 258 | data->tds[0]->cbp, data->tds[0]->next, data->tds[0]->be);
|
---|
[e42dd32] | 259 |
|
---|
| 260 | /* data stage */
|
---|
| 261 | size_t td_current = 1;
|
---|
| 262 | size_t remain_size = instance->buffer_size;
|
---|
[d017cea] | 263 | char *buffer = instance->data_buffer;
|
---|
[e42dd32] | 264 | while (remain_size > 0) {
|
---|
| 265 | size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
|
---|
| 266 | OHCI_TD_MAX_TRANSFER : remain_size;
|
---|
| 267 | toggle = 1 - toggle;
|
---|
| 268 |
|
---|
[9a6fde4] | 269 | td_init(data->tds[td_current], data_dir, buffer,
|
---|
[e42dd32] | 270 | transfer_size, toggle);
|
---|
[9a6fde4] | 271 | td_set_next(data->tds[td_current], data->tds[td_current + 1]);
|
---|
[e42dd32] | 272 | usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
|
---|
[9a6fde4] | 273 | data->tds[td_current]->status, data->tds[td_current]->cbp,
|
---|
| 274 | data->tds[td_current]->next, data->tds[td_current]->be);
|
---|
[e42dd32] | 275 |
|
---|
[d017cea] | 276 | buffer += transfer_size;
|
---|
[e42dd32] | 277 | remain_size -= transfer_size;
|
---|
[9a6fde4] | 278 | assert(td_current < data->td_count - 1);
|
---|
[e42dd32] | 279 | ++td_current;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /* status stage */
|
---|
[9a6fde4] | 283 | assert(td_current == data->td_count - 1);
|
---|
| 284 | td_init(data->tds[td_current], status_dir, NULL, 0, 1);
|
---|
| 285 | td_set_next(data->tds[td_current], data->tds[td_current + 1]);
|
---|
[e42dd32] | 286 | usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
|
---|
[9a6fde4] | 287 | data->tds[td_current]->status, data->tds[td_current]->cbp,
|
---|
| 288 | data->tds[td_current]->next, data->tds[td_current]->be);
|
---|
[f98b8269] | 289 | }
|
---|
| 290 | /*----------------------------------------------------------------------------*/
|
---|
[c6fe469] | 291 | void batch_data(usb_transfer_batch_t *instance)
|
---|
| 292 | {
|
---|
| 293 | assert(instance);
|
---|
[2cc6e97] | 294 | ohci_transfer_batch_t *data = instance->private_data;
|
---|
[c6fe469] | 295 | assert(data);
|
---|
[d6522dd] | 296 | usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
|
---|
[c6fe469] | 297 | data->ed->status, data->ed->td_tail, data->ed->td_head,
|
---|
| 298 | data->ed->next);
|
---|
| 299 |
|
---|
[aa9ccf7] | 300 | size_t td_current = 0;
|
---|
[c6fe469] | 301 | size_t remain_size = instance->buffer_size;
|
---|
[d017cea] | 302 | char *buffer = instance->data_buffer;
|
---|
[c6fe469] | 303 | while (remain_size > 0) {
|
---|
| 304 | size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
|
---|
| 305 | OHCI_TD_MAX_TRANSFER : remain_size;
|
---|
| 306 |
|
---|
[9a6fde4] | 307 | td_init(data->tds[td_current], instance->ep->direction,
|
---|
[d017cea] | 308 | buffer, transfer_size, -1);
|
---|
[9a6fde4] | 309 | td_set_next(data->tds[td_current], data->tds[td_current + 1]);
|
---|
[c6fe469] | 310 | usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
|
---|
[9a6fde4] | 311 | data->tds[td_current]->status, data->tds[td_current]->cbp,
|
---|
| 312 | data->tds[td_current]->next, data->tds[td_current]->be);
|
---|
[c6fe469] | 313 |
|
---|
[d017cea] | 314 | buffer += transfer_size;
|
---|
[c6fe469] | 315 | remain_size -= transfer_size;
|
---|
| 316 | assert(td_current < data->td_count);
|
---|
| 317 | ++td_current;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
[41b96b4] | 320 | /**
|
---|
| 321 | * @}
|
---|
| 322 | */
|
---|