3/5

Dependencies:   mbed

Committer:
yuki0701
Date:
Tue Mar 05 04:30:35 2019 +0000
Revision:
1:530908de68c6
Parent:
0:a56be39653d0
a

Who changed what in which revision?

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