My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

confdescparser.h 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Contact information
  19. * -------------------
  20. *
  21. * Circuits At Home, LTD
  22. * Web : http://www.circuitsathome.com
  23. * e-mail : support@circuitsathome.com
  24. */
  25. #pragma once
  26. #ifndef _usb_h_
  27. #error "Never include confdescparser.h directly; include Usb.h instead"
  28. #endif
  29. class UsbConfigXtracter {
  30. public:
  31. //virtual void ConfigXtract(const USB_CONFIGURATION_DESCRIPTOR *conf) = 0;
  32. //virtual void InterfaceXtract(uint8_t conf, const USB_INTERFACE_DESCRIPTOR *iface) = 0;
  33. virtual void EndpointXtract(uint8_t conf __attribute__((unused)), uint8_t iface __attribute__((unused)), uint8_t alt __attribute__((unused)), uint8_t proto __attribute__((unused)), const USB_ENDPOINT_DESCRIPTOR *ep __attribute__((unused))) {
  34. }
  35. };
  36. #define CP_MASK_COMPARE_CLASS 1
  37. #define CP_MASK_COMPARE_SUBCLASS 2
  38. #define CP_MASK_COMPARE_PROTOCOL 4
  39. #define CP_MASK_COMPARE_ALL 7
  40. // Configuration Descriptor Parser Class Template
  41. template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
  42. class ConfigDescParser : public USBReadParser {
  43. UsbConfigXtracter *theXtractor;
  44. MultiValueBuffer theBuffer;
  45. MultiByteValueParser valParser;
  46. ByteSkipper theSkipper;
  47. uint8_t varBuffer[16 /*sizeof(USB_CONFIGURATION_DESCRIPTOR)*/];
  48. uint8_t stateParseDescr; // ParseDescriptor state
  49. uint8_t dscrLen; // Descriptor length
  50. uint8_t dscrType; // Descriptor type
  51. bool isGoodInterface; // Apropriate interface flag
  52. uint8_t confValue; // Configuration value
  53. uint8_t protoValue; // Protocol value
  54. uint8_t ifaceNumber; // Interface number
  55. uint8_t ifaceAltSet; // Interface alternate settings
  56. bool UseOr;
  57. bool ParseDescriptor(uint8_t **pp, uint16_t *pcntdn);
  58. void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
  59. public:
  60. void SetOR(void) { UseOr = true; }
  61. ConfigDescParser(UsbConfigXtracter *xtractor);
  62. void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset);
  63. };
  64. template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
  65. ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::ConfigDescParser(UsbConfigXtracter *xtractor) :
  66. theXtractor(xtractor),
  67. stateParseDescr(0),
  68. dscrLen(0),
  69. dscrType(0),
  70. UseOr(false) {
  71. theBuffer.pValue = varBuffer;
  72. valParser.Initialize(&theBuffer);
  73. theSkipper.Initialize(&theBuffer);
  74. };
  75. template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
  76. void ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset __attribute__((unused))) {
  77. uint16_t cntdn = (uint16_t)len;
  78. uint8_t *p = (uint8_t*)pbuf;
  79. while (cntdn) if (!ParseDescriptor(&p, &cntdn)) return;
  80. }
  81. /* Parser for the configuration descriptor. Takes values for class, subclass, protocol fields in interface descriptor and
  82. compare masks for them. When the match is found, calls EndpointXtract passing buffer containing endpoint descriptor */
  83. template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
  84. bool ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::ParseDescriptor(uint8_t **pp, uint16_t *pcntdn) {
  85. USB_CONFIGURATION_DESCRIPTOR* ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(varBuffer);
  86. USB_INTERFACE_DESCRIPTOR* uid = reinterpret_cast<USB_INTERFACE_DESCRIPTOR*>(varBuffer);
  87. switch (stateParseDescr) {
  88. case 0:
  89. theBuffer.valueSize = 2;
  90. valParser.Initialize(&theBuffer);
  91. stateParseDescr = 1;
  92. case 1:
  93. if (!valParser.Parse(pp, pcntdn)) return false;
  94. dscrLen = *((uint8_t*)theBuffer.pValue);
  95. dscrType = *((uint8_t*)theBuffer.pValue + 1);
  96. stateParseDescr = 2;
  97. case 2:
  98. // This is a sort of hack. Assuming that two bytes are all ready in the buffer
  99. // the pointer is positioned two bytes ahead in order for the rest of descriptor
  100. // to be read right after the size and the type fields.
  101. // This should be used carefully. varBuffer should be used directly to handle data
  102. // in the buffer.
  103. theBuffer.pValue = varBuffer + 2;
  104. stateParseDescr = 3;
  105. case 3:
  106. switch (dscrType) {
  107. case USB_DESCRIPTOR_INTERFACE:
  108. isGoodInterface = false;
  109. break;
  110. case USB_DESCRIPTOR_CONFIGURATION:
  111. case USB_DESCRIPTOR_ENDPOINT:
  112. case HID_DESCRIPTOR_HID:
  113. break;
  114. }
  115. theBuffer.valueSize = dscrLen - 2;
  116. valParser.Initialize(&theBuffer);
  117. stateParseDescr = 4;
  118. case 4:
  119. switch (dscrType) {
  120. case USB_DESCRIPTOR_CONFIGURATION:
  121. if (!valParser.Parse(pp, pcntdn)) return false;
  122. confValue = ucd->bConfigurationValue;
  123. break;
  124. case USB_DESCRIPTOR_INTERFACE:
  125. if (!valParser.Parse(pp, pcntdn)) return false;
  126. if ((MASK & CP_MASK_COMPARE_CLASS) && uid->bInterfaceClass != CLASS_ID)
  127. break;
  128. if ((MASK & CP_MASK_COMPARE_SUBCLASS) && uid->bInterfaceSubClass != SUBCLASS_ID)
  129. break;
  130. if (UseOr) {
  131. if ((!((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol))) break;
  132. }
  133. else if ((MASK & CP_MASK_COMPARE_PROTOCOL) && uid->bInterfaceProtocol != PROTOCOL_ID)
  134. break;
  135. isGoodInterface = true;
  136. ifaceNumber = uid->bInterfaceNumber;
  137. ifaceAltSet = uid->bAlternateSetting;
  138. protoValue = uid->bInterfaceProtocol;
  139. break;
  140. case USB_DESCRIPTOR_ENDPOINT:
  141. if (!valParser.Parse(pp, pcntdn)) return false;
  142. if (isGoodInterface && theXtractor)
  143. theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer);
  144. break;
  145. //case HID_DESCRIPTOR_HID:
  146. // if (!valParser.Parse(pp, pcntdn)) return false;
  147. // PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer);
  148. // break;
  149. default:
  150. if (!theSkipper.Skip(pp, pcntdn, dscrLen - 2)) return false;
  151. }
  152. theBuffer.pValue = varBuffer;
  153. stateParseDescr = 0;
  154. }
  155. return true;
  156. }
  157. template <const uint8_t CLASS_ID, const uint8_t SUBCLASS_ID, const uint8_t PROTOCOL_ID, const uint8_t MASK>
  158. void ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc) {
  159. Notify(PSTR("\r\n\r\nHID Descriptor:\r\n"), 0x80);
  160. Notify(PSTR("bDescLength:\t\t"), 0x80);
  161. PrintHex<uint8_t > (pDesc->bLength, 0x80);
  162. Notify(PSTR("\r\nbDescriptorType:\t"), 0x80);
  163. PrintHex<uint8_t > (pDesc->bDescriptorType, 0x80);
  164. Notify(PSTR("\r\nbcdHID:\t\t\t"), 0x80);
  165. PrintHex<uint16_t > (pDesc->bcdHID, 0x80);
  166. Notify(PSTR("\r\nbCountryCode:\t\t"), 0x80);
  167. PrintHex<uint8_t > (pDesc->bCountryCode, 0x80);
  168. Notify(PSTR("\r\nbNumDescriptors:\t"), 0x80);
  169. PrintHex<uint8_t > (pDesc->bNumDescriptors, 0x80);
  170. for (uint8_t i = 0; i < pDesc->bNumDescriptors; i++) {
  171. HID_CLASS_DESCRIPTOR_LEN_AND_TYPE *pLT = (HID_CLASS_DESCRIPTOR_LEN_AND_TYPE*)&(pDesc->bDescrType);
  172. Notify(PSTR("\r\nbDescrType:\t\t"), 0x80);
  173. PrintHex<uint8_t > (pLT[i].bDescrType, 0x80);
  174. Notify(PSTR("\r\nwDescriptorLength:\t"), 0x80);
  175. PrintHex<uint16_t > (pLT[i].wDescriptorLength, 0x80);
  176. }
  177. Notify(PSTR("\r\n"), 0x80);
  178. }