Dependents:   Test_Wiimote

Committer:
bediyap
Date:
Sat Dec 17 02:17:39 2011 +0000
Revision:
0:f6f434d9a03a
connect to particular wii

Who changed what in which revision?

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