usb_descriptor.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #ifndef _DESCRIPTORS_H_
  38. #define _DESCRIPTORS_H_
  39. #include <LUFA/Drivers/USB/USB.h>
  40. #ifdef PROTOCOL_CHIBIOS
  41. #include "hal.h"
  42. #endif
  43. typedef struct
  44. {
  45. USB_Descriptor_Configuration_Header_t Config;
  46. #ifndef KEYBOARD_SHARED_EP
  47. // Keyboard HID Interface
  48. USB_Descriptor_Interface_t Keyboard_Interface;
  49. USB_HID_Descriptor_HID_t Keyboard_HID;
  50. USB_Descriptor_Endpoint_t Keyboard_INEndpoint;
  51. #endif
  52. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  53. // Mouse HID Interface
  54. USB_Descriptor_Interface_t Mouse_Interface;
  55. USB_HID_Descriptor_HID_t Mouse_HID;
  56. USB_Descriptor_Endpoint_t Mouse_INEndpoint;
  57. #endif
  58. #if defined(SHARED_EP_ENABLE)
  59. USB_Descriptor_Interface_t Shared_Interface;
  60. USB_HID_Descriptor_HID_t Shared_HID;
  61. USB_Descriptor_Endpoint_t Shared_INEndpoint;
  62. #endif
  63. #if defined(RAW_ENABLE)
  64. // Raw HID Interface
  65. USB_Descriptor_Interface_t Raw_Interface;
  66. USB_HID_Descriptor_HID_t Raw_HID;
  67. USB_Descriptor_Endpoint_t Raw_INEndpoint;
  68. USB_Descriptor_Endpoint_t Raw_OUTEndpoint;
  69. #endif
  70. #ifdef CONSOLE_ENABLE
  71. // Console HID Interface
  72. USB_Descriptor_Interface_t Console_Interface;
  73. USB_HID_Descriptor_HID_t Console_HID;
  74. USB_Descriptor_Endpoint_t Console_INEndpoint;
  75. USB_Descriptor_Endpoint_t Console_OUTEndpoint;
  76. #endif
  77. #ifdef MIDI_ENABLE
  78. USB_Descriptor_Interface_Association_t Audio_Interface_Association;
  79. // MIDI Audio Control Interface
  80. USB_Descriptor_Interface_t Audio_ControlInterface;
  81. USB_Audio_Descriptor_Interface_AC_t Audio_ControlInterface_SPC;
  82. // MIDI Audio Streaming Interface
  83. USB_Descriptor_Interface_t Audio_StreamInterface;
  84. USB_MIDI_Descriptor_AudioInterface_AS_t Audio_StreamInterface_SPC;
  85. USB_MIDI_Descriptor_InputJack_t MIDI_In_Jack_Emb;
  86. USB_MIDI_Descriptor_InputJack_t MIDI_In_Jack_Ext;
  87. USB_MIDI_Descriptor_OutputJack_t MIDI_Out_Jack_Emb;
  88. USB_MIDI_Descriptor_OutputJack_t MIDI_Out_Jack_Ext;
  89. USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_In_Jack_Endpoint;
  90. USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_In_Jack_Endpoint_SPC;
  91. USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_Out_Jack_Endpoint;
  92. USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_Out_Jack_Endpoint_SPC;
  93. #endif
  94. #ifdef VIRTSER_ENABLE
  95. USB_Descriptor_Interface_Association_t CDC_Interface_Association;
  96. // CDC Control Interface
  97. USB_Descriptor_Interface_t CDC_CCI_Interface;
  98. USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
  99. USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
  100. USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
  101. USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
  102. // CDC Data Interface
  103. USB_Descriptor_Interface_t CDC_DCI_Interface;
  104. USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
  105. USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
  106. #endif
  107. } USB_Descriptor_Configuration_t;
  108. /* index of interface */
  109. enum usb_interfaces {
  110. #if !defined(KEYBOARD_SHARED_EP)
  111. KEYBOARD_INTERFACE,
  112. #else
  113. # define KEYBOARD_INTERFACE SHARED_INTERFACE
  114. #endif
  115. // It is important that the Raw HID interface is at a constant
  116. // interface number, to support Linux/OSX platforms and chrome.hid
  117. // If Raw HID is enabled, let it be always 1.
  118. #if defined(RAW_ENABLE)
  119. RAW_INTERFACE,
  120. #endif
  121. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  122. MOUSE_INTERFACE,
  123. #endif
  124. #if defined(SHARED_EP_ENABLE)
  125. SHARED_INTERFACE,
  126. #endif
  127. #if defined(CONSOLE_ENABLE)
  128. CONSOLE_INTERFACE,
  129. #endif
  130. #if defined(MIDI_ENABLE)
  131. AC_INTERFACE,
  132. AS_INTERFACE,
  133. #endif
  134. #if defined(VIRTSER_ENABLE)
  135. CCI_INTERFACE,
  136. CDI_INTERFACE,
  137. #endif
  138. TOTAL_INTERFACES
  139. };
  140. #define NEXT_EPNUM __COUNTER__
  141. enum usb_endpoints {
  142. __unused_epnum__ = NEXT_EPNUM, /* EP numbering starts at 1 */
  143. #if !defined(KEYBOARD_SHARED_EP)
  144. KEYBOARD_IN_EPNUM = NEXT_EPNUM,
  145. #else
  146. # define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM
  147. #endif
  148. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  149. MOUSE_IN_EPNUM = NEXT_EPNUM,
  150. #else
  151. # define MOUSE_IN_EPNUM SHARED_IN_EPNUM
  152. #endif
  153. #if defined(RAW_ENABLE)
  154. RAW_IN_EPNUM = NEXT_EPNUM,
  155. RAW_OUT_EPNUM = NEXT_EPNUM,
  156. #endif
  157. #if defined(SHARED_EP_ENABLE)
  158. SHARED_IN_EPNUM = NEXT_EPNUM,
  159. #endif
  160. #if defined(CONSOLE_ENABLE)
  161. CONSOLE_IN_EPNUM = NEXT_EPNUM,
  162. #ifdef PROTOCOL_CHIBIOS
  163. // ChibiOS has enough memory and descriptor to actually enable the endpoint
  164. // It could use the same endpoint numbers, as that's supported by ChibiOS
  165. // But the QMK code currently assumes that the endpoint numbers are different
  166. CONSOLE_OUT_EPNUM = NEXT_EPNUM,
  167. #else
  168. #define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM
  169. #endif
  170. #endif
  171. #ifdef MIDI_ENABLE
  172. MIDI_STREAM_IN_EPNUM = NEXT_EPNUM,
  173. MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
  174. # define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM)
  175. # define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM)
  176. #endif
  177. #ifdef VIRTSER_ENABLE
  178. CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
  179. CDC_IN_EPNUM = NEXT_EPNUM,
  180. CDC_OUT_EPNUM = NEXT_EPNUM,
  181. # define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM)
  182. # define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM)
  183. # define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM)
  184. #endif
  185. };
  186. #if defined(PROTOCOL_LUFA)
  187. /* LUFA tells us total endpoints including control */
  188. #define MAX_ENDPOINTS (ENDPOINT_TOTAL_ENDPOINTS - 1)
  189. #elif defined(PROTOCOL_CHIBIOS)
  190. /* ChibiOS gives us number of available user endpoints, not control */
  191. #define MAX_ENDPOINTS USB_MAX_ENDPOINTS
  192. #endif
  193. /* TODO - ARM_ATSAM */
  194. #if (NEXT_EPNUM - 1) > MAX_ENDPOINTS
  195. # error There are not enough available endpoints to support all functions. Remove some in the rules.mk file. (MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI, SERIAL, STENO)
  196. #endif
  197. #define KEYBOARD_EPSIZE 8
  198. #define SHARED_EPSIZE 32
  199. #define MOUSE_EPSIZE 8
  200. #define RAW_EPSIZE 32
  201. #define CONSOLE_EPSIZE 32
  202. #define MIDI_STREAM_EPSIZE 64
  203. #define CDC_NOTIFICATION_EPSIZE 8
  204. #define CDC_EPSIZE 16
  205. uint16_t get_usb_descriptor(const uint16_t wValue,
  206. const uint16_t wIndex,
  207. const void** const DescriptorAddress);
  208. /* new API */
  209. #if LUFA_VERSION_INTEGER < 0x140302
  210. #undef VERSION_BCD
  211. #define VERSION_BCD(Major, Minor, Revision) \
  212. CPU_TO_LE16( ((Major & 0xFF) << 8) | \
  213. ((Minor & 0x0F) << 4) | \
  214. (Revision & 0x0F) )
  215. #endif
  216. #endif