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 |
|
---|
29 | /** @addtogroup drvusbuhci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief UHCI driver
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef DRV_UHCI_HW_STRUCT_TRANSFER_DESCRIPTOR_H
|
---|
37 | #define DRV_UHCI_HW_STRUCT_TRANSFER_DESCRIPTOR_H
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
40 | #include <usb/usb.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 | #include <stddef.h>
|
---|
43 | #include <stdint.h>
|
---|
44 |
|
---|
45 | #include "link_pointer.h"
|
---|
46 |
|
---|
47 | /** Transfer Descriptor, defined in UHCI design guide p. 26 */
|
---|
48 | typedef struct transfer_descriptor {
|
---|
49 | /** Pointer to the next entity (TD or QH) */
|
---|
50 | link_pointer_t next;
|
---|
51 |
|
---|
52 | /** Status doubleword */
|
---|
53 | volatile uint32_t status;
|
---|
54 | #define TD_STATUS_RESERVED_MASK 0xc000f800
|
---|
55 | #define TD_STATUS_SPD_FLAG (1 << 29)
|
---|
56 | #define TD_STATUS_ERROR_COUNT_POS 27
|
---|
57 | #define TD_STATUS_ERROR_COUNT_MASK 0x3
|
---|
58 | #define TD_STATUS_LOW_SPEED_FLAG (1 << 26)
|
---|
59 | #define TD_STATUS_ISOCHRONOUS_FLAG (1 << 25)
|
---|
60 | #define TD_STATUS_IOC_FLAG (1 << 24)
|
---|
61 |
|
---|
62 | #define TD_STATUS_ERROR_ACTIVE (1 << 23)
|
---|
63 | #define TD_STATUS_ERROR_STALLED (1 << 22)
|
---|
64 | #define TD_STATUS_ERROR_BUFFER (1 << 21)
|
---|
65 | #define TD_STATUS_ERROR_BABBLE (1 << 20)
|
---|
66 | #define TD_STATUS_ERROR_NAK (1 << 19)
|
---|
67 | #define TD_STATUS_ERROR_CRC (1 << 18)
|
---|
68 | #define TD_STATUS_ERROR_BIT_STUFF (1 << 17)
|
---|
69 | #define TD_STATUS_ERROR_RESERVED (1 << 16)
|
---|
70 | #define TD_STATUS_ERROR_POS 16
|
---|
71 | #define TD_STATUS_ERROR_MASK 0xff
|
---|
72 |
|
---|
73 | #define TD_STATUS_ACTLEN_POS 0
|
---|
74 | #define TD_STATUS_ACTLEN_MASK 0x7ff
|
---|
75 |
|
---|
76 | /** Double word with USB device specific info */
|
---|
77 | volatile uint32_t device;
|
---|
78 | #define TD_DEVICE_MAXLEN_POS 21
|
---|
79 | #define TD_DEVICE_MAXLEN_MASK 0x7ff
|
---|
80 | #define TD_DEVICE_RESERVED_FLAG (1 << 20)
|
---|
81 | #define TD_DEVICE_DATA_TOGGLE_ONE_FLAG (1 << 19)
|
---|
82 | #define TD_DEVICE_ENDPOINT_POS 15
|
---|
83 | #define TD_DEVICE_ENDPOINT_MASK 0xf
|
---|
84 | #define TD_DEVICE_ADDRESS_POS 8
|
---|
85 | #define TD_DEVICE_ADDRESS_MASK 0x7f
|
---|
86 | #define TD_DEVICE_PID_POS 0
|
---|
87 | #define TD_DEVICE_PID_MASK 0xff
|
---|
88 |
|
---|
89 | /** Pointer(physical) to the beginning of the transaction's buffer */
|
---|
90 | volatile uint32_t buffer_ptr;
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * According to UHCI design guide, there is 16 bytes of
|
---|
94 | * data available here.
|
---|
95 | * According to Linux kernel the hardware does not care,
|
---|
96 | * memory just needs to be aligned. We don't use it anyway.
|
---|
97 | */
|
---|
98 | } __attribute__((packed, aligned(16))) td_t;
|
---|
99 |
|
---|
100 | void td_init(td_t *instance, int error_count, size_t size, bool toggle,
|
---|
101 | bool iso, bool low_speed, usb_target_t target, usb_packet_id pid,
|
---|
102 | const void *buffer, const td_t *next);
|
---|
103 |
|
---|
104 | errno_t td_status(const td_t *instance);
|
---|
105 |
|
---|
106 | void td_print_status(const td_t *instance);
|
---|
107 |
|
---|
108 | /** Helper function for parsing actual size out of TD.
|
---|
109 | *
|
---|
110 | * @param[in] instance TD structure to use.
|
---|
111 | * @return Parsed actual size.
|
---|
112 | */
|
---|
113 | static inline size_t td_act_size(const td_t *instance)
|
---|
114 | {
|
---|
115 | assert(instance);
|
---|
116 | const uint32_t s = instance->status;
|
---|
117 | /* Actual size is encoded as n-1 (UHCI design guide p. 23) */
|
---|
118 | return ((s >> TD_STATUS_ACTLEN_POS) + 1) & TD_STATUS_ACTLEN_MASK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Check whether less than max data were received on SPD marked transfer.
|
---|
122 | *
|
---|
123 | * @param[in] instance TD structure to use.
|
---|
124 | * @return True if data packet is short (less than max bytes and SPD set),
|
---|
125 | * false otherwise.
|
---|
126 | */
|
---|
127 | static inline bool td_is_short(const td_t *instance)
|
---|
128 | {
|
---|
129 | const size_t act_size = td_act_size(instance);
|
---|
130 | const size_t max_size =
|
---|
131 | ((instance->device >> TD_DEVICE_MAXLEN_POS) + 1) &
|
---|
132 | TD_DEVICE_MAXLEN_MASK;
|
---|
133 | return
|
---|
134 | (instance->status | TD_STATUS_SPD_FLAG) && act_size < max_size;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Helper function for parsing value of toggle bit.
|
---|
138 | *
|
---|
139 | * @param[in] instance TD structure to use.
|
---|
140 | * @return Toggle bit value.
|
---|
141 | */
|
---|
142 | static inline int td_toggle(const td_t *instance)
|
---|
143 | {
|
---|
144 | assert(instance);
|
---|
145 | return (instance->device & TD_DEVICE_DATA_TOGGLE_ONE_FLAG) ? 1 : 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Helper function for parsing value of active bit
|
---|
149 | *
|
---|
150 | * @param[in] instance TD structure to use.
|
---|
151 | * @return Active bit value.
|
---|
152 | */
|
---|
153 | static inline bool td_is_active(const td_t *instance)
|
---|
154 | {
|
---|
155 | assert(instance);
|
---|
156 | return (instance->status & TD_STATUS_ERROR_ACTIVE) != 0;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Helper function for setting IOC bit.
|
---|
160 | *
|
---|
161 | * @param[in] instance TD structure to use.
|
---|
162 | */
|
---|
163 | static inline void td_set_ioc(td_t *instance)
|
---|
164 | {
|
---|
165 | assert(instance);
|
---|
166 | instance->status |= TD_STATUS_IOC_FLAG;
|
---|
167 | }
|
---|
168 |
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @}
|
---|
173 | */
|
---|