Descriptors.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 Denver Gingerich (denver [at] ossguy [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. /** HID report descriptor. This is a HID class specific descriptor, which defines the structure of the
  35. * reports sent and received by the HID device to and from the USB host. It indicates what data is sent,
  36. * where in the report each element is located and exactly how the data should be interpreted and used.
  37. *
  38. * See the HID class specification for more information on HID report descriptors.
  39. */
  40. const USB_Descriptor_HIDReport_Datatype_t PROGMEM KeyboardReport[] =
  41. {
  42. /* Use the HID class driver's standard Keyboard report.
  43. * Max simultaneous keys: 6
  44. */
  45. HID_DESCRIPTOR_KEYBOARD(6)
  46. };
  47. /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
  48. * device characteristics, including the supported USB version, control endpoint size and the
  49. * number of device configurations. The descriptor is read out by the USB host when the enumeration
  50. * process begins.
  51. */
  52. const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
  53. {
  54. .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
  55. .USBSpecification = VERSION_BCD(1,1,0),
  56. .Class = USB_CSCP_NoDeviceClass,
  57. .SubClass = USB_CSCP_NoDeviceSubclass,
  58. .Protocol = USB_CSCP_NoDeviceProtocol,
  59. .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
  60. .VendorID = 0x03EB,
  61. .ProductID = 0x2042,
  62. .ReleaseNumber = VERSION_BCD(0,0,1),
  63. .ManufacturerStrIndex = STRING_ID_Manufacturer,
  64. .ProductStrIndex = STRING_ID_Product,
  65. .SerialNumStrIndex = USE_INTERNAL_SERIAL,
  66. .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
  67. };
  68. /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
  69. * of the device in one of its supported configurations, including information about any device interfaces
  70. * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
  71. * a configuration so that the host may correctly communicate with the USB device.
  72. */
  73. const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
  74. {
  75. .Config =
  76. {
  77. .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
  78. .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
  79. .TotalInterfaces = 1,
  80. .ConfigurationNumber = 1,
  81. .ConfigurationStrIndex = NO_DESCRIPTOR,
  82. .ConfigAttributes = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
  83. .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
  84. },
  85. .HID_Interface =
  86. {
  87. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  88. .InterfaceNumber = INTERFACE_ID_Keyboard,
  89. .AlternateSetting = 0x00,
  90. .TotalEndpoints = 1,
  91. .Class = HID_CSCP_HIDClass,
  92. .SubClass = HID_CSCP_BootSubclass,
  93. .Protocol = HID_CSCP_KeyboardBootProtocol,
  94. .InterfaceStrIndex = NO_DESCRIPTOR
  95. },
  96. .HID_KeyboardHID =
  97. {
  98. .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
  99. .HIDSpec = VERSION_BCD(1,1,1),
  100. .CountryCode = 0x00,
  101. .TotalReportDescriptors = 1,
  102. .HIDReportType = HID_DTYPE_Report,
  103. .HIDReportLength = sizeof(KeyboardReport)
  104. },
  105. .HID_ReportINEndpoint =
  106. {
  107. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  108. .EndpointAddress = KEYBOARD_EPADDR,
  109. .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  110. .EndpointSize = KEYBOARD_EPSIZE,
  111. .PollingIntervalMS = 0x05
  112. },
  113. };
  114. /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
  115. * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
  116. * via the language ID table available at USB.org what languages the device supports for its string descriptors. */
  117. const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
  118. /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
  119. * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  120. * Descriptor.
  121. */
  122. const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera and Denver Gingerich");
  123. /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
  124. * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
  125. * Descriptor.
  126. */
  127. const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"Magnetic Card Reader");
  128. /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
  129. * documentation) by the application code so that the address and size of a requested descriptor can be given
  130. * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
  131. * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
  132. * USB host.
  133. */
  134. uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
  135. const uint16_t wIndex,
  136. const void** const DescriptorAddress)
  137. {
  138. const uint8_t DescriptorType = (wValue >> 8);
  139. const uint8_t DescriptorNumber = (wValue & 0xFF);
  140. const void* Address = NULL;
  141. uint16_t Size = NO_DESCRIPTOR;
  142. switch (DescriptorType)
  143. {
  144. case DTYPE_Device:
  145. Address = &DeviceDescriptor;
  146. Size = sizeof(USB_Descriptor_Device_t);
  147. break;
  148. case DTYPE_Configuration:
  149. Address = &ConfigurationDescriptor;
  150. Size = sizeof(USB_Descriptor_Configuration_t);
  151. break;
  152. case DTYPE_String:
  153. switch (DescriptorNumber)
  154. {
  155. case STRING_ID_Language:
  156. Address = &LanguageString;
  157. Size = pgm_read_byte(&LanguageString.Header.Size);
  158. break;
  159. case STRING_ID_Manufacturer:
  160. Address = &ManufacturerString;
  161. Size = pgm_read_byte(&ManufacturerString.Header.Size);
  162. break;
  163. case STRING_ID_Product:
  164. Address = &ProductString;
  165. Size = pgm_read_byte(&ProductString.Header.Size);
  166. break;
  167. }
  168. break;
  169. case HID_DTYPE_HID:
  170. Address = &ConfigurationDescriptor.HID_KeyboardHID;
  171. Size = sizeof(USB_HID_Descriptor_HID_t);
  172. break;
  173. case HID_DTYPE_Report:
  174. Address = &KeyboardReport;
  175. Size = sizeof(KeyboardReport);
  176. break;
  177. }
  178. *DescriptorAddress = Address;
  179. return Size;
  180. }