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