Twitter client, with TextLCD, Ethernet Connection, and USB Host for keyboard.

Dependencies:   EthernetNetIf TextLCD mbed

Committer:
blmarket
Date:
Thu Apr 21 05:54:26 2011 +0000
Revision:
0:70571fd24107
Initial Release

Who changed what in which revision?

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