Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Descriptors.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2012 Jun Wako <[email protected]>
  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. * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
  36. * computer-readable structures which the host requests upon device enumeration, to determine
  37. * the device's capabilities and functions.
  38. */
  39. #include "Descriptors.h"
  40. /*******************************************************************************
  41. * HID Report Descriptors
  42. ******************************************************************************/
  43. const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
  44. {
  45. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  46. HID_RI_USAGE(8, 0x06), /* Keyboard */
  47. HID_RI_COLLECTION(8, 0x01), /* Application */
  48. HID_RI_USAGE_PAGE(8, 0x07), /* Key Codes */
  49. HID_RI_USAGE_MINIMUM(8, 0xE0), /* Keyboard Left Control */
  50. HID_RI_USAGE_MAXIMUM(8, 0xE7), /* Keyboard Right GUI */
  51. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  52. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  53. HID_RI_REPORT_SIZE(8, 0x01),
  54. HID_RI_REPORT_COUNT(8, 0x08),
  55. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  56. HID_RI_REPORT_COUNT(8, 0x01),
  57. HID_RI_REPORT_SIZE(8, 0x08),
  58. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  59. HID_RI_USAGE_PAGE(8, 0x08), /* LEDs */
  60. HID_RI_USAGE_MINIMUM(8, 0x01), /* Num Lock */
  61. HID_RI_USAGE_MAXIMUM(8, 0x05), /* Kana */
  62. HID_RI_REPORT_COUNT(8, 0x05),
  63. HID_RI_REPORT_SIZE(8, 0x01),
  64. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  65. HID_RI_REPORT_COUNT(8, 0x01),
  66. HID_RI_REPORT_SIZE(8, 0x03),
  67. HID_RI_OUTPUT(8, HID_IOF_CONSTANT),
  68. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  69. HID_RI_LOGICAL_MAXIMUM(8, 0x65),
  70. HID_RI_USAGE_PAGE(8, 0x07), /* Keyboard */
  71. HID_RI_USAGE_MINIMUM(8, 0x00), /* Reserved (no event indicated) */
  72. HID_RI_USAGE_MAXIMUM(8, 0x65), /* Keyboard Application */
  73. HID_RI_REPORT_COUNT(8, 0x06),
  74. HID_RI_REPORT_SIZE(8, 0x08),
  75. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  76. HID_RI_END_COLLECTION(0),
  77. };
  78. const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
  79. {
  80. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  81. HID_RI_USAGE(8, 0x02), /* Mouse */
  82. HID_RI_COLLECTION(8, 0x01), /* Application */
  83. HID_RI_USAGE(8, 0x01), /* Pointer */
  84. HID_RI_COLLECTION(8, 0x00), /* Physical */
  85. HID_RI_USAGE_PAGE(8, 0x09), /* Button */
  86. HID_RI_USAGE_MINIMUM(8, 0x01),
  87. HID_RI_USAGE_MAXIMUM(8, 0x03),
  88. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  89. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  90. HID_RI_REPORT_COUNT(8, 0x03),
  91. HID_RI_REPORT_SIZE(8, 0x01),
  92. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  93. HID_RI_REPORT_COUNT(8, 0x01),
  94. HID_RI_REPORT_SIZE(8, 0x05),
  95. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  96. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  97. HID_RI_USAGE(8, 0x30), /* Usage X */
  98. HID_RI_USAGE(8, 0x31), /* Usage Y */
  99. HID_RI_LOGICAL_MINIMUM(8, -1),
  100. HID_RI_LOGICAL_MAXIMUM(8, 1),
  101. HID_RI_PHYSICAL_MINIMUM(8, -1),
  102. HID_RI_PHYSICAL_MAXIMUM(8, 1),
  103. HID_RI_REPORT_COUNT(8, 0x02),
  104. HID_RI_REPORT_SIZE(8, 0x08),
  105. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  106. HID_RI_END_COLLECTION(0),
  107. HID_RI_END_COLLECTION(0),
  108. };
  109. const USB_Descriptor_HIDReport_Datatype_t PROGMEM GenericReport[] =
  110. {
  111. HID_RI_USAGE_PAGE(16, 0xFF00), /* Vendor Page 0 */
  112. HID_RI_USAGE(8, 0x01), /* Vendor Usage 1 */
  113. HID_RI_COLLECTION(8, 0x01), /* Vendor Usage 1 */
  114. HID_RI_USAGE(8, 0x02), /* Vendor Usage 2 */
  115. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  116. HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
  117. HID_RI_REPORT_SIZE(8, 0x08),
  118. HID_RI_REPORT_COUNT(8, GENERIC_REPORT_SIZE),
  119. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  120. HID_RI_USAGE(8, 0x03), /* Vendor Usage 3 */
  121. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  122. HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
  123. HID_RI_REPORT_SIZE(8, 0x08),
  124. HID_RI_REPORT_COUNT(8, GENERIC_REPORT_SIZE),
  125. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  126. HID_RI_END_COLLECTION(0),
  127. };
  128. /*******************************************************************************
  129. * Device Descriptors
  130. ******************************************************************************/
  131. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  132. {
  133. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  134. .USBSpecification = VERSION_BCD(01.10),
  135. .Class = USB_CSCP_NoDeviceClass,
  136. .SubClass = USB_CSCP_NoDeviceSubclass,
  137. .Protocol = USB_CSCP_NoDeviceProtocol,
  138. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  139. .VendorID = 0xFEED,
  140. .ProductID = 0x204D,
  141. .ReleaseNumber = VERSION_BCD(00.02),
  142. .ManufacturerStrIndex = 0x01,
  143. .ProductStrIndex = 0x02,
  144. .SerialNumStrIndex = NO_DESCRIPTOR,
  145. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  146. };
  147. /*******************************************************************************
  148. * Configuration Descriptors
  149. ******************************************************************************/
  150. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  151. {
  152. .Config =
  153. {
  154. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  155. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  156. .TotalInterfaces = 3,
  157. .ConfigurationNumber = 1,
  158. .ConfigurationStrIndex = NO_DESCRIPTOR,
  159. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED), //TODO: bus powered?
  160. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  161. },
  162. /*
  163. * Keyboard
  164. */
  165. .HID1_KeyboardInterface =
  166. {
  167. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  168. .InterfaceNumber = 0x00,
  169. .AlternateSetting = 0x00,
  170. .TotalEndpoints = 2,
  171. .Class = HID_CSCP_HIDClass,
  172. .SubClass = HID_CSCP_BootSubclass,
  173. .Protocol = HID_CSCP_KeyboardBootProtocol,
  174. .InterfaceStrIndex = NO_DESCRIPTOR
  175. },
  176. .HID1_KeyboardHID =
  177. {
  178. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  179. .HIDSpec = VERSION_BCD(01.11),
  180. .CountryCode = 0x00,
  181. .TotalReportDescriptors = 1,
  182. .HIDReportType = HID_DTYPE_Report,
  183. .HIDReportLength = sizeof(KeyboardReport)
  184. },
  185. .HID1_ReportINEndpoint =
  186. {
  187. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  188. .EndpointAddress = (ENDPOINT_DIR_IN | KEYBOARD_IN_EPNUM),
  189. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  190. .EndpointSize = HID_EPSIZE,
  191. .PollingIntervalMS = 0x01
  192. },
  193. .HID1_ReportOUTEndpoint =
  194. {
  195. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  196. .EndpointAddress = (ENDPOINT_DIR_OUT | KEYBOARD_OUT_EPNUM),
  197. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  198. .EndpointSize = HID_EPSIZE,
  199. .PollingIntervalMS = 0x01
  200. },
  201. /*
  202. * Mouse
  203. */
  204. .HID2_MouseInterface =
  205. {
  206. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  207. .InterfaceNumber = 0x01,
  208. .AlternateSetting = 0x00,
  209. .TotalEndpoints = 1,
  210. .Class = HID_CSCP_HIDClass,
  211. .SubClass = HID_CSCP_BootSubclass,
  212. .Protocol = HID_CSCP_MouseBootProtocol,
  213. .InterfaceStrIndex = NO_DESCRIPTOR
  214. },
  215. .HID2_MouseHID =
  216. {
  217. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  218. .HIDSpec = VERSION_BCD(01.11),
  219. .CountryCode = 0x00,
  220. .TotalReportDescriptors = 1,
  221. .HIDReportType = HID_DTYPE_Report,
  222. .HIDReportLength = sizeof(MouseReport)
  223. },
  224. .HID2_ReportINEndpoint =
  225. {
  226. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  227. .EndpointAddress = (ENDPOINT_DIR_IN | MOUSE_IN_EPNUM),
  228. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  229. .EndpointSize = HID_EPSIZE,
  230. .PollingIntervalMS = 0x01
  231. },
  232. /*
  233. * Generic
  234. */
  235. .HID3_GenericInterface =
  236. {
  237. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  238. .InterfaceNumber = 0x02,
  239. .AlternateSetting = 0x00,
  240. .TotalEndpoints = 2,
  241. .Class = HID_CSCP_HIDClass,
  242. .SubClass = HID_CSCP_NonBootSubclass,
  243. .Protocol = HID_CSCP_NonBootProtocol,
  244. .InterfaceStrIndex = NO_DESCRIPTOR
  245. },
  246. .HID3_GenericHID =
  247. {
  248. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  249. .HIDSpec = VERSION_BCD(01.11),
  250. .CountryCode = 0x00,
  251. .TotalReportDescriptors = 1,
  252. .HIDReportType = HID_DTYPE_Report,
  253. .HIDReportLength = sizeof(GenericReport)
  254. },
  255. .HID3_ReportINEndpoint =
  256. {
  257. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  258. .EndpointAddress = (ENDPOINT_DIR_IN | GENERIC_IN_EPNUM),
  259. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  260. .EndpointSize = GENERIC_EPSIZE,
  261. .PollingIntervalMS = 0x01
  262. },
  263. .HID3_ReportOUTEndpoint =
  264. {
  265. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  266. .EndpointAddress = (ENDPOINT_DIR_OUT | GENERIC_OUT_EPNUM),
  267. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  268. .EndpointSize = GENERIC_EPSIZE,
  269. .PollingIntervalMS = 0x01
  270. }
  271. };
  272. /*******************************************************************************
  273. * String Descriptors
  274. ******************************************************************************/
  275. const USB_Descriptor_String_t PROGMEM LanguageString =
  276. {
  277. .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
  278. .UnicodeString = {LANGUAGE_ID_ENG}
  279. };
  280. const USB_Descriptor_String_t PROGMEM ManufacturerString =
  281. {
  282. .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
  283. .UnicodeString = L"Dean Camera" // TODO:
  284. };
  285. const USB_Descriptor_String_t PROGMEM ProductString =
  286. {
  287. .Header = {.Size = USB_STRING_LEN(28), .Type = DTYPE_String},
  288. .UnicodeString = L"LUFA Mouse and Keyboard Demo" // TODO:
  289. };
  290. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  291. * documentation) by the application code so that the address and size of a requested descriptor can be given
  292. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  293. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  294. * USB host.
  295. */
  296. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  297. const uint8_t wIndex,
  298. const void** const DescriptorAddress)
  299. {
  300. const uint8_t DescriptorType = (wValue >> 8);
  301. const uint8_t DescriptorIndex = (wValue & 0xFF);
  302. const void* Address = NULL;
  303. uint16_t Size = NO_DESCRIPTOR;
  304. switch (DescriptorType)
  305. {
  306. case DTYPE_Device:
  307. Address = &DeviceDescriptor;
  308. Size = sizeof(USB_Descriptor_Device_t);
  309. break;
  310. case DTYPE_Configuration:
  311. Address = &ConfigurationDescriptor;
  312. Size = sizeof(USB_Descriptor_Configuration_t);
  313. break;
  314. case DTYPE_String:
  315. switch (DescriptorIndex )
  316. {
  317. case 0x00:
  318. Address = &LanguageString;
  319. Size = pgm_read_byte(&LanguageString.Header.Size);
  320. break;
  321. case 0x01:
  322. Address = &ManufacturerString;
  323. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  324. break;
  325. case 0x02:
  326. Address = &ProductString;
  327. Size = pgm_read_byte(&ProductString.Header.Size);
  328. break;
  329. }
  330. break;
  331. case HID_DTYPE_HID:
  332. switch (wIndex) {
  333. case 0:
  334. Address = &ConfigurationDescriptor.HID1_KeyboardHID;
  335. Size = sizeof(USB_HID_Descriptor_HID_t);
  336. break;
  337. case 1:
  338. Address = &ConfigurationDescriptor.HID2_MouseHID;
  339. Size = sizeof(USB_HID_Descriptor_HID_t);
  340. break;
  341. case 2:
  342. Address = &ConfigurationDescriptor.HID3_GenericHID;
  343. Size = sizeof(USB_HID_Descriptor_HID_t);
  344. break;
  345. }
  346. break;
  347. case HID_DTYPE_Report:
  348. switch (wIndex) {
  349. case 0:
  350. Address = &KeyboardReport;
  351. Size = sizeof(KeyboardReport);
  352. break;
  353. case 1:
  354. Address = &MouseReport;
  355. Size = sizeof(MouseReport);
  356. break;
  357. case 2:
  358. Address = &GenericReport;
  359. Size = sizeof(GenericReport);
  360. break;
  361. }
  362. break;
  363. }
  364. *DescriptorAddress = Address;
  365. return Size;
  366. }