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.

descriptor.c 15KB

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