USB Host Shield 2.0
cdc_XR21B1411.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 #include "cdc_XR21B1411.h"
18 
20 ACM(p, pasync) {
21  // Is this needed??
22  _enhanced_status = enhanced_features(); // Set up features
23 }
24 
25 uint8_t XR21B1411::Init(uint8_t parent, uint8_t port, bool lowspeed) {
26  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
27 
28  uint8_t buf[constBufSize];
29  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
30 
31  uint8_t rcode;
32  UsbDevice *p = NULL;
33  EpInfo *oldep_ptr = NULL;
34  uint8_t num_of_conf; // number of configurations
35 
36  AddressPool &addrPool = pUsb->GetAddressPool();
37 
38  USBTRACE("XR Init\r\n");
39 
40  if(bAddress)
42 
43  // Get pointer to pseudo device with address 0 assigned
44  p = addrPool.GetUsbDevicePtr(0);
45 
46  if(!p)
48 
49  if(!p->epinfo) {
50  USBTRACE("epinfo\r\n");
52  }
53 
54  // Save old pointer to EP_RECORD of address 0
55  oldep_ptr = p->epinfo;
56 
57  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
58  p->epinfo = epInfo;
59 
60  p->lowspeed = lowspeed;
61 
62  // Get device descriptor
63  rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf);
64 
65  // Restore p->epinfo
66  p->epinfo = oldep_ptr;
67 
68  if(rcode)
69  goto FailGetDevDescr;
70 
71  // Allocate new address according to device class
72  bAddress = addrPool.AllocAddress(parent, false, port);
73 
74  if(!bAddress)
76 
77  // Extract Max Packet Size from the device descriptor
79 
80  // Assign new address to the device
81  rcode = pUsb->setAddr(0, 0, bAddress);
82 
83  if(rcode) {
84  p->lowspeed = false;
85  addrPool.FreeAddress(bAddress);
86  bAddress = 0;
87  USBTRACE2("setAddr:", rcode);
88  return rcode;
89  }
90 
91  USBTRACE2("Addr:", bAddress);
92 
93  p->lowspeed = false;
94 
95  p = addrPool.GetUsbDevicePtr(bAddress);
96 
97  if(!p)
99 
100  p->lowspeed = lowspeed;
101 
102  num_of_conf = udd->bNumConfigurations;
103 
104  if((((udd->idVendor != 0x2890U) || (udd->idProduct != 0x0201U)) && ((udd->idVendor != 0x04e2U) || (udd->idProduct != 0x1411U))))
106 
107  // Assign epInfo to epinfo pointer
108  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
109 
110  if(rcode)
111  goto FailSetDevTblEntry;
112 
113  USBTRACE2("NC:", num_of_conf);
114 
115  for(uint8_t i = 0; i < num_of_conf; i++) {
121  CP_MASK_COMPARE_PROTOCOL > CdcControlParser(this);
122 
124  CP_MASK_COMPARE_CLASS> CdcDataParser(this);
125 
126  rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcControlParser);
127 
128  if(rcode)
129  goto FailGetConfDescr;
130 
131  rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcDataParser);
132 
133  if(rcode)
134  goto FailGetConfDescr;
135 
136  if(bNumEP > 1)
137  break;
138  } // for
139 
140  if(bNumEP < 4)
142 
143  // Assign epInfo to epinfo pointer
145 
146  USBTRACE2("Conf:", bConfNum);
147 
148  // Set Configuration Value
149  rcode = pUsb->setConf(bAddress, 0, bConfNum);
150 
151  if(rcode)
152  goto FailSetConfDescr;
153 
154  // Set up features status
156  half_duplex(false);
157  autoflowRTS(false);
158  autoflowDSR(false);
159  autoflowXON(false);
160  wide(false); // Always false, because this is only available in custom mode.
161 
162  rcode = pAsync->OnInit(this);
163 
164  if(rcode)
165  goto FailOnInit;
166 
167  USBTRACE("XR configured\r\n");
168 
169  ready = true;
170 
171  //bPollEnable = true;
172 
173  //USBTRACE("Poll enabled\r\n");
174  return 0;
175 
176 FailGetDevDescr:
177 #ifdef DEBUG_USB_HOST
179  goto Fail;
180 #endif
181 
182 FailSetDevTblEntry:
183 #ifdef DEBUG_USB_HOST
185  goto Fail;
186 #endif
187 
188 FailGetConfDescr:
189 #ifdef DEBUG_USB_HOST
191  goto Fail;
192 #endif
193 
194 FailSetConfDescr:
195 #ifdef DEBUG_USB_HOST
197  goto Fail;
198 #endif
199 
200 FailOnInit:
201 #ifdef DEBUG_USB_HOST
202  USBTRACE("OnInit:");
203 #endif
204 
205 #ifdef DEBUG_USB_HOST
206 Fail:
207  NotifyFail(rcode);
208 #endif
209  Release();
210  return rcode;
211 }
ACM
Definition: cdcacm.h:163
USB_DEVICE_DESCRIPTOR::idVendor
uint16_t idVendor
Definition: usb_ch9.h:113
ACM::bNumEP
uint8_t bNumEP
Definition: cdcacm.h:171
ACM::pUsb
USB * pUsb
Definition: cdcacm.h:165
USB_DEVICE_DESCRIPTOR::idProduct
uint16_t idProduct
Definition: usb_ch9.h:114
AddressPool::GetUsbDevicePtr
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
CDC_PROTOCOL_ITU_T_V_250
#define CDC_PROTOCOL_ITU_T_V_250
Definition: cdcacm.h:40
AddressPool
Definition: address.h:90
USB_DEVICE_DESCRIPTOR::bNumConfigurations
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
NotifyFail
#define NotifyFail(...)
Definition: message.h:62
UsbDevice::lowspeed
bool lowspeed
Definition: address.h:86
AddressPool::AllocAddress
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:100
USB_CLASS_COM_AND_CDC_CTRL
#define USB_CLASS_COM_AND_CDC_CTRL
Definition: UsbCore.h:73
NotifyFailSetDevTblEntry
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:58
XR21B1411::autoflowDSR
virtual void autoflowDSR(bool s)
Definition: cdc_XR21B1411.h:176
USB::getDevDescr
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:801
ACM::wide
virtual void wide(bool s)
Definition: cdcacm.h:244
CDCAsyncOper::OnInit
virtual uint8_t OnInit(ACM *pacm)
Definition: cdcacm.h:131
USB_CLASS_CDC_DATA
#define USB_CLASS_CDC_DATA
Definition: UsbCore.h:80
CDC_SUBCLASS_ACM
#define CDC_SUBCLASS_ACM
Definition: cdcacm.h:27
NotifyFailGetConfDescr
#define NotifyFailGetConfDescr(...)
Definition: message.h:59
XR21B1411::enhanced_features
virtual tty_features enhanced_features(void)
Definition: cdc_XR21B1411.h:121
ACM::ready
volatile bool ready
Definition: cdcacm.h:174
NotifyFailGetDevDescr
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
CP_MASK_COMPARE_SUBCLASS
#define CP_MASK_COMPARE_SUBCLASS
Definition: confdescparser.h:40
USB_DEVICE_DESCRIPTOR
Definition: usb_ch9.h:105
ACM::epInfo
EpInfo epInfo[ACM_MAX_ENDPOINTS]
Definition: cdcacm.h:183
USB::getConfDescr
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:806
USB_DEVICE_DESCRIPTOR::bMaxPacketSize0
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:112
CP_MASK_COMPARE_PROTOCOL
#define CP_MASK_COMPARE_PROTOCOL
Definition: confdescparser.h:41
EpInfo
Definition: address.h:39
CP_MASK_COMPARE_CLASS
#define CP_MASK_COMPARE_CLASS
Definition: confdescparser.h:39
XR21B1411::half_duplex
virtual void half_duplex(bool s)
Definition: cdc_XR21B1411.h:243
ACM::pAsync
CDCAsyncOper * pAsync
Definition: cdcacm.h:166
USB
Definition: UsbCore.h:212
ACM::bConfNum
uint8_t bConfNum
Definition: cdcacm.h:168
CDCAsyncOper
Definition: cdcacm.h:128
USB_ERROR_EPINFO_IS_NULL
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:98
XR21B1411::Init
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: cdc_XR21B1411.cpp:25
UsbDevice
Definition: address.h:82
USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:97
USB::setConf
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:845
ConfigDescParser
Definition: confdescparser.h:47
ACM::_enhanced_status
tty_features _enhanced_status
Definition: cdcacm.h:175
XR21B1411::autoflowRTS
virtual void autoflowRTS(bool s)
Definition: cdc_XR21B1411.h:145
XR21B1411::XR21B1411
XR21B1411(USB *pusb, CDCAsyncOper *pasync)
Definition: cdc_XR21B1411.cpp:19
XR21B1411::autoflowXON
virtual void autoflowXON(bool s)
Definition: cdc_XR21B1411.h:211
USBTRACE
#define USBTRACE(s)
Definition: macros.h:82
ACM::bAddress
uint8_t bAddress
Definition: cdcacm.h:167
USBTRACE2
#define USBTRACE2(s, r)
Definition: macros.h:84
USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:95
NotifyFailSetConfDescr
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
AddressPool::FreeAddress
virtual void FreeAddress(uint8_t addr)=0
USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:92
USB::setAddr
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:836
EpInfo::maxPktSize
uint8_t maxPktSize
Definition: address.h:41
USB::setEpInfoEntry
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
cdc_XR21B1411.h
ACM::Release
uint8_t Release()
Definition: cdcacm.cpp:258
USB::GetAddressPool
AddressPool & GetAddressPool()
Definition: UsbCore.h:228
UsbDevice::epinfo
EpInfo * epinfo
Definition: address.h:83