Fixed version check for AOA 2.0

Fork of AndroidAccessory by Giles Barton-Owen

Committer:
vargham
Date:
Sun Nov 29 15:58:55 2015 +0000
Revision:
3:e7435fada96a
Parent:
0:1151e4446dbc
Fixed version check for AOA 2.0

Who changed what in which revision?

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