![](/media/cache/profiles/8474b8609e772af467ac0fc4acad4dd4.jpg.50x50_q85.jpg)
A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.
Fork of mbed_TANK_PS3 by
USBHost.h@2:895f70862eb9, 2013-01-01 (annotated)
- Committer:
- srsmitherman
- Date:
- Tue Jan 01 02:10:08 2013 +0000
- Revision:
- 2:895f70862eb9
- Parent:
- 0:44619612f575
M3pi support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenbumono | 0:44619612f575 | 1 | |
kenbumono | 0:44619612f575 | 2 | /* |
kenbumono | 0:44619612f575 | 3 | Copyright (c) 2010 Peter Barrett |
kenbumono | 0:44619612f575 | 4 | |
kenbumono | 0:44619612f575 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
kenbumono | 0:44619612f575 | 6 | of this software and associated documentation files (the "Software"), to deal |
kenbumono | 0:44619612f575 | 7 | in the Software without restriction, including without limitation the rights |
kenbumono | 0:44619612f575 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenbumono | 0:44619612f575 | 9 | copies of the Software, and to permit persons to whom the Software is |
kenbumono | 0:44619612f575 | 10 | furnished to do so, subject to the following conditions: |
kenbumono | 0:44619612f575 | 11 | |
kenbumono | 0:44619612f575 | 12 | The above copyright notice and this permission notice shall be included in |
kenbumono | 0:44619612f575 | 13 | all copies or substantial portions of the Software. |
kenbumono | 0:44619612f575 | 14 | |
kenbumono | 0:44619612f575 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenbumono | 0:44619612f575 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenbumono | 0:44619612f575 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenbumono | 0:44619612f575 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenbumono | 0:44619612f575 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenbumono | 0:44619612f575 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kenbumono | 0:44619612f575 | 21 | THE SOFTWARE. |
kenbumono | 0:44619612f575 | 22 | */ |
kenbumono | 0:44619612f575 | 23 | |
kenbumono | 0:44619612f575 | 24 | #ifndef USBHOST_H |
kenbumono | 0:44619612f575 | 25 | #define USBHOST_H |
kenbumono | 0:44619612f575 | 26 | |
kenbumono | 0:44619612f575 | 27 | #ifndef u8 |
kenbumono | 0:44619612f575 | 28 | typedef unsigned char u8; |
kenbumono | 0:44619612f575 | 29 | typedef unsigned short u16; |
kenbumono | 0:44619612f575 | 30 | typedef unsigned long u32; |
kenbumono | 0:44619612f575 | 31 | |
kenbumono | 0:44619612f575 | 32 | typedef char s8; |
kenbumono | 0:44619612f575 | 33 | typedef short s16; |
kenbumono | 0:44619612f575 | 34 | typedef char s32; |
kenbumono | 0:44619612f575 | 35 | #endif |
kenbumono | 0:44619612f575 | 36 | |
kenbumono | 0:44619612f575 | 37 | #define ENDPOINT_CONTROL 0 |
kenbumono | 0:44619612f575 | 38 | #define ENDPOINT_ISOCRONOUS 1 |
kenbumono | 0:44619612f575 | 39 | #define ENDPOINT_BULK 2 |
kenbumono | 0:44619612f575 | 40 | #define ENDPOINT_INTERRUPT 3 |
kenbumono | 0:44619612f575 | 41 | |
kenbumono | 0:44619612f575 | 42 | #define DESCRIPTOR_TYPE_DEVICE 1 |
kenbumono | 0:44619612f575 | 43 | #define DESCRIPTOR_TYPE_CONFIGURATION 2 |
kenbumono | 0:44619612f575 | 44 | #define DESCRIPTOR_TYPE_STRING 3 |
kenbumono | 0:44619612f575 | 45 | #define DESCRIPTOR_TYPE_INTERFACE 4 |
kenbumono | 0:44619612f575 | 46 | #define DESCRIPTOR_TYPE_ENDPOINT 5 |
kenbumono | 0:44619612f575 | 47 | |
kenbumono | 0:44619612f575 | 48 | #define DESCRIPTOR_TYPE_HID 0x21 |
kenbumono | 0:44619612f575 | 49 | #define DESCRIPTOR_TYPE_REPORT 0x22 |
kenbumono | 0:44619612f575 | 50 | #define DESCRIPTOR_TYPE_PHYSICAL 0x23 |
kenbumono | 0:44619612f575 | 51 | #define DESCRIPTOR_TYPE_HUB 0x29 |
kenbumono | 0:44619612f575 | 52 | |
kenbumono | 0:44619612f575 | 53 | enum USB_CLASS_CODE |
kenbumono | 0:44619612f575 | 54 | { |
kenbumono | 0:44619612f575 | 55 | CLASS_DEVICE, |
kenbumono | 0:44619612f575 | 56 | CLASS_AUDIO, |
kenbumono | 0:44619612f575 | 57 | CLASS_COMM_AND_CDC_CONTROL, |
kenbumono | 0:44619612f575 | 58 | CLASS_HID, |
kenbumono | 0:44619612f575 | 59 | CLASS_PHYSICAL = 0x05, |
kenbumono | 0:44619612f575 | 60 | CLASS_STILL_IMAGING, |
kenbumono | 0:44619612f575 | 61 | CLASS_PRINTER, |
kenbumono | 0:44619612f575 | 62 | CLASS_MASS_STORAGE, |
kenbumono | 0:44619612f575 | 63 | CLASS_HUB, |
kenbumono | 0:44619612f575 | 64 | CLASS_CDC_DATA, |
kenbumono | 0:44619612f575 | 65 | CLASS_SMART_CARD, |
kenbumono | 0:44619612f575 | 66 | CLASS_CONTENT_SECURITY = 0x0D, |
kenbumono | 0:44619612f575 | 67 | CLASS_VIDEO = 0x0E, |
kenbumono | 0:44619612f575 | 68 | CLASS_DIAGNOSTIC_DEVICE = 0xDC, |
kenbumono | 0:44619612f575 | 69 | CLASS_WIRELESS_CONTROLLER = 0xE0, |
kenbumono | 0:44619612f575 | 70 | CLASS_MISCELLANEOUS = 0xEF, |
kenbumono | 0:44619612f575 | 71 | CLASS_APP_SPECIFIC = 0xFE, |
kenbumono | 0:44619612f575 | 72 | CLASS_VENDOR_SPECIFIC = 0xFF |
kenbumono | 0:44619612f575 | 73 | }; |
kenbumono | 0:44619612f575 | 74 | |
kenbumono | 0:44619612f575 | 75 | #define DEVICE_TO_HOST 0x80 |
kenbumono | 0:44619612f575 | 76 | #define HOST_TO_DEVICE 0x00 |
kenbumono | 0:44619612f575 | 77 | #define REQUEST_TYPE_STANDARD 0x00 |
kenbumono | 0:44619612f575 | 78 | #define REQUEST_TYPE_CLASS 0x20 |
kenbumono | 0:44619612f575 | 79 | #define REQUEST_TYPE_VENDOR 0x40 |
kenbumono | 0:44619612f575 | 80 | #define RECIPIENT_DEVICE 0x00 |
kenbumono | 0:44619612f575 | 81 | #define RECIPIENT_INTERFACE 0x01 |
kenbumono | 0:44619612f575 | 82 | #define RECIPIENT_ENDPOINT 0x02 |
kenbumono | 0:44619612f575 | 83 | #define RECIPIENT_OTHER 0x03 |
kenbumono | 0:44619612f575 | 84 | |
kenbumono | 0:44619612f575 | 85 | #define GET_STATUS 0 |
kenbumono | 0:44619612f575 | 86 | #define CLEAR_FEATURE 1 |
kenbumono | 0:44619612f575 | 87 | #define SET_FEATURE 3 |
kenbumono | 0:44619612f575 | 88 | #define SET_ADDRESS 5 |
kenbumono | 0:44619612f575 | 89 | #define GET_DESCRIPTOR 6 |
kenbumono | 0:44619612f575 | 90 | #define SET_DESCRIPTOR 7 |
kenbumono | 0:44619612f575 | 91 | #define GET_CONFIGURATION 8 |
kenbumono | 0:44619612f575 | 92 | #define SET_CONFIGURATION 9 |
kenbumono | 0:44619612f575 | 93 | #define GET_INTERFACE 10 |
kenbumono | 0:44619612f575 | 94 | #define SET_INTERFACE 11 |
kenbumono | 0:44619612f575 | 95 | #define SYNCH_FRAME 11 |
kenbumono | 0:44619612f575 | 96 | |
kenbumono | 0:44619612f575 | 97 | /* HID Request Codes */ |
kenbumono | 0:44619612f575 | 98 | #define HID_REQUEST_GET_REPORT 0x01 |
kenbumono | 0:44619612f575 | 99 | #define HID_REQUEST_GET_IDLE 0x02 |
kenbumono | 0:44619612f575 | 100 | #define HID_REQUEST_GET_PROTOCOL 0x03 |
kenbumono | 0:44619612f575 | 101 | #define HID_REQUEST_SET_REPORT 0x09 |
kenbumono | 0:44619612f575 | 102 | #define HID_REQUEST_SET_IDLE 0x0A |
kenbumono | 0:44619612f575 | 103 | #define HID_REQUEST_SET_PROTOCOL 0x0B |
kenbumono | 0:44619612f575 | 104 | |
kenbumono | 0:44619612f575 | 105 | /* HID Report Types */ |
kenbumono | 0:44619612f575 | 106 | #define HID_REPORT_INPUT 0x01 |
kenbumono | 0:44619612f575 | 107 | #define HID_REPORT_OUTPUT 0x02 |
kenbumono | 0:44619612f575 | 108 | #define HID_REPORT_FEATURE 0x03 |
kenbumono | 0:44619612f575 | 109 | |
kenbumono | 0:44619612f575 | 110 | |
kenbumono | 0:44619612f575 | 111 | // -5 is nak |
kenbumono | 0:44619612f575 | 112 | /* |
kenbumono | 0:44619612f575 | 113 | 0010 ACK Handshake |
kenbumono | 0:44619612f575 | 114 | 1010 NAK Handshake |
kenbumono | 0:44619612f575 | 115 | 1110 STALL Handshake |
kenbumono | 0:44619612f575 | 116 | 0110 NYET (No Response Yet) |
kenbumono | 0:44619612f575 | 117 | */ |
kenbumono | 0:44619612f575 | 118 | |
kenbumono | 0:44619612f575 | 119 | #define IO_PENDING -100 |
kenbumono | 0:44619612f575 | 120 | #define ERR_ENDPOINT_NONE_LEFT -101 |
kenbumono | 0:44619612f575 | 121 | #define ERR_ENDPOINT_NOT_FOUND -102 |
kenbumono | 0:44619612f575 | 122 | #define ERR_DEVICE_NOT_FOUND -103 |
kenbumono | 0:44619612f575 | 123 | #define ERR_DEVICE_NONE_LEFT -104 |
kenbumono | 0:44619612f575 | 124 | #define ERR_HUB_INIT_FAILED -105 |
kenbumono | 0:44619612f575 | 125 | #define ERR_INTERFACE_NOT_FOUND -106 |
kenbumono | 0:44619612f575 | 126 | |
kenbumono | 0:44619612f575 | 127 | typedef struct |
kenbumono | 0:44619612f575 | 128 | { |
srsmitherman | 2:895f70862eb9 | 129 | u8 bLength; |
srsmitherman | 2:895f70862eb9 | 130 | u8 bDescriptorType; |
srsmitherman | 2:895f70862eb9 | 131 | u16 bcdUSB; |
srsmitherman | 2:895f70862eb9 | 132 | u8 bDeviceClass; |
srsmitherman | 2:895f70862eb9 | 133 | u8 bDeviceSubClass; |
srsmitherman | 2:895f70862eb9 | 134 | u8 bDeviceProtocol; |
srsmitherman | 2:895f70862eb9 | 135 | u8 bMaxPacketSize; |
srsmitherman | 2:895f70862eb9 | 136 | u16 idVendor; |
srsmitherman | 2:895f70862eb9 | 137 | u16 idProduct; |
srsmitherman | 2:895f70862eb9 | 138 | u16 bcdDevice; // version |
srsmitherman | 2:895f70862eb9 | 139 | u8 iManufacturer; |
srsmitherman | 2:895f70862eb9 | 140 | u8 iProduct; |
srsmitherman | 2:895f70862eb9 | 141 | u8 iSerialNumber; |
srsmitherman | 2:895f70862eb9 | 142 | u8 bNumConfigurations; |
kenbumono | 0:44619612f575 | 143 | } DeviceDescriptor; // 16 bytes |
kenbumono | 0:44619612f575 | 144 | |
kenbumono | 0:44619612f575 | 145 | typedef struct |
kenbumono | 0:44619612f575 | 146 | { |
kenbumono | 0:44619612f575 | 147 | u8 bLength; |
kenbumono | 0:44619612f575 | 148 | u8 bDescriptorType; |
srsmitherman | 2:895f70862eb9 | 149 | u16 wTotalLength; |
kenbumono | 0:44619612f575 | 150 | u8 bNumInterfaces; |
kenbumono | 0:44619612f575 | 151 | u8 bConfigurationValue; // Value to use as an argument to select this configuration |
kenbumono | 0:44619612f575 | 152 | u8 iConfiguration; // Index of String Descriptor describing this configuration |
kenbumono | 0:44619612f575 | 153 | u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 |
kenbumono | 0:44619612f575 | 154 | u8 bMaxPower; // Maximum Power Consumption in 2mA units |
kenbumono | 0:44619612f575 | 155 | } ConfigurationDescriptor; |
kenbumono | 0:44619612f575 | 156 | |
kenbumono | 0:44619612f575 | 157 | typedef struct |
kenbumono | 0:44619612f575 | 158 | { |
kenbumono | 0:44619612f575 | 159 | u8 bLength; |
kenbumono | 0:44619612f575 | 160 | u8 bDescriptorType; |
srsmitherman | 2:895f70862eb9 | 161 | u8 bInterfaceNumber; |
kenbumono | 0:44619612f575 | 162 | u8 bAlternateSetting; |
kenbumono | 0:44619612f575 | 163 | u8 bNumEndpoints; |
kenbumono | 0:44619612f575 | 164 | u8 bInterfaceClass; |
kenbumono | 0:44619612f575 | 165 | u8 bInterfaceSubClass; |
kenbumono | 0:44619612f575 | 166 | u8 bInterfaceProtocol; |
kenbumono | 0:44619612f575 | 167 | u8 iInterface; // Index of String Descriptor Describing this interface |
kenbumono | 0:44619612f575 | 168 | } InterfaceDescriptor; |
kenbumono | 0:44619612f575 | 169 | |
kenbumono | 0:44619612f575 | 170 | typedef struct |
kenbumono | 0:44619612f575 | 171 | { |
kenbumono | 0:44619612f575 | 172 | u8 bLength; |
kenbumono | 0:44619612f575 | 173 | u8 bDescriptorType; |
kenbumono | 0:44619612f575 | 174 | u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) |
kenbumono | 0:44619612f575 | 175 | u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt |
srsmitherman | 2:895f70862eb9 | 176 | u16 wMaxPacketSize; |
kenbumono | 0:44619612f575 | 177 | u8 bInterval; // Interval for polling endpoint data transfers. |
kenbumono | 0:44619612f575 | 178 | } EndpointDescriptor; |
kenbumono | 0:44619612f575 | 179 | |
kenbumono | 0:44619612f575 | 180 | typedef struct { |
kenbumono | 0:44619612f575 | 181 | u8 bLength; |
kenbumono | 0:44619612f575 | 182 | u8 bDescriptorType; |
kenbumono | 0:44619612f575 | 183 | u16 bcdHID; |
kenbumono | 0:44619612f575 | 184 | u8 bCountryCode; |
kenbumono | 0:44619612f575 | 185 | u8 bNumDescriptors; |
kenbumono | 0:44619612f575 | 186 | u8 bDescriptorType2; |
kenbumono | 0:44619612f575 | 187 | u16 wDescriptorLength; |
kenbumono | 0:44619612f575 | 188 | } HIDDescriptor; |
kenbumono | 0:44619612f575 | 189 | |
kenbumono | 0:44619612f575 | 190 | //============================================================================ |
kenbumono | 0:44619612f575 | 191 | //============================================================================ |
kenbumono | 0:44619612f575 | 192 | |
kenbumono | 0:44619612f575 | 193 | |
kenbumono | 0:44619612f575 | 194 | void USBInit(); |
kenbumono | 0:44619612f575 | 195 | void USBLoop(); |
kenbumono | 0:44619612f575 | 196 | u8* USBGetBuffer(u32* len); |
kenbumono | 0:44619612f575 | 197 | |
kenbumono | 0:44619612f575 | 198 | // Optional callback for transfers, called at interrupt time |
kenbumono | 0:44619612f575 | 199 | typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); |
kenbumono | 0:44619612f575 | 200 | |
kenbumono | 0:44619612f575 | 201 | // Transfers |
kenbumono | 0:44619612f575 | 202 | int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
kenbumono | 0:44619612f575 | 203 | int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
kenbumono | 0:44619612f575 | 204 | int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); |
kenbumono | 0:44619612f575 | 205 | |
kenbumono | 0:44619612f575 | 206 | // Standard Device methods |
kenbumono | 0:44619612f575 | 207 | int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); |
kenbumono | 0:44619612f575 | 208 | int GetString(int device, int index, char* dst, int length); |
kenbumono | 0:44619612f575 | 209 | int SetAddress(int device, int new_addr); |
kenbumono | 0:44619612f575 | 210 | int SetConfiguration(int device, int configNum); |
kenbumono | 0:44619612f575 | 211 | int SetInterface(int device, int ifNum, int altNum); |
kenbumono | 0:44619612f575 | 212 | |
kenbumono | 0:44619612f575 | 213 | // Implemented to notify app of the arrival of a device |
kenbumono | 0:44619612f575 | 214 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); |
kenbumono | 0:44619612f575 | 215 | |
kenbumono | 0:44619612f575 | 216 | #endif |