| 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 usb
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 |
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "batch.h"
|
|---|
| 40 | #include "transfer_list.h"
|
|---|
| 41 | #include "uhci.h"
|
|---|
| 42 | #include "utils/malloc32.h"
|
|---|
| 43 |
|
|---|
| 44 | #define DEFAULT_ERROR_COUNT 3
|
|---|
| 45 |
|
|---|
| 46 | static int batch_schedule(batch_t *instance);
|
|---|
| 47 |
|
|---|
| 48 | static void batch_call_in(batch_t *instance);
|
|---|
| 49 | static void batch_call_out(batch_t *instance);
|
|---|
| 50 | static void batch_call_in_and_dispose(batch_t *instance);
|
|---|
| 51 | static void batch_call_out_and_dispose(batch_t *instance);
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 55 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
|---|
| 56 | usb_speed_t speed, char *buffer, size_t size,
|
|---|
| 57 | char* setup_buffer, size_t setup_size,
|
|---|
| 58 | usbhc_iface_transfer_in_callback_t func_in,
|
|---|
| 59 | usbhc_iface_transfer_out_callback_t func_out, void *arg)
|
|---|
| 60 | {
|
|---|
| 61 | assert(func_in == NULL || func_out == NULL);
|
|---|
| 62 | assert(func_in != NULL || func_out != NULL);
|
|---|
| 63 |
|
|---|
| 64 | batch_t *instance = malloc(sizeof(batch_t));
|
|---|
| 65 | if (instance == NULL) {
|
|---|
| 66 | usb_log_error("Failed to allocate batch instance.\n");
|
|---|
| 67 | return NULL;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | instance->qh = queue_head_get();
|
|---|
| 71 | if (instance->qh == NULL) {
|
|---|
| 72 | usb_log_error("Failed to allocate queue head.\n");
|
|---|
| 73 | free(instance);
|
|---|
| 74 | return NULL;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | instance->packets = (size + max_packet_size - 1) / max_packet_size;
|
|---|
| 78 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 79 | instance->packets += 2;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | instance->tds = malloc32(sizeof(transfer_descriptor_t) * instance->packets);
|
|---|
| 83 | if (instance->tds == NULL) {
|
|---|
| 84 | usb_log_error("Failed to allocate transfer descriptors.\n");
|
|---|
| 85 | queue_head_dispose(instance->qh);
|
|---|
| 86 | free(instance);
|
|---|
| 87 | return NULL;
|
|---|
| 88 | }
|
|---|
| 89 | bzero(instance->tds, sizeof(transfer_descriptor_t) * instance->packets);
|
|---|
| 90 |
|
|---|
| 91 | const size_t transport_size = max_packet_size * instance->packets;
|
|---|
| 92 |
|
|---|
| 93 | instance->transport_buffer =
|
|---|
| 94 | (size > 0) ? malloc32(transport_size) : NULL;
|
|---|
| 95 | if ((size > 0) && (instance->transport_buffer == NULL)) {
|
|---|
| 96 | usb_log_error("Failed to allocate device accessible buffer.\n");
|
|---|
| 97 | queue_head_dispose(instance->qh);
|
|---|
| 98 | free32(instance->tds);
|
|---|
| 99 | free(instance);
|
|---|
| 100 | return NULL;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | instance->setup_buffer = setup_buffer ? malloc32(setup_size) : NULL;
|
|---|
| 104 | if ((setup_size > 0) && (instance->setup_buffer == NULL)) {
|
|---|
| 105 | usb_log_error("Failed to allocate device accessible setup buffer.\n");
|
|---|
| 106 | queue_head_dispose(instance->qh);
|
|---|
| 107 | free32(instance->tds);
|
|---|
| 108 | free32(instance->transport_buffer);
|
|---|
| 109 | free(instance);
|
|---|
| 110 | return NULL;
|
|---|
| 111 | }
|
|---|
| 112 | if (instance->setup_buffer) {
|
|---|
| 113 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | instance->max_packet_size = max_packet_size;
|
|---|
| 117 |
|
|---|
| 118 | link_initialize(&instance->link);
|
|---|
| 119 |
|
|---|
| 120 | instance->target = target;
|
|---|
| 121 | instance->transfer_type = transfer_type;
|
|---|
| 122 |
|
|---|
| 123 | if (func_out)
|
|---|
| 124 | instance->callback_out = func_out;
|
|---|
| 125 | if (func_in)
|
|---|
| 126 | instance->callback_in = func_in;
|
|---|
| 127 |
|
|---|
| 128 | instance->buffer = buffer;
|
|---|
| 129 | instance->buffer_size = size;
|
|---|
| 130 | instance->setup_size = setup_size;
|
|---|
| 131 | instance->fun = fun;
|
|---|
| 132 | instance->arg = arg;
|
|---|
| 133 | instance->speed = speed;
|
|---|
| 134 |
|
|---|
| 135 | queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
|
|---|
| 136 | return instance;
|
|---|
| 137 | }
|
|---|
| 138 | /*----------------------------------------------------------------------------*/
|
|---|
| 139 | bool batch_is_complete(batch_t *instance)
|
|---|
| 140 | {
|
|---|
| 141 | assert(instance);
|
|---|
| 142 | usb_log_debug("Checking(%p) %d packet for completion.\n",
|
|---|
| 143 | instance, instance->packets);
|
|---|
| 144 | instance->transfered_size = 0;
|
|---|
| 145 | size_t i = 0;
|
|---|
| 146 | for (;i < instance->packets; ++i) {
|
|---|
| 147 | if (transfer_descriptor_is_active(&instance->tds[i])) {
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 | instance->error = transfer_descriptor_status(&instance->tds[i]);
|
|---|
| 151 | if (instance->error != EOK) {
|
|---|
| 152 | if (i > 0)
|
|---|
| 153 | instance->transfered_size -= instance->setup_size;
|
|---|
| 154 | return true;
|
|---|
| 155 | }
|
|---|
| 156 | instance->transfered_size +=
|
|---|
| 157 | transfer_descriptor_actual_size(&instance->tds[i]);
|
|---|
| 158 | }
|
|---|
| 159 | /* This is just an ugly trick to support the old API */
|
|---|
| 160 | instance->transfered_size -= instance->setup_size;
|
|---|
| 161 | return true;
|
|---|
| 162 | }
|
|---|
| 163 | /*----------------------------------------------------------------------------*/
|
|---|
| 164 | void batch_control_write(batch_t *instance)
|
|---|
| 165 | {
|
|---|
| 166 | assert(instance);
|
|---|
| 167 |
|
|---|
| 168 | /* we are data out, we are supposed to provide data */
|
|---|
| 169 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
|---|
| 170 |
|
|---|
| 171 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
|---|
| 172 | int toggle = 0;
|
|---|
| 173 | /* setup stage */
|
|---|
| 174 | transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
|
|---|
| 175 | instance->setup_size, toggle, false, low_speed,
|
|---|
| 176 | instance->target, USB_PID_SETUP, instance->setup_buffer,
|
|---|
| 177 | &instance->tds[1]);
|
|---|
| 178 |
|
|---|
| 179 | /* data stage */
|
|---|
| 180 | size_t i = 1;
|
|---|
| 181 | for (;i < instance->packets - 1; ++i) {
|
|---|
| 182 | char *data =
|
|---|
| 183 | instance->transport_buffer + ((i - 1) * instance->max_packet_size);
|
|---|
| 184 | toggle = 1 - toggle;
|
|---|
| 185 |
|
|---|
| 186 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 187 | instance->max_packet_size, toggle++, false, low_speed,
|
|---|
| 188 | instance->target, USB_PID_OUT, data, &instance->tds[i + 1]);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /* status stage */
|
|---|
| 192 | i = instance->packets - 1;
|
|---|
| 193 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 194 | 0, 1, false, low_speed, instance->target, USB_PID_IN, NULL, NULL);
|
|---|
| 195 |
|
|---|
| 196 | instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
|---|
| 197 | usb_log_debug("Control write last TD status: %x.\n",
|
|---|
| 198 | instance->tds[i].status);
|
|---|
| 199 |
|
|---|
| 200 | instance->next_step = batch_call_out_and_dispose;
|
|---|
| 201 | batch_schedule(instance);
|
|---|
| 202 | }
|
|---|
| 203 | /*----------------------------------------------------------------------------*/
|
|---|
| 204 | void batch_control_read(batch_t *instance)
|
|---|
| 205 | {
|
|---|
| 206 | assert(instance);
|
|---|
| 207 |
|
|---|
| 208 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
|---|
| 209 | int toggle = 0;
|
|---|
| 210 | /* setup stage */
|
|---|
| 211 | transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
|
|---|
| 212 | instance->setup_size, toggle, false, low_speed, instance->target,
|
|---|
| 213 | USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
|
|---|
| 214 |
|
|---|
| 215 | /* data stage */
|
|---|
| 216 | size_t i = 1;
|
|---|
| 217 | for (;i < instance->packets - 1; ++i) {
|
|---|
| 218 | char *data =
|
|---|
| 219 | instance->transport_buffer + ((i - 1) * instance->max_packet_size);
|
|---|
| 220 | toggle = 1 - toggle;
|
|---|
| 221 |
|
|---|
| 222 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 223 | instance->max_packet_size, toggle, false, low_speed,
|
|---|
| 224 | instance->target, USB_PID_IN, data, &instance->tds[i + 1]);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /* status stage */
|
|---|
| 228 | i = instance->packets - 1;
|
|---|
| 229 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 230 | 0, 1, false, low_speed, instance->target, USB_PID_OUT, NULL, NULL);
|
|---|
| 231 |
|
|---|
| 232 | instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
|---|
| 233 | usb_log_debug("Control read last TD status: %x.\n",
|
|---|
| 234 | instance->tds[i].status);
|
|---|
| 235 |
|
|---|
| 236 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 237 | batch_schedule(instance);
|
|---|
| 238 | }
|
|---|
| 239 | /*----------------------------------------------------------------------------*/
|
|---|
| 240 | void batch_interrupt_in(batch_t *instance)
|
|---|
| 241 | {
|
|---|
| 242 | assert(instance);
|
|---|
| 243 |
|
|---|
| 244 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
|---|
| 245 | int toggle = 1;
|
|---|
| 246 | size_t i = 0;
|
|---|
| 247 | for (;i < instance->packets; ++i) {
|
|---|
| 248 | char *data =
|
|---|
| 249 | instance->transport_buffer + (i * instance->max_packet_size);
|
|---|
| 250 | transfer_descriptor_t *next = (i + 1) < instance->packets ?
|
|---|
| 251 | &instance->tds[i + 1] : NULL;
|
|---|
| 252 | toggle = 1 - toggle;
|
|---|
| 253 |
|
|---|
| 254 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 255 | instance->max_packet_size, toggle, false, low_speed,
|
|---|
| 256 | instance->target, USB_PID_IN, data, next);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
|---|
| 260 |
|
|---|
| 261 | instance->next_step = batch_call_in_and_dispose;
|
|---|
| 262 | batch_schedule(instance);
|
|---|
| 263 | }
|
|---|
| 264 | /*----------------------------------------------------------------------------*/
|
|---|
| 265 | void batch_interrupt_out(batch_t *instance)
|
|---|
| 266 | {
|
|---|
| 267 | assert(instance);
|
|---|
| 268 |
|
|---|
| 269 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
|---|
| 270 |
|
|---|
| 271 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
|---|
| 272 | int toggle = 1;
|
|---|
| 273 | size_t i = 0;
|
|---|
| 274 | for (;i < instance->packets; ++i) {
|
|---|
| 275 | char *data =
|
|---|
| 276 | instance->transport_buffer + (i * instance->max_packet_size);
|
|---|
| 277 | transfer_descriptor_t *next = (i + 1) < instance->packets ?
|
|---|
| 278 | &instance->tds[i + 1] : NULL;
|
|---|
| 279 | toggle = 1 - toggle;
|
|---|
| 280 |
|
|---|
| 281 | transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
|
|---|
| 282 | instance->max_packet_size, toggle++, false, low_speed,
|
|---|
| 283 | instance->target, USB_PID_OUT, data, next);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
|---|
| 287 |
|
|---|
| 288 | instance->next_step = batch_call_out_and_dispose;
|
|---|
| 289 | batch_schedule(instance);
|
|---|
| 290 | }
|
|---|
| 291 | /*----------------------------------------------------------------------------*/
|
|---|
| 292 | void batch_call_in(batch_t *instance)
|
|---|
| 293 | {
|
|---|
| 294 | assert(instance);
|
|---|
| 295 | assert(instance->callback_in);
|
|---|
| 296 |
|
|---|
| 297 | memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
|
|---|
| 298 |
|
|---|
| 299 | int err = instance->error;
|
|---|
| 300 | usb_log_info("Callback IN(%d): %s(%d), %zu.\n", instance->transfer_type,
|
|---|
| 301 | str_error(err), err, instance->transfered_size);
|
|---|
| 302 |
|
|---|
| 303 | instance->callback_in(instance->fun,
|
|---|
| 304 | err, instance->transfered_size,
|
|---|
| 305 | instance->arg);
|
|---|
| 306 | }
|
|---|
| 307 | /*----------------------------------------------------------------------------*/
|
|---|
| 308 | void batch_call_out(batch_t *instance)
|
|---|
| 309 | {
|
|---|
| 310 | assert(instance);
|
|---|
| 311 | assert(instance->callback_out);
|
|---|
| 312 |
|
|---|
| 313 | int err = instance->error;
|
|---|
| 314 | usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
|
|---|
| 315 | instance->callback_out(instance->fun,
|
|---|
| 316 | err, instance->arg);
|
|---|
| 317 | }
|
|---|
| 318 | /*----------------------------------------------------------------------------*/
|
|---|
| 319 | void batch_call_in_and_dispose(batch_t *instance)
|
|---|
| 320 | {
|
|---|
| 321 | assert(instance);
|
|---|
| 322 | batch_call_in(instance);
|
|---|
| 323 | usb_log_debug("Disposing batch: %p.\n", instance);
|
|---|
| 324 | free32(instance->tds);
|
|---|
| 325 | free32(instance->qh);
|
|---|
| 326 | free32(instance->setup_buffer);
|
|---|
| 327 | free32(instance->transport_buffer);
|
|---|
| 328 | free(instance);
|
|---|
| 329 | }
|
|---|
| 330 | /*----------------------------------------------------------------------------*/
|
|---|
| 331 | void batch_call_out_and_dispose(batch_t *instance)
|
|---|
| 332 | {
|
|---|
| 333 | assert(instance);
|
|---|
| 334 | batch_call_out(instance);
|
|---|
| 335 | usb_log_debug("Disposing batch: %p.\n", instance);
|
|---|
| 336 | free32(instance->tds);
|
|---|
| 337 | free32(instance->qh);
|
|---|
| 338 | free32(instance->setup_buffer);
|
|---|
| 339 | free32(instance->transport_buffer);
|
|---|
| 340 | free(instance);
|
|---|
| 341 | }
|
|---|
| 342 | /*----------------------------------------------------------------------------*/
|
|---|
| 343 | int batch_schedule(batch_t *instance)
|
|---|
| 344 | {
|
|---|
| 345 | assert(instance);
|
|---|
| 346 | uhci_t *hc = fun_to_uhci(instance->fun);
|
|---|
| 347 | assert(hc);
|
|---|
| 348 | return uhci_schedule(hc, instance);
|
|---|
| 349 | }
|
|---|
| 350 | /**
|
|---|
| 351 | * @}
|
|---|
| 352 | */
|
|---|