usb_descriptor.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright 2012 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. #include "util.h"
  34. #include "report.h"
  35. #include "usb_descriptor.h"
  36. /*
  37. * HID report descriptors
  38. */
  39. #ifdef KEYBOARD_SHARED_EP
  40. const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
  41. # define SHARED_REPORT_STARTED
  42. #else
  43. const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] = {
  44. #endif
  45. HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
  46. HID_RI_USAGE(8, 0x06), // Keyboard
  47. HID_RI_COLLECTION(8, 0x01), // Application
  48. #ifdef KEYBOARD_SHARED_EP
  49. HID_RI_REPORT_ID(8, REPORT_ID_KEYBOARD),
  50. #endif
  51. // Modifiers (8 bits)
  52. HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad
  53. HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control
  54. HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI
  55. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  56. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  57. HID_RI_REPORT_COUNT(8, 0x08),
  58. HID_RI_REPORT_SIZE(8, 0x01),
  59. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  60. // Reserved (1 byte)
  61. HID_RI_REPORT_COUNT(8, 0x01),
  62. HID_RI_REPORT_SIZE(8, 0x08),
  63. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  64. // Keycodes (6 bytes)
  65. HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad
  66. HID_RI_USAGE_MINIMUM(8, 0x00),
  67. HID_RI_USAGE_MAXIMUM(8, 0xFF),
  68. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  69. HID_RI_LOGICAL_MAXIMUM(16, 0x00FF),
  70. HID_RI_REPORT_COUNT(8, 0x06),
  71. HID_RI_REPORT_SIZE(8, 0x08),
  72. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  73. // Status LEDs (5 bits)
  74. HID_RI_USAGE_PAGE(8, 0x08), // LED
  75. HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock
  76. HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana
  77. HID_RI_REPORT_COUNT(8, 0x05),
  78. HID_RI_REPORT_SIZE(8, 0x01),
  79. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  80. // LED padding (3 bits)
  81. HID_RI_REPORT_COUNT(8, 0x01),
  82. HID_RI_REPORT_SIZE(8, 0x03),
  83. HID_RI_OUTPUT(8, HID_IOF_CONSTANT),
  84. HID_RI_END_COLLECTION(0),
  85. #ifndef KEYBOARD_SHARED_EP
  86. };
  87. #endif
  88. #ifdef MOUSE_ENABLE
  89. # ifndef MOUSE_SHARED_EP
  90. const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] = {
  91. # elif !defined(SHARED_REPORT_STARTED)
  92. const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
  93. # define SHARED_REPORT_STARTED
  94. # endif
  95. HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
  96. HID_RI_USAGE(8, 0x02), // Mouse
  97. HID_RI_COLLECTION(8, 0x01), // Application
  98. # ifdef MOUSE_SHARED_EP
  99. HID_RI_REPORT_ID(8, REPORT_ID_MOUSE),
  100. # endif
  101. HID_RI_USAGE(8, 0x01), // Pointer
  102. HID_RI_COLLECTION(8, 0x00), // Physical
  103. // Buttons (5 bits)
  104. HID_RI_USAGE_PAGE(8, 0x09), // Button
  105. HID_RI_USAGE_MINIMUM(8, 0x01), // Button 1
  106. HID_RI_USAGE_MAXIMUM(8, 0x05), // Button 5
  107. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  108. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  109. HID_RI_REPORT_COUNT(8, 0x05),
  110. HID_RI_REPORT_SIZE(8, 0x01),
  111. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  112. // Button padding (3 bits)
  113. HID_RI_REPORT_COUNT(8, 0x01),
  114. HID_RI_REPORT_SIZE(8, 0x03),
  115. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  116. // X/Y position (2 bytes)
  117. HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
  118. HID_RI_USAGE(8, 0x30), // X
  119. HID_RI_USAGE(8, 0x31), // Y
  120. HID_RI_LOGICAL_MINIMUM(8, -127),
  121. HID_RI_LOGICAL_MAXIMUM(8, 127),
  122. HID_RI_REPORT_COUNT(8, 0x02),
  123. HID_RI_REPORT_SIZE(8, 0x08),
  124. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  125. // Vertical wheel (1 byte)
  126. HID_RI_USAGE(8, 0x38), // Wheel
  127. HID_RI_LOGICAL_MINIMUM(8, -127),
  128. HID_RI_LOGICAL_MAXIMUM(8, 127),
  129. HID_RI_REPORT_COUNT(8, 0x01),
  130. HID_RI_REPORT_SIZE(8, 0x08),
  131. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  132. // Horizontal wheel (1 byte)
  133. HID_RI_USAGE_PAGE(8, 0x0C), // Consumer
  134. HID_RI_USAGE(16, 0x0238), // AC Pan
  135. HID_RI_LOGICAL_MINIMUM(8, -127),
  136. HID_RI_LOGICAL_MAXIMUM(8, 127),
  137. HID_RI_REPORT_COUNT(8, 0x01),
  138. HID_RI_REPORT_SIZE(8, 0x08),
  139. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  140. HID_RI_END_COLLECTION(0),
  141. HID_RI_END_COLLECTION(0),
  142. # ifndef MOUSE_SHARED_EP
  143. };
  144. # endif
  145. #endif
  146. #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
  147. const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
  148. #endif
  149. #ifdef EXTRAKEY_ENABLE
  150. HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
  151. HID_RI_USAGE(8, 0x80), // System Control
  152. HID_RI_COLLECTION(8, 0x01), // Application
  153. HID_RI_REPORT_ID(8, REPORT_ID_SYSTEM),
  154. HID_RI_USAGE_MINIMUM(16, 0x0081), // System Power Down
  155. HID_RI_USAGE_MAXIMUM(16, 0x0083), // System Wake Up
  156. HID_RI_LOGICAL_MINIMUM(16, 0x0001),
  157. HID_RI_LOGICAL_MAXIMUM(16, 0x0003),
  158. HID_RI_REPORT_COUNT(8, 1),
  159. HID_RI_REPORT_SIZE(8, 16),
  160. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  161. HID_RI_END_COLLECTION(0),
  162. HID_RI_USAGE_PAGE(8, 0x0C), // Consumer
  163. HID_RI_USAGE(8, 0x01), // Consumer Control
  164. HID_RI_COLLECTION(8, 0x01), // Application
  165. HID_RI_REPORT_ID(8, REPORT_ID_CONSUMER),
  166. HID_RI_USAGE_MINIMUM(16, 0x0001), // Consumer Control
  167. HID_RI_USAGE_MAXIMUM(16, 0x029C), // AC Distribute Vertically
  168. HID_RI_LOGICAL_MINIMUM(16, 0x0001),
  169. HID_RI_LOGICAL_MAXIMUM(16, 0x029C),
  170. HID_RI_REPORT_COUNT(8, 1),
  171. HID_RI_REPORT_SIZE(8, 16),
  172. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  173. HID_RI_END_COLLECTION(0),
  174. #endif
  175. #ifdef NKRO_ENABLE
  176. HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
  177. HID_RI_USAGE(8, 0x06), // Keyboard
  178. HID_RI_COLLECTION(8, 0x01), // Application
  179. HID_RI_REPORT_ID(8, REPORT_ID_NKRO),
  180. // Modifiers (8 bits)
  181. HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad
  182. HID_RI_USAGE_MINIMUM(8, 0xE0), // Keyboard Left Control
  183. HID_RI_USAGE_MAXIMUM(8, 0xE7), // Keyboard Right GUI
  184. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  185. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  186. HID_RI_REPORT_COUNT(8, 0x08),
  187. HID_RI_REPORT_SIZE(8, 0x01),
  188. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  189. // Keycodes
  190. HID_RI_USAGE_PAGE(8, 0x07), // Keyboard/Keypad
  191. HID_RI_USAGE_MINIMUM(8, 0x00),
  192. HID_RI_USAGE_MAXIMUM(8, KEYBOARD_REPORT_BITS * 8 - 1),
  193. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  194. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  195. HID_RI_REPORT_COUNT(8, KEYBOARD_REPORT_BITS * 8),
  196. HID_RI_REPORT_SIZE(8, 0x01),
  197. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  198. // Status LEDs (5 bits)
  199. HID_RI_USAGE_PAGE(8, 0x08), // LED
  200. HID_RI_USAGE_MINIMUM(8, 0x01), // Num Lock
  201. HID_RI_USAGE_MAXIMUM(8, 0x05), // Kana
  202. HID_RI_REPORT_COUNT(8, 0x05),
  203. HID_RI_REPORT_SIZE(8, 0x01),
  204. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  205. // LED padding (3 bits)
  206. HID_RI_REPORT_COUNT(8, 0x01),
  207. HID_RI_REPORT_SIZE(8, 0x03),
  208. HID_RI_OUTPUT(8, HID_IOF_CONSTANT),
  209. HID_RI_END_COLLECTION(0),
  210. #endif
  211. #ifdef SHARED_EP_ENABLE
  212. };
  213. #endif
  214. #ifdef RAW_ENABLE
  215. const USB_Descriptor_HIDReport_Datatype_t PROGMEM RawReport[] = {
  216. HID_RI_USAGE_PAGE(16, 0xFF60), // Vendor Defined
  217. HID_RI_USAGE(8, 0x61), // Vendor Defined
  218. HID_RI_COLLECTION(8, 0x01), // Application
  219. // Data to host
  220. HID_RI_USAGE(8, 0x62), // Vendor Defined
  221. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  222. HID_RI_LOGICAL_MAXIMUM(16, 0x00FF),
  223. HID_RI_REPORT_COUNT(8, RAW_EPSIZE),
  224. HID_RI_REPORT_SIZE(8, 0x08),
  225. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  226. // Data from host
  227. HID_RI_USAGE(8, 0x63), // Vendor Defined
  228. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  229. HID_RI_LOGICAL_MAXIMUM(16, 0x00FF),
  230. HID_RI_REPORT_COUNT(8, RAW_EPSIZE),
  231. HID_RI_REPORT_SIZE(8, 0x08),
  232. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  233. HID_RI_END_COLLECTION(0),
  234. };
  235. #endif
  236. #ifdef CONSOLE_ENABLE
  237. const USB_Descriptor_HIDReport_Datatype_t PROGMEM ConsoleReport[] = {
  238. HID_RI_USAGE_PAGE(16, 0xFF31), // Vendor Defined (PJRC Teensy compatible)
  239. HID_RI_USAGE(8, 0x74), // Vendor Defined (PJRC Teensy compatible)
  240. HID_RI_COLLECTION(8, 0x01), // Application
  241. // Data to host
  242. HID_RI_USAGE(8, 0x75), // Vendor Defined
  243. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  244. HID_RI_LOGICAL_MAXIMUM(16, 0x00FF),
  245. HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE),
  246. HID_RI_REPORT_SIZE(8, 0x08),
  247. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  248. // Data from host
  249. HID_RI_USAGE(8, 0x76), // Vendor Defined
  250. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  251. HID_RI_LOGICAL_MAXIMUM(16, 0x00FF),
  252. HID_RI_REPORT_COUNT(8, CONSOLE_EPSIZE),
  253. HID_RI_REPORT_SIZE(8, 0x08),
  254. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  255. HID_RI_END_COLLECTION(0),
  256. };
  257. #endif
  258. /*
  259. * Device descriptor
  260. */
  261. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = {.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  262. .USBSpecification = VERSION_BCD(1, 1, 0),
  263. #if VIRTSER_ENABLE
  264. .Class = USB_CSCP_IADDeviceClass,
  265. .SubClass = USB_CSCP_IADDeviceSubclass,
  266. .Protocol = USB_CSCP_IADDeviceProtocol,
  267. #else
  268. .Class = USB_CSCP_NoDeviceClass,
  269. .SubClass = USB_CSCP_NoDeviceSubclass,
  270. .Protocol = USB_CSCP_NoDeviceProtocol,
  271. #endif
  272. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  273. // Specified in config.h
  274. .VendorID = VENDOR_ID,
  275. .ProductID = PRODUCT_ID,
  276. .ReleaseNumber = DEVICE_VER,
  277. .ManufacturerStrIndex = 0x01,
  278. .ProductStrIndex = 0x02,
  279. .SerialNumStrIndex = 0x03,
  280. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS};
  281. #ifndef USB_MAX_POWER_CONSUMPTION
  282. # define USB_MAX_POWER_CONSUMPTION 500
  283. #endif
  284. #ifndef USB_POLLING_INTERVAL_MS
  285. # define USB_POLLING_INTERVAL_MS 10
  286. #endif
  287. /*
  288. * Configuration descriptors
  289. */
  290. const USB_Descriptor_Configuration_t PROGMEM
  291. ConfigurationDescriptor =
  292. {
  293. .Config = {.Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration}, .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t), .TotalInterfaces = TOTAL_INTERFACES, .ConfigurationNumber = 1, .ConfigurationStrIndex = NO_DESCRIPTOR, .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP), .MaxPowerConsumption = USB_CONFIG_POWER_MA(USB_MAX_POWER_CONSUMPTION)},
  294. #ifndef KEYBOARD_SHARED_EP
  295. /*
  296. * Keyboard
  297. */
  298. .Keyboard_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  299. .InterfaceNumber = KEYBOARD_INTERFACE,
  300. .AlternateSetting = 0x00,
  301. .TotalEndpoints = 1,
  302. .Class = HID_CSCP_HIDClass,
  303. .SubClass = HID_CSCP_BootSubclass,
  304. .Protocol = HID_CSCP_KeyboardBootProtocol,
  305. .InterfaceStrIndex = NO_DESCRIPTOR},
  306. .Keyboard_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(KeyboardReport)},
  307. .Keyboard_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | KEYBOARD_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = KEYBOARD_EPSIZE, .PollingIntervalMS = USB_POLLING_INTERVAL_MS},
  308. #endif
  309. #ifdef RAW_ENABLE
  310. /*
  311. * Raw HID
  312. */
  313. .Raw_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = RAW_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 2, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_NonBootSubclass, .Protocol = HID_CSCP_NonBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR},
  314. .Raw_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(RawReport)},
  315. .Raw_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | RAW_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = RAW_EPSIZE, .PollingIntervalMS = 0x01},
  316. .Raw_OUTEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_OUT | RAW_OUT_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = RAW_EPSIZE, .PollingIntervalMS = 0x01},
  317. #endif
  318. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  319. /*
  320. * Mouse
  321. */
  322. .Mouse_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = MOUSE_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 1, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_BootSubclass, .Protocol = HID_CSCP_MouseBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR},
  323. .Mouse_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(MouseReport)},
  324. .Mouse_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | MOUSE_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MOUSE_EPSIZE, .PollingIntervalMS = USB_POLLING_INTERVAL_MS},
  325. #endif
  326. #ifdef SHARED_EP_ENABLE
  327. /*
  328. * Shared
  329. */
  330. .Shared_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  331. .InterfaceNumber = SHARED_INTERFACE,
  332. .AlternateSetting = 0x00,
  333. .TotalEndpoints = 1,
  334. .Class = HID_CSCP_HIDClass,
  335. # ifdef KEYBOARD_SHARED_EP
  336. .SubClass = HID_CSCP_BootSubclass,
  337. .Protocol = HID_CSCP_KeyboardBootProtocol,
  338. # else
  339. .SubClass = HID_CSCP_NonBootSubclass,
  340. .Protocol = HID_CSCP_NonBootProtocol,
  341. # endif
  342. .InterfaceStrIndex = NO_DESCRIPTOR},
  343. .Shared_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(SharedReport)},
  344. .Shared_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | SHARED_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = SHARED_EPSIZE, .PollingIntervalMS = USB_POLLING_INTERVAL_MS},
  345. #endif
  346. #ifdef CONSOLE_ENABLE
  347. /*
  348. * Console
  349. */
  350. .Console_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CONSOLE_INTERFACE, .AlternateSetting = 0x00, .TotalEndpoints = 2, .Class = HID_CSCP_HIDClass, .SubClass = HID_CSCP_NonBootSubclass, .Protocol = HID_CSCP_NonBootProtocol, .InterfaceStrIndex = NO_DESCRIPTOR},
  351. .Console_HID = {.Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID}, .HIDSpec = VERSION_BCD(1, 1, 1), .CountryCode = 0x00, .TotalReportDescriptors = 1, .HIDReportType = HID_DTYPE_Report, .HIDReportLength = sizeof(ConsoleReport)},
  352. .Console_INEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_IN | CONSOLE_IN_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CONSOLE_EPSIZE, .PollingIntervalMS = 0x01},
  353. .Console_OUTEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_OUT | CONSOLE_OUT_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CONSOLE_EPSIZE, .PollingIntervalMS = 0x01},
  354. #endif
  355. #ifdef MIDI_ENABLE
  356. /*
  357. * MIDI
  358. */
  359. .Audio_Interface_Association =
  360. {
  361. .Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
  362. .FirstInterfaceIndex = AC_INTERFACE,
  363. .TotalInterfaces = 2,
  364. .Class = AUDIO_CSCP_AudioClass,
  365. .SubClass = AUDIO_CSCP_ControlSubclass,
  366. .Protocol = AUDIO_CSCP_ControlProtocol,
  367. .IADStrIndex = NO_DESCRIPTOR,
  368. },
  369. .Audio_ControlInterface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  370. .InterfaceNumber = AC_INTERFACE,
  371. .AlternateSetting = 0,
  372. .TotalEndpoints = 0,
  373. .Class = AUDIO_CSCP_AudioClass,
  374. .SubClass = AUDIO_CSCP_ControlSubclass,
  375. .Protocol = AUDIO_CSCP_ControlProtocol,
  376. .InterfaceStrIndex = NO_DESCRIPTOR},
  377. .Audio_ControlInterface_SPC =
  378. {
  379. .Header = {.Size = sizeof(USB_Audio_Descriptor_Interface_AC_t), .Type = AUDIO_DTYPE_CSInterface},
  380. .Subtype = AUDIO_DSUBTYPE_CSInterface_Header,
  381. .ACSpecification = VERSION_BCD(1, 0, 0),
  382. .TotalLength = sizeof(USB_Audio_Descriptor_Interface_AC_t),
  383. .InCollection = 1,
  384. .InterfaceNumber = AS_INTERFACE,
  385. },
  386. .Audio_StreamInterface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  387. .InterfaceNumber = AS_INTERFACE,
  388. .AlternateSetting = 0,
  389. .TotalEndpoints = 2,
  390. .Class = AUDIO_CSCP_AudioClass,
  391. .SubClass = AUDIO_CSCP_MIDIStreamingSubclass,
  392. .Protocol = AUDIO_CSCP_StreamingProtocol,
  393. .InterfaceStrIndex = NO_DESCRIPTOR},
  394. .Audio_StreamInterface_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_AudioInterface_AS_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_General, .AudioSpecification = VERSION_BCD(1, 0, 0), .TotalLength = offsetof(USB_Descriptor_Configuration_t, MIDI_Out_Jack_Endpoint_SPC) + sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t) - offsetof(USB_Descriptor_Configuration_t, Audio_StreamInterface_SPC)},
  395. .MIDI_In_Jack_Emb = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, .JackType = MIDI_JACKTYPE_Embedded, .JackID = 0x01, .JackStrIndex = NO_DESCRIPTOR},
  396. .MIDI_In_Jack_Ext = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_InputTerminal, .JackType = MIDI_JACKTYPE_External, .JackID = 0x02, .JackStrIndex = NO_DESCRIPTOR},
  397. .MIDI_Out_Jack_Emb = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, .JackType = MIDI_JACKTYPE_Embedded, .JackID = 0x03, .NumberOfPins = 1, .SourceJackID = {0x02}, .SourcePinID = {0x01}, .JackStrIndex = NO_DESCRIPTOR},
  398. .MIDI_Out_Jack_Ext = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = AUDIO_DTYPE_CSInterface}, .Subtype = AUDIO_DSUBTYPE_CSInterface_OutputTerminal, .JackType = MIDI_JACKTYPE_External, .JackID = 0x04, .NumberOfPins = 1, .SourceJackID = {0x01}, .SourcePinID = {0x01}, .JackStrIndex = NO_DESCRIPTOR},
  399. .MIDI_In_Jack_Endpoint = {.Endpoint = {.Header = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint}, .EndpointAddress = MIDI_STREAM_OUT_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MIDI_STREAM_EPSIZE, .PollingIntervalMS = 0x05},
  400. .Refresh = 0,
  401. .SyncEndpointNumber = 0},
  402. .MIDI_In_Jack_Endpoint_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = AUDIO_DTYPE_CSEndpoint}, .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, .TotalEmbeddedJacks = 0x01, .AssociatedJackID = {0x01}},
  403. .MIDI_Out_Jack_Endpoint = {.Endpoint = {.Header = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint}, .EndpointAddress = MIDI_STREAM_IN_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = MIDI_STREAM_EPSIZE, .PollingIntervalMS = 0x05},
  404. .Refresh = 0,
  405. .SyncEndpointNumber = 0},
  406. .MIDI_Out_Jack_Endpoint_SPC = {.Header = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = AUDIO_DTYPE_CSEndpoint}, .Subtype = AUDIO_DSUBTYPE_CSEndpoint_General, .TotalEmbeddedJacks = 0x01, .AssociatedJackID = {0x03}},
  407. #endif
  408. #ifdef VIRTSER_ENABLE
  409. /*
  410. * Virtual Serial
  411. */
  412. .CDC_Interface_Association =
  413. {
  414. .Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
  415. .FirstInterfaceIndex = CCI_INTERFACE,
  416. .TotalInterfaces = 2,
  417. .Class = CDC_CSCP_CDCClass,
  418. .SubClass = CDC_CSCP_ACMSubclass,
  419. .Protocol = CDC_CSCP_ATCommandProtocol,
  420. .IADStrIndex = NO_DESCRIPTOR,
  421. },
  422. .CDC_CCI_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CCI_INTERFACE, .AlternateSetting = 0, .TotalEndpoints = 1, .Class = CDC_CSCP_CDCClass, .SubClass = CDC_CSCP_ACMSubclass, .Protocol = CDC_CSCP_ATCommandProtocol, .InterfaceStrIndex = NO_DESCRIPTOR},
  423. .CDC_Functional_Header =
  424. {
  425. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface},
  426. .Subtype = 0x00,
  427. .CDCSpecification = VERSION_BCD(1, 1, 0),
  428. },
  429. .CDC_Functional_ACM =
  430. {
  431. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface},
  432. .Subtype = 0x02,
  433. .Capabilities = 0x02,
  434. },
  435. .CDC_Functional_Union =
  436. {
  437. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface},
  438. .Subtype = 0x06,
  439. .MasterInterfaceNumber = CCI_INTERFACE,
  440. .SlaveInterfaceNumber = CDI_INTERFACE,
  441. },
  442. .CDC_NotificationEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_NOTIFICATION_EPADDR, .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_NOTIFICATION_EPSIZE, .PollingIntervalMS = 0xFF},
  443. .CDC_DCI_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, .InterfaceNumber = CDI_INTERFACE, .AlternateSetting = 0, .TotalEndpoints = 2, .Class = CDC_CSCP_CDCDataClass, .SubClass = CDC_CSCP_NoDataSubclass, .Protocol = CDC_CSCP_NoDataProtocol, .InterfaceStrIndex = NO_DESCRIPTOR},
  444. .CDC_DataOutEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_OUT_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_EPSIZE, .PollingIntervalMS = 0x05},
  445. .CDC_DataInEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = CDC_IN_EPADDR, .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CDC_EPSIZE, .PollingIntervalMS = 0x05},
  446. #endif
  447. };
  448. /*
  449. * String descriptors
  450. */
  451. const USB_Descriptor_String_t PROGMEM LanguageString = {.Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String}, .UnicodeString = {LANGUAGE_ID_ENG}};
  452. const USB_Descriptor_String_t PROGMEM ManufacturerString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1), // Subtract 1 for null terminator
  453. .Type = DTYPE_String},
  454. .UnicodeString = LSTR(MANUFACTURER)};
  455. const USB_Descriptor_String_t PROGMEM ProductString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1), // Subtract 1 for null terminator
  456. .Type = DTYPE_String},
  457. .UnicodeString = LSTR(PRODUCT)};
  458. #ifndef SERIAL_NUMBER
  459. # define SERIAL_NUMBER 0
  460. #endif
  461. const USB_Descriptor_String_t PROGMEM SerialNumberString = {.Header = {.Size = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1), // Subtract 1 for null terminator
  462. .Type = DTYPE_String},
  463. .UnicodeString = LSTR(SERIAL_NUMBER)};
  464. /**
  465. * This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  466. * documentation) by the application code so that the address and size of a requested descriptor can be given
  467. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  468. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  469. * USB host.
  470. */
  471. uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const void** const DescriptorAddress) {
  472. const uint8_t DescriptorType = (wValue >> 8);
  473. const uint8_t DescriptorIndex = (wValue & 0xFF);
  474. const void* Address = NULL;
  475. uint16_t Size = NO_DESCRIPTOR;
  476. switch (DescriptorType) {
  477. case DTYPE_Device:
  478. Address = &DeviceDescriptor;
  479. Size = sizeof(USB_Descriptor_Device_t);
  480. break;
  481. case DTYPE_Configuration:
  482. Address = &ConfigurationDescriptor;
  483. Size = sizeof(USB_Descriptor_Configuration_t);
  484. break;
  485. case DTYPE_String:
  486. switch (DescriptorIndex) {
  487. case 0x00:
  488. Address = &LanguageString;
  489. Size = pgm_read_byte(&LanguageString.Header.Size);
  490. break;
  491. case 0x01:
  492. Address = &ManufacturerString;
  493. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  494. break;
  495. case 0x02:
  496. Address = &ProductString;
  497. Size = pgm_read_byte(&ProductString.Header.Size);
  498. break;
  499. case 0x03:
  500. Address = &SerialNumberString;
  501. Size = pgm_read_byte(&SerialNumberString.Header.Size);
  502. break;
  503. }
  504. break;
  505. case HID_DTYPE_HID:
  506. switch (wIndex) {
  507. #ifndef KEYBOARD_SHARED_EP
  508. case KEYBOARD_INTERFACE:
  509. Address = &ConfigurationDescriptor.Keyboard_HID;
  510. Size = sizeof(USB_HID_Descriptor_HID_t);
  511. break;
  512. #endif
  513. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  514. case MOUSE_INTERFACE:
  515. Address = &ConfigurationDescriptor.Mouse_HID;
  516. Size = sizeof(USB_HID_Descriptor_HID_t);
  517. break;
  518. #endif
  519. #ifdef SHARED_EP_ENABLE
  520. case SHARED_INTERFACE:
  521. Address = &ConfigurationDescriptor.Shared_HID;
  522. Size = sizeof(USB_HID_Descriptor_HID_t);
  523. break;
  524. #endif
  525. #ifdef RAW_ENABLE
  526. case RAW_INTERFACE:
  527. Address = &ConfigurationDescriptor.Raw_HID;
  528. Size = sizeof(USB_HID_Descriptor_HID_t);
  529. break;
  530. #endif
  531. #ifdef CONSOLE_ENABLE
  532. case CONSOLE_INTERFACE:
  533. Address = &ConfigurationDescriptor.Console_HID;
  534. Size = sizeof(USB_HID_Descriptor_HID_t);
  535. break;
  536. #endif
  537. }
  538. break;
  539. case HID_DTYPE_Report:
  540. switch (wIndex) {
  541. #ifndef KEYBOARD_SHARED_EP
  542. case KEYBOARD_INTERFACE:
  543. Address = &KeyboardReport;
  544. Size = sizeof(KeyboardReport);
  545. break;
  546. #endif
  547. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  548. case MOUSE_INTERFACE:
  549. Address = &MouseReport;
  550. Size = sizeof(MouseReport);
  551. break;
  552. #endif
  553. #ifdef SHARED_EP_ENABLE
  554. case SHARED_INTERFACE:
  555. Address = &SharedReport;
  556. Size = sizeof(SharedReport);
  557. break;
  558. #endif
  559. #ifdef RAW_ENABLE
  560. case RAW_INTERFACE:
  561. Address = &RawReport;
  562. Size = sizeof(RawReport);
  563. break;
  564. #endif
  565. #ifdef CONSOLE_ENABLE
  566. case CONSOLE_INTERFACE:
  567. Address = &ConsoleReport;
  568. Size = sizeof(ConsoleReport);
  569. break;
  570. #endif
  571. }
  572. break;
  573. }
  574. *DescriptorAddress = Address;
  575. return Size;
  576. }