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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

cdc_XR21B1411.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #include "cdc_XR21B1411.h"
  15. XR21B1411::XR21B1411(USB *p, CDCAsyncOper *pasync) :
  16. ACM(p, pasync) {
  17. // Is this needed??
  18. _enhanced_status = enhanced_features(); // Set up features
  19. }
  20. uint8_t XR21B1411::Init(uint8_t parent, uint8_t port, bool lowspeed) {
  21. const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
  22. uint8_t buf[constBufSize];
  23. USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
  24. uint8_t rcode;
  25. UsbDevice *p = NULL;
  26. EpInfo *oldep_ptr = NULL;
  27. uint8_t num_of_conf; // number of configurations
  28. AddressPool &addrPool = pUsb->GetAddressPool();
  29. USBTRACE("XR Init\r\n");
  30. if(bAddress)
  31. return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
  32. // Get pointer to pseudo device with address 0 assigned
  33. p = addrPool.GetUsbDevicePtr(0);
  34. if(!p)
  35. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  36. if(!p->epinfo) {
  37. USBTRACE("epinfo\r\n");
  38. return USB_ERROR_EPINFO_IS_NULL;
  39. }
  40. // Save old pointer to EP_RECORD of address 0
  41. oldep_ptr = p->epinfo;
  42. // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
  43. p->epinfo = epInfo;
  44. p->lowspeed = lowspeed;
  45. // Get device descriptor
  46. rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf);
  47. // Restore p->epinfo
  48. p->epinfo = oldep_ptr;
  49. if(rcode)
  50. goto FailGetDevDescr;
  51. // Allocate new address according to device class
  52. bAddress = addrPool.AllocAddress(parent, false, port);
  53. if(!bAddress)
  54. return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
  55. // Extract Max Packet Size from the device descriptor
  56. epInfo[0].maxPktSize = udd->bMaxPacketSize0;
  57. // Assign new address to the device
  58. rcode = pUsb->setAddr(0, 0, bAddress);
  59. if(rcode) {
  60. p->lowspeed = false;
  61. addrPool.FreeAddress(bAddress);
  62. bAddress = 0;
  63. USBTRACE2("setAddr:", rcode);
  64. return rcode;
  65. }
  66. USBTRACE2("Addr:", bAddress);
  67. p->lowspeed = false;
  68. p = addrPool.GetUsbDevicePtr(bAddress);
  69. if(!p)
  70. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  71. p->lowspeed = lowspeed;
  72. num_of_conf = udd->bNumConfigurations;
  73. if((((udd->idVendor != 0x2890U) || (udd->idProduct != 0x0201U)) && ((udd->idVendor != 0x04e2U) || (udd->idProduct != 0x1411U))))
  74. return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
  75. // Assign epInfo to epinfo pointer
  76. rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
  77. if(rcode)
  78. goto FailSetDevTblEntry;
  79. USBTRACE2("NC:", num_of_conf);
  80. for(uint8_t i = 0; i < num_of_conf; i++) {
  81. ConfigDescParser< USB_CLASS_COM_AND_CDC_CTRL,
  82. CDC_SUBCLASS_ACM,
  83. CDC_PROTOCOL_ITU_T_V_250,
  84. CP_MASK_COMPARE_CLASS |
  85. CP_MASK_COMPARE_SUBCLASS |
  86. CP_MASK_COMPARE_PROTOCOL > CdcControlParser(this);
  87. ConfigDescParser<USB_CLASS_CDC_DATA, 0, 0,
  88. CP_MASK_COMPARE_CLASS> CdcDataParser(this);
  89. rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcControlParser);
  90. if(rcode)
  91. goto FailGetConfDescr;
  92. rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcDataParser);
  93. if(rcode)
  94. goto FailGetConfDescr;
  95. if(bNumEP > 1)
  96. break;
  97. } // for
  98. if(bNumEP < 4)
  99. return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
  100. // Assign epInfo to epinfo pointer
  101. rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
  102. USBTRACE2("Conf:", bConfNum);
  103. // Set Configuration Value
  104. rcode = pUsb->setConf(bAddress, 0, bConfNum);
  105. if(rcode)
  106. goto FailSetConfDescr;
  107. // Set up features status
  108. _enhanced_status = enhanced_features();
  109. half_duplex(false);
  110. autoflowRTS(false);
  111. autoflowDSR(false);
  112. autoflowXON(false);
  113. wide(false); // Always false, because this is only available in custom mode.
  114. rcode = pAsync->OnInit(this);
  115. if(rcode)
  116. goto FailOnInit;
  117. USBTRACE("XR configured\r\n");
  118. ready = true;
  119. //bPollEnable = true;
  120. //USBTRACE("Poll enabled\r\n");
  121. return 0;
  122. FailGetDevDescr:
  123. #ifdef DEBUG_USB_HOST
  124. NotifyFailGetDevDescr();
  125. goto Fail;
  126. #endif
  127. FailSetDevTblEntry:
  128. #ifdef DEBUG_USB_HOST
  129. NotifyFailSetDevTblEntry();
  130. goto Fail;
  131. #endif
  132. FailGetConfDescr:
  133. #ifdef DEBUG_USB_HOST
  134. NotifyFailGetConfDescr();
  135. goto Fail;
  136. #endif
  137. FailSetConfDescr:
  138. #ifdef DEBUG_USB_HOST
  139. NotifyFailSetConfDescr();
  140. goto Fail;
  141. #endif
  142. FailOnInit:
  143. #ifdef DEBUG_USB_HOST
  144. USBTRACE("OnInit:");
  145. #endif
  146. #ifdef DEBUG_USB_HOST
  147. Fail:
  148. NotifyFail(rcode);
  149. #endif
  150. Release();
  151. return rcode;
  152. }