1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
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
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
17 #include "cdcprolific.h"
19 PL2303::PL2303(USB *p, CDCAsyncOper *pasync) :
24 uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed) {
25 const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
27 uint8_t buf[constBufSize];
28 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
31 EpInfo *oldep_ptr = NULL;
32 uint8_t num_of_conf; // number of configurations
34 enum pl2303_type pltype = unknown;
37 AddressPool &addrPool = pUsb->GetAddressPool();
39 USBTRACE("PL Init\r\n");
42 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
44 // Get pointer to pseudo device with address 0 assigned
45 p = addrPool.GetUsbDevicePtr(0);
48 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
51 USBTRACE("epinfo\r\n");
52 return USB_ERROR_EPINFO_IS_NULL;
55 // Save old pointer to EP_RECORD of address 0
56 oldep_ptr = p->epinfo;
58 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
61 p->lowspeed = lowspeed;
63 // Get device descriptor
64 rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
67 p->epinfo = oldep_ptr;
72 if(udd->idVendor != PL_VID && CHECK_PID(udd->idProduct))
73 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
75 /* determine chip variant */
77 if(udd->bDeviceClass == 0x02 )
79 else if(udd->bMaxPacketSize0 == 0x40 )
81 else if(udd->bDeviceClass == 0x00)
83 else if(udd->bDeviceClass == 0xff)
87 // Save type of PL chip
88 wPLType = udd->bcdDevice;
90 // Allocate new address according to device class
91 bAddress = addrPool.AllocAddress(parent, false, port);
94 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
96 // Extract Max Packet Size from the device descriptor
97 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
99 // Assign new address to the device
100 rcode = pUsb->setAddr(0, 0, bAddress);
104 addrPool.FreeAddress(bAddress);
106 USBTRACE2("setAddr:", rcode);
110 USBTRACE2("Addr:", bAddress);
114 p = addrPool.GetUsbDevicePtr(bAddress);
117 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
119 p->lowspeed = lowspeed;
121 num_of_conf = udd->bNumConfigurations;
123 // Assign epInfo to epinfo pointer
124 rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
127 goto FailSetDevTblEntry;
129 USBTRACE2("NC:", num_of_conf);
131 for(uint8_t i = 0; i < num_of_conf; i++) {
132 HexDumper<USBReadParser, uint16_t, uint16_t> HexDump;
133 ConfigDescParser < 0xFF, 0, 0, CP_MASK_COMPARE_CLASS> confDescrParser(this);
135 rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
138 goto FailGetConfDescr;
140 rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
143 goto FailGetConfDescr;
150 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
152 // Assign epInfo to epinfo pointer
153 rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
155 USBTRACE2("Conf:", bConfNum);
157 // Set Configuration Value
158 rcode = pUsb->setConf(bAddress, 0, bConfNum);
161 goto FailSetConfDescr;
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
180 vendorWrite( 2, 0, 0x24 );
182 /* Shamanic dance end */
184 /* Calling post-init callback */
185 rcode = pAsync->OnInit(this);
190 USBTRACE("PL configured\r\n");
192 //bPollEnable = true;
197 #ifdef DEBUG_USB_HOST
198 NotifyFailGetDevDescr();
203 #ifdef DEBUG_USB_HOST
204 NotifyFailSetDevTblEntry();
209 #ifdef DEBUG_USB_HOST
210 NotifyFailGetConfDescr();
215 #ifdef DEBUG_USB_HOST
216 NotifyFailSetConfDescr();
221 #ifdef DEBUG_USB_HOST
225 #ifdef DEBUG_USB_HOST
235 // uint8_t rcode = 0;
237 // //if (!bPollEnable)
240 // //if (qNextPollTime <= millis())
242 // // USB_HOST_SERIAL.println(bAddress, HEX);
244 // // qNextPollTime = millis() + 100;