Descriptors.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. Copyright 2012 Simon Foster (simon.foster [at] inbox [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
  30. * computer-readable structures which the host requests upon device enumeration, to determine
  31. * the device's capabilities and functions.
  32. */
  33. #include "Descriptors.h"
  34. /* On some devices, there is a factory set internal serial number which can be automatically sent to the host as
  35. * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL.
  36. * This allows the host to track a device across insertions on different ports, allowing them to retain allocated
  37. * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices
  38. * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value
  39. * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and
  40. * port location).
  41. */
  42. #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR)
  43. #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
  44. #endif
  45. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  46. * device characteristics, including the supported USB version, control endpoint size and the
  47. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  48. * process begins.
  49. */
  50. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  51. {
  52. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  53. .USBSpecification = VERSION_BCD(1,1,0),
  54. .Class = CDC_CSCP_CDCClass,
  55. .SubClass = CDC_CSCP_NoSpecificSubclass,
  56. .Protocol = CDC_CSCP_NoSpecificProtocol,
  57. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  58. .VendorID = 0x03EB,
  59. .ProductID = 0x204B,
  60. .ReleaseNumber = VERSION_BCD(0,0,1),
  61. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  62. .ProductStrIndex = STRING_ID_Product,
  63. .SerialNumStrIndex = USE_INTERNAL_SERIAL,
  64. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  65. };
  66. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  67. * of the device in one of its supported configurations, including information about any device interfaces
  68. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  69. * a configuration so that the host may correctly communicate with the USB device.
  70. */
  71. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  72. {
  73. .Config =
  74. {
  75. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  76. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  77. .TotalInterfaces = 2,
  78. .ConfigurationNumber = 1,
  79. .ConfigurationStrIndex = NO_DESCRIPTOR,
  80. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  81. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  82. },
  83. .CDC_CCI_Interface =
  84. {
  85. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  86. .InterfaceNumber = INTERFACE_ID_CDC_CCI,
  87. .AlternateSetting = 0,
  88. .TotalEndpoints = 1,
  89. .Class = CDC_CSCP_CDCClass,
  90. .SubClass = CDC_CSCP_ACMSubclass,
  91. .Protocol = CDC_CSCP_ATCommandProtocol,
  92. .InterfaceStrIndex = NO_DESCRIPTOR
  93. },
  94. .CDC_Functional_Header =
  95. {
  96. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
  97. .Subtype = CDC_DSUBTYPE_CSInterface_Header,
  98. .CDCSpecification = VERSION_BCD(1,1,0),
  99. },
  100. .CDC_Functional_ACM =
  101. {
  102. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
  103. .Subtype = CDC_DSUBTYPE_CSInterface_ACM,
  104. .Capabilities = 0x06,
  105. },
  106. .CDC_Functional_Union =
  107. {
  108. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
  109. .Subtype = CDC_DSUBTYPE_CSInterface_Union,
  110. .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI,
  111. .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI,
  112. },
  113. .CDC_NotificationEndpoint =
  114. {
  115. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  116. .EndpointAddress = CDC_NOTIFICATION_EPADDR,
  117. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  118. .EndpointSize = CDC_NOTIFICATION_EPSIZE,
  119. .PollingIntervalMS = 0xFF
  120. },
  121. .CDC_DCI_Interface =
  122. {
  123. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  124. .InterfaceNumber = INTERFACE_ID_CDC_DCI,
  125. .AlternateSetting = 0,
  126. .TotalEndpoints = 2,
  127. .Class = CDC_CSCP_CDCDataClass,
  128. .SubClass = CDC_CSCP_NoDataSubclass,
  129. .Protocol = CDC_CSCP_NoDataProtocol,
  130. .InterfaceStrIndex = NO_DESCRIPTOR
  131. },
  132. .CDC_DataOutEndpoint =
  133. {
  134. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  135. .EndpointAddress = CDC_RX_EPADDR,
  136. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  137. .EndpointSize = CDC_TXRX_EPSIZE,
  138. .PollingIntervalMS = 0x05
  139. },
  140. .CDC_DataInEndpoint =
  141. {
  142. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  143. .EndpointAddress = CDC_TX_EPADDR,
  144. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  145. .EndpointSize = CDC_TXRX_EPSIZE,
  146. .PollingIntervalMS = 0x05
  147. }
  148. };
  149. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  150. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  151. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  152. */
  153. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  154. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  155. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  156. * Descriptor.
  157. */
  158. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Simon Foster");
  159. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  160. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  161. * Descriptor.
  162. */
  163. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"USB-HD44780 Adapter");
  164. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  165. * documentation) by the application code so that the address and size of a requested descriptor can be given
  166. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  167. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  168. * USB host.
  169. */
  170. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  171. const uint16_t wIndex,
  172. const void** const DescriptorAddress)
  173. {
  174. const uint8_t DescriptorType = (wValue >> 8);
  175. const uint8_t DescriptorNumber = (wValue & 0xFF);
  176. const void* Address = NULL;
  177. uint16_t Size = NO_DESCRIPTOR;
  178. switch (DescriptorType)
  179. {
  180. case DTYPE_Device:
  181. Address = &DeviceDescriptor;
  182. Size = sizeof(USB_Descriptor_Device_t);
  183. break;
  184. case DTYPE_Configuration:
  185. Address = &ConfigurationDescriptor;
  186. Size = sizeof(USB_Descriptor_Configuration_t);
  187. break;
  188. case DTYPE_String:
  189. switch (DescriptorNumber)
  190. {
  191. case STRING_ID_Language:
  192. Address = &LanguageString;
  193. Size = pgm_read_byte(&LanguageString.Header.Size);
  194. break;
  195. case STRING_ID_Manufacturer:
  196. Address = &ManufacturerString;
  197. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  198. break;
  199. case STRING_ID_Product:
  200. Address = &ProductString;
  201. Size = pgm_read_byte(&ProductString.Header.Size);
  202. break;
  203. }
  204. break;
  205. }
  206. *DescriptorAddress = Address;
  207. return Size;
  208. }