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