Descriptors.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 HIDReport[] =
  40. {
  41. HID_RI_USAGE_PAGE(16, 0xFFDC), /* Vendor Page 0xDC */
  42. HID_RI_USAGE(8, 0xFB), /* Vendor Usage 0xFB */
  43. HID_RI_COLLECTION(8, 0x01), /* Vendor Usage 1 */
  44. HID_RI_USAGE(8, 0x02), /* Vendor Usage 2 */
  45. HID_RI_LOGICAL_MINIMUM(8, 0x00),
  46. HID_RI_LOGICAL_MAXIMUM(8, 0xFF),
  47. HID_RI_REPORT_SIZE(8, 0x08),
  48. HID_RI_REPORT_COUNT(16, (sizeof(uint16_t) + SPM_PAGESIZE)),
  49. HID_RI_OUTPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE | HID_IOF_NON_VOLATILE),
  50. HID_RI_END_COLLECTION(0),
  51. };
  52. /** Device descriptor structure. This descriptor, located in SRAM memory, describes the overall
  53. * device characteristics, including the supported USB version, control endpoint size and the
  54. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  55. * process begins.
  56. */
  57. const USB_Descriptor_Device_t DeviceDescriptor =
  58. {
  59. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  60. .USBSpecification = VERSION_BCD(1,1,0),
  61. .Class = USB_CSCP_NoDeviceClass,
  62. .SubClass = USB_CSCP_NoDeviceSubclass,
  63. .Protocol = USB_CSCP_NoDeviceProtocol,
  64. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  65. .VendorID = 0x03EB,
  66. .ProductID = 0x2067,
  67. .ReleaseNumber = VERSION_BCD(0,0,1),
  68. .ManufacturerStrIndex = NO_DESCRIPTOR,
  69. .ProductStrIndex = NO_DESCRIPTOR,
  70. .SerialNumStrIndex = NO_DESCRIPTOR,
  71. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  72. };
  73. /** Configuration descriptor structure. This descriptor, located in SRAM memory, describes the usage
  74. * of the device in one of its supported configurations, including information about any device interfaces
  75. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  76. * a configuration so that the host may correctly communicate with the USB device.
  77. */
  78. const USB_Descriptor_Configuration_t ConfigurationDescriptor =
  79. {
  80. .Config =
  81. {
  82. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  83. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  84. .TotalInterfaces = 1,
  85. .ConfigurationNumber = 1,
  86. .ConfigurationStrIndex = NO_DESCRIPTOR,
  87. .ConfigAttributes = USB_CONFIG_ATTR_RESERVED,
  88. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  89. },
  90. .HID_Interface =
  91. {
  92. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  93. .InterfaceNumber = INTERFACE_ID_GenericHID,
  94. .AlternateSetting = 0x00,
  95. .TotalEndpoints = 1,
  96. .Class = HID_CSCP_HIDClass,
  97. .SubClass = HID_CSCP_NonBootSubclass,
  98. .Protocol = HID_CSCP_NonBootProtocol,
  99. .InterfaceStrIndex = NO_DESCRIPTOR
  100. },
  101. .HID_VendorHID =
  102. {
  103. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  104. .HIDSpec = VERSION_BCD(1,1,1),
  105. .CountryCode = 0x00,
  106. .TotalReportDescriptors = 1,
  107. .HIDReportType = HID_DTYPE_Report,
  108. .HIDReportLength = sizeof(HIDReport)
  109. },
  110. .HID_ReportINEndpoint =
  111. {
  112. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  113. .EndpointAddress = HID_IN_EPADDR,
  114. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  115. .EndpointSize = HID_IN_EPSIZE,
  116. .PollingIntervalMS = 0x05
  117. },
  118. };
  119. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  120. * documentation) by the application code so that the address and size of a requested descriptor can be given
  121. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  122. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  123. * USB host.
  124. */
  125. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  126. const uint16_t wIndex,
  127. const void** const DescriptorAddress)
  128. {
  129. const uint8_t DescriptorType = (wValue >> 8);
  130. const void* Address = NULL;
  131. uint16_t Size = NO_DESCRIPTOR;
  132. /* If/Else If chain compiles slightly smaller than a switch case */
  133. if (DescriptorType == DTYPE_Device)
  134. {
  135. Address = &DeviceDescriptor;
  136. Size = sizeof(USB_Descriptor_Device_t);
  137. }
  138. else if (DescriptorType == DTYPE_Configuration)
  139. {
  140. Address = &ConfigurationDescriptor;
  141. Size = sizeof(USB_Descriptor_Configuration_t);
  142. }
  143. else if (DescriptorType == HID_DTYPE_HID)
  144. {
  145. Address = &ConfigurationDescriptor.HID_VendorHID;
  146. Size = sizeof(USB_HID_Descriptor_HID_t);
  147. }
  148. else if (DescriptorType == HID_DTYPE_Report)
  149. {
  150. Address = &HIDReport;
  151. Size = sizeof(HIDReport);
  152. }
  153. *DescriptorAddress = Address;
  154. return Size;
  155. }