Descriptors.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 2010 OBinou (obconseil [at] gmail [dot] com)
  9. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [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. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  35. * device characteristics, including the supported USB version, control endpoint size and the
  36. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  37. * process begins.
  38. */
  39. const USB_Descriptor_Device_t PROGMEM RelayBoard_DeviceDescriptor =
  40. {
  41. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  42. .USBSpecification = VERSION_BCD(1,1,0),
  43. .Class = USB_CSCP_VendorSpecificClass,
  44. .SubClass = USB_CSCP_NoDeviceSubclass,
  45. .Protocol = USB_CSCP_NoDeviceProtocol,
  46. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  47. .VendorID = 0x04B4,
  48. .ProductID = 0xFD11,
  49. .ReleaseNumber = VERSION_BCD(2,0,0),
  50. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  51. .ProductStrIndex = STRING_ID_Product,
  52. .SerialNumStrIndex = STRING_ID_Serial,
  53. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  54. };
  55. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  56. * of the device in one of its supported configurations, including information about any device interfaces
  57. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  58. * a configuration so that the host may correctly communicate with the USB device.
  59. */
  60. const USB_Descriptor_Configuration_t PROGMEM RelayBoard_ConfigurationDescriptor =
  61. {
  62. .Config =
  63. {
  64. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  65. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  66. .TotalInterfaces = 1,
  67. .ConfigurationNumber = 1,
  68. .ConfigurationStrIndex = NO_DESCRIPTOR,
  69. .ConfigAttributes = USB_CONFIG_ATTR_RESERVED,
  70. .MaxPowerConsumption = USB_CONFIG_POWER_MA(500)
  71. },
  72. .RelayBoardInterface =
  73. {
  74. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  75. .InterfaceNumber = INTERFACE_ID_RelayBoard,
  76. .AlternateSetting = 0,
  77. .TotalEndpoints = 0,
  78. .Class = USB_CSCP_VendorSpecificClass,
  79. .SubClass = 0x00,
  80. .Protocol = 0x00,
  81. .InterfaceStrIndex = NO_DESCRIPTOR
  82. },
  83. };
  84. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  85. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  86. * via the language ID table available at USB.org what languages the device supports for its string descriptors.
  87. */
  88. const USB_Descriptor_String_t PROGMEM RelayBoard_LanguageString =
  89. {
  90. .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
  91. .UnicodeString = {LANGUAGE_ID_ENG}
  92. };
  93. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  94. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  95. * Descriptor.
  96. */
  97. const USB_Descriptor_String_t PROGMEM RelayBoard_ManufacturerString = USB_STRING_DESCRIPTOR(L"SISPM");
  98. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  99. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  100. * Descriptor.
  101. */
  102. const USB_Descriptor_String_t PROGMEM RelayBoard_ProductString = USB_STRING_DESCRIPTOR(L"RelayBoard");
  103. /** Serial number string. This is a Unicode string containing the device's unique serial number, expressed as a
  104. * series of uppercase hexadecimal digits.
  105. */
  106. const USB_Descriptor_String_t PROGMEM RelayBoard_SerialString = USB_STRING_DESCRIPTOR(L"00001");
  107. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  108. * documentation) by the application code so that the address and size of a requested descriptor can be given
  109. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  110. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  111. * USB host.
  112. */
  113. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  114. const uint16_t wIndex,
  115. const void** const DescriptorAddress)
  116. {
  117. const uint8_t DescriptorType = (wValue >> 8);
  118. const uint8_t DescriptorNumber = (wValue & 0xFF);
  119. const void* Address = NULL;
  120. uint16_t Size = NO_DESCRIPTOR;
  121. switch (DescriptorType)
  122. {
  123. case DTYPE_Device:
  124. Address = &RelayBoard_DeviceDescriptor;
  125. Size = sizeof(USB_Descriptor_Device_t);
  126. break;
  127. case DTYPE_Configuration:
  128. Address = &RelayBoard_ConfigurationDescriptor;
  129. Size = sizeof(USB_Descriptor_Configuration_t);
  130. break;
  131. case DTYPE_String:
  132. switch (DescriptorNumber)
  133. {
  134. case STRING_ID_Language:
  135. Address = &RelayBoard_LanguageString;
  136. Size = pgm_read_byte(&RelayBoard_LanguageString.Header.Size);
  137. break;
  138. case STRING_ID_Manufacturer:
  139. Address = &RelayBoard_ManufacturerString;
  140. Size = pgm_read_byte(&RelayBoard_ManufacturerString.Header.Size);
  141. break;
  142. case STRING_ID_Product:
  143. Address = &RelayBoard_ProductString;
  144. Size = pgm_read_byte(&RelayBoard_ProductString.Header.Size);
  145. break;
  146. case STRING_ID_Serial:
  147. Address = &RelayBoard_SerialString;
  148. Size = pgm_read_byte(&RelayBoard_SerialString.Header.Size);
  149. break;
  150. }
  151. break;
  152. }
  153. *DescriptorAddress = Address;
  154. return Size;
  155. }