| 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 |
#ifndef USB_CONFIG_POWER |
| 101 |
#define USB_CONFIG_POWER (500) |
| 102 |
#endif |
| 103 |
|
| 104 |
// bEndpointAddress in Endpoint Descriptor |
| 105 |
#define USB_ENDPOINT_DIRECTION_MASK 0x80 |
| 106 |
#define USB_ENDPOINT_OUT(addr) (lowByte((addr) | 0x00)) |
| 107 |
#define USB_ENDPOINT_IN(addr) (lowByte((addr) | 0x80)) |
| 108 |
|
| 109 |
#define USB_ENDPOINT_TYPE_MASK 0x03 |
| 110 |
#define USB_ENDPOINT_TYPE_CONTROL 0x00 |
| 111 |
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 |
| 112 |
#define USB_ENDPOINT_TYPE_BULK 0x02 |
| 113 |
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03 |
| 114 |
|
| 115 |
#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF) |
| 116 |
|
| 117 |
#define CDC_V1_10 0x0110 |
| 118 |
#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 |
| 119 |
|
| 120 |
#define CDC_CALL_MANAGEMENT 0x01 |
| 121 |
#define CDC_ABSTRACT_CONTROL_MODEL 0x02 |
| 122 |
#define CDC_HEADER 0x00 |
| 123 |
#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 |
| 124 |
#define CDC_UNION 0x06 |
| 125 |
#define CDC_CS_INTERFACE 0x24 |
| 126 |
#define CDC_CS_ENDPOINT 0x25 |
| 127 |
#define CDC_DATA_INTERFACE_CLASS 0x0A |
| 128 |
|
| 129 |
#define MSC_SUBCLASS_SCSI 0x06 |
| 130 |
#define MSC_PROTOCOL_BULK_ONLY 0x50 |
| 131 |
|
| 132 |
#ifndef USB_VERSION |
| 133 |
#define USB_VERSION 0x200 |
| 134 |
#endif |
| 135 |
|
| 136 |
// Device |
| 137 |
typedef struct { |
| 138 |
u8 len; // 18 |
| 139 |
u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE |
| 140 |
u16 usbVersion; // 0x200 or 0x210 |
| 141 |
u8 deviceClass; |
| 142 |
u8 deviceSubClass; |
| 143 |
u8 deviceProtocol; |
| 144 |
u8 packetSize0; // Packet 0 |
| 145 |
u16 idVendor; |
| 146 |
u16 idProduct; |
| 147 |
u16 deviceVersion; // 0x100 |
| 148 |
u8 iManufacturer; |
| 149 |
u8 iProduct; |
| 150 |
u8 iSerialNumber; |
| 151 |
u8 bNumConfigurations; |
| 152 |
} DeviceDescriptor; |
| 153 |
|
| 154 |
// Config |
| 155 |
typedef struct { |
| 156 |
u8 len; // 9 |
| 157 |
u8 dtype; // 2 |
| 158 |
u16 clen; // total length |
| 159 |
u8 numInterfaces; |
| 160 |
u8 config; |
| 161 |
u8 iconfig; |
| 162 |
u8 attributes; |
| 163 |
u8 maxPower; |
| 164 |
} ConfigDescriptor; |
| 165 |
|
| 166 |
// String |
| 167 |
|
| 168 |
// Interface |
| 169 |
typedef struct |
| 170 |
{ |
| 171 |
u8 len; // 9 |
| 172 |
u8 dtype; // 4 |
| 173 |
u8 number; |
| 174 |
u8 alternate; |
| 175 |
u8 numEndpoints; |
| 176 |
u8 interfaceClass; |
| 177 |
u8 interfaceSubClass; |
| 178 |
u8 protocol; |
| 179 |
u8 iInterface; |
| 180 |
} InterfaceDescriptor; |
| 181 |
|
| 182 |
// Endpoint |
| 183 |
typedef struct |
| 184 |
{ |
| 185 |
u8 len; // 7 |
| 186 |
u8 dtype; // 5 |
| 187 |
u8 addr; |
| 188 |
u8 attr; |
| 189 |
u16 packetSize; |
| 190 |
u8 interval; |
| 191 |
} EndpointDescriptor; |
| 192 |
|
| 193 |
// Interface Association Descriptor |
| 194 |
// Used to bind 2 interfaces together in CDC composite device |
| 195 |
typedef struct |
| 196 |
{ |
| 197 |
u8 len; // 8 |
| 198 |
u8 dtype; // 11 |
| 199 |
u8 firstInterface; |
| 200 |
u8 interfaceCount; |
| 201 |
u8 functionClass; |
| 202 |
u8 funtionSubClass; |
| 203 |
u8 functionProtocol; |
| 204 |
u8 iInterface; |
| 205 |
} IADDescriptor; |
| 206 |
|
| 207 |
// CDC CS interface descriptor |
| 208 |
typedef struct |
| 209 |
{ |
| 210 |
u8 len; // 5 |
| 211 |
u8 dtype; // 0x24 |
| 212 |
u8 subtype; |
| 213 |
u8 d0; |
| 214 |
u8 d1; |
| 215 |
} CDCCSInterfaceDescriptor; |
| 216 |
|
| 217 |
typedef struct |
| 218 |
{ |
| 219 |
u8 len; // 4 |
| 220 |
u8 dtype; // 0x24 |
| 221 |
u8 subtype; |
| 222 |
u8 d0; |
| 223 |
} CDCCSInterfaceDescriptor4; |
| 224 |
|
| 225 |
typedef struct |
| 226 |
{ |
| 227 |
u8 len; |
| 228 |
u8 dtype; // 0x24 |
| 229 |
u8 subtype; // 1 |
| 230 |
u8 bmCapabilities; |
| 231 |
u8 bDataInterface; |
| 232 |
} CMFunctionalDescriptor; |
| 233 |
|
| 234 |
typedef struct |
| 235 |
{ |
| 236 |
u8 len; |
| 237 |
u8 dtype; // 0x24 |
| 238 |
u8 subtype; // 1 |
| 239 |
u8 bmCapabilities; |
| 240 |
} ACMFunctionalDescriptor; |
| 241 |
|
| 242 |
typedef struct |
| 243 |
{ |
| 244 |
// IAD |
| 245 |
IADDescriptor iad; // Only needed on compound device |
| 246 |
|
| 247 |
// Control |
| 248 |
InterfaceDescriptor cif; // |
| 249 |
CDCCSInterfaceDescriptor header; |
| 250 |
CMFunctionalDescriptor callManagement; // Call Management |
| 251 |
ACMFunctionalDescriptor controlManagement; // ACM |
| 252 |
CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION |
| 253 |
EndpointDescriptor cifin; |
| 254 |
|
| 255 |
// Data |
| 256 |
InterfaceDescriptor dif; |
| 257 |
EndpointDescriptor in; |
| 258 |
EndpointDescriptor out; |
| 259 |
} CDCDescriptor; |
| 260 |
|
| 261 |
typedef struct |
| 262 |
{ |
| 263 |
InterfaceDescriptor msc; |
| 264 |
EndpointDescriptor in; |
| 265 |
EndpointDescriptor out; |
| 266 |
} MSCDescriptor; |
| 267 |
|
| 268 |
|
| 269 |
#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ |
| 270 |
{ 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } |
| 271 |
|
| 272 |
#define D_CONFIG(_totalLength,_interfaces) \ |
| 273 |
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(USB_CONFIG_POWER) } |
| 274 |
|
| 275 |
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ |
| 276 |
{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } |
| 277 |
|
| 278 |
#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ |
| 279 |
{ 7, 5, _addr,_attr,_packetSize, _interval } |
| 280 |
|
| 281 |
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ |
| 282 |
{ 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } |
| 283 |
|
| 284 |
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } |
| 285 |
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } |
| 286 |
|
| 287 |
// Bootloader related fields |
| 288 |
// Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten |
| 289 |
// by the running sketch before to actual reboot). |
| 290 |
// Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both |
| 291 |
// the usafe and the safe location. |
| 292 |
#ifndef MAGIC_KEY |
| 293 |
#define MAGIC_KEY 0x7777 |
| 294 |
#endif |
| 295 |
|
| 296 |
#ifndef MAGIC_KEY_POS |
| 297 |
#define MAGIC_KEY_POS 0x0800 |
| 298 |
#endif |
| 299 |
|
| 300 |
#ifndef NEW_LUFA_SIGNATURE |
| 301 |
#define NEW_LUFA_SIGNATURE 0xDCFB |
| 302 |
#endif |
| 303 |
|
| 304 |
#endif |