1 |
|
2 | // Copyright (c) 2010, Peter Barrett |
3 | /* |
4 | ** Permission to use, copy, modify, and/or distribute this software for |
5 | ** any purpose with or without fee is hereby granted, provided that the |
6 | ** above copyright notice and this permission notice appear in all copies. |
7 | ** |
8 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
9 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
10 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR |
11 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES |
12 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
13 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
14 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
15 | ** SOFTWARE. |
16 | */ |
17 |
|
18 | #ifndef __USBCORE_H__ |
19 | #define __USBCORE_H__ |
20 |
|
21 | #include "USBAPI.h" |
22 |
|
23 | // Standard requests |
24 | #define GET_STATUS 0 |
25 | #define CLEAR_FEATURE 1 |
26 | #define SET_FEATURE 3 |
27 | #define SET_ADDRESS 5 |
28 | #define GET_DESCRIPTOR 6 |
29 | #define SET_DESCRIPTOR 7 |
30 | #define GET_CONFIGURATION 8 |
31 | #define SET_CONFIGURATION 9 |
32 | #define GET_INTERFACE 10 |
33 | #define SET_INTERFACE 11 |
34 |
|
35 |
|
36 | // bmRequestType |
37 | #define REQUEST_HOSTTODEVICE 0x00 |
38 | #define REQUEST_DEVICETOHOST 0x80 |
39 | #define REQUEST_DIRECTION 0x80 |
40 |
|
41 | #define REQUEST_STANDARD 0x00 |
42 | #define REQUEST_CLASS 0x20 |
43 | #define REQUEST_VENDOR 0x40 |
44 | #define REQUEST_TYPE 0x60 |
45 |
|
46 | #define REQUEST_DEVICE 0x00 |
47 | #define REQUEST_INTERFACE 0x01 |
48 | #define REQUEST_ENDPOINT 0x02 |
49 | #define REQUEST_OTHER 0x03 |
50 | #define REQUEST_RECIPIENT 0x03 |
51 |
|
52 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_CLASS | REQUEST_INTERFACE) |
53 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE | REQUEST_CLASS | REQUEST_INTERFACE) |
54 | #define REQUEST_DEVICETOHOST_STANDARD_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_INTERFACE) |
55 |
|
56 | // Class requests |
57 |
|
58 | #define CDC_SET_LINE_CODING 0x20 |
59 | #define CDC_GET_LINE_CODING 0x21 |
60 | #define CDC_SET_CONTROL_LINE_STATE 0x22 |
61 | #define CDC_SEND_BREAK 0x23 |
62 |
|
63 | #define MSC_RESET 0xFF |
64 | #define MSC_GET_MAX_LUN 0xFE |
65 |
|
66 | // Descriptors |
67 |
|
68 | #define USB_DEVICE_DESC_SIZE 18 |
69 | #define USB_CONFIGUARTION_DESC_SIZE 9 |
70 | #define USB_INTERFACE_DESC_SIZE 9 |
71 | #define USB_ENDPOINT_DESC_SIZE 7 |
72 |
|
73 | #define USB_DEVICE_DESCRIPTOR_TYPE 1 |
74 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 |
75 | #define USB_STRING_DESCRIPTOR_TYPE 3 |
76 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4 |
77 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5 |
78 |
|
79 | // usb_20.pdf Table 9.6 Standard Feature Selectors |
80 | #define DEVICE_REMOTE_WAKEUP 1 |
81 | #define ENDPOINT_HALT 2 |
82 | #define TEST_MODE 3 |
83 |
|
84 | // usb_20.pdf Figure 9-4. Information Returned by a GetStatus() Request to a Device |
85 | #define FEATURE_SELFPOWERED_ENABLED (1 << 0) |
86 | #define FEATURE_REMOTE_WAKEUP_ENABLED (1 << 1) |
87 |
|
88 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 |
89 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 |
90 | #define USB_DEVICE_CLASS_STORAGE 0x08 |
91 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF |
92 |
|
93 | #define USB_CONFIG_POWERED_MASK 0x40 |
94 | #define USB_CONFIG_BUS_POWERED 0x80 |
95 | #define USB_CONFIG_SELF_POWERED 0xC0 |
96 | #define USB_CONFIG_REMOTE_WAKEUP 0x20 |
97 |
|
98 | // bMaxPower in Configuration Descriptor |
99 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2) |
100 |
|
101 | // bEndpointAddress in Endpoint Descriptor |
102 | #define USB_ENDPOINT_DIRECTION_MASK 0x80 |
103 | #define USB_ENDPOINT_OUT(addr) (lowByte((addr) | 0x00)) |
104 | #define USB_ENDPOINT_IN(addr) (lowByte((addr) | 0x80)) |
105 |
|
106 | #define USB_ENDPOINT_TYPE_MASK 0x03 |
107 | #define USB_ENDPOINT_TYPE_CONTROL 0x00 |
108 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 |
109 | #define USB_ENDPOINT_TYPE_BULK 0x02 |
110 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03 |
111 |
|
112 | #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF) |
113 |
|
114 | #define CDC_V1_10 0x0110 |
115 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 |
116 |
|
117 | #define CDC_CALL_MANAGEMENT 0x01 |
118 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02 |
119 | #define CDC_HEADER 0x00 |
120 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 |
121 | #define CDC_UNION 0x06 |
122 | #define CDC_CS_INTERFACE 0x24 |
123 | #define CDC_CS_ENDPOINT 0x25 |
124 | #define CDC_DATA_INTERFACE_CLASS 0x0A |
125 |
|
126 | #define MSC_SUBCLASS_SCSI 0x06 |
127 | #define MSC_PROTOCOL_BULK_ONLY 0x50 |
128 |
|
129 | #ifndef USB_VERSION |
130 | #define USB_VERSION 0x200 |
131 | #endif |
132 |
|
133 | // Device |
134 | typedef struct { |
135 | u8 len; // 18 |
136 | u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE |
137 | u16 usbVersion; // 0x200 or 0x210 |
138 | u8 deviceClass; |
139 | u8 deviceSubClass; |
140 | u8 deviceProtocol; |
141 | u8 packetSize0; // Packet 0 |
142 | u16 idVendor; |
143 | u16 idProduct; |
144 | u16 deviceVersion; // 0x100 |
145 | u8 iManufacturer; |
146 | u8 iProduct; |
147 | u8 iSerialNumber; |
148 | u8 bNumConfigurations; |
149 | } DeviceDescriptor; |
150 |
|
151 | // Config |
152 | typedef struct { |
153 | u8 len; // 9 |
154 | u8 dtype; // 2 |
155 | u16 clen; // total length |
156 | u8 numInterfaces; |
157 | u8 config; |
158 | u8 iconfig; |
159 | u8 attributes; |
160 | u8 maxPower; |
161 | } ConfigDescriptor; |
162 |
|
163 | // String |
164 |
|
165 | // Interface |
166 | typedef struct |
167 | { |
168 | u8 len; // 9 |
169 | u8 dtype; // 4 |
170 | u8 number; |
171 | u8 alternate; |
172 | u8 numEndpoints; |
173 | u8 interfaceClass; |
174 | u8 interfaceSubClass; |
175 | u8 protocol; |
176 | u8 iInterface; |
177 | } InterfaceDescriptor; |
178 |
|
179 | // Endpoint |
180 | typedef struct |
181 | { |
182 | u8 len; // 7 |
183 | u8 dtype; // 5 |
184 | u8 addr; |
185 | u8 attr; |
186 | u16 packetSize; |
187 | u8 interval; |
188 | } EndpointDescriptor; |
189 |
|
190 | // Interface Association Descriptor |
191 | // Used to bind 2 interfaces together in CDC compostite device |
192 | typedef struct |
193 | { |
194 | u8 len; // 8 |
195 | u8 dtype; // 11 |
196 | u8 firstInterface; |
197 | u8 interfaceCount; |
198 | u8 functionClass; |
199 | u8 funtionSubClass; |
200 | u8 functionProtocol; |
201 | u8 iInterface; |
202 | } IADDescriptor; |
203 |
|
204 | // CDC CS interface descriptor |
205 | typedef struct |
206 | { |
207 | u8 len; // 5 |
208 | u8 dtype; // 0x24 |
209 | u8 subtype; |
210 | u8 d0; |
211 | u8 d1; |
212 | } CDCCSInterfaceDescriptor; |
213 |
|
214 | typedef struct |
215 | { |
216 | u8 len; // 4 |
217 | u8 dtype; // 0x24 |
218 | u8 subtype; |
219 | u8 d0; |
220 | } CDCCSInterfaceDescriptor4; |
221 |
|
222 | typedef struct |
223 | { |
224 | u8 len; |
225 | u8 dtype; // 0x24 |
226 | u8 subtype; // 1 |
227 | u8 bmCapabilities; |
228 | u8 bDataInterface; |
229 | } CMFunctionalDescriptor; |
230 | |
231 | typedef struct |
232 | { |
233 | u8 len; |
234 | u8 dtype; // 0x24 |
235 | u8 subtype; // 1 |
236 | u8 bmCapabilities; |
237 | } ACMFunctionalDescriptor; |
238 |
|
239 | typedef struct |
240 | { |
241 | // IAD |
242 | IADDescriptor iad; // Only needed on compound device |
243 |
|
244 | // Control |
245 | InterfaceDescriptor cif; // |
246 | CDCCSInterfaceDescriptor header; |
247 | CMFunctionalDescriptor callManagement; // Call Management |
248 | ACMFunctionalDescriptor controlManagement; // ACM |
249 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION |
250 | EndpointDescriptor cifin; |
251 |
|
252 | // Data |
253 | InterfaceDescriptor dif; |
254 | EndpointDescriptor in; |
255 | EndpointDescriptor out; |
256 | } CDCDescriptor; |
257 |
|
258 | typedef struct |
259 | { |
260 | InterfaceDescriptor msc; |
261 | EndpointDescriptor in; |
262 | EndpointDescriptor out; |
263 | } MSCDescriptor; |
264 |
|
265 |
|
266 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ |
267 | { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } |
268 |
|
269 | #define D_CONFIG(_totalLength,_interfaces) \ |
270 | { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(500) } |
271 |
|
272 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ |
273 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } |
274 |
|
275 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ |
276 | { 7, 5, _addr,_attr,_packetSize, _interval } |
277 |
|
278 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ |
279 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } |
280 |
|
281 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } |
282 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } |
283 |
|
284 | // Bootloader related fields |
285 | // Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten |
286 | // by the running sketch before to actual reboot). |
287 | // Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both |
288 | // the usafe and the safe location. |
289 | #ifndef MAGIC_KEY |
290 | #define MAGIC_KEY 0x7777 |
291 | #endif |
292 |
|
293 | #ifndef MAGIC_KEY_POS |
294 | #define MAGIC_KEY_POS 0x0800 |
295 | #endif |
296 |
|
297 | #ifndef NEW_LUFA_SIGNATURE |
298 | #define NEW_LUFA_SIGNATURE 0xDCFB |
299 | #endif |
300 |
|
301 | #endif |
302 |
|