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 | const uint32_t next_pa = addr_to_phys(next);
|
---|
72 | assert((next_pa & LINK_POINTER_ADDRESS_MASK) == next_pa);
|
---|
73 |
|
---|
74 | instance->next = 0
|
---|
75 | | LINK_POINTER_VERTICAL_FLAG
|
---|
76 | | (next_pa ? next_pa : LINK_POINTER_TERMINATE_FLAG);
|
---|
77 |
|
---|
78 | instance->status = 0
|
---|
79 | | ((err_count & TD_STATUS_ERROR_COUNT_MASK) << TD_STATUS_ERROR_COUNT_POS)
|
---|
80 | | (low_speed ? TD_STATUS_LOW_SPEED_FLAG : 0)
|
---|
81 | | (iso ? TD_STATUS_ISOCHRONOUS_FLAG : 0)
|
---|
82 | | TD_STATUS_ERROR_ACTIVE;
|
---|
83 |
|
---|
84 | if (pid == USB_PID_IN && !iso) {
|
---|
85 | instance->status |= TD_STATUS_SPD_FLAG;
|
---|
86 | }
|
---|
87 |
|
---|
88 | instance->device = 0
|
---|
89 | | (((size - 1) & TD_DEVICE_MAXLEN_MASK) << TD_DEVICE_MAXLEN_POS)
|
---|
90 | | (toggle ? TD_DEVICE_DATA_TOGGLE_ONE_FLAG : 0)
|
---|
91 | | ((target.address & TD_DEVICE_ADDRESS_MASK) << TD_DEVICE_ADDRESS_POS)
|
---|
92 | | ((target.endpoint & TD_DEVICE_ENDPOINT_MASK) << TD_DEVICE_ENDPOINT_POS)
|
---|
93 | | ((pid & TD_DEVICE_PID_MASK) << TD_DEVICE_PID_POS);
|
---|
94 |
|
---|
95 | instance->buffer_ptr = addr_to_phys(buffer);
|
---|
96 |
|
---|
97 | usb_log_debug2("Created TD(%p): %X:%X:%X:%X(%p).\n",
|
---|
98 | instance, instance->next, instance->status, instance->device,
|
---|
99 | instance->buffer_ptr, buffer);
|
---|
100 | td_print_status(instance);
|
---|
101 | if (pid == USB_PID_SETUP) {
|
---|
102 | usb_log_debug("SETUP BUFFER: %s\n",
|
---|
103 | usb_debug_str_buffer(buffer, 8, 8));
|
---|
104 | }
|
---|
105 | }
|
---|
106 | /*----------------------------------------------------------------------------*/
|
---|
107 | /** Convert TD status into standard error code
|
---|
108 | *
|
---|
109 | * @param[in] instance TD structure to use.
|
---|
110 | * @return Error code.
|
---|
111 | */
|
---|
112 | int td_status(td_t *instance)
|
---|
113 | {
|
---|
114 | assert(instance);
|
---|
115 |
|
---|
116 | /* this is hc internal error it should never be reported */
|
---|
117 | if ((instance->status & TD_STATUS_ERROR_BIT_STUFF) != 0)
|
---|
118 | return EAGAIN;
|
---|
119 |
|
---|
120 | /* CRC or timeout error, like device not present or bad data,
|
---|
121 | * it won't be reported unless err count reached zero */
|
---|
122 | if ((instance->status & TD_STATUS_ERROR_CRC) != 0)
|
---|
123 | return EBADCHECKSUM;
|
---|
124 |
|
---|
125 | /* hc does not end transaction on these, it should never be reported */
|
---|
126 | if ((instance->status & TD_STATUS_ERROR_NAK) != 0)
|
---|
127 | return EAGAIN;
|
---|
128 |
|
---|
129 | /* buffer overrun or underrun */
|
---|
130 | if ((instance->status & TD_STATUS_ERROR_BUFFER) != 0)
|
---|
131 | return ERANGE;
|
---|
132 |
|
---|
133 | /* device babble is something serious */
|
---|
134 | if ((instance->status & TD_STATUS_ERROR_BABBLE) != 0)
|
---|
135 | return EIO;
|
---|
136 |
|
---|
137 | /* stall might represent err count reaching zero or stall response from
|
---|
138 | * the device, is err count reached zero, one of the above is reported*/
|
---|
139 | if ((instance->status & TD_STATUS_ERROR_STALLED) != 0)
|
---|
140 | return ESTALL;
|
---|
141 |
|
---|
142 | return EOK;
|
---|
143 | }
|
---|
144 | /*----------------------------------------------------------------------------*/
|
---|
145 | /** Print values in status field (dw1) in a human readable way.
|
---|
146 | *
|
---|
147 | * @param[in] instance TD structure to use.
|
---|
148 | */
|
---|
149 | void td_print_status(td_t *instance)
|
---|
150 | {
|
---|
151 | assert(instance);
|
---|
152 | const uint32_t s = instance->status;
|
---|
153 | usb_log_debug2("TD(%p) status(%#x):%s %d,%s%s%s%s%s%s%s%s%s%s%s %d.\n",
|
---|
154 | instance, instance->status,
|
---|
155 | (s & TD_STATUS_SPD_FLAG) ? " SPD," : "",
|
---|
156 | (s >> TD_STATUS_ERROR_COUNT_POS) & TD_STATUS_ERROR_COUNT_MASK,
|
---|
157 | (s & TD_STATUS_LOW_SPEED_FLAG) ? " LOW SPEED," : "",
|
---|
158 | (s & TD_STATUS_ISOCHRONOUS_FLAG) ? " ISOCHRONOUS," : "",
|
---|
159 | (s & TD_STATUS_IOC_FLAG) ? " IOC," : "",
|
---|
160 | (s & TD_STATUS_ERROR_ACTIVE) ? " ACTIVE," : "",
|
---|
161 | (s & TD_STATUS_ERROR_STALLED) ? " STALLED," : "",
|
---|
162 | (s & TD_STATUS_ERROR_BUFFER) ? " BUFFER," : "",
|
---|
163 | (s & TD_STATUS_ERROR_BABBLE) ? " BABBLE," : "",
|
---|
164 | (s & TD_STATUS_ERROR_NAK) ? " NAK," : "",
|
---|
165 | (s & TD_STATUS_ERROR_CRC) ? " CRC/TIMEOUT," : "",
|
---|
166 | (s & TD_STATUS_ERROR_BIT_STUFF) ? " BIT_STUFF," : "",
|
---|
167 | (s & TD_STATUS_ERROR_RESERVED) ? " RESERVED," : "",
|
---|
168 | td_act_size(instance)
|
---|
169 | );
|
---|
170 | }
|
---|
171 | /**
|
---|
172 | * @}
|
---|
173 | */
|
---|