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