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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "ConfigDescriptor.h"
  27. /** Interface number for the bidirectional Printer interface found within the device. */
  28. uint8_t PrinterInterfaceNumber;
  29. /** Interface Alternate Setting number for the bidirectional Printer interface found within the device. */
  30. uint8_t PrinterAltSetting;
  31. /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
  32. * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
  33. * with compatible devices.
  34. *
  35. * This routine searches for a bidirectional Printer interface descriptor containing bulk IN and OUT data endpoints.
  36. *
  37. * \return An error code from the \ref PrinterHost_GetConfigDescriptorDataCodes_t enum.
  38. */
  39. uint8_t ProcessConfigurationDescriptor(void)
  40. {
  41. uint8_t ConfigDescriptorData[512];
  42. void* CurrConfigLocation = ConfigDescriptorData;
  43. uint16_t CurrConfigBytesRem;
  44. USB_Descriptor_Interface_t* PrinterInterface = NULL;
  45. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  46. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  47. /* Retrieve the entire configuration descriptor into the allocated buffer */
  48. switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
  49. {
  50. case HOST_GETCONFIG_Successful:
  51. break;
  52. case HOST_GETCONFIG_InvalidData:
  53. return InvalidConfigDataReturned;
  54. case HOST_GETCONFIG_BuffOverflow:
  55. return DescriptorTooLarge;
  56. default:
  57. return ControlError;
  58. }
  59. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  60. {
  61. /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
  62. if (!(PrinterInterface) ||
  63. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  64. DComp_NextPrinterInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  65. {
  66. /* Get the next Printer interface from the configuration descriptor */
  67. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  68. DComp_NextBidirectionalPrinterInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  69. {
  70. /* Descriptor not found, error out */
  71. return NoCompatibleInterfaceFound;
  72. }
  73. /* Save the interface in case we need to refer back to it later */
  74. PrinterInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  75. /* Clear any found endpoints */
  76. DataINEndpoint = NULL;
  77. DataOUTEndpoint = NULL;
  78. /* Skip the remainder of the loop as we have not found an endpoint yet */
  79. continue;
  80. }
  81. /* Retrieve the endpoint address from the endpoint descriptor */
  82. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  83. /* If the endpoint is a IN type endpoint */
  84. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  85. DataINEndpoint = EndpointData;
  86. else
  87. DataOUTEndpoint = EndpointData;
  88. }
  89. /* Save Printer interface details for later use */
  90. PrinterInterfaceNumber = PrinterInterface->InterfaceNumber;
  91. PrinterAltSetting = PrinterInterface->AlternateSetting;
  92. /* Configure the Printer data IN pipe */
  93. Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  94. /* Configure the Printer data OUT pipe */
  95. Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
  96. /* Valid data found, return success */
  97. return SuccessfulConfigRead;
  98. }
  99. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  100. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  101. * descriptor processing if an incompatible descriptor configuration is found.
  102. *
  103. * This comparator searches for the next Bidirectional Printer Interface descriptor of the current Printer interface,
  104. * aborting the search if the end of the descriptors is found.
  105. *
  106. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  107. */
  108. uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor)
  109. {
  110. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  111. if (Header->Type == DTYPE_Interface)
  112. {
  113. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  114. /* Check the descriptor class, subclass and protocol, break out if correct value interface found */
  115. if ((Interface->Class == PRNT_CSCP_PrinterClass) &&
  116. (Interface->SubClass == PRNT_CSCP_PrinterSubclass) &&
  117. (Interface->Protocol == PRNT_CSCP_BidirectionalProtocol))
  118. {
  119. return DESCRIPTOR_SEARCH_Found;
  120. }
  121. }
  122. return DESCRIPTOR_SEARCH_NotFound;
  123. }
  124. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  125. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  126. * descriptor processing if an incompatible descriptor configuration is found.
  127. *
  128. * This comparator searches for the next Bulk Endpoint descriptor of the current Printer interface, aborting the
  129. * search if another interface descriptor is found before the next endpoint.
  130. *
  131. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  132. */
  133. uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor)
  134. {
  135. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  136. if (Header->Type == DTYPE_Endpoint)
  137. {
  138. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  139. /* Check the endpoint type, break out if correct BULK type endpoint found */
  140. if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK)
  141. return DESCRIPTOR_SEARCH_Found;
  142. }
  143. else if (Header->Type == DTYPE_Interface)
  144. {
  145. return DESCRIPTOR_SEARCH_Fail;
  146. }
  147. return DESCRIPTOR_SEARCH_NotFound;
  148. }