Descriptors.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 SRAM 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 DeviceDescriptor =
  39. {
  40. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  41. .USBSpecification = VERSION_BCD(1,1,0),
  42. .Class = USB_CSCP_NoDeviceClass,
  43. .SubClass = USB_CSCP_NoDeviceSubclass,
  44. .Protocol = USB_CSCP_NoDeviceProtocol,
  45. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  46. .VendorID = 0x03EB,
  47. .ProductID = PRODUCT_ID_CODE,
  48. .ReleaseNumber = VERSION_BCD(0,0,0),
  49. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  50. .ProductStrIndex = STRING_ID_Product,
  51. .SerialNumStrIndex = NO_DESCRIPTOR,
  52. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  53. };
  54. /** Configuration descriptor structure. This descriptor, located in SRAM 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 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 = 1,
  66. .ConfigurationNumber = 1,
  67. .ConfigurationStrIndex = NO_DESCRIPTOR,
  68. .ConfigAttributes = USB_CONFIG_ATTR_RESERVED,
  69. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  70. },
  71. .DFU_Interface =
  72. {
  73. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  74. .InterfaceNumber = INTERFACE_ID_DFU,
  75. .AlternateSetting = 0,
  76. .TotalEndpoints = 0,
  77. .Class = 0xFE,
  78. .SubClass = 0x01,
  79. .Protocol = 0x02,
  80. .InterfaceStrIndex = NO_DESCRIPTOR
  81. },
  82. .DFU_Functional =
  83. {
  84. .Header = {.Size = sizeof(USB_Descriptor_DFU_Functional_t), .Type = DTYPE_DFUFunctional},
  85. .Attributes = (ATTR_CAN_UPLOAD | ATTR_CAN_DOWNLOAD),
  86. .DetachTimeout = 0x0000,
  87. .TransferSize = 0x0C00,
  88. .DFUSpecification = VERSION_BCD(1,1,0)
  89. }
  90. };
  91. /** Language descriptor structure. This descriptor, located in SRAM memory, is returned when the host requests
  92. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  93. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  94. */
  95. const USB_Descriptor_String_t LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  96. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  97. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  98. * Descriptor.
  99. */
  100. const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
  101. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  102. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  103. * Descriptor.
  104. */
  105. const USB_Descriptor_String_t ProductString = USB_STRING_DESCRIPTOR(L"LUFA DFU");
  106. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  107. * documentation) by the application code so that the address and size of a requested descriptor can be given
  108. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  109. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  110. * USB host.
  111. */
  112. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  113. const uint16_t wIndex,
  114. const void** const DescriptorAddress)
  115. {
  116. const uint8_t DescriptorType = (wValue >> 8);
  117. const uint8_t DescriptorNumber = (wValue & 0xFF);
  118. const void* Address = NULL;
  119. uint16_t Size = NO_DESCRIPTOR;
  120. switch (DescriptorType)
  121. {
  122. case DTYPE_Device:
  123. Address = &DeviceDescriptor;
  124. Size = sizeof(USB_Descriptor_Device_t);
  125. break;
  126. case DTYPE_Configuration:
  127. Address = &ConfigurationDescriptor;
  128. Size = sizeof(USB_Descriptor_Configuration_t);
  129. break;
  130. case DTYPE_String:
  131. if (DescriptorNumber == STRING_ID_Language)
  132. {
  133. Address = &LanguageString;
  134. Size = LanguageString.Header.Size;
  135. }
  136. else if (DescriptorNumber == STRING_ID_Manufacturer)
  137. {
  138. Address = &ManufacturerString;
  139. Size = ManufacturerString.Header.Size;
  140. }
  141. else if (DescriptorNumber == STRING_ID_Product)
  142. {
  143. Address = &ProductString;
  144. Size = ProductString.Header.Size;
  145. }
  146. break;
  147. }
  148. *DescriptorAddress = Address;
  149. return Size;
  150. }