usb_descriptor.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
  3. * This file is based on:
  4. * LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
  5. * LUFA-120219/Demos/Device/Lowlevel/GenericHID
  6. */
  7. /*
  8. LUFA Library
  9. Copyright (C) Dean Camera, 2012.
  10. dean [at] fourwalledcubicle [dot] com
  11. www.lufa-lib.org
  12. */
  13. /*
  14. Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  15. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  16. Permission to use, copy, modify, distribute, and sell this
  17. software and its documentation for any purpose is hereby granted
  18. without fee, provided that the above copyright notice appear in
  19. all copies and that both that the copyright notice and this
  20. permission notice and warranty disclaimer appear in supporting
  21. documentation, and that the name of the author not be used in
  22. advertising or publicity pertaining to distribution of the
  23. software without specific, written prior permission.
  24. The author disclaim all warranties with regard to this
  25. software, including all implied warranties of merchantability
  26. and fitness. In no event shall the author be liable for any
  27. special, indirect or consequential damages or any damages
  28. whatsoever resulting from loss of use, data or profits, whether
  29. in an action of contract, negligence or other tortious action,
  30. arising out of or in connection with the use or performance of
  31. this software.
  32. */
  33. /** \file
  34. *
  35. * Header file for Descriptors.c.
  36. */
  37. #pragma once
  38. #include <LUFA/Drivers/USB/USB.h>
  39. #ifdef PROTOCOL_CHIBIOS
  40. # include <hal.h>
  41. # if STM32_USB_USE_OTG1 == TRUE || STM32_USB_USE_OTG2 == TRUE
  42. # define USB_ENDPOINTS_ARE_REORDERABLE
  43. # endif
  44. #endif
  45. /*
  46. * USB descriptor structure
  47. */
  48. typedef struct {
  49. USB_Descriptor_Configuration_Header_t Config;
  50. #ifndef KEYBOARD_SHARED_EP
  51. // Keyboard HID Interface
  52. USB_Descriptor_Interface_t Keyboard_Interface;
  53. USB_HID_Descriptor_HID_t Keyboard_HID;
  54. USB_Descriptor_Endpoint_t Keyboard_INEndpoint;
  55. #else
  56. // Shared Interface
  57. USB_Descriptor_Interface_t Shared_Interface;
  58. USB_HID_Descriptor_HID_t Shared_HID;
  59. USB_Descriptor_Endpoint_t Shared_INEndpoint;
  60. #endif
  61. #ifdef RAW_ENABLE
  62. // Raw HID Interface
  63. USB_Descriptor_Interface_t Raw_Interface;
  64. USB_HID_Descriptor_HID_t Raw_HID;
  65. USB_Descriptor_Endpoint_t Raw_INEndpoint;
  66. USB_Descriptor_Endpoint_t Raw_OUTEndpoint;
  67. #endif
  68. #ifdef PLOVER_HID_ENABLE
  69. // Plover HID Interface
  70. USB_Descriptor_Interface_t Plover_Interface;
  71. USB_HID_Descriptor_HID_t Plover_HID;
  72. USB_Descriptor_Endpoint_t Plover_INEndpoint;
  73. #endif
  74. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  75. // Mouse HID Interface
  76. USB_Descriptor_Interface_t Mouse_Interface;
  77. USB_HID_Descriptor_HID_t Mouse_HID;
  78. USB_Descriptor_Endpoint_t Mouse_INEndpoint;
  79. #endif
  80. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  81. // Shared Interface
  82. USB_Descriptor_Interface_t Shared_Interface;
  83. USB_HID_Descriptor_HID_t Shared_HID;
  84. USB_Descriptor_Endpoint_t Shared_INEndpoint;
  85. #endif
  86. #ifdef CONSOLE_ENABLE
  87. // Console HID Interface
  88. USB_Descriptor_Interface_t Console_Interface;
  89. USB_HID_Descriptor_HID_t Console_HID;
  90. USB_Descriptor_Endpoint_t Console_INEndpoint;
  91. #endif
  92. #ifdef MIDI_ENABLE
  93. USB_Descriptor_Interface_Association_t Audio_Interface_Association;
  94. // MIDI Audio Control Interface
  95. USB_Descriptor_Interface_t Audio_ControlInterface;
  96. USB_Audio_Descriptor_Interface_AC_t Audio_ControlInterface_SPC;
  97. // MIDI Audio Streaming Interface
  98. USB_Descriptor_Interface_t Audio_StreamInterface;
  99. USB_MIDI_Descriptor_AudioInterface_AS_t Audio_StreamInterface_SPC;
  100. USB_MIDI_Descriptor_InputJack_t MIDI_In_Jack_Emb;
  101. USB_MIDI_Descriptor_InputJack_t MIDI_In_Jack_Ext;
  102. USB_MIDI_Descriptor_OutputJack_t MIDI_Out_Jack_Emb;
  103. USB_MIDI_Descriptor_OutputJack_t MIDI_Out_Jack_Ext;
  104. USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_In_Jack_Endpoint;
  105. USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_In_Jack_Endpoint_SPC;
  106. USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_Out_Jack_Endpoint;
  107. USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_Out_Jack_Endpoint_SPC;
  108. #endif
  109. #ifdef VIRTSER_ENABLE
  110. USB_Descriptor_Interface_Association_t CDC_Interface_Association;
  111. // CDC Control Interface
  112. USB_Descriptor_Interface_t CDC_CCI_Interface;
  113. USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
  114. USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
  115. USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
  116. USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
  117. // CDC Data Interface
  118. USB_Descriptor_Interface_t CDC_DCI_Interface;
  119. USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
  120. USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
  121. #endif
  122. #if defined(JOYSTICK_ENABLE) && !defined(JOYSTICK_SHARED_EP)
  123. // Joystick HID Interface
  124. USB_Descriptor_Interface_t Joystick_Interface;
  125. USB_HID_Descriptor_HID_t Joystick_HID;
  126. USB_Descriptor_Endpoint_t Joystick_INEndpoint;
  127. #endif
  128. #if defined(DIGITIZER_ENABLE) && !defined(DIGITIZER_SHARED_EP)
  129. // Digitizer HID Interface
  130. USB_Descriptor_Interface_t Digitizer_Interface;
  131. USB_HID_Descriptor_HID_t Digitizer_HID;
  132. USB_Descriptor_Endpoint_t Digitizer_INEndpoint;
  133. #endif
  134. #ifdef XAP_ENABLE
  135. // XAP HID Interface
  136. USB_Descriptor_Interface_t Xap_Interface;
  137. USB_HID_Descriptor_HID_t Xap_HID;
  138. USB_Descriptor_Endpoint_t Xap_INEndpoint;
  139. USB_Descriptor_Endpoint_t Xap_OUTEndpoint;
  140. #endif
  141. } USB_Descriptor_Configuration_t;
  142. /*
  143. * Interface indexes
  144. */
  145. enum usb_interfaces {
  146. #ifndef KEYBOARD_SHARED_EP
  147. KEYBOARD_INTERFACE,
  148. #else
  149. SHARED_INTERFACE,
  150. # define KEYBOARD_INTERFACE SHARED_INTERFACE
  151. #endif
  152. // It is important that the Raw HID interface is at a constant
  153. // interface number, to support Linux/OSX platforms and chrome.hid
  154. // If Raw HID is enabled, let it be always 1.
  155. #ifdef RAW_ENABLE
  156. RAW_INTERFACE,
  157. #endif
  158. #ifdef PLOVER_HID_ENABLE
  159. PLOVER_HID_INTERFACE,
  160. #endif
  161. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  162. MOUSE_INTERFACE,
  163. #endif
  164. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  165. SHARED_INTERFACE,
  166. #endif
  167. #ifdef CONSOLE_ENABLE
  168. CONSOLE_INTERFACE,
  169. #endif
  170. #ifdef MIDI_ENABLE
  171. AC_INTERFACE,
  172. AS_INTERFACE,
  173. #endif
  174. #ifdef VIRTSER_ENABLE
  175. CCI_INTERFACE,
  176. CDI_INTERFACE,
  177. #endif
  178. #if defined(JOYSTICK_ENABLE) && !defined(JOYSTICK_SHARED_EP)
  179. JOYSTICK_INTERFACE,
  180. #endif
  181. #if defined(DIGITIZER_ENABLE) && !defined(DIGITIZER_SHARED_EP)
  182. DIGITIZER_INTERFACE,
  183. #endif
  184. #ifdef XAP_ENABLE
  185. XAP_INTERFACE,
  186. #endif
  187. TOTAL_INTERFACES
  188. };
  189. #define IS_VALID_INTERFACE(i) ((i) >= 0 && (i) < TOTAL_INTERFACES)
  190. #define NEXT_EPNUM __COUNTER__
  191. /*
  192. * Endpoint numbers
  193. */
  194. enum usb_endpoints {
  195. __unused_epnum__ = NEXT_EPNUM, // Endpoint numbering starts at 1
  196. #ifndef KEYBOARD_SHARED_EP
  197. KEYBOARD_IN_EPNUM = NEXT_EPNUM,
  198. #else
  199. # define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM
  200. #endif
  201. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  202. MOUSE_IN_EPNUM = NEXT_EPNUM,
  203. #else
  204. # define MOUSE_IN_EPNUM SHARED_IN_EPNUM
  205. #endif
  206. #ifdef RAW_ENABLE
  207. RAW_IN_EPNUM = NEXT_EPNUM,
  208. # ifdef USB_ENDPOINTS_ARE_REORDERABLE
  209. # define RAW_OUT_EPNUM RAW_IN_EPNUM
  210. # else
  211. RAW_OUT_EPNUM = NEXT_EPNUM,
  212. # endif
  213. #endif
  214. #ifdef PLOVER_HID_ENABLE
  215. PLOVER_HID_IN_EPNUM = NEXT_EPNUM,
  216. #endif
  217. #ifdef SHARED_EP_ENABLE
  218. SHARED_IN_EPNUM = NEXT_EPNUM,
  219. #endif
  220. #ifdef CONSOLE_ENABLE
  221. CONSOLE_IN_EPNUM = NEXT_EPNUM,
  222. #endif
  223. #ifdef MIDI_ENABLE
  224. MIDI_STREAM_IN_EPNUM = NEXT_EPNUM,
  225. # ifdef USB_ENDPOINTS_ARE_REORDERABLE
  226. # define MIDI_STREAM_OUT_EPNUM MIDI_STREAM_IN_EPNUM
  227. # else
  228. MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
  229. # endif
  230. #endif
  231. #ifdef VIRTSER_ENABLE
  232. CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
  233. CDC_IN_EPNUM = NEXT_EPNUM,
  234. # ifdef USB_ENDPOINTS_ARE_REORDERABLE
  235. # define CDC_OUT_EPNUM CDC_IN_EPNUM
  236. # else
  237. CDC_OUT_EPNUM = NEXT_EPNUM,
  238. # endif
  239. #endif
  240. #ifdef JOYSTICK_ENABLE
  241. # if !defined(JOYSTICK_SHARED_EP)
  242. JOYSTICK_IN_EPNUM = NEXT_EPNUM,
  243. # else
  244. # define JOYSTICK_IN_EPNUM SHARED_IN_EPNUM
  245. # endif
  246. #endif
  247. #ifdef DIGITIZER_ENABLE
  248. # if !defined(DIGITIZER_SHARED_EP)
  249. DIGITIZER_IN_EPNUM = NEXT_EPNUM,
  250. # else
  251. # define DIGITIZER_IN_EPNUM SHARED_IN_EPNUM
  252. # endif
  253. #endif
  254. #ifdef XAP_ENABLE
  255. XAP_IN_EPNUM = NEXT_EPNUM,
  256. # ifdef USB_ENDPOINTS_ARE_REORDERABLE
  257. # define XAP_OUT_EPNUM XAP_IN_EPNUM
  258. # else
  259. XAP_OUT_EPNUM = NEXT_EPNUM,
  260. # endif
  261. #endif
  262. };
  263. #ifdef PROTOCOL_LUFA
  264. // LUFA tells us total endpoints including control
  265. # define MAX_ENDPOINTS (ENDPOINT_TOTAL_ENDPOINTS - 1)
  266. #elif defined(PROTOCOL_CHIBIOS)
  267. // ChibiOS gives us number of available user endpoints, not control
  268. # define MAX_ENDPOINTS USB_MAX_ENDPOINTS
  269. #endif
  270. #if (NEXT_EPNUM - 1) > MAX_ENDPOINTS
  271. # error There are not enough available endpoints to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Console, NKRO, MIDI, Serial, Steno, XAP
  272. #endif
  273. #define KEYBOARD_EPSIZE 8
  274. #define SHARED_EPSIZE 32
  275. #define MOUSE_EPSIZE 16
  276. #define RAW_EPSIZE 32
  277. #define PLOVER_HID_EPSIZE 9
  278. #define CONSOLE_EPSIZE 32
  279. #define MIDI_STREAM_EPSIZE 64
  280. #define CDC_NOTIFICATION_EPSIZE 8
  281. #define CDC_EPSIZE 16
  282. #define JOYSTICK_EPSIZE 8
  283. #define DIGITIZER_EPSIZE 8
  284. #define XAP_EPSIZE 64
  285. uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const uint16_t wLength, const void** const DescriptorAddress);