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