USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
25 uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed)
26 {
27  const uint8_t constBufSize = sizeof(USB_DEVICE_DESCRIPTOR);
28 
29  uint8_t buf[constBufSize];
30  uint8_t rcode;
31  UsbDevice *p = NULL;
32  EpInfo *oldep_ptr = NULL;
33  uint8_t num_of_conf; // number of configurations
34  enum pl2303_type pltype = unknown;
35 
36  AddressPool &addrPool = pUsb->GetAddressPool();
37 
38  USBTRACE("PL 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  {
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 (((USB_DEVICE_DESCRIPTOR*)buf)->idVendor != PL_VID && ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct != PL_PID ) {
74  }
75 
76  /* determine chip variant */
77 
78  if (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass == 0x02 ) {
79  pltype = type_0;
80  }
81  else if (((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0 == 0x40 ) {
82  pltype = rev_HX;
83  }
84  else if (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass == 0x00) {
85  pltype = type_1;
86  }
87  else if (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass == 0xff) {
88  pltype = type_1;
89  }
90 
91  // Allocate new address according to device class
92  bAddress = addrPool.AllocAddress(parent, false, port);
93 
94  if (!bAddress)
96 
97  // Extract Max Packet Size from the device descriptor
98  epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
99 
100  // Assign new address to the device
101  rcode = pUsb->setAddr( 0, 0, bAddress );
102 
103  if (rcode)
104  {
105  p->lowspeed = false;
106  addrPool.FreeAddress(bAddress);
107  bAddress = 0;
108  USBTRACE2("setAddr:",rcode);
109  return rcode;
110  }
111 
112  USBTRACE2("Addr:", bAddress);
113 
114  p->lowspeed = false;
115 
116  p = addrPool.GetUsbDevicePtr(bAddress);
117 
118  if (!p)
120 
121  p->lowspeed = lowspeed;
122 
123  num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
124 
125  // Assign epInfo to epinfo pointer
126  rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
127 
128  if (rcode)
129  goto FailSetDevTblEntry;
130 
131  USBTRACE2("NC:", num_of_conf);
132 
133  for( uint8_t i=0; i<num_of_conf; i++ )
134  {
135  //HexDumper<USBReadParser, uint16_t, uint16_t> HexDump;
137 
138  //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
139  rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
140 
141  if (bNumEP > 1)
142  break;
143  } // for
144 
145  if ( bNumEP < 2 )
147 
148  // Assign epInfo to epinfo pointer
149  rcode = pUsb->setEpInfoEntry( bAddress, bNumEP, epInfo );
150 
151  USBTRACE2("Conf:", bConfNum);
152 
153  // Set Configuration Value
154  rcode = pUsb->setConf(bAddress, 0, bConfNum);
155 
156  if (rcode)
157  goto FailSetConfDescr;
158 
159 #if defined(PL2303_COMPAT)
160  /* shamanic dance - sending Prolific init data as-is */
161  vendorRead( 0x84, 0x84, 0, buf );
162  vendorWrite( 0x04, 0x04, 0 );
163  vendorRead( 0x84, 0x84, 0, buf );
164  vendorRead( 0x83, 0x83, 0, buf );
165  vendorRead( 0x84, 0x84, 0, buf );
166  vendorWrite( 0x04, 0x04, 1 );
167  vendorRead( 0x84, 0x84, 0, buf);
168  vendorRead( 0x83, 0x83, 0, buf);
169  vendorWrite( 0, 0, 1 );
170  vendorWrite( 1, 0, 0 );
171  if ( pltype == rev_HX ) {
172  vendorWrite( 2, 0, 0x44 );
173  vendorWrite( 0x06, 0x06, 0 ); //from W7 init
174  }
175  else {
176  vendorWrite( 2, 0, 0x24 );
177  }
178  /* shamanic dance end */
179 #endif
180 
181  /* calling post-init callback */
182  rcode = pAsync->OnInit(this);
183 
184  if (rcode)
185  goto FailOnInit;
186 
187  USBTRACE("PL configured\r\n");
188 
189  //bPollEnable = true;
190  ready = true;
191  return 0;
192 
193 FailGetDevDescr:
194  USBTRACE("getDevDescr:");
195  goto Fail;
196 
197 FailSetDevTblEntry:
198  USBTRACE("setDevTblEn:");
199  goto Fail;
200 
201 FailGetConfDescr:
202  USBTRACE("getConf:");
203  goto Fail;
204 
205 FailSetConfDescr:
206  USBTRACE("setConf:");
207  goto Fail;
208 
209 FailSetControlLineState:
210  USBTRACE("SetControlLineState:");
211  goto Fail;
212 
213 FailSetLineCoding:
214  USBTRACE("SetLineCoding:");
215  goto Fail;
216 
217 FailOnInit:
218  USBTRACE("OnInit:");
219  goto Fail;
220 
221 Fail:
222  Serial.println(rcode, HEX);
223  Release();
224  return rcode;
225 }
226 
227 //uint8_t PL::Poll()
228 //{
229 // uint8_t rcode = 0;
230 //
231 // //if (!bPollEnable)
232 // // return 0;
233 //
234 // //if (qNextPollTime <= millis())
235 // //{
236 // // Serial.println(bAddress, HEX);
237 //
238 // // qNextPollTime = millis() + 100;
239 // //}
240 // return rcode;
241 //}
242 
243