Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigDescriptor.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 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 Configuration Descriptor processing routines, to determine the correct pipe configurations
  29. * needed to communication with an attached USB device. Descriptors are special computer-readable structures
  30. * which the host requests upon device enumeration, to determine the device's capabilities and functions.
  31. */
  32. #include "ConfigDescriptor.h"
  33. /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
  34. * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
  35. * with compatible devices.
  36. *
  37. * This routine searches for the first interface containing bulk IN and OUT data endpoints.
  38. *
  39. * \return An error code from the \ref AndroidHost_GetConfigDescriptorDataCodes_t enum.
  40. */
  41. uint8_t ProcessConfigurationDescriptor(void)
  42. {
  43. uint8_t ConfigDescriptorData[512];
  44. void* CurrConfigLocation = ConfigDescriptorData;
  45. uint16_t CurrConfigBytesRem;
  46. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  47. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  48. /* Retrieve the entire configuration descriptor into the allocated buffer */
  49. switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
  50. {
  51. case HOST_GETCONFIG_Successful:
  52. break;
  53. case HOST_GETCONFIG_InvalidData:
  54. return InvalidConfigDataReturned;
  55. case HOST_GETCONFIG_BuffOverflow:
  56. return DescriptorTooLarge;
  57. default:
  58. return DevControlError;
  59. }
  60. /* There should be only one compatible Android Accessory Mode interface in the device, attempt to find it */
  61. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  62. DCOMP_NextAndroidAccessoryInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  63. {
  64. return NoCompatibleInterfaceFound;
  65. }
  66. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  67. {
  68. /* Get the next Android Accessory Mode interface's data endpoint descriptor */
  69. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  70. DCOMP_NextInterfaceBulkEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  71. {
  72. /* Data endpoints not found within the first Android Accessory device interface, error out */
  73. return NoCompatibleInterfaceFound;
  74. }
  75. /* Retrieve the endpoint address from the endpoint descriptor */
  76. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  77. /* If the endpoint is a IN type endpoint */
  78. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  79. DataINEndpoint = EndpointData;
  80. else
  81. DataOUTEndpoint = EndpointData;
  82. }
  83. /* Configure the Android Accessory data IN pipe */
  84. Pipe_ConfigurePipe(ANDROID_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  85. /* Configure the Android Accessory data OUT pipe */
  86. Pipe_ConfigurePipe(ANDROID_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
  87. /* Valid data found, return success */
  88. return SuccessfulConfigRead;
  89. }
  90. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  91. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  92. * descriptor processing if an incompatible descriptor configuration is found.
  93. *
  94. * This comparator searches for the next Interface descriptor containing the correct Android Accessory Mode Class, Subclass
  95. * and Protocol values.
  96. *
  97. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  98. */
  99. uint8_t DCOMP_NextAndroidAccessoryInterface(void* const CurrentDescriptor)
  100. {
  101. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  102. if (Header->Type == DTYPE_Interface)
  103. {
  104. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  105. if ((Interface->Class == AOA_CSCP_AOADataClass) &&
  106. (Interface->SubClass == AOA_CSCP_AOADataSubclass) &&
  107. (Interface->Protocol == AOA_CSCP_AOADataProtocol))
  108. {
  109. return DESCRIPTOR_SEARCH_Found;
  110. }
  111. }
  112. return DESCRIPTOR_SEARCH_NotFound;
  113. }
  114. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  115. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  116. * descriptor processing if an incompatible descriptor configuration is found.
  117. *
  118. * This comparator searches for the next bulk Endpoint descriptor inside the current interface descriptor, aborting the
  119. * search if another interface descriptor is found before the required endpoint.
  120. *
  121. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  122. */
  123. uint8_t DCOMP_NextInterfaceBulkEndpoint(void* const CurrentDescriptor)
  124. {
  125. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  126. if (Header->Type == DTYPE_Endpoint)
  127. {
  128. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  129. uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
  130. if ((EndpointType == EP_TYPE_BULK) && (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress))))
  131. return DESCRIPTOR_SEARCH_Found;
  132. }
  133. else if (Header->Type == DTYPE_Interface)
  134. {
  135. return DESCRIPTOR_SEARCH_Fail;
  136. }
  137. return DESCRIPTOR_SEARCH_NotFound;
  138. }