BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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