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.

ConfigDescriptors.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #define __INCLUDE_FROM_USB_DRIVER
  27. #include "ConfigDescriptors.h"
  28. #if defined(USB_CAN_BE_HOST)
  29. uint8_t USB_Host_GetDeviceConfigDescriptor(const uint8_t ConfigNumber,
  30. uint16_t* const ConfigSizePtr,
  31. void* const BufferPtr,
  32. const uint16_t BufferSize)
  33. {
  34. uint8_t ErrorCode;
  35. uint8_t ConfigHeader[sizeof(USB_Descriptor_Configuration_Header_t)];
  36. USB_ControlRequest = (USB_Request_Header_t)
  37. {
  38. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
  39. .bRequest = REQ_GetDescriptor,
  40. .wValue = ((DTYPE_Configuration << 8) | (ConfigNumber - 1)),
  41. .wIndex = 0,
  42. .wLength = sizeof(USB_Descriptor_Configuration_Header_t),
  43. };
  44. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  45. if ((ErrorCode = USB_Host_SendControlRequest(ConfigHeader)) != HOST_SENDCONTROL_Successful)
  46. return ErrorCode;
  47. *ConfigSizePtr = le16_to_cpu(DESCRIPTOR_PCAST(ConfigHeader, USB_Descriptor_Configuration_Header_t)->TotalConfigurationSize);
  48. if (*ConfigSizePtr > BufferSize)
  49. return HOST_GETCONFIG_BuffOverflow;
  50. USB_ControlRequest.wLength = *ConfigSizePtr;
  51. if ((ErrorCode = USB_Host_SendControlRequest(BufferPtr)) != HOST_SENDCONTROL_Successful)
  52. return ErrorCode;
  53. if (DESCRIPTOR_TYPE(BufferPtr) != DTYPE_Configuration)
  54. return HOST_GETCONFIG_InvalidData;
  55. return HOST_GETCONFIG_Successful;
  56. }
  57. #endif
  58. void USB_GetNextDescriptorOfType(uint16_t* const BytesRem,
  59. void** const CurrConfigLoc,
  60. const uint8_t Type)
  61. {
  62. while (*BytesRem)
  63. {
  64. USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
  65. if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
  66. return;
  67. }
  68. }
  69. void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
  70. void** const CurrConfigLoc,
  71. const uint8_t Type,
  72. const uint8_t BeforeType)
  73. {
  74. while (*BytesRem)
  75. {
  76. USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
  77. if (DESCRIPTOR_TYPE(*CurrConfigLoc) == Type)
  78. {
  79. return;
  80. }
  81. else if (DESCRIPTOR_TYPE(*CurrConfigLoc) == BeforeType)
  82. {
  83. *BytesRem = 0;
  84. return;
  85. }
  86. }
  87. }
  88. void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
  89. void** const CurrConfigLoc,
  90. const uint8_t Type,
  91. const uint8_t AfterType)
  92. {
  93. USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, AfterType);
  94. if (*BytesRem)
  95. USB_GetNextDescriptorOfType(BytesRem, CurrConfigLoc, Type);
  96. }
  97. uint8_t USB_GetNextDescriptorComp(uint16_t* const BytesRem,
  98. void** const CurrConfigLoc,
  99. ConfigComparatorPtr_t const ComparatorRoutine)
  100. {
  101. uint8_t ErrorCode;
  102. while (*BytesRem)
  103. {
  104. uint8_t* PrevDescLoc = *CurrConfigLoc;
  105. uint16_t PrevBytesRem = *BytesRem;
  106. USB_GetNextDescriptor(BytesRem, CurrConfigLoc);
  107. if ((ErrorCode = ComparatorRoutine(*CurrConfigLoc)) != DESCRIPTOR_SEARCH_NotFound)
  108. {
  109. if (ErrorCode == DESCRIPTOR_SEARCH_Fail)
  110. {
  111. *CurrConfigLoc = PrevDescLoc;
  112. *BytesRem = PrevBytesRem;
  113. }
  114. return ErrorCode;
  115. }
  116. }
  117. return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor;
  118. }