[136] | 1 | // Copyright (c) 2010, Peter Barrett
|
---|
| 2 | /*
|
---|
| 3 | ** Permission to use, copy, modify, and/or distribute this software for
|
---|
| 4 | ** any purpose with or without fee is hereby granted, provided that the
|
---|
| 5 | ** above copyright notice and this permission notice appear in all copies.
|
---|
| 6 | **
|
---|
| 7 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
---|
| 8 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
---|
| 9 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
---|
| 10 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
---|
| 11 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
---|
| 12 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
---|
| 13 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
---|
| 14 | ** SOFTWARE.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | #ifndef __USBCORE_H__
|
---|
| 18 | #define __USBCORE_H__
|
---|
| 19 |
|
---|
| 20 | // Standard requests
|
---|
| 21 | #define GET_STATUS 0
|
---|
| 22 | #define CLEAR_FEATURE 1
|
---|
| 23 | #define SET_FEATURE 3
|
---|
| 24 | #define SET_ADDRESS 5
|
---|
| 25 | #define GET_DESCRIPTOR 6
|
---|
| 26 | #define SET_DESCRIPTOR 7
|
---|
| 27 | #define GET_CONFIGURATION 8
|
---|
| 28 | #define SET_CONFIGURATION 9
|
---|
| 29 | #define GET_INTERFACE 10
|
---|
| 30 | #define SET_INTERFACE 11
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | // bmRequestType
|
---|
| 34 | #define REQUEST_HOSTTODEVICE 0x00
|
---|
| 35 | #define REQUEST_DEVICETOHOST 0x80
|
---|
| 36 | #define REQUEST_DIRECTION 0x80
|
---|
| 37 |
|
---|
| 38 | #define REQUEST_STANDARD 0x00
|
---|
| 39 | #define REQUEST_CLASS 0x20
|
---|
| 40 | #define REQUEST_VENDOR 0x40
|
---|
| 41 | #define REQUEST_TYPE 0x60
|
---|
| 42 |
|
---|
| 43 | #define REQUEST_DEVICE 0x00
|
---|
| 44 | #define REQUEST_INTERFACE 0x01
|
---|
| 45 | #define REQUEST_ENDPOINT 0x02
|
---|
| 46 | #define REQUEST_OTHER 0x03
|
---|
| 47 | #define REQUEST_RECIPIENT 0x1F
|
---|
| 48 |
|
---|
| 49 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
|
---|
| 50 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
|
---|
| 51 |
|
---|
| 52 | // Class requests
|
---|
| 53 |
|
---|
| 54 | #define CDC_SET_LINE_CODING 0x20
|
---|
| 55 | #define CDC_GET_LINE_CODING 0x21
|
---|
| 56 | #define CDC_SET_CONTROL_LINE_STATE 0x22
|
---|
| 57 |
|
---|
| 58 | #define MSC_RESET 0xFF
|
---|
| 59 | #define MSC_GET_MAX_LUN 0xFE
|
---|
| 60 |
|
---|
| 61 | #define HID_GET_REPORT 0x01
|
---|
| 62 | #define HID_GET_IDLE 0x02
|
---|
| 63 | #define HID_GET_PROTOCOL 0x03
|
---|
| 64 | #define HID_SET_REPORT 0x09
|
---|
| 65 | #define HID_SET_IDLE 0x0A
|
---|
| 66 | #define HID_SET_PROTOCOL 0x0B
|
---|
| 67 |
|
---|
| 68 | // Descriptors
|
---|
| 69 |
|
---|
| 70 | // #define USB_DEVICE_DESC_SIZE 18
|
---|
| 71 | // #define USB_CONFIGUARTION_DESC_SIZE 9
|
---|
| 72 | // #define USB_INTERFACE_DESC_SIZE 9
|
---|
| 73 | // #define USB_ENDPOINT_DESC_SIZE 7
|
---|
| 74 |
|
---|
| 75 | #define USB_DEVICE_DESCRIPTOR_TYPE 1
|
---|
| 76 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
|
---|
| 77 | #define USB_STRING_DESCRIPTOR_TYPE 3
|
---|
| 78 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4
|
---|
| 79 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5
|
---|
| 80 | #define USB_DEVICE_QUALIFIER 6
|
---|
| 81 | #define USB_OTHER_SPEED_CONFIGURATION 7
|
---|
| 82 |
|
---|
| 83 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
|
---|
| 84 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
|
---|
| 85 | #define USB_DEVICE_CLASS_STORAGE 0x08
|
---|
| 86 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
|
---|
| 87 |
|
---|
| 88 | #define USB_CONFIG_POWERED_MASK 0x40
|
---|
| 89 | #define USB_CONFIG_BUS_POWERED 0x80
|
---|
| 90 | #define USB_CONFIG_SELF_POWERED 0xC0
|
---|
| 91 | #define USB_CONFIG_REMOTE_WAKEUP 0x20
|
---|
| 92 |
|
---|
| 93 | // bMaxPower in Configuration Descriptor
|
---|
| 94 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2)
|
---|
| 95 |
|
---|
| 96 | #define CDC_V1_10 0x0110
|
---|
| 97 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
|
---|
| 98 |
|
---|
| 99 | #define CDC_CALL_MANAGEMENT 0x01
|
---|
| 100 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02
|
---|
| 101 | #define CDC_HEADER 0x00
|
---|
| 102 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
|
---|
| 103 | #define CDC_UNION 0x06
|
---|
| 104 | #define CDC_CS_INTERFACE 0x24
|
---|
| 105 | #define CDC_CS_ENDPOINT 0x25
|
---|
| 106 | #define CDC_DATA_INTERFACE_CLASS 0x0A
|
---|
| 107 |
|
---|
| 108 | #define MSC_SUBCLASS_SCSI 0x06
|
---|
| 109 | #define MSC_PROTOCOL_BULK_ONLY 0x50
|
---|
| 110 |
|
---|
| 111 | #define HID_HID_DESCRIPTOR_TYPE 0x21
|
---|
| 112 | #define HID_REPORT_DESCRIPTOR_TYPE 0x22
|
---|
| 113 | #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
|
---|
| 114 |
|
---|
| 115 | #define TX_RX_LED_PULSE_MS 100 //----- Tx & Rx led blinking during transmission (declaration)
|
---|
| 116 |
|
---|
| 117 | _Pragma("pack(1)")
|
---|
| 118 |
|
---|
| 119 | // Device
|
---|
| 120 | typedef struct {
|
---|
| 121 | uint8_t len; // 18
|
---|
| 122 | uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
|
---|
| 123 | uint16_t usbVersion; // 0x200
|
---|
| 124 | uint8_t deviceClass;
|
---|
| 125 | uint8_t deviceSubClass;
|
---|
| 126 | uint8_t deviceProtocol;
|
---|
| 127 | uint8_t packetSize0; // Packet 0
|
---|
| 128 | uint16_t idVendor;
|
---|
| 129 | uint16_t idProduct;
|
---|
| 130 | uint16_t deviceVersion; // 0x100
|
---|
| 131 | uint8_t iManufacturer;
|
---|
| 132 | uint8_t iProduct;
|
---|
| 133 | uint8_t iSerialNumber;
|
---|
| 134 | uint8_t bNumConfigurations;
|
---|
| 135 | } DeviceDescriptor;
|
---|
| 136 |
|
---|
| 137 | // Config
|
---|
| 138 | typedef struct {
|
---|
| 139 | uint8_t len; // 9
|
---|
| 140 | uint8_t dtype; // 2
|
---|
| 141 | uint16_t clen; // total length
|
---|
| 142 | uint8_t numInterfaces;
|
---|
| 143 | uint8_t config;
|
---|
| 144 | uint8_t iconfig;
|
---|
| 145 | uint8_t attributes;
|
---|
| 146 | uint8_t maxPower;
|
---|
| 147 | } ConfigDescriptor;
|
---|
| 148 |
|
---|
| 149 | // String
|
---|
| 150 |
|
---|
| 151 | // Interface
|
---|
| 152 | typedef struct
|
---|
| 153 | {
|
---|
| 154 | uint8_t len; // 9
|
---|
| 155 | uint8_t dtype; // 4
|
---|
| 156 | uint8_t number;
|
---|
| 157 | uint8_t alternate;
|
---|
| 158 | uint8_t numEndpoints;
|
---|
| 159 | uint8_t interfaceClass;
|
---|
| 160 | uint8_t interfaceSubClass;
|
---|
| 161 | uint8_t protocol;
|
---|
| 162 | uint8_t iInterface;
|
---|
| 163 | } InterfaceDescriptor;
|
---|
| 164 |
|
---|
| 165 | // Endpoint
|
---|
| 166 | typedef struct
|
---|
| 167 | {
|
---|
| 168 | uint8_t len; // 7
|
---|
| 169 | uint8_t dtype; // 5
|
---|
| 170 | uint8_t addr;
|
---|
| 171 | uint8_t attr;
|
---|
| 172 | uint16_t packetSize;
|
---|
| 173 | uint8_t interval;
|
---|
| 174 | } EndpointDescriptor;
|
---|
| 175 |
|
---|
| 176 | // Interface Association Descriptor
|
---|
| 177 | // Used to bind 2 interfaces together in CDC compostite device
|
---|
| 178 | typedef struct
|
---|
| 179 | {
|
---|
| 180 | uint8_t len; // 8
|
---|
| 181 | uint8_t dtype; // 11
|
---|
| 182 | uint8_t firstInterface;
|
---|
| 183 | uint8_t interfaceCount;
|
---|
| 184 | uint8_t functionClass;
|
---|
| 185 | uint8_t funtionSubClass;
|
---|
| 186 | uint8_t functionProtocol;
|
---|
| 187 | uint8_t iInterface;
|
---|
| 188 | } IADDescriptor;
|
---|
| 189 |
|
---|
| 190 | // CDC CS interface descriptor
|
---|
| 191 | typedef struct
|
---|
| 192 | {
|
---|
| 193 | uint8_t len; // 5
|
---|
| 194 | uint8_t dtype; // 0x24
|
---|
| 195 | uint8_t subtype;
|
---|
| 196 | uint8_t d0;
|
---|
| 197 | uint8_t d1;
|
---|
| 198 | } CDCCSInterfaceDescriptor;
|
---|
| 199 |
|
---|
| 200 | typedef struct
|
---|
| 201 | {
|
---|
| 202 | uint8_t len; // 4
|
---|
| 203 | uint8_t dtype; // 0x24
|
---|
| 204 | uint8_t subtype;
|
---|
| 205 | uint8_t d0;
|
---|
| 206 | } CDCCSInterfaceDescriptor4;
|
---|
| 207 |
|
---|
| 208 | typedef struct
|
---|
| 209 | {
|
---|
| 210 | uint8_t len;
|
---|
| 211 | uint8_t dtype; // 0x24
|
---|
| 212 | uint8_t subtype; // 1
|
---|
| 213 | uint8_t bmCapabilities;
|
---|
| 214 | uint8_t bDataInterface;
|
---|
| 215 | } CMFunctionalDescriptor;
|
---|
| 216 |
|
---|
| 217 | typedef struct
|
---|
| 218 | {
|
---|
| 219 | uint8_t len;
|
---|
| 220 | uint8_t dtype; // 0x24
|
---|
| 221 | uint8_t subtype; // 1
|
---|
| 222 | uint8_t bmCapabilities;
|
---|
| 223 | } ACMFunctionalDescriptor;
|
---|
| 224 |
|
---|
| 225 | typedef struct
|
---|
| 226 | {
|
---|
| 227 | #if (defined CDC_ENABLED) && defined(HID_ENABLED)
|
---|
| 228 | // IAD
|
---|
| 229 | IADDescriptor iad; // Only needed on compound device
|
---|
| 230 | #endif
|
---|
| 231 | // Control
|
---|
| 232 | InterfaceDescriptor cif;
|
---|
| 233 | CDCCSInterfaceDescriptor header;
|
---|
| 234 | ACMFunctionalDescriptor controlManagement; // ACM
|
---|
| 235 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
|
---|
| 236 | CMFunctionalDescriptor callManagement; // Call Management
|
---|
| 237 | EndpointDescriptor cifin;
|
---|
| 238 |
|
---|
| 239 | // Data
|
---|
| 240 | InterfaceDescriptor dif;
|
---|
| 241 | EndpointDescriptor in;
|
---|
| 242 | EndpointDescriptor out;
|
---|
| 243 | } CDCDescriptor;
|
---|
| 244 |
|
---|
| 245 | typedef struct
|
---|
| 246 | {
|
---|
| 247 | InterfaceDescriptor msc;
|
---|
| 248 | EndpointDescriptor in;
|
---|
| 249 | EndpointDescriptor out;
|
---|
| 250 | } MSCDescriptor;
|
---|
| 251 |
|
---|
| 252 | typedef struct
|
---|
| 253 | {
|
---|
| 254 | uint8_t len; // 9
|
---|
| 255 | uint8_t dtype; // 0x21
|
---|
| 256 | uint8_t addr;
|
---|
| 257 | uint8_t versionL; // 0x101
|
---|
| 258 | uint8_t versionH; // 0x101
|
---|
| 259 | uint8_t country;
|
---|
| 260 | uint8_t desctype; // 0x22 report
|
---|
| 261 | uint8_t descLenL;
|
---|
| 262 | uint8_t descLenH;
|
---|
| 263 | } HIDDescDescriptor;
|
---|
| 264 |
|
---|
| 265 | typedef struct
|
---|
| 266 | {
|
---|
| 267 | InterfaceDescriptor hid;
|
---|
| 268 | HIDDescDescriptor desc;
|
---|
| 269 | EndpointDescriptor in;
|
---|
| 270 | } HIDDescriptor;
|
---|
| 271 |
|
---|
| 272 | _Pragma("pack()")
|
---|
| 273 |
|
---|
| 274 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
|
---|
| 275 | { 18, 1, 0x110, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
|
---|
| 276 | /* Table 9-8. Standard Device Descriptor
|
---|
| 277 | * bLength, bDescriptorType, bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, bMaxPacketSize0,
|
---|
| 278 | * idVendor, idProduct, bcdDevice, iManufacturer, iProduct, iSerialNumber, bNumConfigurations */
|
---|
| 279 |
|
---|
| 280 | #define D_CONFIG(_totalLength,_interfaces) \
|
---|
| 281 | { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_SELF_POWERED, USB_CONFIG_POWER_MA(500) }
|
---|
| 282 | /* Table 9-10. Standard Configuration Descriptor
|
---|
| 283 | * bLength, bDescriptorType, wTotalLength, bNumInterfaces, bConfigurationValue, iConfiguration
|
---|
| 284 | * bmAttributes, bMaxPower */
|
---|
| 285 |
|
---|
| 286 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
|
---|
| 287 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
|
---|
| 288 | /* Table 9-12. Standard Interface Descriptor
|
---|
| 289 | * bLength, bDescriptorType, bInterfaceNumber, bAlternateSetting, bNumEndpoints, bInterfaceClass,
|
---|
| 290 | * bInterfaceSubClass, bInterfaceProtocol, iInterface */
|
---|
| 291 |
|
---|
| 292 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
|
---|
| 293 | { 7, 5, _addr,_attr,_packetSize, _interval }
|
---|
| 294 | /* Table 9-13. Standard Endpoint Descriptor
|
---|
| 295 | * bLength, bDescriptorType, bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval */
|
---|
| 296 |
|
---|
| 297 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
|
---|
| 298 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
|
---|
| 299 | /* iadclasscode_r10.pdf, Table 9Z. Standard Interface Association Descriptor
|
---|
| 300 | * bLength, bDescriptorType, bFirstInterface, bInterfaceCount, bFunctionClass, bFunctionSubClass, bFunctionProtocol, iFunction */
|
---|
| 301 | #define D_HIDREPORT(_descriptorLength) \
|
---|
| 302 | { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
|
---|
| 303 | /* HID1_11.pdf E.8 HID Descriptor (Mouse)
|
---|
| 304 | * bLength, bDescriptorType, bcdHID, bCountryCode, bNumDescriptors, bDescriptorType, wItemLength */
|
---|
| 305 |
|
---|
| 306 | // Functional Descriptor General Format
|
---|
| 307 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
|
---|
| 308 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
---|
| 309 | /* bFunctionLength, bDescriptorType, bDescriptorSubtype, function specific data0, functional specific data N-1
|
---|
| 310 | * CS_INTERFACE 24h */
|
---|
| 311 |
|
---|
| 312 | #endif
|
---|