| 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 drvusbuhcihc
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief UHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <usb/debug.h>
|
|---|
| 36 |
|
|---|
| 37 | #include "transfer_descriptor.h"
|
|---|
| 38 | #include "utils/malloc32.h"
|
|---|
| 39 |
|
|---|
| 40 | /** Initialize Transfer Descriptor
|
|---|
| 41 | *
|
|---|
| 42 | * @param[in] instance Memory place to initialize.
|
|---|
| 43 | * @param[in] err_count Number of retries hc should attempt.
|
|---|
| 44 | * @param[in] size Size of data source.
|
|---|
| 45 | * @param[in] toggle Value of toggle bit.
|
|---|
| 46 | * @param[in] iso True if TD represents Isochronous transfer.
|
|---|
| 47 | * @param[in] low_speed Target device's speed.
|
|---|
| 48 | * @param[in] target Address and endpoint receiving the transfer.
|
|---|
| 49 | * @param[in] pid Packet identification (SETUP, IN or OUT).
|
|---|
| 50 | * @param[in] buffer Source of data.
|
|---|
| 51 | * @param[in] next Net TD in transaction.
|
|---|
| 52 | * @return Error code.
|
|---|
| 53 | *
|
|---|
| 54 | * Uses a mix of supplied and default values.
|
|---|
| 55 | * Implicit values:
|
|---|
| 56 | * - all TDs have vertical flag set (makes transfers to endpoints atomic)
|
|---|
| 57 | * - in the error field only active it is set
|
|---|
| 58 | * - if the packet uses PID_IN and is not isochronous SPD is set
|
|---|
| 59 | *
|
|---|
| 60 | * Dumps 8 bytes of buffer if PID_SETUP is used.
|
|---|
| 61 | */
|
|---|
| 62 | void td_init(td_t *instance, int err_count, size_t size, bool toggle, bool iso,
|
|---|
| 63 | bool low_speed, usb_target_t target, usb_packet_id pid, void *buffer,
|
|---|
| 64 | td_t *next)
|
|---|
| 65 | {
|
|---|
| 66 | assert(instance);
|
|---|
| 67 | assert(size < 1024);
|
|---|
| 68 | assert((pid == USB_PID_SETUP) || (pid == USB_PID_IN)
|
|---|
| 69 | || (pid == USB_PID_OUT));
|
|---|
| 70 |
|
|---|
| 71 | instance->next = 0
|
|---|
| 72 | | LINK_POINTER_VERTICAL_FLAG
|
|---|
| 73 | | ((next != NULL) ? addr_to_phys(next) : LINK_POINTER_TERMINATE_FLAG);
|
|---|
| 74 |
|
|---|
| 75 | instance->status = 0
|
|---|
| 76 | | ((err_count & TD_STATUS_ERROR_COUNT_MASK) << TD_STATUS_ERROR_COUNT_POS)
|
|---|
| 77 | | (low_speed ? TD_STATUS_LOW_SPEED_FLAG : 0)
|
|---|
| 78 | | (iso ? TD_STATUS_ISOCHRONOUS_FLAG : 0)
|
|---|
| 79 | | TD_STATUS_ERROR_ACTIVE;
|
|---|
| 80 |
|
|---|
| 81 | if (pid == USB_PID_IN && !iso) {
|
|---|
| 82 | instance->status |= TD_STATUS_SPD_FLAG;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | instance->device = 0
|
|---|
| 86 | | (((size - 1) & TD_DEVICE_MAXLEN_MASK) << TD_DEVICE_MAXLEN_POS)
|
|---|
| 87 | | (toggle ? TD_DEVICE_DATA_TOGGLE_ONE_FLAG : 0)
|
|---|
| 88 | | ((target.address & TD_DEVICE_ADDRESS_MASK) << TD_DEVICE_ADDRESS_POS)
|
|---|
| 89 | | ((target.endpoint & TD_DEVICE_ENDPOINT_MASK) << TD_DEVICE_ENDPOINT_POS)
|
|---|
| 90 | | ((pid & TD_DEVICE_PID_MASK) << TD_DEVICE_PID_POS);
|
|---|
| 91 |
|
|---|
| 92 | instance->buffer_ptr = 0;
|
|---|
| 93 |
|
|---|
| 94 | if (size) {
|
|---|
| 95 | instance->buffer_ptr = (uintptr_t)addr_to_phys(buffer);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | usb_log_debug2("Created TD(%p): %X:%X:%X:%X(%p).\n",
|
|---|
| 99 | instance, instance->next, instance->status, instance->device,
|
|---|
| 100 | instance->buffer_ptr, buffer);
|
|---|
| 101 | td_print_status(instance);
|
|---|
| 102 | if (pid == USB_PID_SETUP) {
|
|---|
| 103 | usb_log_debug("SETUP BUFFER: %s\n",
|
|---|
| 104 | usb_debug_str_buffer(buffer, 8, 8));
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | /*----------------------------------------------------------------------------*/
|
|---|
| 108 | /** Convert TD status into standard error code
|
|---|
| 109 | *
|
|---|
| 110 | * @param[in] instance TD structure to use.
|
|---|
| 111 | * @return Error code.
|
|---|
| 112 | */
|
|---|
| 113 | int td_status(td_t *instance)
|
|---|
| 114 | {
|
|---|
| 115 | assert(instance);
|
|---|
| 116 |
|
|---|
| 117 | if ((instance->status & TD_STATUS_ERROR_STALLED) != 0)
|
|---|
| 118 | return ESTALL;
|
|---|
| 119 |
|
|---|
| 120 | if ((instance->status & TD_STATUS_ERROR_CRC) != 0)
|
|---|
| 121 | return EBADCHECKSUM;
|
|---|
| 122 |
|
|---|
| 123 | if ((instance->status & TD_STATUS_ERROR_BUFFER) != 0)
|
|---|
| 124 | return EAGAIN;
|
|---|
| 125 |
|
|---|
| 126 | if ((instance->status & TD_STATUS_ERROR_BABBLE) != 0)
|
|---|
| 127 | return EIO;
|
|---|
| 128 |
|
|---|
| 129 | if ((instance->status & TD_STATUS_ERROR_NAK) != 0)
|
|---|
| 130 | return EAGAIN;
|
|---|
| 131 |
|
|---|
| 132 | if ((instance->status & TD_STATUS_ERROR_BIT_STUFF) != 0)
|
|---|
| 133 | return EAGAIN;
|
|---|
| 134 |
|
|---|
| 135 | return EOK;
|
|---|
| 136 | }
|
|---|
| 137 | /*----------------------------------------------------------------------------*/
|
|---|
| 138 | /** Print values in status field (dw1) in a human readable way.
|
|---|
| 139 | *
|
|---|
| 140 | * @param[in] instance TD structure to use.
|
|---|
| 141 | */
|
|---|
| 142 | void td_print_status(td_t *instance)
|
|---|
| 143 | {
|
|---|
| 144 | assert(instance);
|
|---|
| 145 | const uint32_t s = instance->status;
|
|---|
| 146 | usb_log_debug2("TD(%p) status(%#x):%s %d,%s%s%s%s%s%s%s%s%s%s%s %d.\n",
|
|---|
| 147 | instance, instance->status,
|
|---|
| 148 | (s & TD_STATUS_SPD_FLAG) ? " SPD," : "",
|
|---|
| 149 | (s >> TD_STATUS_ERROR_COUNT_POS) & TD_STATUS_ERROR_COUNT_MASK,
|
|---|
| 150 | (s & TD_STATUS_LOW_SPEED_FLAG) ? " LOW SPEED," : "",
|
|---|
| 151 | (s & TD_STATUS_ISOCHRONOUS_FLAG) ? " ISOCHRONOUS," : "",
|
|---|
| 152 | (s & TD_STATUS_IOC_FLAG) ? " IOC," : "",
|
|---|
| 153 | (s & TD_STATUS_ERROR_ACTIVE) ? " ACTIVE," : "",
|
|---|
| 154 | (s & TD_STATUS_ERROR_STALLED) ? " STALLED," : "",
|
|---|
| 155 | (s & TD_STATUS_ERROR_BUFFER) ? " BUFFER," : "",
|
|---|
| 156 | (s & TD_STATUS_ERROR_BABBLE) ? " BABBLE," : "",
|
|---|
| 157 | (s & TD_STATUS_ERROR_NAK) ? " NAK," : "",
|
|---|
| 158 | (s & TD_STATUS_ERROR_CRC) ? " CRC/TIMEOUT," : "",
|
|---|
| 159 | (s & TD_STATUS_ERROR_BIT_STUFF) ? " BIT_STUFF," : "",
|
|---|
| 160 | (s & TD_STATUS_ERROR_RESERVED) ? " RESERVED," : "",
|
|---|
| 161 | td_act_size(instance)
|
|---|
| 162 | );
|
|---|
| 163 | }
|
|---|
| 164 | /**
|
|---|
| 165 | * @}
|
|---|
| 166 | */
|
|---|