Descriptors.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 describes the overall device
  34. * characteristics, including the supported USB version, control endpoint size
  35. * and the number of device configurations. The descriptor is read out by the
  36. * USB host when the enumeration 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(2,0,0),
  42. .Class = USB_CSCP_NoDeviceClass,
  43. .SubClass = USB_CSCP_NoDeviceSubclass,
  44. .Protocol = USB_CSCP_NoDeviceProtocol,
  45. .Endpoint0Size = 64,
  46. .VendorID = 0x0000,
  47. .ProductID = 0x0000,
  48. .ReleaseNumber = VERSION_BCD(0,0,2),
  49. .ManufacturerStrIndex = 0x01,
  50. .ProductStrIndex = 0x02,
  51. .SerialNumStrIndex = NO_DESCRIPTOR,
  52. .NumberOfConfigurations = 1
  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 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 = 0,
  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. };
  72. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  73. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  74. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  75. */
  76. const USB_Descriptor_String_t LanguageString =
  77. {
  78. .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
  79. .UnicodeString = {LANGUAGE_ID_ENG}
  80. };
  81. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  82. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  83. * Descriptor.
  84. */
  85. const USB_Descriptor_String_t ManufacturerString =
  86. {
  87. .Header = {.Size = USB_STRING_LEN(14), .Type = DTYPE_String},
  88. .UnicodeString = L"Your Name Here"
  89. };
  90. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  91. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  92. * Descriptor.
  93. */
  94. const USB_Descriptor_String_t ProductString =
  95. {
  96. .Header = {.Size = USB_STRING_LEN(15), .Type = DTYPE_String},
  97. .UnicodeString = L"LUFA USB Device"
  98. };
  99. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  100. * documentation) by the application code so that the address and size of a requested descriptor can be given
  101. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  102. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  103. * USB host.
  104. */
  105. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  106. const uint16_t wIndex,
  107. const void** const DescriptorAddress
  108. #if defined(HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES)
  109. , uint8_t* const DescriptorMemorySpace
  110. #endif
  111. )
  112. {
  113. const uint8_t DescriptorType = (wValue >> 8);
  114. const uint8_t DescriptorNumber = (wValue & 0xFF);
  115. const void* Address = NULL;
  116. uint16_t Size = NO_DESCRIPTOR;
  117. switch (DescriptorType)
  118. {
  119. case DTYPE_Device:
  120. Address = &DeviceDescriptor;
  121. Size = sizeof(USB_Descriptor_Device_t);
  122. break;
  123. case DTYPE_Configuration:
  124. Address = &ConfigurationDescriptor;
  125. Size = sizeof(USB_Descriptor_Configuration_t);
  126. break;
  127. case DTYPE_String:
  128. switch (DescriptorNumber)
  129. {
  130. case 0x00:
  131. Address = &LanguageString;
  132. Size = pgm_read_byte(&LanguageString.Header.Size);
  133. break;
  134. case 0x01:
  135. Address = &ManufacturerString;
  136. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  137. break;
  138. case 0x02:
  139. Address = &ProductString;
  140. Size = pgm_read_byte(&ProductString.Header.Size);
  141. break;
  142. }
  143. break;
  144. }
  145. #if defined(HAS_MULTIPLE_DESCRIPTOR_ADDRESS_SPACES)
  146. *DescriptorMemorySpace = MEMSPACE_RAM;
  147. #endif
  148. *DescriptorAddress = Address;
  149. return Size;
  150. }