Descriptors.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "Config/LUFAConfig.h"
  33. #include "Descriptors.h"
  34. /** HID class report descriptor. This is a special descriptor constructed with values from the
  35. * USBIF HID class specification to describe the reports and capabilities of the HID device. This
  36. * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
  37. * the device will send, and what it may be sent back from the host. Refer to the HID specification for
  38. * more details on HID report descriptors.
  39. */
  40. const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
  41. {
  42. /* Use the HID class driver's standard Keyboard report.
  43. * Max simultaneous keys: 6
  44. */
  45. HID_DESCRIPTOR_KEYBOARD(6)
  46. };
  47. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  48. * device characteristics, including the supported USB version, control endpoint size and the
  49. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  50. * process begins.
  51. */
  52. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  53. {
  54. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  55. .USBSpecification = VERSION_BCD(1,1,0),
  56. .Class = USB_CSCP_NoDeviceClass,
  57. .SubClass = USB_CSCP_NoDeviceSubclass,
  58. .Protocol = USB_CSCP_NoDeviceProtocol,
  59. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  60. .VendorID = 0x03EB,
  61. .ProductID = 0x2042,
  62. .ReleaseNumber = VERSION_BCD(0,0,1),
  63. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  64. .ProductStrIndex = STRING_ID_Product,
  65. .SerialNumStrIndex = NO_DESCRIPTOR,
  66. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  67. };
  68. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  69. * of the device in one of its supported configurations, including information about any device interfaces
  70. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  71. * a configuration so that the host may correctly communicate with the USB device.
  72. */
  73. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  74. {
  75. .Config =
  76. {
  77. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  78. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  79. .TotalInterfaces = 1,
  80. .ConfigurationNumber = 1,
  81. .ConfigurationStrIndex = NO_DESCRIPTOR,
  82. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  83. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  84. },
  85. .HID_Interface =
  86. {
  87. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  88. .InterfaceNumber = INTERFACE_ID_Keyboard,
  89. .AlternateSetting = 0x00,
  90. .TotalEndpoints = 1,
  91. .Class = HID_CSCP_HIDClass,
  92. .SubClass = HID_CSCP_BootSubclass,
  93. .Protocol = HID_CSCP_KeyboardBootProtocol,
  94. .InterfaceStrIndex = NO_DESCRIPTOR
  95. },
  96. .HID_KeyboardHID =
  97. {
  98. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  99. .HIDSpec = VERSION_BCD(1,1,1),
  100. .CountryCode = 0x00,
  101. .TotalReportDescriptors = 1,
  102. .HIDReportType = HID_DTYPE_Report,
  103. .HIDReportLength = sizeof(KeyboardReport)
  104. },
  105. .HID_ReportINEndpoint =
  106. {
  107. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  108. .EndpointAddress = KEYBOARD_EPADDR,
  109. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  110. .EndpointSize = KEYBOARD_EPSIZE,
  111. .PollingIntervalMS = 0x05
  112. },
  113. };
  114. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  115. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  116. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  117. */
  118. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  119. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  120. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  121. * Descriptor.
  122. */
  123. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"MNT");
  124. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  125. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  126. * Descriptor.
  127. */
  128. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Reform Keyboard");
  129. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  130. * documentation) by the application code so that the address and size of a requested descriptor can be given
  131. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  132. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  133. * USB host.
  134. */
  135. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  136. const uint16_t wIndex,
  137. const void** const DescriptorAddress)
  138. {
  139. const uint8_t DescriptorType = (wValue >> 8);
  140. const uint8_t DescriptorNumber = (wValue & 0xFF);
  141. const void* Address = NULL;
  142. uint16_t Size = NO_DESCRIPTOR;
  143. switch (DescriptorType)
  144. {
  145. case DTYPE_Device:
  146. Address = &DeviceDescriptor;
  147. Size = sizeof(USB_Descriptor_Device_t);
  148. break;
  149. case DTYPE_Configuration:
  150. Address = &ConfigurationDescriptor;
  151. Size = sizeof(USB_Descriptor_Configuration_t);
  152. break;
  153. case DTYPE_String:
  154. switch (DescriptorNumber)
  155. {
  156. case STRING_ID_Language:
  157. Address = &LanguageString;
  158. Size = pgm_read_byte(&LanguageString.Header.Size);
  159. break;
  160. case STRING_ID_Manufacturer:
  161. Address = &ManufacturerString;
  162. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  163. break;
  164. case STRING_ID_Product:
  165. Address = &ProductString;
  166. Size = pgm_read_byte(&ProductString.Header.Size);
  167. break;
  168. }
  169. break;
  170. case HID_DTYPE_HID:
  171. Address = &ConfigurationDescriptor.HID_KeyboardHID;
  172. Size = sizeof(USB_HID_Descriptor_HID_t);
  173. break;
  174. case HID_DTYPE_Report:
  175. Address = &KeyboardReport;
  176. Size = sizeof(KeyboardReport);
  177. break;
  178. }
  179. *DescriptorAddress = Address;
  180. return Size;
  181. }