User | Revision | Line number | New contents of line |
abe00makoto |
0:e939856c1939
|
1
|
|
abe00makoto |
0:e939856c1939
|
2
|
/*
|
abe00makoto |
0:e939856c1939
|
3
|
Copyright (c) 2010 Peter Barrett
|
abe00makoto |
0:e939856c1939
|
4
|
|
abe00makoto |
0:e939856c1939
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
abe00makoto |
0:e939856c1939
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
abe00makoto |
0:e939856c1939
|
7
|
in the Software without restriction, including without limitation the rights
|
abe00makoto |
0:e939856c1939
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
abe00makoto |
0:e939856c1939
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
abe00makoto |
0:e939856c1939
|
10
|
furnished to do so, subject to the following conditions:
|
abe00makoto |
0:e939856c1939
|
11
|
|
abe00makoto |
0:e939856c1939
|
12
|
The above copyright notice and this permission notice shall be included in
|
abe00makoto |
0:e939856c1939
|
13
|
all copies or substantial portions of the Software.
|
abe00makoto |
0:e939856c1939
|
14
|
|
abe00makoto |
0:e939856c1939
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
abe00makoto |
0:e939856c1939
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
abe00makoto |
0:e939856c1939
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
abe00makoto |
0:e939856c1939
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
abe00makoto |
0:e939856c1939
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
abe00makoto |
0:e939856c1939
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
abe00makoto |
0:e939856c1939
|
21
|
THE SOFTWARE.
|
abe00makoto |
0:e939856c1939
|
22
|
*/
|
abe00makoto |
0:e939856c1939
|
23
|
|
abe00makoto |
0:e939856c1939
|
24
|
#include "mbed.h"
|
abe00makoto |
0:e939856c1939
|
25
|
#include "USBHost.h"
|
abe00makoto |
0:e939856c1939
|
26
|
|
abe00makoto |
0:e939856c1939
|
27
|
// Config (default uses x bytes)
|
abe00makoto |
0:e939856c1939
|
28
|
#define MAX_DEVICES 8 // Max number of devices
|
abe00makoto |
0:e939856c1939
|
29
|
#define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total
|
abe00makoto |
0:e939856c1939
|
30
|
#define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device
|
abe00makoto |
0:e939856c1939
|
31
|
|
abe00makoto |
0:e939856c1939
|
32
|
#define USBLOG 1
|
abe00makoto |
0:e939856c1939
|
33
|
#if USBLOG
|
abe00makoto |
0:e939856c1939
|
34
|
#define LOG(...) printf(__VA_ARGS__)
|
abe00makoto |
0:e939856c1939
|
35
|
#else
|
abe00makoto |
0:e939856c1939
|
36
|
#define LOG(...) do {} while(0)
|
abe00makoto |
0:e939856c1939
|
37
|
#endif
|
abe00makoto |
0:e939856c1939
|
38
|
|
abe00makoto |
0:e939856c1939
|
39
|
// USB host structures
|
abe00makoto |
0:e939856c1939
|
40
|
|
abe00makoto |
0:e939856c1939
|
41
|
#define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT
|
abe00makoto |
0:e939856c1939
|
42
|
#define USB_RAM_BASE 0x2007C000
|
abe00makoto |
0:e939856c1939
|
43
|
|
abe00makoto |
0:e939856c1939
|
44
|
#define TOKEN_SETUP 0
|
abe00makoto |
0:e939856c1939
|
45
|
#define TOKEN_IN 1
|
abe00makoto |
0:e939856c1939
|
46
|
#define TOKEN_OUT 2
|
abe00makoto |
0:e939856c1939
|
47
|
|
abe00makoto |
0:e939856c1939
|
48
|
// Status flags from hub
|
abe00makoto |
0:e939856c1939
|
49
|
#define PORT_CONNECTION 0
|
abe00makoto |
0:e939856c1939
|
50
|
#define PORT_ENABLE 1
|
abe00makoto |
0:e939856c1939
|
51
|
#define PORT_SUSPEND 2
|
abe00makoto |
0:e939856c1939
|
52
|
#define PORT_OVER_CURRENT 3
|
abe00makoto |
0:e939856c1939
|
53
|
#define PORT_RESET 4
|
abe00makoto |
0:e939856c1939
|
54
|
#define PORT_POWER 8
|
abe00makoto |
0:e939856c1939
|
55
|
#define PORT_LOW_SPEED 9
|
abe00makoto |
0:e939856c1939
|
56
|
|
abe00makoto |
0:e939856c1939
|
57
|
#define C_PORT_CONNECTION 16
|
abe00makoto |
0:e939856c1939
|
58
|
#define C_PORT_ENABLE 17
|
abe00makoto |
0:e939856c1939
|
59
|
#define C_PORT_SUSPEND 18
|
abe00makoto |
0:e939856c1939
|
60
|
#define C_PORT_OVER_CURRENT 19
|
abe00makoto |
0:e939856c1939
|
61
|
#define C_PORT_RESET 20
|
abe00makoto |
0:e939856c1939
|
62
|
|
abe00makoto |
0:e939856c1939
|
63
|
typedef struct {
|
abe00makoto |
0:e939856c1939
|
64
|
u8 bm_request_type;
|
abe00makoto |
0:e939856c1939
|
65
|
u8 b_request;
|
abe00makoto |
0:e939856c1939
|
66
|
u16 w_value;
|
abe00makoto |
0:e939856c1939
|
67
|
u16 w_index;
|
abe00makoto |
0:e939856c1939
|
68
|
u16 w_length;
|
abe00makoto |
0:e939856c1939
|
69
|
} Setup;
|
abe00makoto |
0:e939856c1939
|
70
|
|
abe00makoto |
0:e939856c1939
|
71
|
|
abe00makoto |
0:e939856c1939
|
72
|
// Hub stuff is kept private just to keep api simple
|
abe00makoto |
0:e939856c1939
|
73
|
int SetPortFeature(int device, int feature, int index);
|
abe00makoto |
0:e939856c1939
|
74
|
int ClearPortFeature(int device, int feature, int index);
|
abe00makoto |
0:e939856c1939
|
75
|
int SetPortPower(int device, int port);
|
abe00makoto |
0:e939856c1939
|
76
|
int SetPortReset(int device, int port);
|
abe00makoto |
0:e939856c1939
|
77
|
int GetPortStatus(int device, int port, u32* status);
|
abe00makoto |
0:e939856c1939
|
78
|
|
abe00makoto |
0:e939856c1939
|
79
|
//===================================================================
|
abe00makoto |
0:e939856c1939
|
80
|
//===================================================================
|
abe00makoto |
0:e939856c1939
|
81
|
// Hardware defines
|
abe00makoto |
0:e939856c1939
|
82
|
|
abe00makoto |
0:e939856c1939
|
83
|
// HcControl
|
abe00makoto |
0:e939856c1939
|
84
|
#define PeriodicListEnable 0x00000004
|
abe00makoto |
0:e939856c1939
|
85
|
#define IsochronousEnable 0x00000008
|
abe00makoto |
0:e939856c1939
|
86
|
#define ControlListEnable 0x00000010
|
abe00makoto |
0:e939856c1939
|
87
|
#define BulkListEnable 0x00000020
|
abe00makoto |
0:e939856c1939
|
88
|
#define OperationalMask 0x00000080
|
abe00makoto |
0:e939856c1939
|
89
|
#define HostControllerFunctionalState 0x000000C0
|
abe00makoto |
0:e939856c1939
|
90
|
|
abe00makoto |
0:e939856c1939
|
91
|
// HcCommandStatus
|
abe00makoto |
0:e939856c1939
|
92
|
#define HostControllerReset 0x00000001
|
abe00makoto |
0:e939856c1939
|
93
|
#define ControlListFilled 0x00000002
|
abe00makoto |
0:e939856c1939
|
94
|
#define BulkListFilled 0x00000004
|
abe00makoto |
0:e939856c1939
|
95
|
|
abe00makoto |
0:e939856c1939
|
96
|
// HcInterruptStatus Register
|
abe00makoto |
0:e939856c1939
|
97
|
#define WritebackDoneHead 0x00000002
|
abe00makoto |
0:e939856c1939
|
98
|
#define StartofFrame 0x00000004
|
abe00makoto |
0:e939856c1939
|
99
|
#define ResumeDetected 0x00000008
|
abe00makoto |
0:e939856c1939
|
100
|
#define UnrecoverableError 0x00000010
|
abe00makoto |
0:e939856c1939
|
101
|
#define FrameNumberOverflow 0x00000020
|
abe00makoto |
0:e939856c1939
|
102
|
#define RootHubStatusChange 0x00000040
|
abe00makoto |
0:e939856c1939
|
103
|
#define OwnershipChange 0x00000080
|
abe00makoto |
0:e939856c1939
|
104
|
#define MasterInterruptEnable 0x80000000
|
abe00makoto |
0:e939856c1939
|
105
|
|
abe00makoto |
0:e939856c1939
|
106
|
// HcRhStatus
|
abe00makoto |
0:e939856c1939
|
107
|
#define SetGlobalPower 0x00010000
|
abe00makoto |
0:e939856c1939
|
108
|
#define DeviceRemoteWakeupEnable 0x00008000
|
abe00makoto |
0:e939856c1939
|
109
|
|
abe00makoto |
0:e939856c1939
|
110
|
// HcRhPortStatus (hub 0, port 1)
|
abe00makoto |
0:e939856c1939
|
111
|
#define CurrentConnectStatus 0x00000001
|
abe00makoto |
0:e939856c1939
|
112
|
#define PortEnableStatus 0x00000002
|
abe00makoto |
0:e939856c1939
|
113
|
#define PortSuspendStatus 0x00000004
|
abe00makoto |
0:e939856c1939
|
114
|
#define PortOverCurrentIndicator 0x00000008
|
abe00makoto |
0:e939856c1939
|
115
|
#define PortResetStatus 0x00000010
|
abe00makoto |
0:e939856c1939
|
116
|
|
abe00makoto |
0:e939856c1939
|
117
|
#define PortPowerStatus 0x00000100
|
abe00makoto |
0:e939856c1939
|
118
|
#define LowspeedDevice 0x00000200
|
abe00makoto |
0:e939856c1939
|
119
|
#define HighspeedDevice 0x00000400
|
abe00makoto |
0:e939856c1939
|
120
|
|
abe00makoto |
0:e939856c1939
|
121
|
#define ConnectStatusChange (CurrentConnectStatus << 16)
|
abe00makoto |
0:e939856c1939
|
122
|
#define PortResetStatusChange (PortResetStatus << 16)
|
abe00makoto |
0:e939856c1939
|
123
|
|
abe00makoto |
0:e939856c1939
|
124
|
|
abe00makoto |
0:e939856c1939
|
125
|
#define TD_ROUNDING (u32)0x00040000
|
abe00makoto |
0:e939856c1939
|
126
|
#define TD_SETUP (u32)0x00000000
|
abe00makoto |
0:e939856c1939
|
127
|
#define TD_IN (u32)0x00100000
|
abe00makoto |
0:e939856c1939
|
128
|
#define TD_OUT (u32)0x00080000
|
abe00makoto |
0:e939856c1939
|
129
|
#define TD_DELAY_INT(x) (u32)((x) << 21)
|
abe00makoto |
0:e939856c1939
|
130
|
#define TD_TOGGLE_0 (u32)0x02000000
|
abe00makoto |
0:e939856c1939
|
131
|
#define TD_TOGGLE_1 (u32)0x03000000
|
abe00makoto |
0:e939856c1939
|
132
|
#define TD_CC (u32)0xF0000000
|
abe00makoto |
0:e939856c1939
|
133
|
|
abe00makoto |
0:e939856c1939
|
134
|
// HostController EndPoint Descriptor
|
abe00makoto |
0:e939856c1939
|
135
|
typedef struct {
|
abe00makoto |
0:e939856c1939
|
136
|
volatile u32 Control;
|
abe00makoto |
0:e939856c1939
|
137
|
volatile u32 TailTd;
|
abe00makoto |
0:e939856c1939
|
138
|
volatile u32 HeadTd;
|
abe00makoto |
0:e939856c1939
|
139
|
volatile u32 Next;
|
abe00makoto |
0:e939856c1939
|
140
|
} HCED;
|
abe00makoto |
0:e939856c1939
|
141
|
|
abe00makoto |
0:e939856c1939
|
142
|
// HostController Transfer Descriptor
|
abe00makoto |
0:e939856c1939
|
143
|
typedef struct {
|
abe00makoto |
0:e939856c1939
|
144
|
volatile u32 Control;
|
abe00makoto |
0:e939856c1939
|
145
|
volatile u32 CurrBufPtr;
|
abe00makoto |
0:e939856c1939
|
146
|
volatile u32 Next;
|
abe00makoto |
0:e939856c1939
|
147
|
volatile u32 BufEnd;
|
abe00makoto |
0:e939856c1939
|
148
|
} HCTD;
|
abe00makoto |
0:e939856c1939
|
149
|
|
abe00makoto |
0:e939856c1939
|
150
|
// Host Controller Communication Area
|
abe00makoto |
0:e939856c1939
|
151
|
typedef struct {
|
abe00makoto |
0:e939856c1939
|
152
|
volatile u32 InterruptTable[32];
|
abe00makoto |
0:e939856c1939
|
153
|
volatile u16 FrameNumber;
|
abe00makoto |
0:e939856c1939
|
154
|
volatile u16 FrameNumberPad;
|
abe00makoto |
0:e939856c1939
|
155
|
volatile u32 DoneHead;
|
abe00makoto |
0:e939856c1939
|
156
|
volatile u8 Reserved[120];
|
abe00makoto |
0:e939856c1939
|
157
|
} HCCA;
|
abe00makoto |
0:e939856c1939
|
158
|
|
abe00makoto |
0:e939856c1939
|
159
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
160
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
161
|
|
abe00makoto |
0:e939856c1939
|
162
|
class HostController;
|
abe00makoto |
0:e939856c1939
|
163
|
class Endpoint;
|
abe00makoto |
0:e939856c1939
|
164
|
class Device;
|
abe00makoto |
0:e939856c1939
|
165
|
|
abe00makoto |
0:e939856c1939
|
166
|
// must be 3*16 bytes long
|
abe00makoto |
0:e939856c1939
|
167
|
class Endpoint
|
abe00makoto |
0:e939856c1939
|
168
|
{
|
abe00makoto |
0:e939856c1939
|
169
|
public:
|
abe00makoto |
0:e939856c1939
|
170
|
HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint
|
abe00makoto |
0:e939856c1939
|
171
|
HCTD TDHead;
|
abe00makoto |
0:e939856c1939
|
172
|
|
abe00makoto |
0:e939856c1939
|
173
|
enum State
|
abe00makoto |
0:e939856c1939
|
174
|
{
|
abe00makoto |
0:e939856c1939
|
175
|
Free,
|
abe00makoto |
0:e939856c1939
|
176
|
NotQueued,
|
abe00makoto |
0:e939856c1939
|
177
|
Idle,
|
abe00makoto |
0:e939856c1939
|
178
|
SetupQueued,
|
abe00makoto |
0:e939856c1939
|
179
|
DataQueued,
|
abe00makoto |
0:e939856c1939
|
180
|
StatusQueued,
|
abe00makoto |
0:e939856c1939
|
181
|
CallbackPending
|
abe00makoto |
0:e939856c1939
|
182
|
};
|
abe00makoto |
0:e939856c1939
|
183
|
|
abe00makoto |
0:e939856c1939
|
184
|
volatile u8 CurrentState;
|
abe00makoto |
0:e939856c1939
|
185
|
u8 Flags; // 0x80 In, 0x03 mask endpoint type
|
abe00makoto |
0:e939856c1939
|
186
|
|
abe00makoto |
0:e939856c1939
|
187
|
u16 Length;
|
abe00makoto |
0:e939856c1939
|
188
|
u8* Data;
|
abe00makoto |
0:e939856c1939
|
189
|
USBCallback Callback; // Must be a multiple of 16 bytes long
|
abe00makoto |
0:e939856c1939
|
190
|
void* UserData;
|
abe00makoto |
0:e939856c1939
|
191
|
|
abe00makoto |
0:e939856c1939
|
192
|
int Address()
|
abe00makoto |
0:e939856c1939
|
193
|
{
|
abe00makoto |
0:e939856c1939
|
194
|
int ep = (EndpointDescriptor.Control >> 7) & 0xF;
|
abe00makoto |
0:e939856c1939
|
195
|
if (ep)
|
abe00makoto |
0:e939856c1939
|
196
|
ep |= Flags & 0x80;
|
abe00makoto |
0:e939856c1939
|
197
|
return ep;
|
abe00makoto |
0:e939856c1939
|
198
|
}
|
abe00makoto |
0:e939856c1939
|
199
|
|
abe00makoto |
0:e939856c1939
|
200
|
int Device()
|
abe00makoto |
0:e939856c1939
|
201
|
{
|
abe00makoto |
0:e939856c1939
|
202
|
return EndpointDescriptor.Control & 0x7F;
|
abe00makoto |
0:e939856c1939
|
203
|
}
|
abe00makoto |
0:e939856c1939
|
204
|
|
abe00makoto |
0:e939856c1939
|
205
|
int Status()
|
abe00makoto |
0:e939856c1939
|
206
|
{
|
abe00makoto |
0:e939856c1939
|
207
|
return (TDHead.Control >> 28) & 0xF;
|
abe00makoto |
0:e939856c1939
|
208
|
}
|
abe00makoto |
0:e939856c1939
|
209
|
|
abe00makoto |
0:e939856c1939
|
210
|
u32 Enqueue(u32 head)
|
abe00makoto |
0:e939856c1939
|
211
|
{
|
abe00makoto |
0:e939856c1939
|
212
|
if (CurrentState == NotQueued)
|
abe00makoto |
0:e939856c1939
|
213
|
{
|
abe00makoto |
0:e939856c1939
|
214
|
EndpointDescriptor.Next = head;
|
abe00makoto |
0:e939856c1939
|
215
|
head = (u32)&EndpointDescriptor;
|
abe00makoto |
0:e939856c1939
|
216
|
CurrentState = Idle;
|
abe00makoto |
0:e939856c1939
|
217
|
}
|
abe00makoto |
0:e939856c1939
|
218
|
return head;
|
abe00makoto |
0:e939856c1939
|
219
|
}
|
abe00makoto |
0:e939856c1939
|
220
|
};
|
abe00makoto |
0:e939856c1939
|
221
|
|
abe00makoto |
0:e939856c1939
|
222
|
class Device
|
abe00makoto |
0:e939856c1939
|
223
|
{
|
abe00makoto |
0:e939856c1939
|
224
|
public:
|
abe00makoto |
0:e939856c1939
|
225
|
u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2];
|
abe00makoto |
0:e939856c1939
|
226
|
u8 Hub;
|
abe00makoto |
0:e939856c1939
|
227
|
u8 Port;
|
abe00makoto |
0:e939856c1939
|
228
|
u8 Addr;
|
abe00makoto |
0:e939856c1939
|
229
|
u8 Pad;
|
abe00makoto |
0:e939856c1939
|
230
|
|
abe00makoto |
0:e939856c1939
|
231
|
// Only if this device is a hub
|
abe00makoto |
0:e939856c1939
|
232
|
u8 HubPortCount; // nonzero if this is a hub
|
abe00makoto |
0:e939856c1939
|
233
|
u8 HubInterruptData;
|
abe00makoto |
0:e939856c1939
|
234
|
u8 HubMap;
|
abe00makoto |
0:e939856c1939
|
235
|
u8 HubMask;
|
abe00makoto |
0:e939856c1939
|
236
|
|
abe00makoto |
0:e939856c1939
|
237
|
int Flags; // 1 = Disconnected
|
abe00makoto |
0:e939856c1939
|
238
|
|
abe00makoto |
0:e939856c1939
|
239
|
Setup SetupBuffer;
|
abe00makoto |
0:e939856c1939
|
240
|
|
abe00makoto |
0:e939856c1939
|
241
|
// Allocate endpoint zero
|
abe00makoto |
0:e939856c1939
|
242
|
int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed)
|
abe00makoto |
0:e939856c1939
|
243
|
{
|
abe00makoto |
0:e939856c1939
|
244
|
Hub = hub;
|
abe00makoto |
0:e939856c1939
|
245
|
Port = port;
|
abe00makoto |
0:e939856c1939
|
246
|
Addr = addr;
|
abe00makoto |
0:e939856c1939
|
247
|
Flags = lowSpeed;
|
abe00makoto |
0:e939856c1939
|
248
|
memset(_endpointMap,0xFF,sizeof(_endpointMap));
|
abe00makoto |
0:e939856c1939
|
249
|
return 0;
|
abe00makoto |
0:e939856c1939
|
250
|
}
|
abe00makoto |
0:e939856c1939
|
251
|
|
abe00makoto |
0:e939856c1939
|
252
|
int SetEndpointIndex(int ep, int endpointIndex)
|
abe00makoto |
0:e939856c1939
|
253
|
{
|
abe00makoto |
0:e939856c1939
|
254
|
for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2)
|
abe00makoto |
0:e939856c1939
|
255
|
{
|
abe00makoto |
0:e939856c1939
|
256
|
if (_endpointMap[i] == 0xFF) // Add endpoint to map
|
abe00makoto |
0:e939856c1939
|
257
|
{
|
abe00makoto |
0:e939856c1939
|
258
|
_endpointMap[i] = ep;
|
abe00makoto |
0:e939856c1939
|
259
|
_endpointMap[i+1] = endpointIndex;
|
abe00makoto |
0:e939856c1939
|
260
|
return 0;
|
abe00makoto |
0:e939856c1939
|
261
|
}
|
abe00makoto |
0:e939856c1939
|
262
|
}
|
abe00makoto |
0:e939856c1939
|
263
|
return ERR_ENDPOINT_NONE_LEFT;
|
abe00makoto |
0:e939856c1939
|
264
|
}
|
abe00makoto |
0:e939856c1939
|
265
|
|
abe00makoto |
0:e939856c1939
|
266
|
int GetEndpointIndex(int ep)
|
abe00makoto |
0:e939856c1939
|
267
|
{
|
abe00makoto |
0:e939856c1939
|
268
|
for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2)
|
abe00makoto |
0:e939856c1939
|
269
|
{
|
abe00makoto |
0:e939856c1939
|
270
|
if (_endpointMap[i] == ep)
|
abe00makoto |
0:e939856c1939
|
271
|
return _endpointMap[i+1];
|
abe00makoto |
0:e939856c1939
|
272
|
if (_endpointMap[i] == 0xFF)
|
abe00makoto |
0:e939856c1939
|
273
|
break;
|
abe00makoto |
0:e939856c1939
|
274
|
}
|
abe00makoto |
0:e939856c1939
|
275
|
return -1;
|
abe00makoto |
0:e939856c1939
|
276
|
}
|
abe00makoto |
0:e939856c1939
|
277
|
};
|
abe00makoto |
0:e939856c1939
|
278
|
|
abe00makoto |
0:e939856c1939
|
279
|
class HostController
|
abe00makoto |
0:e939856c1939
|
280
|
{
|
abe00makoto |
0:e939856c1939
|
281
|
public:
|
abe00makoto |
0:e939856c1939
|
282
|
HCCA CommunicationArea;
|
abe00makoto |
0:e939856c1939
|
283
|
Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16
|
abe00makoto |
0:e939856c1939
|
284
|
|
abe00makoto |
0:e939856c1939
|
285
|
Endpoint EndpointZero; // For device enumeration
|
abe00makoto |
0:e939856c1939
|
286
|
HCTD _commonTail;
|
abe00makoto |
0:e939856c1939
|
287
|
Setup _setupZero;
|
abe00makoto |
0:e939856c1939
|
288
|
|
abe00makoto |
0:e939856c1939
|
289
|
Device Devices[MAX_DEVICES];
|
abe00makoto |
0:e939856c1939
|
290
|
u32 _frameNumber; // 32 bit ms counter
|
abe00makoto |
0:e939856c1939
|
291
|
|
abe00makoto |
0:e939856c1939
|
292
|
u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue
|
abe00makoto |
0:e939856c1939
|
293
|
u8 _rootHubStatusChange; // Root hub status has changed, set from ISR
|
abe00makoto |
0:e939856c1939
|
294
|
u8 _unused0;
|
abe00makoto |
0:e939856c1939
|
295
|
u8 _unused1;
|
abe00makoto |
0:e939856c1939
|
296
|
|
abe00makoto |
0:e939856c1939
|
297
|
u8 _connectPending; // Reset has initiated a connect
|
abe00makoto |
0:e939856c1939
|
298
|
u8 _connectCountdown; // Number of ms left after reset before we can connect
|
abe00makoto |
0:e939856c1939
|
299
|
u8 _connectHub; // Will connect on this hub
|
abe00makoto |
0:e939856c1939
|
300
|
u8 _connectPort; // ... and this port
|
abe00makoto |
0:e939856c1939
|
301
|
|
abe00makoto |
0:e939856c1939
|
302
|
u8 SRAM[0]; // Start of free SRAM
|
abe00makoto |
0:e939856c1939
|
303
|
|
abe00makoto |
0:e939856c1939
|
304
|
void Loop()
|
abe00makoto |
0:e939856c1939
|
305
|
{
|
abe00makoto |
0:e939856c1939
|
306
|
// printf("Enter Loop \r\n");
|
abe00makoto |
0:e939856c1939
|
307
|
u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits
|
abe00makoto |
0:e939856c1939
|
308
|
_frameNumber += elapsed;
|
abe00makoto |
0:e939856c1939
|
309
|
|
abe00makoto |
0:e939856c1939
|
310
|
// Do callbacks, if any
|
abe00makoto |
0:e939856c1939
|
311
|
while (_callbacksPending)
|
abe00makoto |
0:e939856c1939
|
312
|
{
|
abe00makoto |
0:e939856c1939
|
313
|
for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++)
|
abe00makoto |
0:e939856c1939
|
314
|
{
|
abe00makoto |
0:e939856c1939
|
315
|
Endpoint* endpoint = Endpoints + i;
|
abe00makoto |
0:e939856c1939
|
316
|
if (endpoint->CurrentState == Endpoint::CallbackPending)
|
abe00makoto |
0:e939856c1939
|
317
|
{
|
abe00makoto |
0:e939856c1939
|
318
|
_callbacksPending--;
|
abe00makoto |
0:e939856c1939
|
319
|
endpoint->CurrentState = Endpoint::Idle;
|
abe00makoto |
0:e939856c1939
|
320
|
LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
321
|
endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData);
|
abe00makoto |
0:e939856c1939
|
322
|
}
|
abe00makoto |
0:e939856c1939
|
323
|
}
|
abe00makoto |
0:e939856c1939
|
324
|
}
|
abe00makoto |
0:e939856c1939
|
325
|
|
abe00makoto |
0:e939856c1939
|
326
|
// Deal with changes on the root hub
|
abe00makoto |
0:e939856c1939
|
327
|
if (_rootHubStatusChange)
|
abe00makoto |
0:e939856c1939
|
328
|
{
|
abe00makoto |
0:e939856c1939
|
329
|
u32 status = LPC_USB->HcRhPortStatus1;
|
abe00makoto |
0:e939856c1939
|
330
|
_rootHubStatusChange = 0;
|
abe00makoto |
0:e939856c1939
|
331
|
if (status >> 16)
|
abe00makoto |
0:e939856c1939
|
332
|
{
|
abe00makoto |
0:e939856c1939
|
333
|
HubStatusChange(0,1,status);
|
abe00makoto |
0:e939856c1939
|
334
|
LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes
|
abe00makoto |
0:e939856c1939
|
335
|
}
|
abe00makoto |
0:e939856c1939
|
336
|
}
|
abe00makoto |
0:e939856c1939
|
337
|
|
abe00makoto |
0:e939856c1939
|
338
|
// Connect after reset timeout
|
abe00makoto |
0:e939856c1939
|
339
|
if (_connectCountdown)
|
abe00makoto |
0:e939856c1939
|
340
|
{
|
abe00makoto |
0:e939856c1939
|
341
|
if (elapsed >= _connectCountdown)
|
abe00makoto |
0:e939856c1939
|
342
|
{
|
abe00makoto |
0:e939856c1939
|
343
|
_connectCountdown = 0;
|
abe00makoto |
0:e939856c1939
|
344
|
Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80);
|
abe00makoto |
0:e939856c1939
|
345
|
} else
|
abe00makoto |
0:e939856c1939
|
346
|
_connectCountdown -= elapsed;
|
abe00makoto |
0:e939856c1939
|
347
|
}
|
abe00makoto |
0:e939856c1939
|
348
|
|
abe00makoto |
0:e939856c1939
|
349
|
// printf("Outer Loop \r\n");
|
abe00makoto |
0:e939856c1939
|
350
|
}
|
abe00makoto |
0:e939856c1939
|
351
|
|
abe00makoto |
0:e939856c1939
|
352
|
// HubInterrupt - bitmap in dev->HubInterruptData
|
abe00makoto |
0:e939856c1939
|
353
|
void HubInterrupt(int device)
|
abe00makoto |
0:e939856c1939
|
354
|
{
|
abe00makoto |
0:e939856c1939
|
355
|
Device* dev = &Devices[device-1];
|
abe00makoto |
0:e939856c1939
|
356
|
for (int i = 0; i < dev->HubPortCount; i++)
|
abe00makoto |
0:e939856c1939
|
357
|
{
|
abe00makoto |
0:e939856c1939
|
358
|
int port = i+1;
|
abe00makoto |
0:e939856c1939
|
359
|
if (dev->HubInterruptData & (1 << port))
|
abe00makoto |
0:e939856c1939
|
360
|
{
|
abe00makoto |
0:e939856c1939
|
361
|
u32 status = 0;
|
abe00makoto |
0:e939856c1939
|
362
|
GetPortStatus(device,port,&status);
|
abe00makoto |
0:e939856c1939
|
363
|
if (status >> 16)
|
abe00makoto |
0:e939856c1939
|
364
|
{
|
abe00makoto |
0:e939856c1939
|
365
|
if (_connectPending && (status & ConnectStatusChange))
|
abe00makoto |
0:e939856c1939
|
366
|
continue; // Don't connect again until previous device has been added and addressed
|
abe00makoto |
0:e939856c1939
|
367
|
|
abe00makoto |
0:e939856c1939
|
368
|
HubStatusChange(device,port,status);
|
abe00makoto |
0:e939856c1939
|
369
|
if (status & ConnectStatusChange)
|
abe00makoto |
0:e939856c1939
|
370
|
ClearPortFeature(device,C_PORT_CONNECTION,port);
|
abe00makoto |
0:e939856c1939
|
371
|
if (status & PortResetStatusChange)
|
abe00makoto |
0:e939856c1939
|
372
|
ClearPortFeature(device,C_PORT_RESET,port);
|
abe00makoto |
0:e939856c1939
|
373
|
}
|
abe00makoto |
0:e939856c1939
|
374
|
}
|
abe00makoto |
0:e939856c1939
|
375
|
}
|
abe00makoto |
0:e939856c1939
|
376
|
}
|
abe00makoto |
0:e939856c1939
|
377
|
|
abe00makoto |
0:e939856c1939
|
378
|
static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
|
abe00makoto |
0:e939856c1939
|
379
|
{
|
abe00makoto |
0:e939856c1939
|
380
|
HostController* controller = (HostController*)userData;
|
abe00makoto |
0:e939856c1939
|
381
|
if (status == 0)
|
abe00makoto |
0:e939856c1939
|
382
|
controller->HubInterrupt(device);
|
abe00makoto |
0:e939856c1939
|
383
|
USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData);
|
abe00makoto |
0:e939856c1939
|
384
|
}
|
abe00makoto |
0:e939856c1939
|
385
|
|
abe00makoto |
0:e939856c1939
|
386
|
int InitHub(int device)
|
abe00makoto |
0:e939856c1939
|
387
|
{
|
abe00makoto |
0:e939856c1939
|
388
|
u8 buf[16];
|
abe00makoto |
0:e939856c1939
|
389
|
int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf));
|
abe00makoto |
0:e939856c1939
|
390
|
if (r < 0)
|
abe00makoto |
0:e939856c1939
|
391
|
return ERR_HUB_INIT_FAILED;
|
abe00makoto |
0:e939856c1939
|
392
|
|
abe00makoto |
0:e939856c1939
|
393
|
// turn on power on the hubs ports
|
abe00makoto |
0:e939856c1939
|
394
|
Device* dev = &Devices[device-1];
|
abe00makoto |
0:e939856c1939
|
395
|
int ports = buf[2];
|
abe00makoto |
0:e939856c1939
|
396
|
dev->HubPortCount = ports;
|
abe00makoto |
0:e939856c1939
|
397
|
for (int i = 0; i < ports; i++)
|
abe00makoto |
0:e939856c1939
|
398
|
SetPortPower(device,i+1);
|
abe00makoto |
0:e939856c1939
|
399
|
|
abe00makoto |
0:e939856c1939
|
400
|
// Enable hub change interrupts
|
abe00makoto |
0:e939856c1939
|
401
|
return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this);
|
abe00makoto |
0:e939856c1939
|
402
|
}
|
abe00makoto |
0:e939856c1939
|
403
|
|
abe00makoto |
0:e939856c1939
|
404
|
int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval)
|
abe00makoto |
0:e939856c1939
|
405
|
{
|
abe00makoto |
0:e939856c1939
|
406
|
LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\r\n",device,ep,attributes,maxPacketSize,interval);
|
abe00makoto |
0:e939856c1939
|
407
|
Device* dev = &Devices[device-1];
|
abe00makoto |
0:e939856c1939
|
408
|
Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize);
|
abe00makoto |
0:e939856c1939
|
409
|
if (!endpoint)
|
abe00makoto |
0:e939856c1939
|
410
|
return ERR_ENDPOINT_NONE_LEFT;
|
abe00makoto |
0:e939856c1939
|
411
|
dev->SetEndpointIndex(ep,endpoint - Endpoints);
|
abe00makoto |
0:e939856c1939
|
412
|
endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed
|
abe00makoto |
0:e939856c1939
|
413
|
return 0; // TODO ed->bInterval
|
abe00makoto |
0:e939856c1939
|
414
|
}
|
abe00makoto |
0:e939856c1939
|
415
|
|
abe00makoto |
0:e939856c1939
|
416
|
int AddEndpoint(int device, EndpointDescriptor* ed)
|
abe00makoto |
0:e939856c1939
|
417
|
{
|
abe00makoto |
0:e939856c1939
|
418
|
return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval);
|
abe00makoto |
0:e939856c1939
|
419
|
}
|
abe00makoto |
0:e939856c1939
|
420
|
|
abe00makoto |
0:e939856c1939
|
421
|
// allocate a endpoint
|
abe00makoto |
0:e939856c1939
|
422
|
Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize)
|
abe00makoto |
0:e939856c1939
|
423
|
{
|
abe00makoto |
0:e939856c1939
|
424
|
for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++)
|
abe00makoto |
0:e939856c1939
|
425
|
{
|
abe00makoto |
0:e939856c1939
|
426
|
Endpoint* ep = &Endpoints[i];
|
abe00makoto |
0:e939856c1939
|
427
|
if (ep->CurrentState == 0)
|
abe00makoto |
0:e939856c1939
|
428
|
{
|
abe00makoto |
0:e939856c1939
|
429
|
//LOG("Allocated endpoint %d to %02X:%02X\r\n",i,device,endpointAddress);
|
abe00makoto |
0:e939856c1939
|
430
|
ep->Flags = (endpointAddress & 0x80) | (type & 3);
|
abe00makoto |
0:e939856c1939
|
431
|
ep->CurrentState = Endpoint::NotQueued;
|
abe00makoto |
0:e939856c1939
|
432
|
ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device;
|
abe00makoto |
0:e939856c1939
|
433
|
return ep;
|
abe00makoto |
0:e939856c1939
|
434
|
}
|
abe00makoto |
0:e939856c1939
|
435
|
}
|
abe00makoto |
0:e939856c1939
|
436
|
return 0;
|
abe00makoto |
0:e939856c1939
|
437
|
}
|
abe00makoto |
0:e939856c1939
|
438
|
|
abe00makoto |
0:e939856c1939
|
439
|
Endpoint* GetEndpoint(int device, int ep)
|
abe00makoto |
0:e939856c1939
|
440
|
{
|
abe00makoto |
0:e939856c1939
|
441
|
if (device == 0)
|
abe00makoto |
0:e939856c1939
|
442
|
{
|
abe00makoto |
0:e939856c1939
|
443
|
//printf("WARNING: USING DEVICE 0\n");
|
abe00makoto |
0:e939856c1939
|
444
|
return &EndpointZero;
|
abe00makoto |
0:e939856c1939
|
445
|
}
|
abe00makoto |
0:e939856c1939
|
446
|
if (device > MAX_DEVICES)
|
abe00makoto |
0:e939856c1939
|
447
|
return 0;
|
abe00makoto |
0:e939856c1939
|
448
|
int i = Devices[device-1].GetEndpointIndex(ep);
|
abe00makoto |
0:e939856c1939
|
449
|
if (i == -1)
|
abe00makoto |
0:e939856c1939
|
450
|
return 0;
|
abe00makoto |
0:e939856c1939
|
451
|
return Endpoints + i;
|
abe00makoto |
0:e939856c1939
|
452
|
}
|
abe00makoto |
0:e939856c1939
|
453
|
|
abe00makoto |
0:e939856c1939
|
454
|
int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state)
|
abe00makoto |
0:e939856c1939
|
455
|
{
|
abe00makoto |
0:e939856c1939
|
456
|
//Makoto edit comment out
|
abe00makoto |
0:e939856c1939
|
457
|
// LOG("Transfer start\r\n");
|
abe00makoto |
0:e939856c1939
|
458
|
LOG("Transfer ep=%02X Token:%d Len:%d State:%d\r\n",endpoint->Address(),token,len,state);
|
abe00makoto |
0:e939856c1939
|
459
|
|
abe00makoto |
0:e939856c1939
|
460
|
int toggle = 0;
|
abe00makoto |
0:e939856c1939
|
461
|
if (endpoint->Address() == 0)
|
abe00makoto |
0:e939856c1939
|
462
|
toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1;
|
abe00makoto |
0:e939856c1939
|
463
|
|
abe00makoto |
0:e939856c1939
|
464
|
if (token != TOKEN_SETUP)
|
abe00makoto |
0:e939856c1939
|
465
|
token = (token == TOKEN_IN ? TD_IN : TD_OUT);
|
abe00makoto |
0:e939856c1939
|
466
|
|
abe00makoto |
0:e939856c1939
|
467
|
HCTD* head = &endpoint->TDHead;
|
abe00makoto |
0:e939856c1939
|
468
|
HCTD* tail = &_commonTail;
|
abe00makoto |
0:e939856c1939
|
469
|
|
abe00makoto |
0:e939856c1939
|
470
|
head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC;
|
abe00makoto |
0:e939856c1939
|
471
|
head->CurrBufPtr = (u32)data;
|
abe00makoto |
0:e939856c1939
|
472
|
head->BufEnd = (u32)(data + len - 1);
|
abe00makoto |
0:e939856c1939
|
473
|
head->Next = (u32)tail;
|
abe00makoto |
0:e939856c1939
|
474
|
|
abe00makoto |
0:e939856c1939
|
475
|
HCED* ed = &endpoint->EndpointDescriptor;
|
abe00makoto |
0:e939856c1939
|
476
|
ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle
|
abe00makoto |
0:e939856c1939
|
477
|
ed->TailTd = (u32)tail;
|
abe00makoto |
0:e939856c1939
|
478
|
|
abe00makoto |
0:e939856c1939
|
479
|
//HCTD* td = head;
|
abe00makoto |
0:e939856c1939
|
480
|
//LOG("%04X TD %08X %08X %08X Next:%08X\r\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next);
|
abe00makoto |
0:e939856c1939
|
481
|
//LOG("%04X ED %08X %08X %08X\r\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd);
|
abe00makoto |
0:e939856c1939
|
482
|
|
abe00makoto |
0:e939856c1939
|
483
|
//LOG("Transfer run01\r\n");
|
abe00makoto |
0:e939856c1939
|
484
|
switch (endpoint->Flags & 3)
|
abe00makoto |
0:e939856c1939
|
485
|
{
|
abe00makoto |
0:e939856c1939
|
486
|
case ENDPOINT_CONTROL:
|
abe00makoto |
0:e939856c1939
|
487
|
LOG("Transfer run ENDPOINT_CONTROL\r\n");
|
abe00makoto |
0:e939856c1939
|
488
|
LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle
|
abe00makoto |
0:e939856c1939
|
489
|
//LOG("Transfer run02\r\n");
|
abe00makoto |
0:e939856c1939
|
490
|
LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state);
|
abe00makoto |
0:e939856c1939
|
491
|
endpoint->CurrentState = state; // Get in before an int
|
abe00makoto |
0:e939856c1939
|
492
|
LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled;
|
abe00makoto |
0:e939856c1939
|
493
|
LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable;
|
abe00makoto |
0:e939856c1939
|
494
|
|
abe00makoto |
0:e939856c1939
|
495
|
break;
|
abe00makoto |
0:e939856c1939
|
496
|
|
abe00makoto |
0:e939856c1939
|
497
|
case ENDPOINT_BULK:
|
abe00makoto |
0:e939856c1939
|
498
|
LOG("Transfer run ENDPOINT_BULK\r\n");
|
abe00makoto |
0:e939856c1939
|
499
|
LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED);
|
abe00makoto |
0:e939856c1939
|
500
|
LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state);
|
abe00makoto |
0:e939856c1939
|
501
|
endpoint->CurrentState = state;
|
abe00makoto |
0:e939856c1939
|
502
|
LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled;
|
abe00makoto |
0:e939856c1939
|
503
|
LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable;
|
abe00makoto |
0:e939856c1939
|
504
|
break;
|
abe00makoto |
0:e939856c1939
|
505
|
|
abe00makoto |
0:e939856c1939
|
506
|
case ENDPOINT_INTERRUPT:
|
abe00makoto |
0:e939856c1939
|
507
|
LOG("Transfer run ENDPOINT_INTERRUPT\r\n");
|
abe00makoto |
0:e939856c1939
|
508
|
CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]);
|
abe00makoto |
0:e939856c1939
|
509
|
LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state);
|
abe00makoto |
0:e939856c1939
|
510
|
endpoint->CurrentState = state;
|
abe00makoto |
0:e939856c1939
|
511
|
LPC_USB->HcControl |= PeriodicListEnable;
|
abe00makoto |
0:e939856c1939
|
512
|
break;
|
abe00makoto |
0:e939856c1939
|
513
|
}
|
abe00makoto |
0:e939856c1939
|
514
|
|
abe00makoto |
0:e939856c1939
|
515
|
LOG("Transfer end\r\n");
|
abe00makoto |
0:e939856c1939
|
516
|
return 0;
|
abe00makoto |
0:e939856c1939
|
517
|
}
|
abe00makoto |
0:e939856c1939
|
518
|
|
abe00makoto |
0:e939856c1939
|
519
|
// Remove an endpoint from an active queue
|
abe00makoto |
0:e939856c1939
|
520
|
bool Remove(HCED* ed, volatile HCED** queue)
|
abe00makoto |
0:e939856c1939
|
521
|
{
|
abe00makoto |
0:e939856c1939
|
522
|
if (*queue == 0)
|
abe00makoto |
0:e939856c1939
|
523
|
return false;
|
abe00makoto |
0:e939856c1939
|
524
|
if (*queue == (volatile HCED*)ed)
|
abe00makoto |
0:e939856c1939
|
525
|
{
|
abe00makoto |
0:e939856c1939
|
526
|
*queue = (volatile HCED*)ed->Next; // At head of queue
|
abe00makoto |
0:e939856c1939
|
527
|
return true;
|
abe00makoto |
0:e939856c1939
|
528
|
}
|
abe00makoto |
0:e939856c1939
|
529
|
|
abe00makoto |
0:e939856c1939
|
530
|
volatile HCED* head = *queue;
|
abe00makoto |
0:e939856c1939
|
531
|
while (head)
|
abe00makoto |
0:e939856c1939
|
532
|
{
|
abe00makoto |
0:e939856c1939
|
533
|
if (head->Next == (u32)ed)
|
abe00makoto |
0:e939856c1939
|
534
|
{
|
abe00makoto |
0:e939856c1939
|
535
|
head->Next = ed->Next;
|
abe00makoto |
0:e939856c1939
|
536
|
return true;
|
abe00makoto |
0:e939856c1939
|
537
|
}
|
abe00makoto |
0:e939856c1939
|
538
|
head = (volatile HCED*)head->Next;
|
abe00makoto |
0:e939856c1939
|
539
|
}
|
abe00makoto |
0:e939856c1939
|
540
|
return false;
|
abe00makoto |
0:e939856c1939
|
541
|
}
|
abe00makoto |
0:e939856c1939
|
542
|
|
abe00makoto |
0:e939856c1939
|
543
|
void Release(Endpoint* endpoint)
|
abe00makoto |
0:e939856c1939
|
544
|
{
|
abe00makoto |
0:e939856c1939
|
545
|
if (endpoint->CurrentState == Endpoint::NotQueued)
|
abe00makoto |
0:e939856c1939
|
546
|
{
|
abe00makoto |
0:e939856c1939
|
547
|
// Never event used it, nothing to do
|
abe00makoto |
0:e939856c1939
|
548
|
}
|
abe00makoto |
0:e939856c1939
|
549
|
else
|
abe00makoto |
0:e939856c1939
|
550
|
{
|
abe00makoto |
0:e939856c1939
|
551
|
HCED* ed = (HCED*)endpoint;
|
abe00makoto |
0:e939856c1939
|
552
|
ed->Control |= 0x4000; // SKIP
|
abe00makoto |
0:e939856c1939
|
553
|
switch (endpoint->Flags & 0x03)
|
abe00makoto |
0:e939856c1939
|
554
|
{
|
abe00makoto |
0:e939856c1939
|
555
|
case ENDPOINT_CONTROL:
|
abe00makoto |
0:e939856c1939
|
556
|
Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED);
|
abe00makoto |
0:e939856c1939
|
557
|
break;
|
abe00makoto |
0:e939856c1939
|
558
|
case ENDPOINT_BULK:
|
abe00makoto |
0:e939856c1939
|
559
|
Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED);
|
abe00makoto |
0:e939856c1939
|
560
|
break;
|
abe00makoto |
0:e939856c1939
|
561
|
case ENDPOINT_INTERRUPT:
|
abe00makoto |
0:e939856c1939
|
562
|
for (int i = 0; i < 32; i++)
|
abe00makoto |
0:e939856c1939
|
563
|
Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]);
|
abe00makoto |
0:e939856c1939
|
564
|
break;
|
abe00makoto |
0:e939856c1939
|
565
|
}
|
abe00makoto |
0:e939856c1939
|
566
|
|
abe00makoto |
0:e939856c1939
|
567
|
u16 fn = CommunicationArea.FrameNumber;
|
abe00makoto |
0:e939856c1939
|
568
|
while (fn == CommunicationArea.FrameNumber)
|
abe00makoto |
0:e939856c1939
|
569
|
; // Wait for next frame
|
abe00makoto |
0:e939856c1939
|
570
|
|
abe00makoto |
0:e939856c1939
|
571
|
}
|
abe00makoto |
0:e939856c1939
|
572
|
|
abe00makoto |
0:e939856c1939
|
573
|
// In theory, the endpoint is now dead.
|
abe00makoto |
0:e939856c1939
|
574
|
// TODO: Will Callbacks ever be pending? BUGBUG
|
abe00makoto |
0:e939856c1939
|
575
|
memset(endpoint,0,sizeof(Endpoint));
|
abe00makoto |
0:e939856c1939
|
576
|
}
|
abe00makoto |
0:e939856c1939
|
577
|
|
abe00makoto |
0:e939856c1939
|
578
|
// Pop the last TD from the list
|
abe00makoto |
0:e939856c1939
|
579
|
HCTD* Reverse(HCTD* current)
|
abe00makoto |
0:e939856c1939
|
580
|
{
|
abe00makoto |
0:e939856c1939
|
581
|
HCTD *result = NULL,*temp;
|
abe00makoto |
0:e939856c1939
|
582
|
while (current)
|
abe00makoto |
0:e939856c1939
|
583
|
{
|
abe00makoto |
0:e939856c1939
|
584
|
temp = (HCTD*)current->Next;
|
abe00makoto |
0:e939856c1939
|
585
|
current->Next = (u32)result;
|
abe00makoto |
0:e939856c1939
|
586
|
result = current;
|
abe00makoto |
0:e939856c1939
|
587
|
current = temp;
|
abe00makoto |
0:e939856c1939
|
588
|
}
|
abe00makoto |
0:e939856c1939
|
589
|
return result;
|
abe00makoto |
0:e939856c1939
|
590
|
}
|
abe00makoto |
0:e939856c1939
|
591
|
|
abe00makoto |
0:e939856c1939
|
592
|
// Called from interrupt...
|
abe00makoto |
0:e939856c1939
|
593
|
// Control endpoints use a state machine to progress through the transfers
|
abe00makoto |
0:e939856c1939
|
594
|
void ProcessDoneQueue(u32 tdList)
|
abe00makoto |
0:e939856c1939
|
595
|
{
|
abe00makoto |
0:e939856c1939
|
596
|
|
abe00makoto |
0:e939856c1939
|
597
|
LOG("<<ProcessDoneQueue enter>>\r\n");
|
abe00makoto |
0:e939856c1939
|
598
|
HCTD* list = Reverse((HCTD*)tdList);
|
abe00makoto |
0:e939856c1939
|
599
|
while (list)
|
abe00makoto |
0:e939856c1939
|
600
|
{
|
abe00makoto |
0:e939856c1939
|
601
|
Endpoint* endpoint = (Endpoint*)(list-1);
|
abe00makoto |
0:e939856c1939
|
602
|
list = (HCTD*)list->Next;
|
abe00makoto |
0:e939856c1939
|
603
|
int ep = endpoint->Address();
|
abe00makoto |
0:e939856c1939
|
604
|
bool in = endpoint->Flags & 0x80;
|
abe00makoto |
0:e939856c1939
|
605
|
int status = (endpoint->TDHead.Control >> 28) & 0xF;
|
abe00makoto |
0:e939856c1939
|
606
|
|
abe00makoto |
0:e939856c1939
|
607
|
//LOG("ProcessDoneQueue %02X %08X\r\n",ep,endpoint->TDHead.Control);
|
abe00makoto |
0:e939856c1939
|
608
|
|
abe00makoto |
0:e939856c1939
|
609
|
if (status != 0)
|
abe00makoto |
0:e939856c1939
|
610
|
{
|
abe00makoto |
0:e939856c1939
|
611
|
LOG("ProcessDoneQueue status %02X %d\r\n",ep,status);
|
abe00makoto |
0:e939856c1939
|
612
|
LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
613
|
endpoint->CurrentState = Endpoint::Idle;
|
abe00makoto |
0:e939856c1939
|
614
|
|
abe00makoto |
0:e939856c1939
|
615
|
|
abe00makoto |
0:e939856c1939
|
616
|
} else {
|
abe00makoto |
0:e939856c1939
|
617
|
switch (endpoint->CurrentState)
|
abe00makoto |
0:e939856c1939
|
618
|
{
|
abe00makoto |
0:e939856c1939
|
619
|
case Endpoint::SetupQueued:
|
abe00makoto |
0:e939856c1939
|
620
|
if (endpoint->Length == 0)
|
abe00makoto |
0:e939856c1939
|
621
|
{
|
abe00makoto |
0:e939856c1939
|
622
|
LOG("ProcessDoneQueue endpoint->Length == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
623
|
Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase
|
abe00makoto |
0:e939856c1939
|
624
|
}
|
abe00makoto |
0:e939856c1939
|
625
|
else
|
abe00makoto |
0:e939856c1939
|
626
|
{
|
abe00makoto |
0:e939856c1939
|
627
|
LOG("ProcessDoneQueue endpoint->Length != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
628
|
Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data
|
abe00makoto |
0:e939856c1939
|
629
|
}
|
abe00makoto |
0:e939856c1939
|
630
|
break;
|
abe00makoto |
0:e939856c1939
|
631
|
|
abe00makoto |
0:e939856c1939
|
632
|
case Endpoint::DataQueued:
|
abe00makoto |
0:e939856c1939
|
633
|
if (endpoint->TDHead.CurrBufPtr)
|
abe00makoto |
0:e939856c1939
|
634
|
endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data;
|
abe00makoto |
0:e939856c1939
|
635
|
|
abe00makoto |
0:e939856c1939
|
636
|
if (ep == 0)
|
abe00makoto |
0:e939856c1939
|
637
|
{
|
abe00makoto |
0:e939856c1939
|
638
|
LOG("ProcessDoneQueue ep == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
639
|
Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only
|
abe00makoto |
0:e939856c1939
|
640
|
}
|
abe00makoto |
0:e939856c1939
|
641
|
else
|
abe00makoto |
0:e939856c1939
|
642
|
{
|
abe00makoto |
0:e939856c1939
|
643
|
|
abe00makoto |
0:e939856c1939
|
644
|
LOG("ProcessDoneQueue ep != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
645
|
endpoint->CurrentState = Endpoint::Idle;
|
abe00makoto |
0:e939856c1939
|
646
|
LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
647
|
}
|
abe00makoto |
0:e939856c1939
|
648
|
break;
|
abe00makoto |
0:e939856c1939
|
649
|
|
abe00makoto |
0:e939856c1939
|
650
|
case Endpoint::StatusQueued: // Transaction is done
|
abe00makoto |
0:e939856c1939
|
651
|
LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
652
|
endpoint->CurrentState = Endpoint::Idle;
|
abe00makoto |
0:e939856c1939
|
653
|
break;
|
abe00makoto |
0:e939856c1939
|
654
|
}
|
abe00makoto |
0:e939856c1939
|
655
|
}
|
abe00makoto |
0:e939856c1939
|
656
|
|
abe00makoto |
0:e939856c1939
|
657
|
// Complete, flag if we need a callback
|
abe00makoto |
0:e939856c1939
|
658
|
if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle)
|
abe00makoto |
0:e939856c1939
|
659
|
{
|
abe00makoto |
0:e939856c1939
|
660
|
endpoint->CurrentState = Endpoint::CallbackPending;
|
abe00makoto |
0:e939856c1939
|
661
|
_callbacksPending++;
|
abe00makoto |
0:e939856c1939
|
662
|
}
|
abe00makoto |
0:e939856c1939
|
663
|
}
|
abe00makoto |
0:e939856c1939
|
664
|
LOG("<<ProcessDoneQueue out>>\r\n");
|
abe00makoto |
0:e939856c1939
|
665
|
}
|
abe00makoto |
0:e939856c1939
|
666
|
|
abe00makoto |
0:e939856c1939
|
667
|
// Hack to reset devices that don't want to connect
|
abe00makoto |
0:e939856c1939
|
668
|
int AddDevice(int hub, int port, bool isLowSpeed)
|
abe00makoto |
0:e939856c1939
|
669
|
{
|
abe00makoto |
0:e939856c1939
|
670
|
int device = AddDeviceCore(hub,port,isLowSpeed);
|
abe00makoto |
0:e939856c1939
|
671
|
if (device < 0)
|
abe00makoto |
0:e939856c1939
|
672
|
{
|
abe00makoto |
0:e939856c1939
|
673
|
LOG("========RETRY ADD DEVICE========\r\n"); // This will go for ever.. TODO power cycle root?
|
abe00makoto |
0:e939856c1939
|
674
|
Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again
|
abe00makoto |
0:e939856c1939
|
675
|
ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug
|
abe00makoto |
0:e939856c1939
|
676
|
return -1;
|
abe00makoto |
0:e939856c1939
|
677
|
}
|
abe00makoto |
0:e939856c1939
|
678
|
return device;
|
abe00makoto |
0:e939856c1939
|
679
|
}
|
abe00makoto |
0:e939856c1939
|
680
|
|
abe00makoto |
0:e939856c1939
|
681
|
int AddDeviceCore(int hub, int port, bool isLowSpeed)
|
abe00makoto |
0:e939856c1939
|
682
|
{
|
abe00makoto |
0:e939856c1939
|
683
|
int lowSpeed = isLowSpeed ? 0x2000 : 0;
|
abe00makoto |
0:e939856c1939
|
684
|
DeviceDescriptor desc;
|
abe00makoto |
0:e939856c1939
|
685
|
EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8
|
abe00makoto |
0:e939856c1939
|
686
|
int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8);
|
abe00makoto |
0:e939856c1939
|
687
|
if (r < 0)
|
abe00makoto |
0:e939856c1939
|
688
|
{
|
abe00makoto |
0:e939856c1939
|
689
|
LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\r\n");
|
abe00makoto |
0:e939856c1939
|
690
|
return r;
|
abe00makoto |
0:e939856c1939
|
691
|
}
|
abe00makoto |
0:e939856c1939
|
692
|
|
abe00makoto |
0:e939856c1939
|
693
|
EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize
|
abe00makoto |
0:e939856c1939
|
694
|
r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc));
|
abe00makoto |
0:e939856c1939
|
695
|
if (r < 0)
|
abe00makoto |
0:e939856c1939
|
696
|
return r;
|
abe00makoto |
0:e939856c1939
|
697
|
|
abe00makoto |
0:e939856c1939
|
698
|
LOG("\nClass %02X found %04X:%04X\r\n",desc.bDeviceClass,desc.idVendor,desc.idProduct);
|
abe00makoto |
0:e939856c1939
|
699
|
|
abe00makoto |
0:e939856c1939
|
700
|
// Now assign the device an address, move off EndpointZero
|
abe00makoto |
0:e939856c1939
|
701
|
int device = 0;
|
abe00makoto |
0:e939856c1939
|
702
|
for (int i = 0; i < MAX_DEVICES; i++)
|
abe00makoto |
0:e939856c1939
|
703
|
{
|
abe00makoto |
0:e939856c1939
|
704
|
if (Devices[i].Port == 0)
|
abe00makoto |
0:e939856c1939
|
705
|
{
|
abe00makoto |
0:e939856c1939
|
706
|
device = i+1;
|
abe00makoto |
0:e939856c1939
|
707
|
break;
|
abe00makoto |
0:e939856c1939
|
708
|
}
|
abe00makoto |
0:e939856c1939
|
709
|
}
|
abe00makoto |
0:e939856c1939
|
710
|
if (!device)
|
abe00makoto |
0:e939856c1939
|
711
|
return ERR_DEVICE_NONE_LEFT;
|
abe00makoto |
0:e939856c1939
|
712
|
|
abe00makoto |
0:e939856c1939
|
713
|
r = SetAddress(0,device);
|
abe00makoto |
0:e939856c1939
|
714
|
if (r)
|
abe00makoto |
0:e939856c1939
|
715
|
return r;
|
abe00makoto |
0:e939856c1939
|
716
|
DelayMS(2);
|
abe00makoto |
0:e939856c1939
|
717
|
|
abe00makoto |
0:e939856c1939
|
718
|
// Now at a nonzero address, create control endpoint
|
abe00makoto |
0:e939856c1939
|
719
|
Device* dev = &Devices[device-1];
|
abe00makoto |
0:e939856c1939
|
720
|
dev->Init(&desc,hub,port,device,lowSpeed);
|
abe00makoto |
0:e939856c1939
|
721
|
AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0);
|
abe00makoto |
0:e939856c1939
|
722
|
_connectPending = 0;
|
abe00makoto |
0:e939856c1939
|
723
|
|
abe00makoto |
0:e939856c1939
|
724
|
// Verify this all works
|
abe00makoto |
0:e939856c1939
|
725
|
r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc));
|
abe00makoto |
0:e939856c1939
|
726
|
if (r < 0)
|
abe00makoto |
0:e939856c1939
|
727
|
return r;
|
abe00makoto |
0:e939856c1939
|
728
|
|
abe00makoto |
0:e939856c1939
|
729
|
// Set to interface 0 by default
|
abe00makoto |
0:e939856c1939
|
730
|
// Calls LoadDevice if interface is found
|
abe00makoto |
0:e939856c1939
|
731
|
r = SetConfigurationAndInterface(device,1,0,&desc);
|
abe00makoto |
0:e939856c1939
|
732
|
|
abe00makoto |
0:e939856c1939
|
733
|
if (desc.bDeviceClass == CLASS_HUB)
|
abe00makoto |
0:e939856c1939
|
734
|
InitHub(device); // Handle hubs in this code
|
abe00makoto |
0:e939856c1939
|
735
|
|
abe00makoto |
0:e939856c1939
|
736
|
return device;
|
abe00makoto |
0:e939856c1939
|
737
|
}
|
abe00makoto |
0:e939856c1939
|
738
|
|
abe00makoto |
0:e939856c1939
|
739
|
// Walk descriptors and create endpoints for a given device
|
abe00makoto |
0:e939856c1939
|
740
|
// TODO configuration !=1, alternate settings etc.
|
abe00makoto |
0:e939856c1939
|
741
|
int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc)
|
abe00makoto |
0:e939856c1939
|
742
|
{
|
abe00makoto |
0:e939856c1939
|
743
|
u8 buffer[255];
|
abe00makoto |
0:e939856c1939
|
744
|
int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer));
|
abe00makoto |
0:e939856c1939
|
745
|
if (err < 0)
|
abe00makoto |
0:e939856c1939
|
746
|
return err;
|
abe00makoto |
0:e939856c1939
|
747
|
|
abe00makoto |
0:e939856c1939
|
748
|
err = SetConfiguration(device,configuration);
|
abe00makoto |
0:e939856c1939
|
749
|
if (err < 0)
|
abe00makoto |
0:e939856c1939
|
750
|
return err;
|
abe00makoto |
0:e939856c1939
|
751
|
|
abe00makoto |
0:e939856c1939
|
752
|
// Add the endpoints for this interface
|
abe00makoto |
0:e939856c1939
|
753
|
int len = buffer[2] | (buffer[3] << 8);
|
abe00makoto |
0:e939856c1939
|
754
|
u8* d = buffer;
|
abe00makoto |
0:e939856c1939
|
755
|
u8* end = d + len;
|
abe00makoto |
0:e939856c1939
|
756
|
InterfaceDescriptor* found = 0;
|
abe00makoto |
0:e939856c1939
|
757
|
while (d < end)
|
abe00makoto |
0:e939856c1939
|
758
|
{
|
abe00makoto |
0:e939856c1939
|
759
|
if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
|
abe00makoto |
0:e939856c1939
|
760
|
{
|
abe00makoto |
0:e939856c1939
|
761
|
InterfaceDescriptor* id = (InterfaceDescriptor*)d;
|
abe00makoto |
0:e939856c1939
|
762
|
if (id->bInterfaceNumber == interfaceNumber)
|
abe00makoto |
0:e939856c1939
|
763
|
{
|
abe00makoto |
0:e939856c1939
|
764
|
found = id;
|
abe00makoto |
0:e939856c1939
|
765
|
d += d[0];
|
abe00makoto |
0:e939856c1939
|
766
|
while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
|
abe00makoto |
0:e939856c1939
|
767
|
{
|
abe00makoto |
0:e939856c1939
|
768
|
switch (d[1])
|
abe00makoto |
0:e939856c1939
|
769
|
{
|
abe00makoto |
0:e939856c1939
|
770
|
case DESCRIPTOR_TYPE_ENDPOINT:
|
abe00makoto |
0:e939856c1939
|
771
|
AddEndpoint(device,(EndpointDescriptor*)d);
|
abe00makoto |
0:e939856c1939
|
772
|
break;
|
abe00makoto |
0:e939856c1939
|
773
|
default:
|
abe00makoto |
0:e939856c1939
|
774
|
LOG("Skipping descriptor %02X (%d bytes)\r\n",d[1],d[0]);
|
abe00makoto |
0:e939856c1939
|
775
|
}
|
abe00makoto |
0:e939856c1939
|
776
|
d += d[0];
|
abe00makoto |
0:e939856c1939
|
777
|
}
|
abe00makoto |
0:e939856c1939
|
778
|
}
|
abe00makoto |
0:e939856c1939
|
779
|
}
|
abe00makoto |
0:e939856c1939
|
780
|
d += d[0];
|
abe00makoto |
0:e939856c1939
|
781
|
}
|
abe00makoto |
0:e939856c1939
|
782
|
|
abe00makoto |
0:e939856c1939
|
783
|
if (!found)
|
abe00makoto |
0:e939856c1939
|
784
|
return ERR_INTERFACE_NOT_FOUND;
|
abe00makoto |
0:e939856c1939
|
785
|
OnLoadDevice(device,desc,found);
|
abe00makoto |
0:e939856c1939
|
786
|
return 0;
|
abe00makoto |
0:e939856c1939
|
787
|
}
|
abe00makoto |
0:e939856c1939
|
788
|
|
abe00makoto |
0:e939856c1939
|
789
|
void Init()
|
abe00makoto |
0:e939856c1939
|
790
|
{
|
abe00makoto |
0:e939856c1939
|
791
|
LOG("USB INIT (Controller is %d bytes)\r\n",sizeof(*this));
|
abe00makoto |
0:e939856c1939
|
792
|
memset(this,0,sizeof(HostController));
|
abe00makoto |
0:e939856c1939
|
793
|
EndpointZero.CurrentState = Endpoint::NotQueued;
|
abe00makoto |
0:e939856c1939
|
794
|
HWInit(&CommunicationArea);
|
abe00makoto |
0:e939856c1939
|
795
|
DelayMS(10);
|
abe00makoto |
0:e939856c1939
|
796
|
}
|
abe00makoto |
0:e939856c1939
|
797
|
|
abe00makoto |
0:e939856c1939
|
798
|
void ResetPort(int hub, int port)
|
abe00makoto |
0:e939856c1939
|
799
|
{
|
abe00makoto |
0:e939856c1939
|
800
|
LOG("ResetPort Hub:%d Port:%d\r\n",hub,port);
|
abe00makoto |
0:e939856c1939
|
801
|
_connectPending++; // Only reset/add 1 device at a time
|
abe00makoto |
0:e939856c1939
|
802
|
if (hub == 0)
|
abe00makoto |
0:e939856c1939
|
803
|
LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1
|
abe00makoto |
0:e939856c1939
|
804
|
else
|
abe00makoto |
0:e939856c1939
|
805
|
SetPortReset(hub,port); // or reset other hub
|
abe00makoto |
0:e939856c1939
|
806
|
}
|
abe00makoto |
0:e939856c1939
|
807
|
|
abe00makoto |
0:e939856c1939
|
808
|
void Disconnect(int hub, int port)
|
abe00makoto |
0:e939856c1939
|
809
|
{
|
abe00makoto |
0:e939856c1939
|
810
|
LOG("Disconnect Hub:%d Port:%d\r\n",hub,port); // Mark a device for destruction
|
abe00makoto |
0:e939856c1939
|
811
|
for (int i = 0; i < MAX_DEVICES; i++)
|
abe00makoto |
0:e939856c1939
|
812
|
{
|
abe00makoto |
0:e939856c1939
|
813
|
Device* dev = Devices + i;
|
abe00makoto |
0:e939856c1939
|
814
|
if (dev->Port == port && dev->Hub == hub)
|
abe00makoto |
0:e939856c1939
|
815
|
{
|
abe00makoto |
0:e939856c1939
|
816
|
// Disconnect everything that is attached to this device if it is a hub
|
abe00makoto |
0:e939856c1939
|
817
|
for (int p = 0; p < dev->HubPortCount; p++)
|
abe00makoto |
0:e939856c1939
|
818
|
Disconnect(i+1,p+1);
|
abe00makoto |
0:e939856c1939
|
819
|
|
abe00makoto |
0:e939856c1939
|
820
|
// Now release endpoints
|
abe00makoto |
0:e939856c1939
|
821
|
for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2)
|
abe00makoto |
0:e939856c1939
|
822
|
{
|
abe00makoto |
0:e939856c1939
|
823
|
u8 endpointIndex = dev->_endpointMap[j];
|
abe00makoto |
0:e939856c1939
|
824
|
if (endpointIndex != 0xFF)
|
abe00makoto |
0:e939856c1939
|
825
|
Release(Endpoints + endpointIndex);
|
abe00makoto |
0:e939856c1939
|
826
|
}
|
abe00makoto |
0:e939856c1939
|
827
|
dev->Port = 0; // Device is now free
|
abe00makoto |
0:e939856c1939
|
828
|
dev->Flags = 0;
|
abe00makoto |
0:e939856c1939
|
829
|
return;
|
abe00makoto |
0:e939856c1939
|
830
|
}
|
abe00makoto |
0:e939856c1939
|
831
|
}
|
abe00makoto |
0:e939856c1939
|
832
|
}
|
abe00makoto |
0:e939856c1939
|
833
|
|
abe00makoto |
0:e939856c1939
|
834
|
// called after reset
|
abe00makoto |
0:e939856c1939
|
835
|
void Connect(int hub, int port, bool lowspeed)
|
abe00makoto |
0:e939856c1939
|
836
|
{
|
abe00makoto |
0:e939856c1939
|
837
|
LOG("Connect Hub:%d Port:%d %s\r\n",hub,port,lowspeed ? "slow" : "full");
|
abe00makoto |
0:e939856c1939
|
838
|
AddDevice(hub,port,lowspeed);
|
abe00makoto |
0:e939856c1939
|
839
|
}
|
abe00makoto |
0:e939856c1939
|
840
|
|
abe00makoto |
0:e939856c1939
|
841
|
// Called from interrupt
|
abe00makoto |
0:e939856c1939
|
842
|
void HubStatusChange(int hub, int port, u32 status)
|
abe00makoto |
0:e939856c1939
|
843
|
{
|
abe00makoto |
0:e939856c1939
|
844
|
LOG("HubStatusChange Hub:%d Port:%d %08X\r\n",hub,port,status);
|
abe00makoto |
0:e939856c1939
|
845
|
if (status & ConnectStatusChange)
|
abe00makoto |
0:e939856c1939
|
846
|
{
|
abe00makoto |
0:e939856c1939
|
847
|
if (status & CurrentConnectStatus) // Connecting
|
abe00makoto |
0:e939856c1939
|
848
|
ResetPort(hub,port); // Reset to initiate connect (state machine?)
|
abe00makoto |
0:e939856c1939
|
849
|
else
|
abe00makoto |
0:e939856c1939
|
850
|
Disconnect(hub,port);
|
abe00makoto |
0:e939856c1939
|
851
|
}
|
abe00makoto |
0:e939856c1939
|
852
|
|
abe00makoto |
0:e939856c1939
|
853
|
if (status & PortResetStatusChange)
|
abe00makoto |
0:e939856c1939
|
854
|
{
|
abe00makoto |
0:e939856c1939
|
855
|
if (!(status & PortResetStatus))
|
abe00makoto |
0:e939856c1939
|
856
|
{
|
abe00makoto |
0:e939856c1939
|
857
|
_connectCountdown = 200; // Schedule a connection in 200ms
|
abe00makoto |
0:e939856c1939
|
858
|
if (status & LowspeedDevice)
|
abe00makoto |
0:e939856c1939
|
859
|
port |= 0x80;
|
abe00makoto |
0:e939856c1939
|
860
|
_connectHub = hub;
|
abe00makoto |
0:e939856c1939
|
861
|
_connectPort = port;
|
abe00makoto |
0:e939856c1939
|
862
|
}
|
abe00makoto |
0:e939856c1939
|
863
|
}
|
abe00makoto |
0:e939856c1939
|
864
|
}
|
abe00makoto |
0:e939856c1939
|
865
|
|
abe00makoto |
0:e939856c1939
|
866
|
#define HOST_CLK_EN (1<<0)
|
abe00makoto |
0:e939856c1939
|
867
|
#define PORTSEL_CLK_EN (1<<3)
|
abe00makoto |
0:e939856c1939
|
868
|
#define AHB_CLK_EN (1<<4)
|
abe00makoto |
0:e939856c1939
|
869
|
#define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN)
|
abe00makoto |
0:e939856c1939
|
870
|
|
abe00makoto |
0:e939856c1939
|
871
|
#define FRAMEINTERVAL (12000-1) // 1ms
|
abe00makoto |
0:e939856c1939
|
872
|
#define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL)
|
abe00makoto |
0:e939856c1939
|
873
|
|
abe00makoto |
0:e939856c1939
|
874
|
void DelayMS(int ms)
|
abe00makoto |
0:e939856c1939
|
875
|
{
|
abe00makoto |
0:e939856c1939
|
876
|
u16 f = ms + CommunicationArea.FrameNumber;
|
abe00makoto |
0:e939856c1939
|
877
|
while (f != CommunicationArea.FrameNumber)
|
abe00makoto |
0:e939856c1939
|
878
|
;
|
abe00makoto |
0:e939856c1939
|
879
|
}
|
abe00makoto |
0:e939856c1939
|
880
|
|
abe00makoto |
0:e939856c1939
|
881
|
static void HWInit(HCCA* cca)
|
abe00makoto |
0:e939856c1939
|
882
|
{
|
abe00makoto |
0:e939856c1939
|
883
|
NVIC_DisableIRQ(USB_IRQn);
|
abe00makoto |
0:e939856c1939
|
884
|
|
abe00makoto |
0:e939856c1939
|
885
|
// turn on power for USB
|
abe00makoto |
0:e939856c1939
|
886
|
LPC_SC->PCONP |= (1UL<<31);
|
abe00makoto |
0:e939856c1939
|
887
|
// Enable USB host clock, port selection and AHB clock
|
abe00makoto |
0:e939856c1939
|
888
|
LPC_USB->USBClkCtrl |= CLOCK_MASK;
|
abe00makoto |
0:e939856c1939
|
889
|
// Wait for clocks to become available
|
abe00makoto |
0:e939856c1939
|
890
|
while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK)
|
abe00makoto |
0:e939856c1939
|
891
|
;
|
abe00makoto |
0:e939856c1939
|
892
|
|
abe00makoto |
0:e939856c1939
|
893
|
// We are a Host
|
abe00makoto |
0:e939856c1939
|
894
|
LPC_USB->OTGStCtrl |= 1;
|
abe00makoto |
0:e939856c1939
|
895
|
LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG
|
abe00makoto |
0:e939856c1939
|
896
|
|
abe00makoto |
0:e939856c1939
|
897
|
// configure USB pins
|
abe00makoto |
0:e939856c1939
|
898
|
LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28));
|
abe00makoto |
0:e939856c1939
|
899
|
LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D-
|
abe00makoto |
0:e939856c1939
|
900
|
|
abe00makoto |
0:e939856c1939
|
901
|
LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR
|
abe00makoto |
0:e939856c1939
|
902
|
LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22));
|
abe00makoto |
0:e939856c1939
|
903
|
|
abe00makoto |
0:e939856c1939
|
904
|
LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT
|
abe00makoto |
0:e939856c1939
|
905
|
LPC_PINCON->PINSEL4 |= (1 << 18);
|
abe00makoto |
0:e939856c1939
|
906
|
|
abe00makoto |
0:e939856c1939
|
907
|
// Reset OHCI block
|
abe00makoto |
0:e939856c1939
|
908
|
LPC_USB->HcControl = 0;
|
abe00makoto |
0:e939856c1939
|
909
|
LPC_USB->HcControlHeadED = 0;
|
abe00makoto |
0:e939856c1939
|
910
|
LPC_USB->HcBulkHeadED = 0;
|
abe00makoto |
0:e939856c1939
|
911
|
|
abe00makoto |
0:e939856c1939
|
912
|
LPC_USB->HcCommandStatus = HostControllerReset;
|
abe00makoto |
0:e939856c1939
|
913
|
LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL;
|
abe00makoto |
0:e939856c1939
|
914
|
LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100;
|
abe00makoto |
0:e939856c1939
|
915
|
|
abe00makoto |
0:e939856c1939
|
916
|
LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask;
|
abe00makoto |
0:e939856c1939
|
917
|
LPC_USB->HcRhStatus = SetGlobalPower;
|
abe00makoto |
0:e939856c1939
|
918
|
|
abe00makoto |
0:e939856c1939
|
919
|
LPC_USB->HcHCCA = (u32)cca;
|
abe00makoto |
0:e939856c1939
|
920
|
LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus;
|
abe00makoto |
0:e939856c1939
|
921
|
LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow;
|
abe00makoto |
0:e939856c1939
|
922
|
|
abe00makoto |
0:e939856c1939
|
923
|
NVIC_SetPriority(USB_IRQn, 0);
|
abe00makoto |
0:e939856c1939
|
924
|
NVIC_EnableIRQ(USB_IRQn);
|
abe00makoto |
0:e939856c1939
|
925
|
while (cca->FrameNumber < 10)
|
abe00makoto |
0:e939856c1939
|
926
|
; // 10ms delay before diving in
|
abe00makoto |
0:e939856c1939
|
927
|
}
|
abe00makoto |
0:e939856c1939
|
928
|
};
|
abe00makoto |
0:e939856c1939
|
929
|
|
abe00makoto |
0:e939856c1939
|
930
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
931
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
932
|
// Host controller instance and Interrupt handler
|
abe00makoto |
0:e939856c1939
|
933
|
|
abe00makoto |
0:e939856c1939
|
934
|
static HostController _controller __attribute__((at(USB_RAM_BASE)));
|
abe00makoto |
0:e939856c1939
|
935
|
|
abe00makoto |
0:e939856c1939
|
936
|
extern "C" void USB_IRQHandler(void) __irq;
|
abe00makoto |
0:e939856c1939
|
937
|
void USB_IRQHandler (void) __irq
|
abe00makoto |
0:e939856c1939
|
938
|
{
|
abe00makoto |
0:e939856c1939
|
939
|
LOG("USB_IRQHandler start \r\n");
|
abe00makoto |
0:e939856c1939
|
940
|
u32 int_status = LPC_USB->HcInterruptStatus;
|
abe00makoto |
0:e939856c1939
|
941
|
LOG("USB_IRQHandler int_status=%0x\r\n",int_status);
|
abe00makoto |
0:e939856c1939
|
942
|
|
abe00makoto |
0:e939856c1939
|
943
|
if(int_status & UnrecoverableError) //Error
|
abe00makoto |
0:e939856c1939
|
944
|
{
|
abe00makoto |
0:e939856c1939
|
945
|
LOG("USB_IRQHandler UnrecoverableError Please reset\r\n");
|
abe00makoto |
0:e939856c1939
|
946
|
}
|
abe00makoto |
0:e939856c1939
|
947
|
|
abe00makoto |
0:e939856c1939
|
948
|
if(int_status & MasterInterruptEnable)
|
abe00makoto |
0:e939856c1939
|
949
|
{
|
abe00makoto |
0:e939856c1939
|
950
|
LOG("USB_IRQHandler MasterInterruptEnable\r\n");
|
abe00makoto |
0:e939856c1939
|
951
|
}
|
abe00makoto |
0:e939856c1939
|
952
|
|
abe00makoto |
0:e939856c1939
|
953
|
if(int_status &OwnershipChange)
|
abe00makoto |
0:e939856c1939
|
954
|
{
|
abe00makoto |
0:e939856c1939
|
955
|
LOG("USB_IRQHandler OwnershipChange\r\n");
|
abe00makoto |
0:e939856c1939
|
956
|
}
|
abe00makoto |
0:e939856c1939
|
957
|
|
abe00makoto |
0:e939856c1939
|
958
|
if(int_status &FrameNumberOverflow)
|
abe00makoto |
0:e939856c1939
|
959
|
{
|
abe00makoto |
0:e939856c1939
|
960
|
LOG("USB_IRQHandler FrameNumberOverflow\r\n");
|
abe00makoto |
0:e939856c1939
|
961
|
}
|
abe00makoto |
0:e939856c1939
|
962
|
if(int_status&ResumeDetected)
|
abe00makoto |
0:e939856c1939
|
963
|
{
|
abe00makoto |
0:e939856c1939
|
964
|
LOG("USB_IRQHandler ResumeDetected\r\n");
|
abe00makoto |
0:e939856c1939
|
965
|
}
|
abe00makoto |
0:e939856c1939
|
966
|
if(int_status&StartofFrame)
|
abe00makoto |
0:e939856c1939
|
967
|
{
|
abe00makoto |
0:e939856c1939
|
968
|
LOG("USB_IRQHandler StartofFrame\r\n");
|
abe00makoto |
0:e939856c1939
|
969
|
}
|
abe00makoto |
0:e939856c1939
|
970
|
|
abe00makoto |
0:e939856c1939
|
971
|
|
abe00makoto |
0:e939856c1939
|
972
|
if (int_status & RootHubStatusChange) // Root hub status change
|
abe00makoto |
0:e939856c1939
|
973
|
{
|
abe00makoto |
0:e939856c1939
|
974
|
LOG("USB_IRQHandler _rootHubStatusChange %0x->%0x \r\n", _controller._rootHubStatusChange, _controller._rootHubStatusChange+1);
|
abe00makoto |
0:e939856c1939
|
975
|
_controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop
|
abe00makoto |
0:e939856c1939
|
976
|
}
|
abe00makoto |
0:e939856c1939
|
977
|
|
abe00makoto |
0:e939856c1939
|
978
|
u32 head = 0;
|
abe00makoto |
0:e939856c1939
|
979
|
if (int_status & WritebackDoneHead)
|
abe00makoto |
0:e939856c1939
|
980
|
{
|
abe00makoto |
0:e939856c1939
|
981
|
head = _controller.CommunicationArea.DoneHead; // Writeback Done
|
abe00makoto |
0:e939856c1939
|
982
|
_controller.CommunicationArea.DoneHead = 0;
|
abe00makoto |
0:e939856c1939
|
983
|
LOG("USB_IRQHandler head=%0x\r\n",head);
|
abe00makoto |
0:e939856c1939
|
984
|
}
|
abe00makoto |
0:e939856c1939
|
985
|
LPC_USB->HcInterruptStatus = int_status;
|
abe00makoto |
0:e939856c1939
|
986
|
|
abe00makoto |
0:e939856c1939
|
987
|
if (head)
|
abe00makoto |
0:e939856c1939
|
988
|
_controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG
|
abe00makoto |
0:e939856c1939
|
989
|
LOG("USB_IRQHandler end \r\n");
|
abe00makoto |
0:e939856c1939
|
990
|
}
|
abe00makoto |
0:e939856c1939
|
991
|
|
abe00makoto |
0:e939856c1939
|
992
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
993
|
//====================================================================================
|
abe00makoto |
0:e939856c1939
|
994
|
// API Methods
|
abe00makoto |
0:e939856c1939
|
995
|
|
abe00makoto |
0:e939856c1939
|
996
|
void USBInit()
|
abe00makoto |
0:e939856c1939
|
997
|
{
|
abe00makoto |
0:e939856c1939
|
998
|
return _controller.Init();
|
abe00makoto |
0:e939856c1939
|
999
|
}
|
abe00makoto |
0:e939856c1939
|
1000
|
|
abe00makoto |
0:e939856c1939
|
1001
|
void USBLoop()
|
abe00makoto |
0:e939856c1939
|
1002
|
{
|
abe00makoto |
0:e939856c1939
|
1003
|
return _controller.Loop();
|
abe00makoto |
0:e939856c1939
|
1004
|
}
|
abe00makoto |
0:e939856c1939
|
1005
|
|
abe00makoto |
0:e939856c1939
|
1006
|
u8* USBGetBuffer(u32* len)
|
abe00makoto |
0:e939856c1939
|
1007
|
{
|
abe00makoto |
0:e939856c1939
|
1008
|
*len = USB_RAM_SIZE - sizeof(HostController);
|
abe00makoto |
0:e939856c1939
|
1009
|
return _controller.SRAM;
|
abe00makoto |
0:e939856c1939
|
1010
|
}
|
abe00makoto |
0:e939856c1939
|
1011
|
|
abe00makoto |
0:e939856c1939
|
1012
|
static Setup* GetSetup(int device)
|
abe00makoto |
0:e939856c1939
|
1013
|
{
|
abe00makoto |
0:e939856c1939
|
1014
|
if (device == 0)
|
abe00makoto |
0:e939856c1939
|
1015
|
return &_controller._setupZero;
|
abe00makoto |
0:e939856c1939
|
1016
|
|
abe00makoto |
0:e939856c1939
|
1017
|
if (device < 1 || device > MAX_DEVICES)
|
abe00makoto |
0:e939856c1939
|
1018
|
return 0;
|
abe00makoto |
0:e939856c1939
|
1019
|
return &_controller.Devices[device-1].SetupBuffer;
|
abe00makoto |
0:e939856c1939
|
1020
|
}
|
abe00makoto |
0:e939856c1939
|
1021
|
|
abe00makoto |
0:e939856c1939
|
1022
|
// Loop until IO on endpoint is complete
|
abe00makoto |
0:e939856c1939
|
1023
|
static int WaitIODone(Endpoint* endpoint)
|
abe00makoto |
0:e939856c1939
|
1024
|
{
|
abe00makoto |
0:e939856c1939
|
1025
|
LOG("WaitIODone start\r\n");
|
abe00makoto |
0:e939856c1939
|
1026
|
if (endpoint->CurrentState == Endpoint::NotQueued)
|
abe00makoto |
0:e939856c1939
|
1027
|
return 0;
|
abe00makoto |
0:e939856c1939
|
1028
|
while (endpoint->CurrentState != Endpoint::Idle)
|
abe00makoto |
0:e939856c1939
|
1029
|
{
|
abe00makoto |
0:e939856c1939
|
1030
|
LOG("WaitIODone goto USBloop endpoint->CurrentState =%d \r\n",endpoint->CurrentState);
|
abe00makoto |
0:e939856c1939
|
1031
|
USBLoop(); // May generate callbacks, mount or unmount devices etc
|
abe00makoto |
0:e939856c1939
|
1032
|
}
|
abe00makoto |
0:e939856c1939
|
1033
|
int status = endpoint->Status();
|
abe00makoto |
0:e939856c1939
|
1034
|
|
abe00makoto |
0:e939856c1939
|
1035
|
LOG("WaitIODone end\r\n");
|
abe00makoto |
0:e939856c1939
|
1036
|
if (status == 0)
|
abe00makoto |
0:e939856c1939
|
1037
|
return endpoint->Length;
|
abe00makoto |
0:e939856c1939
|
1038
|
return -status;
|
abe00makoto |
0:e939856c1939
|
1039
|
}
|
abe00makoto |
0:e939856c1939
|
1040
|
|
abe00makoto |
0:e939856c1939
|
1041
|
int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData)
|
abe00makoto |
0:e939856c1939
|
1042
|
{
|
abe00makoto |
0:e939856c1939
|
1043
|
//LOG("USBTRANSFER start\r\n");
|
abe00makoto |
0:e939856c1939
|
1044
|
Endpoint* endpoint = _controller.GetEndpoint(device,ep);
|
abe00makoto |
0:e939856c1939
|
1045
|
if (!endpoint)
|
abe00makoto |
0:e939856c1939
|
1046
|
{
|
abe00makoto |
0:e939856c1939
|
1047
|
LOG("USBTRANSFER error Endpoint not found!!\r\n");
|
abe00makoto |
0:e939856c1939
|
1048
|
return ERR_ENDPOINT_NOT_FOUND;
|
abe00makoto |
0:e939856c1939
|
1049
|
}
|
abe00makoto |
0:e939856c1939
|
1050
|
|
abe00makoto |
0:e939856c1939
|
1051
|
//LOG("USBTRANSFER run01\r\n");
|
abe00makoto |
0:e939856c1939
|
1052
|
WaitIODone(endpoint);
|
abe00makoto |
0:e939856c1939
|
1053
|
endpoint->Flags = flags;
|
abe00makoto |
0:e939856c1939
|
1054
|
endpoint->Data = data;
|
abe00makoto |
0:e939856c1939
|
1055
|
endpoint->Length = length;
|
abe00makoto |
0:e939856c1939
|
1056
|
endpoint->Callback = callback;
|
abe00makoto |
0:e939856c1939
|
1057
|
endpoint->UserData = userData;
|
abe00makoto |
0:e939856c1939
|
1058
|
//LOG("USBTRANSFER run02\r\n");
|
abe00makoto |
0:e939856c1939
|
1059
|
if (ep == 0)
|
abe00makoto |
0:e939856c1939
|
1060
|
{
|
abe00makoto |
0:e939856c1939
|
1061
|
LOG("USBTRANSFER ep == 0(Setup) ------------------------------\r\n");
|
abe00makoto |
0:e939856c1939
|
1062
|
_controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued);
|
abe00makoto |
0:e939856c1939
|
1063
|
}
|
abe00makoto |
0:e939856c1939
|
1064
|
else
|
abe00makoto |
0:e939856c1939
|
1065
|
{
|
abe00makoto |
0:e939856c1939
|
1066
|
LOG("USBTRANSFER ep != 0\r\n");
|
abe00makoto |
0:e939856c1939
|
1067
|
_controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued);
|
abe00makoto |
0:e939856c1939
|
1068
|
}
|
abe00makoto |
0:e939856c1939
|
1069
|
if (callback)
|
abe00makoto |
0:e939856c1939
|
1070
|
{
|
abe00makoto |
0:e939856c1939
|
1071
|
LOG("USBTRANSFER end io_pending\r\n");
|
abe00makoto |
0:e939856c1939
|
1072
|
return IO_PENDING;
|
abe00makoto |
0:e939856c1939
|
1073
|
}
|
abe00makoto |
0:e939856c1939
|
1074
|
LOG("USBTRANSFER end wait io done \r\n");
|
abe00makoto |
0:e939856c1939
|
1075
|
return WaitIODone(endpoint);
|
abe00makoto |
0:e939856c1939
|
1076
|
}
|
abe00makoto |
0:e939856c1939
|
1077
|
|
abe00makoto |
0:e939856c1939
|
1078
|
int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData)
|
abe00makoto |
0:e939856c1939
|
1079
|
{
|
abe00makoto |
0:e939856c1939
|
1080
|
Setup* setup = GetSetup(device);
|
abe00makoto |
0:e939856c1939
|
1081
|
if (!setup)
|
abe00makoto |
0:e939856c1939
|
1082
|
return ERR_DEVICE_NOT_FOUND;
|
abe00makoto |
0:e939856c1939
|
1083
|
|
abe00makoto |
0:e939856c1939
|
1084
|
// Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call
|
abe00makoto |
0:e939856c1939
|
1085
|
WaitIODone(_controller.GetEndpoint(device,0));
|
abe00makoto |
0:e939856c1939
|
1086
|
|
abe00makoto |
0:e939856c1939
|
1087
|
setup->bm_request_type = request_type;
|
abe00makoto |
0:e939856c1939
|
1088
|
setup->b_request = request;
|
abe00makoto |
0:e939856c1939
|
1089
|
setup->w_value = value;
|
abe00makoto |
0:e939856c1939
|
1090
|
setup->w_index = index;
|
abe00makoto |
0:e939856c1939
|
1091
|
setup->w_length = length;
|
abe00makoto |
0:e939856c1939
|
1092
|
LOG("USBControlTransfer(device=%d,reqtype=%d,req=%d,value=%d,index=%d,data=%p,len=%d,callbak=%p,udata=%p)\r\n",
|
abe00makoto |
0:e939856c1939
|
1093
|
device,request_type,request,value,index,data,length,callback,userData);
|
abe00makoto |
0:e939856c1939
|
1094
|
return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData);
|
abe00makoto |
0:e939856c1939
|
1095
|
}
|
abe00makoto |
0:e939856c1939
|
1096
|
|
abe00makoto |
0:e939856c1939
|
1097
|
int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
|
abe00makoto |
0:e939856c1939
|
1098
|
{
|
abe00makoto |
0:e939856c1939
|
1099
|
return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData);
|
abe00makoto |
0:e939856c1939
|
1100
|
}
|
abe00makoto |
0:e939856c1939
|
1101
|
|
abe00makoto |
0:e939856c1939
|
1102
|
int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData)
|
abe00makoto |
0:e939856c1939
|
1103
|
{
|
abe00makoto |
0:e939856c1939
|
1104
|
return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData);
|
abe00makoto |
0:e939856c1939
|
1105
|
}
|
abe00makoto |
0:e939856c1939
|
1106
|
|
abe00makoto |
0:e939856c1939
|
1107
|
int GetDescriptor(int device, int descType,int descIndex, u8* data, int length)
|
abe00makoto |
0:e939856c1939
|
1108
|
{
|
abe00makoto |
0:e939856c1939
|
1109
|
return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0);
|
abe00makoto |
0:e939856c1939
|
1110
|
}
|
abe00makoto |
0:e939856c1939
|
1111
|
|
abe00makoto |
0:e939856c1939
|
1112
|
int GetString(int device, int index, char* dst, int length)
|
abe00makoto |
0:e939856c1939
|
1113
|
{
|
abe00makoto |
0:e939856c1939
|
1114
|
u8 buffer[255];
|
abe00makoto |
0:e939856c1939
|
1115
|
int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer));
|
abe00makoto |
0:e939856c1939
|
1116
|
if (le < 0)
|
abe00makoto |
0:e939856c1939
|
1117
|
return le;
|
abe00makoto |
0:e939856c1939
|
1118
|
if (length < 1)
|
abe00makoto |
0:e939856c1939
|
1119
|
return -1;
|
abe00makoto |
0:e939856c1939
|
1120
|
length <<= 1;
|
abe00makoto |
0:e939856c1939
|
1121
|
if (le > length)
|
abe00makoto |
0:e939856c1939
|
1122
|
le = length;
|
abe00makoto |
0:e939856c1939
|
1123
|
for (int j = 2; j < le; j += 2)
|
abe00makoto |
0:e939856c1939
|
1124
|
*dst++ = buffer[j];
|
abe00makoto |
0:e939856c1939
|
1125
|
*dst = 0;
|
abe00makoto |
0:e939856c1939
|
1126
|
return (le>>1)-1;
|
abe00makoto |
0:e939856c1939
|
1127
|
}
|
abe00makoto |
0:e939856c1939
|
1128
|
|
abe00makoto |
0:e939856c1939
|
1129
|
int SetAddress(int device, int new_addr)
|
abe00makoto |
0:e939856c1939
|
1130
|
{
|
abe00makoto |
0:e939856c1939
|
1131
|
return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0);
|
abe00makoto |
0:e939856c1939
|
1132
|
}
|
abe00makoto |
0:e939856c1939
|
1133
|
|
abe00makoto |
0:e939856c1939
|
1134
|
int SetConfiguration(int device, int configNum)
|
abe00makoto |
0:e939856c1939
|
1135
|
{
|
abe00makoto |
0:e939856c1939
|
1136
|
return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0);
|
abe00makoto |
0:e939856c1939
|
1137
|
}
|
abe00makoto |
0:e939856c1939
|
1138
|
|
abe00makoto |
0:e939856c1939
|
1139
|
int SetInterface(int device, int ifNum, int altNum)
|
abe00makoto |
0:e939856c1939
|
1140
|
{
|
abe00makoto |
0:e939856c1939
|
1141
|
return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0);
|
abe00makoto |
0:e939856c1939
|
1142
|
}
|
abe00makoto |
0:e939856c1939
|
1143
|
|
abe00makoto |
0:e939856c1939
|
1144
|
// HUB stuff
|
abe00makoto |
0:e939856c1939
|
1145
|
int SetPortFeature(int device, int feature, int index)
|
abe00makoto |
0:e939856c1939
|
1146
|
{
|
abe00makoto |
0:e939856c1939
|
1147
|
return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0);
|
abe00makoto |
0:e939856c1939
|
1148
|
}
|
abe00makoto |
0:e939856c1939
|
1149
|
|
abe00makoto |
0:e939856c1939
|
1150
|
int ClearPortFeature(int device, int feature, int index)
|
abe00makoto |
0:e939856c1939
|
1151
|
{
|
abe00makoto |
0:e939856c1939
|
1152
|
return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0);
|
abe00makoto |
0:e939856c1939
|
1153
|
}
|
abe00makoto |
0:e939856c1939
|
1154
|
|
abe00makoto |
0:e939856c1939
|
1155
|
int SetPortPower(int device, int port)
|
abe00makoto |
0:e939856c1939
|
1156
|
{
|
abe00makoto |
0:e939856c1939
|
1157
|
int r = SetPortFeature(device,PORT_POWER,port);
|
abe00makoto |
0:e939856c1939
|
1158
|
_controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo
|
abe00makoto |
0:e939856c1939
|
1159
|
return r;
|
abe00makoto |
0:e939856c1939
|
1160
|
}
|
abe00makoto |
0:e939856c1939
|
1161
|
|
abe00makoto |
0:e939856c1939
|
1162
|
int SetPortReset(int device, int port)
|
abe00makoto |
0:e939856c1939
|
1163
|
{
|
abe00makoto |
0:e939856c1939
|
1164
|
return SetPortFeature(device,PORT_RESET,port);
|
abe00makoto |
0:e939856c1939
|
1165
|
}
|
abe00makoto |
0:e939856c1939
|
1166
|
|
abe00makoto |
0:e939856c1939
|
1167
|
int GetPortStatus(int device, int port, u32* status)
|
abe00makoto |
0:e939856c1939
|
1168
|
{
|
abe00makoto |
0:e939856c1939
|
1169
|
return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4);
|
abe00makoto |
0:e939856c1939
|
1170
|
} |