Descriptors.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = 0x2045,
  48. .ReleaseNumber = VERSION_BCD(0,0,1),
  49. .ManufacturerStrIndex = NO_DESCRIPTOR,
  50. .ProductStrIndex = NO_DESCRIPTOR,
  51. .SerialNumStrIndex = NO_DESCRIPTOR,
  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 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. .MS_Interface =
  72. {
  73. .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
  74. .InterfaceNumber = INTERFACE_ID_MassStorage,
  75. .AlternateSetting = 0,
  76. .TotalEndpoints = 2,
  77. .Class = MS_CSCP_MassStorageClass,
  78. .SubClass = MS_CSCP_SCSITransparentSubclass,
  79. .Protocol = MS_CSCP_BulkOnlyTransportProtocol,
  80. .InterfaceStrIndex = NO_DESCRIPTOR
  81. },
  82. .MS_DataInEndpoint =
  83. {
  84. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  85. .EndpointAddress = MASS_STORAGE_IN_EPADDR,
  86. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  87. .EndpointSize = MASS_STORAGE_IO_EPSIZE,
  88. .PollingIntervalMS = 0x05
  89. },
  90. .MS_DataOutEndpoint =
  91. {
  92. .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
  93. .EndpointAddress = MASS_STORAGE_OUT_EPADDR,
  94. .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
  95. .EndpointSize = MASS_STORAGE_IO_EPSIZE,
  96. .PollingIntervalMS = 0x05
  97. }
  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. {
  109. const uint8_t DescriptorType = (wValue >> 8);
  110. const void* Address = NULL;
  111. uint16_t Size = NO_DESCRIPTOR;
  112. /* If/Else If chain compiles slightly smaller than a switch case */
  113. if (DescriptorType == DTYPE_Device)
  114. {
  115. Address = &DeviceDescriptor;
  116. Size = sizeof(USB_Descriptor_Device_t);
  117. }
  118. else if (DescriptorType == DTYPE_Configuration)
  119. {
  120. Address = &ConfigurationDescriptor;
  121. Size = sizeof(USB_Descriptor_Configuration_t);
  122. }
  123. *DescriptorAddress = Address;
  124. return Size;
  125. }