USB Host Shield 2.0
cdcprolific.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 "cdcprolific.h"
18 
20 ACM(p, pasync),
21 wPLType(0) {
22 }
23 
24 uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed) {
25  const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
26 
27  uint8_t buf[constBufSize];
28  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
29  uint8_t rcode;
30  UsbDevice *p = NULL;
31  EpInfo *oldep_ptr = NULL;
32  uint8_t num_of_conf; // number of configurations
33 #ifdef PL2303_COMPAT
34  enum pl2303_type pltype = unknown;
35 #endif
36 
37  AddressPool &addrPool = pUsb->GetAddressPool();
38 
39  USBTRACE("PL Init\r\n");
40 
41  if(bAddress)
43 
44  // Get pointer to pseudo device with address 0 assigned
45  p = addrPool.GetUsbDevicePtr(0);
46 
47  if(!p)
49 
50  if(!p->epinfo) {
51  USBTRACE("epinfo\r\n");
53  }
54 
55  // Save old pointer to EP_RECORD of address 0
56  oldep_ptr = p->epinfo;
57 
58  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
59  p->epinfo = epInfo;
60 
61  p->lowspeed = lowspeed;
62 
63  // Get device descriptor
64  rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
65 
66  // Restore p->epinfo
67  p->epinfo = oldep_ptr;
68 
69  if(rcode)
70  goto FailGetDevDescr;
71 
72  if(udd->idVendor != PL_VID && CHECK_PID(udd->idProduct))
74 
75  /* determine chip variant */
76 #ifdef PL2303_COMPAT
77  if(udd->bDeviceClass == 0x02 )
78  pltype = type_0;
79  else if(udd->bMaxPacketSize0 == 0x40 )
80  pltype = rev_HX;
81  else if(udd->bDeviceClass == 0x00)
82  pltype = type_1;
83  else if(udd->bDeviceClass == 0xff)
84  pltype = type_1;
85 #endif
86 
87  // Save type of PL chip
88  wPLType = udd->bcdDevice;
89 
90  // Allocate new address according to device class
91  bAddress = addrPool.AllocAddress(parent, false, port);
92 
93  if(!bAddress)
95 
96  // Extract Max Packet Size from the device descriptor
98 
99  // Assign new address to the device
100  rcode = pUsb->setAddr(0, 0, bAddress);
101 
102  if(rcode) {
103  p->lowspeed = false;
104  addrPool.FreeAddress(bAddress);
105  bAddress = 0;
106  USBTRACE2("setAddr:", rcode);
107  return rcode;
108  }
109 
110  USBTRACE2("Addr:", bAddress);
111 
112  p->lowspeed = false;
113 
114  p = addrPool.GetUsbDevicePtr(bAddress);
115 
116  if(!p)
118 
119  p->lowspeed = lowspeed;
120 
121  num_of_conf = udd->bNumConfigurations;
122 
123  // Assign epInfo to epinfo pointer
124  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
125 
126  if(rcode)
127  goto FailSetDevTblEntry;
128 
129  USBTRACE2("NC:", num_of_conf);
130 
131  for(uint8_t i = 0; i < num_of_conf; i++) {
134 
135  rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
136 
137  if(rcode)
138  goto FailGetConfDescr;
139 
140  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
141 
142  if(rcode)
143  goto FailGetConfDescr;
144 
145  if(bNumEP > 1)
146  break;
147  } // for
148 
149  if(bNumEP < 2)
151 
152  // Assign epInfo to epinfo pointer
154 
155  USBTRACE2("Conf:", bConfNum);
156 
157  // Set Configuration Value
158  rcode = pUsb->setConf(bAddress, 0, bConfNum);
159 
160  if(rcode)
161  goto FailSetConfDescr;
162 
163 #ifdef PL2303_COMPAT
164  /* Shamanic dance - sending Prolific init data as-is */
165  vendorRead( 0x84, 0x84, 0, buf );
166  vendorWrite( 0x04, 0x04, 0 );
167  vendorRead( 0x84, 0x84, 0, buf );
168  vendorRead( 0x83, 0x83, 0, buf );
169  vendorRead( 0x84, 0x84, 0, buf );
170  vendorWrite( 0x04, 0x04, 1 );
171  vendorRead( 0x84, 0x84, 0, buf);
172  vendorRead( 0x83, 0x83, 0, buf);
173  vendorWrite( 0, 0, 1 );
174  vendorWrite( 1, 0, 0 );
175  if( pltype == rev_HX ) {
176  vendorWrite( 2, 0, 0x44 );
177  vendorWrite( 0x06, 0x06, 0 ); // From W7 init
178  }
179  else {
180  vendorWrite( 2, 0, 0x24 );
181  }
182  /* Shamanic dance end */
183 #endif
184  /* Calling post-init callback */
185  rcode = pAsync->OnInit(this);
186 
187  if(rcode)
188  goto FailOnInit;
189 
190  USBTRACE("PL configured\r\n");
191 
192  //bPollEnable = true;
193  ready = true;
194  return 0;
195 
196 FailGetDevDescr:
197 #ifdef DEBUG_USB_HOST
199  goto Fail;
200 #endif
201 
202 FailSetDevTblEntry:
203 #ifdef DEBUG_USB_HOST
205  goto Fail;
206 #endif
207 
208 FailGetConfDescr:
209 #ifdef DEBUG_USB_HOST
211  goto Fail;
212 #endif
213 
214 FailSetConfDescr:
215 #ifdef DEBUG_USB_HOST
217  goto Fail;
218 #endif
219 
220 FailOnInit:
221 #ifdef DEBUG_USB_HOST
222  USBTRACE("OnInit:");
223 #endif
224 
225 #ifdef DEBUG_USB_HOST
226 Fail:
227  NotifyFail(rcode);
228 #endif
229  Release();
230  return rcode;
231 }
232 
233 //uint8_t PL::Poll()
234 //{
235 // uint8_t rcode = 0;
236 //
237 // //if (!bPollEnable)
238 // // return 0;
239 //
240 // //if (qNextPollTime <= (uint32_t)millis())
241 // //{
242 // // USB_HOST_SERIAL.println(bAddress, HEX);
243 //
244 // // qNextPollTime = (uint32_t)millis() + 100;
245 // //}
246 // return rcode;
247 //}
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
AddressPool
Definition: address.h:90
USB_DEVICE_DESCRIPTOR::bNumConfigurations
uint8_t bNumConfigurations
Definition: usb_ch9.h:119
type_1
@ type_1
Definition: cdcprolific.h:115
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:98
USB_DEVICE_DESCRIPTOR::bcdDevice
uint16_t bcdDevice
Definition: usb_ch9.h:115
NotifyFailSetDevTblEntry
#define NotifyFailSetDevTblEntry(...)
Definition: message.h:58
USB::getDevDescr
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:796
CDCAsyncOper::OnInit
virtual uint8_t OnInit(ACM *pacm)
Definition: cdcacm.h:131
NotifyFailGetConfDescr
#define NotifyFailGetConfDescr(...)
Definition: message.h:59
ACM::ready
volatile bool ready
Definition: cdcacm.h:174
NotifyFailGetDevDescr
#define NotifyFailGetDevDescr(...)
Definition: message.h:57
USB_DEVICE_DESCRIPTOR
Definition: usb_ch9.h:105
cdcprolific.h
ACM::epInfo
EpInfo epInfo[ACM_MAX_ENDPOINTS]
Definition: cdcacm.h:183
HexDumper
Definition: hexdump.h:33
USB::getConfDescr
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:801
pl2303_type
pl2303_type
Definition: cdcprolific.h:112
unknown
@ unknown
Definition: cdcprolific.h:113
USB_DEVICE_DESCRIPTOR::bMaxPacketSize0
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:112
PL2303::PL2303
PL2303(USB *pusb, CDCAsyncOper *pasync)
Definition: cdcprolific.cpp:19
EpInfo
Definition: address.h:39
ACM::pAsync
CDCAsyncOper * pAsync
Definition: cdcacm.h:166
USB
Definition: UsbCore.h:210
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:96
UsbDevice
Definition: address.h:82
type_0
@ type_0
Definition: cdcprolific.h:114
USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:95
USB::setConf
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:840
ConfigDescParser
Definition: confdescparser.h:47
PL2303::Init
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: cdcprolific.cpp:24
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:93
USB_DEVICE_DESCRIPTOR::bDeviceClass
uint8_t bDeviceClass
Definition: usb_ch9.h:109
NotifyFailSetConfDescr
#define NotifyFailSetConfDescr(...)
Definition: message.h:60
rev_HX
@ rev_HX
Definition: cdcprolific.h:117
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:90
USB::setAddr
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:831
EpInfo::maxPktSize
uint8_t maxPktSize
Definition: address.h:41
CHECK_PID
#define CHECK_PID(pid)
Definition: cdcprolific.h:25
USB::setEpInfoEntry
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
ACM::Release
uint8_t Release()
Definition: cdcacm.cpp:258
PL_VID
#define PL_VID
Definition: cdcprolific.h:24
USB::GetAddressPool
AddressPool & GetAddressPool()
Definition: UsbCore.h:226
UsbDevice::epinfo
EpInfo * epinfo
Definition: address.h:83