USBCore.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifndef __USBCORE_H__
  17. #define __USBCORE_H__
  18. #define USB_VID 0x16D0
  19. #define USB_PID 0x076B
  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. // bmRequestType
  32. #define REQUEST_HOSTTODEVICE 0x00
  33. #define REQUEST_DEVICETOHOST 0x80
  34. #define REQUEST_DIRECTION 0x80
  35. #define REQUEST_STANDARD 0x00
  36. #define REQUEST_CLASS 0x20
  37. #define REQUEST_VENDOR 0x40
  38. #define REQUEST_TYPE 0x60
  39. #define REQUEST_DEVICE 0x00
  40. #define REQUEST_INTERFACE 0x01
  41. #define REQUEST_ENDPOINT 0x02
  42. #define REQUEST_OTHER 0x03
  43. #define REQUEST_RECIPIENT 0x03
  44. #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
  45. #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
  46. // Class requests
  47. #define CDC_SET_LINE_CODING 0x20
  48. #define CDC_GET_LINE_CODING 0x21
  49. #define CDC_SET_CONTROL_LINE_STATE 0x22
  50. #define MSC_RESET 0xFF
  51. #define MSC_GET_MAX_LUN 0xFE
  52. #define HID_GET_REPORT 0x01
  53. #define HID_GET_IDLE 0x02
  54. #define HID_GET_PROTOCOL 0x03
  55. #define HID_SET_REPORT 0x09
  56. #define HID_SET_IDLE 0x0A
  57. #define HID_SET_PROTOCOL 0x0B
  58. // Descriptors
  59. #define USB_DEVICE_DESC_SIZE 18
  60. #define USB_CONFIGUARTION_DESC_SIZE 9
  61. #define USB_INTERFACE_DESC_SIZE 9
  62. #define USB_ENDPOINT_DESC_SIZE 7
  63. #define USB_DEVICE_DESCRIPTOR_TYPE 1
  64. #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
  65. #define USB_STRING_DESCRIPTOR_TYPE 3
  66. #define USB_INTERFACE_DESCRIPTOR_TYPE 4
  67. #define USB_ENDPOINT_DESCRIPTOR_TYPE 5
  68. #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
  69. #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
  70. #define USB_DEVICE_CLASS_STORAGE 0x08
  71. #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
  72. #define USB_CONFIG_POWERED_MASK 0x40
  73. #define USB_CONFIG_BUS_POWERED 0x80
  74. #define USB_CONFIG_SELF_POWERED 0xC0
  75. #define USB_CONFIG_REMOTE_WAKEUP 0x20
  76. // bMaxPower in Configuration Descriptor
  77. #define USB_CONFIG_POWER_MA(mA) ((mA)/2)
  78. // bEndpointAddress in Endpoint Descriptor
  79. #define USB_ENDPOINT_DIRECTION_MASK 0x80
  80. #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
  81. #define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
  82. #define USB_ENDPOINT_TYPE_MASK 0x03
  83. #define USB_ENDPOINT_TYPE_CONTROL 0x00
  84. #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
  85. #define USB_ENDPOINT_TYPE_BULK 0x02
  86. #define USB_ENDPOINT_TYPE_INTERRUPT 0x03
  87. #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
  88. #define CDC_V1_10 0x0110
  89. #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
  90. #define CDC_CALL_MANAGEMENT 0x01
  91. #define CDC_ABSTRACT_CONTROL_MODEL 0x02
  92. #define CDC_HEADER 0x00
  93. #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
  94. #define CDC_UNION 0x06
  95. #define CDC_CS_INTERFACE 0x24
  96. #define CDC_CS_ENDPOINT 0x25
  97. #define CDC_DATA_INTERFACE_CLASS 0x0A
  98. #define MSC_SUBCLASS_SCSI 0x06
  99. #define MSC_PROTOCOL_BULK_ONLY 0x50
  100. #define HID_HID_DESCRIPTOR_TYPE 0x21
  101. #define HID_REPORT_DESCRIPTOR_TYPE 0x22
  102. #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
  103. // Device
  104. typedef struct {
  105. u8 len; // 18
  106. u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
  107. u16 usbVersion; // 0x200
  108. u8 deviceClass;
  109. u8 deviceSubClass;
  110. u8 deviceProtocol;
  111. u8 packetSize0; // Packet 0
  112. u16 idVendor;
  113. u16 idProduct;
  114. u16 deviceVersion; // 0x100
  115. u8 iManufacturer;
  116. u8 iProduct;
  117. u8 iSerialNumber;
  118. u8 bNumConfigurations;
  119. } DeviceDescriptor;
  120. // Config
  121. typedef struct {
  122. u8 len; // 9
  123. u8 dtype; // 2
  124. u16 clen; // total length
  125. u8 numInterfaces;
  126. u8 config;
  127. u8 iconfig;
  128. u8 attributes;
  129. u8 maxPower;
  130. } ConfigDescriptor;
  131. // String
  132. // Interface
  133. typedef struct
  134. {
  135. u8 len; // 9
  136. u8 dtype; // 4
  137. u8 number;
  138. u8 alternate;
  139. u8 numEndpoints;
  140. u8 interfaceClass;
  141. u8 interfaceSubClass;
  142. u8 protocol;
  143. u8 iInterface;
  144. } InterfaceDescriptor;
  145. // Endpoint
  146. typedef struct
  147. {
  148. u8 len; // 7
  149. u8 dtype; // 5
  150. u8 addr;
  151. u8 attr;
  152. u16 packetSize;
  153. u8 interval;
  154. } EndpointDescriptor;
  155. // Interface Association Descriptor
  156. // Used to bind 2 interfaces together in CDC compostite device
  157. typedef struct
  158. {
  159. u8 len; // 8
  160. u8 dtype; // 11
  161. u8 firstInterface;
  162. u8 interfaceCount;
  163. u8 functionClass;
  164. u8 funtionSubClass;
  165. u8 functionProtocol;
  166. u8 iInterface;
  167. } IADDescriptor;
  168. // CDC CS interface descriptor
  169. typedef struct
  170. {
  171. u8 len; // 5
  172. u8 dtype; // 0x24
  173. u8 subtype;
  174. u8 d0;
  175. u8 d1;
  176. } CDCCSInterfaceDescriptor;
  177. typedef struct
  178. {
  179. u8 len; // 4
  180. u8 dtype; // 0x24
  181. u8 subtype;
  182. u8 d0;
  183. } CDCCSInterfaceDescriptor4;
  184. typedef struct
  185. {
  186. u8 len;
  187. u8 dtype; // 0x24
  188. u8 subtype; // 1
  189. u8 bmCapabilities;
  190. u8 bDataInterface;
  191. } CMFunctionalDescriptor;
  192. typedef struct
  193. {
  194. u8 len;
  195. u8 dtype; // 0x24
  196. u8 subtype; // 1
  197. u8 bmCapabilities;
  198. } ACMFunctionalDescriptor;
  199. typedef struct
  200. {
  201. // IAD
  202. IADDescriptor iad; // Only needed on compound device
  203. // Control
  204. InterfaceDescriptor cif; //
  205. CDCCSInterfaceDescriptor header;
  206. CMFunctionalDescriptor callManagement; // Call Management
  207. ACMFunctionalDescriptor controlManagement; // ACM
  208. CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
  209. EndpointDescriptor cifin;
  210. // Data
  211. InterfaceDescriptor dif;
  212. EndpointDescriptor in;
  213. EndpointDescriptor out;
  214. } CDCDescriptor;
  215. typedef struct
  216. {
  217. InterfaceDescriptor msc;
  218. EndpointDescriptor in;
  219. EndpointDescriptor out;
  220. } MSCDescriptor;
  221. typedef struct
  222. {
  223. u8 len; // 9
  224. u8 dtype; // 0x21
  225. u8 addr;
  226. u8 versionL; // 0x101
  227. u8 versionH; // 0x101
  228. u8 country;
  229. u8 desctype; // 0x22 report
  230. u8 descLenL;
  231. u8 descLenH;
  232. } HIDDescDescriptor;
  233. typedef struct
  234. {
  235. InterfaceDescriptor hid;
  236. HIDDescDescriptor desc;
  237. EndpointDescriptor in;
  238. } HIDDescriptor;
  239. #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
  240. { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
  241. #define D_CONFIG(_totalLength,_interfaces) \
  242. { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
  243. #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
  244. { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
  245. #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
  246. { 7, 5, _addr,_attr,_packetSize, _interval }
  247. #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
  248. { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
  249. #define D_HIDREPORT(_descriptorLength) \
  250. { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
  251. #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
  252. #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
  253. #endif