This program uses code taken from another program called BlueUSB

Dependencies:   mbed

Committer:
madcowswe
Date:
Sat Dec 10 18:45:31 2011 +0000
Revision:
0:31713f62f35b

        

Who changed what in which revision?

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