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