MNT Reform: Open Source Portable Computer https://mntre.com/reform
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.

262 lines
10KB

  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
  29. * computer-readable structures which the host requests upon device enumeration, to determine
  30. * the device's capabilities and functions.
  31. */
  32. #include "Descriptors.h"
  33. /** HID class report descriptor. This is a special descriptor constructed with values from the
  34. * USBIF HID class specification to describe the reports and capabilities of the HID device. This
  35. * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
  36. * the device will send, and what it may be sent back from the host. Refer to the HID specification for
  37. * more details on HID report descriptors.
  38. */
  39. const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
  40. {
  41. /* Use the HID class driver's standard Mouse report.
  42. * Min X/Y Axis values: -1
  43. * Max X/Y Axis values: 1
  44. * Min physical X/Y Axis values (used to determine resolution): -1
  45. * Max physical X/Y Axis values (used to determine resolution): 1
  46. * Buttons: 3
  47. * Absolute screen coordinates: false
  48. */
  49. //HID_DESCRIPTOR_MOUSE(-127, 127, -127, 127, 3, false)
  50. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  51. HID_RI_USAGE(8, 0x02), /* Mouse */
  52. HID_RI_COLLECTION(8, 0x01), /* Application */
  53. HID_RI_USAGE(8, 0x01), /* Pointer */
  54. HID_RI_COLLECTION(8, 0x00), /* Physical */
  55. HID_RI_USAGE_PAGE(8, 0x09), /* Button */
  56. HID_RI_USAGE_MINIMUM(8, 0x01),
  57. HID_RI_USAGE_MAXIMUM(8, 0x03),
  58. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  59. HID_RI_LOGICAL_MAXIMUM(8, 0x01),
  60. HID_RI_REPORT_COUNT(8, 0x03),
  61. HID_RI_REPORT_SIZE(8, 0x01),
  62. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  63. HID_RI_REPORT_COUNT(8, 0x01), // 5 bits padding
  64. HID_RI_REPORT_SIZE(8, 0x05),
  65. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  66. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  67. HID_RI_USAGE(8, 0x30), /* Usage X */
  68. HID_RI_USAGE(8, 0x31), /* Usage Y */
  69. HID_RI_LOGICAL_MINIMUM(8, -127),
  70. HID_RI_LOGICAL_MAXIMUM(8, 127),
  71. HID_RI_PHYSICAL_MINIMUM(8, -127),
  72. HID_RI_PHYSICAL_MAXIMUM(8, 127),
  73. HID_RI_REPORT_COUNT(8, 0x02),
  74. HID_RI_REPORT_SIZE(8, 0x08),
  75. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  76. // ---
  77. HID_RI_USAGE_PAGE(8, 0x01), /* Generic Desktop */
  78. HID_RI_USAGE(8, 0x38), /* Usage Wheel */
  79. HID_RI_LOGICAL_MINIMUM(8, -127),
  80. HID_RI_LOGICAL_MAXIMUM(8, 127),
  81. HID_RI_PHYSICAL_MINIMUM(8, -127),
  82. HID_RI_PHYSICAL_MAXIMUM(8, 127),
  83. HID_RI_REPORT_COUNT(8, 0x01),
  84. HID_RI_REPORT_SIZE(8, 0x08),
  85. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  86. // ---
  87. HID_RI_END_COLLECTION(0),
  88. HID_RI_END_COLLECTION(0),
  89. };
  90. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  91. * device characteristics, including the supported USB version, control endpoint size and the
  92. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  93. * process begins.
  94. */
  95. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  96. {
  97. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  98. .USBSpecification = VERSION_BCD(1,1,0),
  99. .Class = USB_CSCP_NoDeviceClass,
  100. .SubClass = USB_CSCP_NoDeviceSubclass,
  101. .Protocol = USB_CSCP_NoDeviceProtocol,
  102. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  103. .VendorID = 0x03EB,
  104. .ProductID = 0x2041,
  105. .ReleaseNumber = VERSION_BCD(0,0,1),
  106. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  107. .ProductStrIndex = STRING_ID_Product,
  108. .SerialNumStrIndex = NO_DESCRIPTOR,
  109. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  110. };
  111. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  112. * of the device in one of its supported configurations, including information about any device interfaces
  113. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  114. * a configuration so that the host may correctly communicate with the USB device.
  115. */
  116. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  117. {
  118. .Config =
  119. {
  120. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  121. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  122. .TotalInterfaces = 1,
  123. .ConfigurationNumber = 1,
  124. .ConfigurationStrIndex = NO_DESCRIPTOR,
  125. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  126. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  127. },
  128. .HID_Interface =
  129. {
  130. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  131. .InterfaceNumber = INTERFACE_ID_Mouse,
  132. .AlternateSetting = 0x00,
  133. .TotalEndpoints = 1,
  134. .Class = HID_CSCP_HIDClass,
  135. .SubClass = HID_CSCP_BootSubclass,
  136. .Protocol = HID_CSCP_MouseBootProtocol,
  137. .InterfaceStrIndex = NO_DESCRIPTOR
  138. },
  139. .HID_MouseHID =
  140. {
  141. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  142. .HIDSpec = VERSION_BCD(1,1,1),
  143. .CountryCode = 0x00,
  144. .TotalReportDescriptors = 1,
  145. .HIDReportType = HID_DTYPE_Report,
  146. .HIDReportLength = sizeof(MouseReport)
  147. },
  148. .HID_ReportINEndpoint =
  149. {
  150. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  151. .EndpointAddress = MOUSE_EPADDR,
  152. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  153. .EndpointSize = MOUSE_EPSIZE,
  154. .PollingIntervalMS = 0x05
  155. }
  156. };
  157. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  158. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  159. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  160. */
  161. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  162. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  163. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  164. * Descriptor.
  165. */
  166. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"MNT");
  167. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  168. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  169. * Descriptor.
  170. */
  171. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Reform Trackpad");
  172. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  173. * documentation) by the application code so that the address and size of a requested descriptor can be given
  174. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  175. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  176. * USB host.
  177. */
  178. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  179. const uint16_t wIndex,
  180. const void** const DescriptorAddress)
  181. {
  182. const uint8_t DescriptorType = (wValue >> 8);
  183. const uint8_t DescriptorNumber = (wValue & 0xFF);
  184. const void* Address = NULL;
  185. uint16_t Size = NO_DESCRIPTOR;
  186. switch (DescriptorType)
  187. {
  188. case DTYPE_Device:
  189. Address = &DeviceDescriptor;
  190. Size = sizeof(USB_Descriptor_Device_t);
  191. break;
  192. case DTYPE_Configuration:
  193. Address = &ConfigurationDescriptor;
  194. Size = sizeof(USB_Descriptor_Configuration_t);
  195. break;
  196. case DTYPE_String:
  197. switch (DescriptorNumber)
  198. {
  199. case STRING_ID_Language:
  200. Address = &LanguageString;
  201. Size = pgm_read_byte(&LanguageString.Header.Size);
  202. break;
  203. case STRING_ID_Manufacturer:
  204. Address = &ManufacturerString;
  205. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  206. break;
  207. case STRING_ID_Product:
  208. Address = &ProductString;
  209. Size = pgm_read_byte(&ProductString.Header.Size);
  210. break;
  211. }
  212. break;
  213. case HID_DTYPE_HID:
  214. Address = &ConfigurationDescriptor.HID_MouseHID;
  215. Size = sizeof(USB_HID_Descriptor_HID_t);
  216. break;
  217. case HID_DTYPE_Report:
  218. Address = &MouseReport;
  219. Size = sizeof(MouseReport);
  220. break;
  221. }
  222. *DescriptorAddress = Address;
  223. return Size;
  224. }