Descriptors.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  34. * device characteristics, including the supported USB version, control endpoint size and the
  35. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  36. * process begins.
  37. */
  38. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  39. {
  40. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  41. .USBSpecification = VERSION_BCD(1,1,0),
  42. .Class = CDC_CSCP_CDCClass,
  43. .SubClass = CDC_CSCP_NoSpecificSubclass,
  44. .Protocol = CDC_CSCP_NoSpecificProtocol,
  45. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  46. .VendorID = 0x03EB,
  47. .ProductID = 0x204B,
  48. .ReleaseNumber = VERSION_BCD(0,0,1),
  49. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  50. .ProductStrIndex = STRING_ID_Product,
  51. .SerialNumStrIndex = USE_INTERNAL_SERIAL,
  52. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  53. };
  54. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  55. * of the device in one of its supported configurations, including information about any device interfaces
  56. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  57. * a configuration so that the host may correctly communicate with the USB device.
  58. */
  59. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  60. {
  61. .Config =
  62. {
  63. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  64. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  65. .TotalInterfaces = 2,
  66. .ConfigurationNumber = 1,
  67. .ConfigurationStrIndex = NO_DESCRIPTOR,
  68. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  69. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  70. },
  71. .CDC_CCI_Interface =
  72. {
  73. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  74. .InterfaceNumber = INTERFACE_ID_CDC_CCI,
  75. .AlternateSetting = 0,
  76. .TotalEndpoints = 1,
  77. .Class = CDC_CSCP_CDCClass,
  78. .SubClass = CDC_CSCP_ACMSubclass,
  79. .Protocol = CDC_CSCP_ATCommandProtocol,
  80. .InterfaceStrIndex = NO_DESCRIPTOR
  81. },
  82. .CDC_Functional_Header =
  83. {
  84. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
  85. .Subtype = CDC_DSUBTYPE_CSInterface_Header,
  86. .CDCSpecification = VERSION_BCD(1,1,0),
  87. },
  88. .CDC_Functional_ACM =
  89. {
  90. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
  91. .Subtype = CDC_DSUBTYPE_CSInterface_ACM,
  92. .Capabilities = 0x06,
  93. },
  94. .CDC_Functional_Union =
  95. {
  96. .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
  97. .Subtype = CDC_DSUBTYPE_CSInterface_Union,
  98. .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI,
  99. .SlaveInterfaceNumber = INTERFACE_ID_CDC_DCI,
  100. },
  101. .CDC_NotificationEndpoint =
  102. {
  103. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  104. .EndpointAddress = CDC_NOTIFICATION_EPADDR,
  105. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  106. .EndpointSize = CDC_NOTIFICATION_EPSIZE,
  107. .PollingIntervalMS = 0xFF
  108. },
  109. .CDC_DCI_Interface =
  110. {
  111. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  112. .InterfaceNumber = INTERFACE_ID_CDC_DCI,
  113. .AlternateSetting = 0,
  114. .TotalEndpoints = 2,
  115. .Class = CDC_CSCP_CDCDataClass,
  116. .SubClass = CDC_CSCP_NoDataSubclass,
  117. .Protocol = CDC_CSCP_NoDataProtocol,
  118. .InterfaceStrIndex = NO_DESCRIPTOR
  119. },
  120. .CDC_DataOutEndpoint =
  121. {
  122. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  123. .EndpointAddress = CDC_RX_EPADDR,
  124. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  125. .EndpointSize = CDC_TXRX_EPSIZE,
  126. .PollingIntervalMS = 0x05
  127. },
  128. .CDC_DataInEndpoint =
  129. {
  130. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  131. .EndpointAddress = CDC_TX_EPADDR,
  132. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  133. .EndpointSize = CDC_TXRX_EPSIZE,
  134. .PollingIntervalMS = 0x05
  135. }
  136. };
  137. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  138. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  139. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  140. */
  141. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  142. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  143. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  144. * Descriptor.
  145. */
  146. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
  147. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  148. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  149. * Descriptor.
  150. */
  151. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA USB-RS232 Adapter");
  152. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  153. * documentation) by the application code so that the address and size of a requested descriptor can be given
  154. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  155. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  156. * USB host.
  157. */
  158. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  159. const uint16_t wIndex,
  160. const void** const DescriptorAddress)
  161. {
  162. const uint8_t DescriptorType = (wValue >> 8);
  163. const uint8_t DescriptorNumber = (wValue & 0xFF);
  164. const void* Address = NULL;
  165. uint16_t Size = NO_DESCRIPTOR;
  166. switch (DescriptorType)
  167. {
  168. case DTYPE_Device:
  169. Address = &DeviceDescriptor;
  170. Size = sizeof(USB_Descriptor_Device_t);
  171. break;
  172. case DTYPE_Configuration:
  173. Address = &ConfigurationDescriptor;
  174. Size = sizeof(USB_Descriptor_Configuration_t);
  175. break;
  176. case DTYPE_String:
  177. switch (DescriptorNumber)
  178. {
  179. case STRING_ID_Language:
  180. Address = &LanguageString;
  181. Size = pgm_read_byte(&LanguageString.Header.Size);
  182. break;
  183. case STRING_ID_Manufacturer:
  184. Address = &ManufacturerString;
  185. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  186. break;
  187. case STRING_ID_Product:
  188. Address = &ProductString;
  189. Size = pgm_read_byte(&ProductString.Header.Size);
  190. break;
  191. }
  192. break;
  193. }
  194. *DescriptorAddress = Address;
  195. return Size;
  196. }