[136] | 1 | /*
|
---|
[224] | 2 | Copyright (c) 2014 Arduino LLC. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 12 | See the GNU Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
[136] | 17 | */
|
---|
[224] | 18 | /*
|
---|
| 19 | **Modified 04/04/2016 by Arduino.org development team
|
---|
| 20 | */
|
---|
[136] | 21 |
|
---|
[224] | 22 |
|
---|
[136] | 23 | #ifndef __USBCORE_H__
|
---|
| 24 | #define __USBCORE_H__
|
---|
| 25 |
|
---|
| 26 | // Standard requests
|
---|
| 27 | #define GET_STATUS 0
|
---|
| 28 | #define CLEAR_FEATURE 1
|
---|
| 29 | #define SET_FEATURE 3
|
---|
| 30 | #define SET_ADDRESS 5
|
---|
| 31 | #define GET_DESCRIPTOR 6
|
---|
| 32 | #define SET_DESCRIPTOR 7
|
---|
| 33 | #define GET_CONFIGURATION 8
|
---|
| 34 | #define SET_CONFIGURATION 9
|
---|
| 35 | #define GET_INTERFACE 10
|
---|
| 36 | #define SET_INTERFACE 11
|
---|
| 37 |
|
---|
[224] | 38 | // bEndpointAddress in Endpoint Descriptor
|
---|
| 39 | #define USB_ENDPOINT_DIRECTION_MASK 0x80
|
---|
| 40 | #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
|
---|
| 41 | #define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
|
---|
[136] | 42 |
|
---|
[224] | 43 | #define USB_ENDPOINTS 7
|
---|
| 44 |
|
---|
| 45 | #define USB_ENDPOINT_TYPE_MASK 0x03
|
---|
| 46 | #define USB_ENDPOINT_TYPE_CONTROL 0x00
|
---|
| 47 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
|
---|
| 48 | #define USB_ENDPOINT_TYPE_BULK 0x02
|
---|
| 49 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03
|
---|
| 50 |
|
---|
[136] | 51 | // bmRequestType
|
---|
| 52 | #define REQUEST_HOSTTODEVICE 0x00
|
---|
| 53 | #define REQUEST_DEVICETOHOST 0x80
|
---|
| 54 | #define REQUEST_DIRECTION 0x80
|
---|
| 55 |
|
---|
| 56 | #define REQUEST_STANDARD 0x00
|
---|
| 57 | #define REQUEST_CLASS 0x20
|
---|
| 58 | #define REQUEST_VENDOR 0x40
|
---|
| 59 | #define REQUEST_TYPE 0x60
|
---|
| 60 |
|
---|
| 61 | #define REQUEST_DEVICE 0x00
|
---|
| 62 | #define REQUEST_INTERFACE 0x01
|
---|
| 63 | #define REQUEST_ENDPOINT 0x02
|
---|
| 64 | #define REQUEST_OTHER 0x03
|
---|
| 65 | #define REQUEST_RECIPIENT 0x1F
|
---|
| 66 |
|
---|
[224] | 67 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_CLASS | REQUEST_INTERFACE)
|
---|
| 68 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE | REQUEST_CLASS | REQUEST_INTERFACE)
|
---|
| 69 | #define REQUEST_DEVICETOHOST_STANDARD_INTERFACE (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_INTERFACE)
|
---|
[136] | 70 |
|
---|
| 71 | // Class requests
|
---|
| 72 |
|
---|
| 73 | #define CDC_SET_LINE_CODING 0x20
|
---|
| 74 | #define CDC_GET_LINE_CODING 0x21
|
---|
| 75 | #define CDC_SET_CONTROL_LINE_STATE 0x22
|
---|
[224] | 76 | #define CDC_SEND_BREAK 0x23
|
---|
[136] | 77 |
|
---|
| 78 | #define MSC_RESET 0xFF
|
---|
| 79 | #define MSC_GET_MAX_LUN 0xFE
|
---|
| 80 |
|
---|
| 81 | #define HID_GET_REPORT 0x01
|
---|
| 82 | #define HID_GET_IDLE 0x02
|
---|
| 83 | #define HID_GET_PROTOCOL 0x03
|
---|
| 84 | #define HID_SET_REPORT 0x09
|
---|
| 85 | #define HID_SET_IDLE 0x0A
|
---|
| 86 | #define HID_SET_PROTOCOL 0x0B
|
---|
| 87 |
|
---|
| 88 | // Descriptors
|
---|
| 89 |
|
---|
| 90 | // #define USB_DEVICE_DESC_SIZE 18
|
---|
| 91 | // #define USB_CONFIGUARTION_DESC_SIZE 9
|
---|
| 92 | // #define USB_INTERFACE_DESC_SIZE 9
|
---|
| 93 | // #define USB_ENDPOINT_DESC_SIZE 7
|
---|
| 94 |
|
---|
| 95 | #define USB_DEVICE_DESCRIPTOR_TYPE 1
|
---|
| 96 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
|
---|
| 97 | #define USB_STRING_DESCRIPTOR_TYPE 3
|
---|
| 98 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4
|
---|
| 99 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5
|
---|
| 100 | #define USB_DEVICE_QUALIFIER 6
|
---|
| 101 | #define USB_OTHER_SPEED_CONFIGURATION 7
|
---|
| 102 |
|
---|
| 103 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
|
---|
| 104 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
|
---|
| 105 | #define USB_DEVICE_CLASS_STORAGE 0x08
|
---|
| 106 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
|
---|
| 107 |
|
---|
| 108 | #define USB_CONFIG_POWERED_MASK 0x40
|
---|
| 109 | #define USB_CONFIG_BUS_POWERED 0x80
|
---|
| 110 | #define USB_CONFIG_SELF_POWERED 0xC0
|
---|
| 111 | #define USB_CONFIG_REMOTE_WAKEUP 0x20
|
---|
| 112 |
|
---|
| 113 | // bMaxPower in Configuration Descriptor
|
---|
| 114 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2)
|
---|
| 115 |
|
---|
| 116 | #define CDC_V1_10 0x0110
|
---|
| 117 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
|
---|
| 118 |
|
---|
| 119 | #define CDC_CALL_MANAGEMENT 0x01
|
---|
| 120 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02
|
---|
| 121 | #define CDC_HEADER 0x00
|
---|
| 122 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
|
---|
| 123 | #define CDC_UNION 0x06
|
---|
| 124 | #define CDC_CS_INTERFACE 0x24
|
---|
| 125 | #define CDC_CS_ENDPOINT 0x25
|
---|
| 126 | #define CDC_DATA_INTERFACE_CLASS 0x0A
|
---|
| 127 |
|
---|
| 128 | #define MSC_SUBCLASS_SCSI 0x06
|
---|
| 129 | #define MSC_PROTOCOL_BULK_ONLY 0x50
|
---|
| 130 |
|
---|
| 131 | _Pragma("pack(1)")
|
---|
| 132 |
|
---|
| 133 | // Device
|
---|
| 134 | typedef struct {
|
---|
| 135 | uint8_t len; // 18
|
---|
| 136 | uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
|
---|
| 137 | uint16_t usbVersion; // 0x200
|
---|
| 138 | uint8_t deviceClass;
|
---|
| 139 | uint8_t deviceSubClass;
|
---|
| 140 | uint8_t deviceProtocol;
|
---|
| 141 | uint8_t packetSize0; // Packet 0
|
---|
| 142 | uint16_t idVendor;
|
---|
| 143 | uint16_t idProduct;
|
---|
| 144 | uint16_t deviceVersion; // 0x100
|
---|
| 145 | uint8_t iManufacturer;
|
---|
| 146 | uint8_t iProduct;
|
---|
| 147 | uint8_t iSerialNumber;
|
---|
| 148 | uint8_t bNumConfigurations;
|
---|
| 149 | } DeviceDescriptor;
|
---|
| 150 |
|
---|
| 151 | // Config
|
---|
| 152 | typedef struct {
|
---|
| 153 | uint8_t len; // 9
|
---|
| 154 | uint8_t dtype; // 2
|
---|
| 155 | uint16_t clen; // total length
|
---|
| 156 | uint8_t numInterfaces;
|
---|
| 157 | uint8_t config;
|
---|
| 158 | uint8_t iconfig;
|
---|
| 159 | uint8_t attributes;
|
---|
| 160 | uint8_t maxPower;
|
---|
| 161 | } ConfigDescriptor;
|
---|
| 162 |
|
---|
| 163 | // String
|
---|
| 164 |
|
---|
| 165 | // Interface
|
---|
| 166 | typedef struct
|
---|
| 167 | {
|
---|
| 168 | uint8_t len; // 9
|
---|
| 169 | uint8_t dtype; // 4
|
---|
| 170 | uint8_t number;
|
---|
| 171 | uint8_t alternate;
|
---|
| 172 | uint8_t numEndpoints;
|
---|
| 173 | uint8_t interfaceClass;
|
---|
| 174 | uint8_t interfaceSubClass;
|
---|
| 175 | uint8_t protocol;
|
---|
| 176 | uint8_t iInterface;
|
---|
| 177 | } InterfaceDescriptor;
|
---|
| 178 |
|
---|
| 179 | // Endpoint
|
---|
| 180 | typedef struct
|
---|
| 181 | {
|
---|
| 182 | uint8_t len; // 7
|
---|
| 183 | uint8_t dtype; // 5
|
---|
| 184 | uint8_t addr;
|
---|
| 185 | uint8_t attr;
|
---|
| 186 | uint16_t packetSize;
|
---|
| 187 | uint8_t interval;
|
---|
| 188 | } EndpointDescriptor;
|
---|
| 189 |
|
---|
| 190 | // Interface Association Descriptor
|
---|
| 191 | // Used to bind 2 interfaces together in CDC compostite device
|
---|
| 192 | typedef struct
|
---|
| 193 | {
|
---|
| 194 | uint8_t len; // 8
|
---|
| 195 | uint8_t dtype; // 11
|
---|
| 196 | uint8_t firstInterface;
|
---|
| 197 | uint8_t interfaceCount;
|
---|
| 198 | uint8_t functionClass;
|
---|
| 199 | uint8_t funtionSubClass;
|
---|
| 200 | uint8_t functionProtocol;
|
---|
| 201 | uint8_t iInterface;
|
---|
| 202 | } IADDescriptor;
|
---|
| 203 |
|
---|
| 204 | // CDC CS interface descriptor
|
---|
| 205 | typedef struct
|
---|
| 206 | {
|
---|
| 207 | uint8_t len; // 5
|
---|
| 208 | uint8_t dtype; // 0x24
|
---|
| 209 | uint8_t subtype;
|
---|
| 210 | uint8_t d0;
|
---|
| 211 | uint8_t d1;
|
---|
| 212 | } CDCCSInterfaceDescriptor;
|
---|
| 213 |
|
---|
| 214 | typedef struct
|
---|
| 215 | {
|
---|
| 216 | uint8_t len; // 4
|
---|
| 217 | uint8_t dtype; // 0x24
|
---|
| 218 | uint8_t subtype;
|
---|
| 219 | uint8_t d0;
|
---|
| 220 | } CDCCSInterfaceDescriptor4;
|
---|
| 221 |
|
---|
| 222 | typedef struct
|
---|
| 223 | {
|
---|
| 224 | uint8_t len;
|
---|
| 225 | uint8_t dtype; // 0x24
|
---|
| 226 | uint8_t subtype; // 1
|
---|
| 227 | uint8_t bmCapabilities;
|
---|
| 228 | uint8_t bDataInterface;
|
---|
| 229 | } CMFunctionalDescriptor;
|
---|
| 230 |
|
---|
| 231 | typedef struct
|
---|
| 232 | {
|
---|
| 233 | uint8_t len;
|
---|
| 234 | uint8_t dtype; // 0x24
|
---|
| 235 | uint8_t subtype; // 1
|
---|
| 236 | uint8_t bmCapabilities;
|
---|
| 237 | } ACMFunctionalDescriptor;
|
---|
| 238 |
|
---|
| 239 | typedef struct
|
---|
| 240 | {
|
---|
| 241 | // IAD
|
---|
| 242 | IADDescriptor iad; // Only needed on compound device
|
---|
| 243 | // Control
|
---|
| 244 | InterfaceDescriptor cif;
|
---|
| 245 | CDCCSInterfaceDescriptor header;
|
---|
| 246 | ACMFunctionalDescriptor controlManagement; // ACM
|
---|
| 247 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
|
---|
| 248 | CMFunctionalDescriptor callManagement; // Call Management
|
---|
| 249 | EndpointDescriptor cifin;
|
---|
| 250 |
|
---|
| 251 | // Data
|
---|
| 252 | InterfaceDescriptor dif;
|
---|
| 253 | EndpointDescriptor in;
|
---|
| 254 | EndpointDescriptor out;
|
---|
| 255 | } CDCDescriptor;
|
---|
| 256 |
|
---|
| 257 | typedef struct
|
---|
| 258 | {
|
---|
| 259 | InterfaceDescriptor msc;
|
---|
| 260 | EndpointDescriptor in;
|
---|
| 261 | EndpointDescriptor out;
|
---|
| 262 | } MSCDescriptor;
|
---|
| 263 |
|
---|
[224] | 264 |
|
---|
[136] | 265 | typedef struct
|
---|
| 266 | {
|
---|
| 267 | uint8_t len; // 9
|
---|
| 268 | uint8_t dtype; // 0x21
|
---|
| 269 | uint8_t addr;
|
---|
| 270 | uint8_t versionL; // 0x101
|
---|
| 271 | uint8_t versionH; // 0x101
|
---|
| 272 | uint8_t country;
|
---|
| 273 | uint8_t desctype; // 0x22 report
|
---|
| 274 | uint8_t descLenL;
|
---|
| 275 | uint8_t descLenH;
|
---|
| 276 | } HIDDescDescriptor;
|
---|
| 277 |
|
---|
| 278 | typedef struct
|
---|
| 279 | {
|
---|
| 280 | InterfaceDescriptor hid;
|
---|
| 281 | HIDDescDescriptor desc;
|
---|
| 282 | EndpointDescriptor in;
|
---|
| 283 | } HIDDescriptor;
|
---|
| 284 |
|
---|
| 285 | _Pragma("pack()")
|
---|
| 286 |
|
---|
| 287 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
|
---|
[224] | 288 | { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
|
---|
[136] | 289 | /* Table 9-8. Standard Device Descriptor
|
---|
| 290 | * bLength, bDescriptorType, bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, bMaxPacketSize0,
|
---|
| 291 | * idVendor, idProduct, bcdDevice, iManufacturer, iProduct, iSerialNumber, bNumConfigurations */
|
---|
| 292 |
|
---|
| 293 | #define D_CONFIG(_totalLength,_interfaces) \
|
---|
[224] | 294 | { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
|
---|
[136] | 295 | /* Table 9-10. Standard Configuration Descriptor
|
---|
| 296 | * bLength, bDescriptorType, wTotalLength, bNumInterfaces, bConfigurationValue, iConfiguration
|
---|
| 297 | * bmAttributes, bMaxPower */
|
---|
[224] | 298 |
|
---|
[136] | 299 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
|
---|
| 300 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
|
---|
| 301 | /* Table 9-12. Standard Interface Descriptor
|
---|
| 302 | * bLength, bDescriptorType, bInterfaceNumber, bAlternateSetting, bNumEndpoints, bInterfaceClass,
|
---|
| 303 | * bInterfaceSubClass, bInterfaceProtocol, iInterface */
|
---|
[224] | 304 |
|
---|
[136] | 305 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
|
---|
| 306 | { 7, 5, _addr,_attr,_packetSize, _interval }
|
---|
| 307 | /* Table 9-13. Standard Endpoint Descriptor
|
---|
| 308 | * bLength, bDescriptorType, bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval */
|
---|
| 309 |
|
---|
| 310 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
|
---|
| 311 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
|
---|
[224] | 312 | /* iadclasscode_r10.pdf, Table 9\96Z. Standard Interface Association Descriptor
|
---|
[136] | 313 | * bLength, bDescriptorType, bFirstInterface, bInterfaceCount, bFunctionClass, bFunctionSubClass, bFunctionProtocol, iFunction */
|
---|
| 314 | #define D_HIDREPORT(_descriptorLength) \
|
---|
| 315 | { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
|
---|
| 316 | /* HID1_11.pdf E.8 HID Descriptor (Mouse)
|
---|
| 317 | * bLength, bDescriptorType, bcdHID, bCountryCode, bNumDescriptors, bDescriptorType, wItemLength */
|
---|
[224] | 318 |
|
---|
[136] | 319 | // Functional Descriptor General Format
|
---|
| 320 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
|
---|
| 321 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
---|
| 322 | /* bFunctionLength, bDescriptorType, bDescriptorSubtype, function specific data0, functional specific data N-1
|
---|
| 323 | * CS_INTERFACE 24h */
|
---|
| 324 |
|
---|
| 325 | #endif
|
---|