Descriptors.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 MediaControlReport[] =
  40. {
  41. HID_RI_USAGE_PAGE(8, 0x0C), /* Consumer Page */
  42. HID_RI_USAGE(8, 0x01), /* Consumer Controls */
  43. HID_RI_COLLECTION(8, 0x01), /* Application */
  44. HID_RI_USAGE(8, 0xB0), /* Play */
  45. HID_RI_USAGE(8, 0xB1), /* Pause */
  46. HID_RI_USAGE(8, 0xB3), /* Fast Forward */
  47. HID_RI_USAGE(8, 0xB4), /* Rewind */
  48. HID_RI_USAGE(8, 0xB5), /* Next Track */
  49. HID_RI_USAGE(8, 0xB6), /* Previous Track */
  50. HID_RI_USAGE(8, 0xB7), /* Stop */
  51. HID_RI_USAGE(8, 0xCD), /* Play/Pause (toggle) */
  52. HID_RI_USAGE(8, 0xE2), /* Mute */
  53. HID_RI_USAGE(8, 0xE9), /* Volume Up */
  54. HID_RI_USAGE(8, 0xEA), /* Volume Down */
  55. HID_RI_REPORT_SIZE(8, 0x01),
  56. HID_RI_REPORT_COUNT(8, 0x0B),
  57. HID_RI_LOGICAL_MINIMUM(8, 0),
  58. HID_RI_LOGICAL_MAXIMUM(8, 1),
  59. HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  60. HID_RI_REPORT_COUNT(8, 0x05),
  61. HID_RI_INPUT(8, HID_IOF_CONSTANT),
  62. HID_RI_END_COLLECTION(0),
  63. };
  64. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  65. * device characteristics, including the supported USB version, control endpoint size and the
  66. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  67. * process begins.
  68. */
  69. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  70. {
  71. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  72. .USBSpecification = VERSION_BCD(1,1,0),
  73. .Class = USB_CSCP_NoDeviceClass,
  74. .SubClass = USB_CSCP_NoDeviceSubclass,
  75. .Protocol = USB_CSCP_NoDeviceProtocol,
  76. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  77. .VendorID = 0x03EB,
  78. .ProductID = 0x206A,
  79. .ReleaseNumber = VERSION_BCD(0,0,1),
  80. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  81. .ProductStrIndex = STRING_ID_Product,
  82. .SerialNumStrIndex = NO_DESCRIPTOR,
  83. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  84. };
  85. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  86. * of the device in one of its supported configurations, including information about any device interfaces
  87. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  88. * a configuration so that the host may correctly communicate with the USB device.
  89. */
  90. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  91. {
  92. .Config =
  93. {
  94. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  95. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  96. .TotalInterfaces = 1,
  97. .ConfigurationNumber = 1,
  98. .ConfigurationStrIndex = NO_DESCRIPTOR,
  99. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  100. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  101. },
  102. .HID_Interface =
  103. {
  104. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  105. .InterfaceNumber = INTERFACE_ID_HID,
  106. .AlternateSetting = 0x00,
  107. .TotalEndpoints = 1,
  108. .Class = HID_CSCP_HIDClass,
  109. .SubClass = HID_CSCP_NonBootSubclass,
  110. .Protocol = HID_CSCP_NonBootProtocol,
  111. .InterfaceStrIndex = NO_DESCRIPTOR
  112. },
  113. .HID_MediaControlHID =
  114. {
  115. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  116. .HIDSpec = VERSION_BCD(1,1,1),
  117. .CountryCode = 0x00,
  118. .TotalReportDescriptors = 1,
  119. .HIDReportType = HID_DTYPE_Report,
  120. .HIDReportLength = sizeof(MediaControlReport)
  121. },
  122. .HID_ReportINEndpoint =
  123. {
  124. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  125. .EndpointAddress = MEDIACONTROL_HID_EPADDR,
  126. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  127. .EndpointSize = MEDIACONTROL_HID_EPSIZE,
  128. .PollingIntervalMS = 0x05
  129. },
  130. };
  131. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  132. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  133. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  134. */
  135. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  136. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  137. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  138. * Descriptor.
  139. */
  140. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
  141. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  142. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  143. * Descriptor.
  144. */
  145. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA Media Controller");
  146. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  147. * documentation) by the application code so that the address and size of a requested descriptor can be given
  148. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  149. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  150. * USB host.
  151. */
  152. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  153. const uint16_t wIndex,
  154. const void** const DescriptorAddress)
  155. {
  156. const uint8_t DescriptorType = (wValue >> 8);
  157. const uint8_t DescriptorNumber = (wValue & 0xFF);
  158. const void* Address = NULL;
  159. uint16_t Size = NO_DESCRIPTOR;
  160. switch (DescriptorType)
  161. {
  162. case DTYPE_Device:
  163. Address = &DeviceDescriptor;
  164. Size = sizeof(USB_Descriptor_Device_t);
  165. break;
  166. case DTYPE_Configuration:
  167. Address = &ConfigurationDescriptor;
  168. Size = sizeof(USB_Descriptor_Configuration_t);
  169. break;
  170. case DTYPE_String:
  171. switch (DescriptorNumber)
  172. {
  173. case STRING_ID_Language:
  174. Address = &LanguageString;
  175. Size = pgm_read_byte(&LanguageString.Header.Size);
  176. break;
  177. case STRING_ID_Manufacturer:
  178. Address = &ManufacturerString;
  179. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  180. break;
  181. case STRING_ID_Product:
  182. Address = &ProductString;
  183. Size = pgm_read_byte(&ProductString.Header.Size);
  184. break;
  185. }
  186. break;
  187. case HID_DTYPE_HID:
  188. Address = &ConfigurationDescriptor.HID_MediaControlHID;
  189. Size = sizeof(USB_HID_Descriptor_HID_t);
  190. break;
  191. case HID_DTYPE_Report:
  192. Address = &MediaControlReport;
  193. Size = sizeof(MediaControlReport);
  194. break;
  195. }
  196. *DescriptorAddress = Address;
  197. return Size;
  198. }