USBCore.h 7.7 KB

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