Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdcprolific.cpp Source File

cdcprolific.cpp

00001 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
00002 
00003 This software may be distributed and modified under the terms of the GNU
00004 General Public License version 2 (GPL2) as published by the Free Software
00005 Foundation and appearing in the file GPL2.TXT included in the packaging of
00006 this file. Please note that GPL2 Section 2[b] requires that all works based
00007 on this software must also be made publicly available under the terms of
00008 the GPL2 ("Copyleft").
00009 
00010 Contact information
00011 -------------------
00012 
00013 Circuits At Home, LTD
00014 Web      :  http://www.circuitsathome.com
00015 e-mail   :  support@circuitsathome.com
00016  */
00017 #include "cdcprolific.h"
00018 
00019 PL2303::PL2303(Usb *p, CDCAsyncOper *pasync) :
00020 ACM(p, pasync),
00021 wPLType(0) {
00022 }
00023 
00024 uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed) {
00025         const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
00026 
00027         uint8_t buf[constBufSize];
00028         USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
00029         uint8_t rcode;
00030         UsbDevice *p = NULL;
00031         EpInfo *oldep_ptr = NULL;
00032         uint8_t num_of_conf; // number of configurations
00033 #ifdef PL2303_COMPAT
00034         enum pl2303_type pltype = unknown;
00035 #endif
00036 
00037         AddressPool &addrPool = pUsb->GetAddressPool();
00038 
00039         USBTRACE("PL Init\r\n");
00040 
00041         if(bAddress)
00042                 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
00043 
00044         // Get pointer to pseudo device with address 0 assigned
00045         p = addrPool.GetUsbDevicePtr(0);
00046 
00047         if(!p)
00048                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
00049 
00050         if(!p->epinfo) {
00051                 USBTRACE("epinfo\r\n");
00052                 return USB_ERROR_EPINFO_IS_NULL;
00053         }
00054 
00055         // Save old pointer to EP_RECORD of address 0
00056         oldep_ptr = p->epinfo;
00057 
00058         // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
00059         p->epinfo = epInfo;
00060 
00061         p->lowspeed = lowspeed;
00062 
00063         // Get device descriptor
00064         rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf);
00065 
00066         // Restore p->epinfo
00067         p->epinfo = oldep_ptr;
00068 
00069         if(rcode)
00070                 goto FailGetDevDescr;
00071 
00072         if(udd->idVendor != PL_VID && CHECK_PID(udd->idProduct))
00073                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
00074 
00075         /* determine chip variant */
00076 #ifdef PL2303_COMPAT
00077         if(udd->bDeviceClass == 0x02 )
00078                 pltype = type_0;
00079         else if(udd->bMaxPacketSize0 == 0x40 )
00080                 pltype = rev_HX;
00081         else if(udd->bDeviceClass == 0x00)
00082                 pltype = type_1;
00083         else if(udd->bDeviceClass == 0xff)
00084                 pltype = type_1;
00085 #endif
00086 
00087         // Save type of PL chip
00088         wPLType = udd->bcdDevice;
00089 
00090         // Allocate new address according to device class
00091         bAddress = addrPool.AllocAddress(parent, false, port);
00092 
00093         if(!bAddress)
00094                 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
00095 
00096         // Extract Max Packet Size from the device descriptor
00097         epInfo[0].maxPktSize = udd->bMaxPacketSize0;
00098 
00099         // Assign new address to the device
00100         rcode = pUsb->setAddr(0, 0, bAddress);
00101 
00102         if(rcode) {
00103                 p->lowspeed = false;
00104                 addrPool.FreeAddress(bAddress);
00105                 bAddress = 0;
00106                 USBTRACE2("setAddr:", rcode);
00107                 return rcode;
00108         }
00109 
00110         USBTRACE2("Addr:", bAddress);
00111 
00112         p->lowspeed = false;
00113 
00114         p = addrPool.GetUsbDevicePtr(bAddress);
00115 
00116         if(!p)
00117                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
00118 
00119         p->lowspeed = lowspeed;
00120 
00121         num_of_conf = udd->bNumConfigurations;
00122 
00123         // Assign epInfo to epinfo pointer
00124         rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
00125 
00126         if(rcode)
00127                 goto FailSetDevTblEntry;
00128 
00129         USBTRACE2("NC:", num_of_conf);
00130 
00131         for(uint8_t i = 0; i < num_of_conf; i++) {
00132                 HexDumper<USBReadParser, uint16_t, uint16_t> HexDump;
00133                 ConfigDescParser < 0xFF, 0, 0, CP_MASK_COMPARE_CLASS> confDescrParser(this);
00134 
00135                 rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
00136 
00137                 if(rcode)
00138                         goto FailGetConfDescr;
00139 
00140                 rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
00141 
00142                 if(rcode)
00143                         goto FailGetConfDescr;
00144 
00145                 if(bNumEP > 1)
00146                         break;
00147         } // for
00148 
00149         if(bNumEP < 2)
00150                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
00151 
00152         // Assign epInfo to epinfo pointer
00153         rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
00154 
00155         USBTRACE2("Conf:", bConfNum);
00156 
00157         // Set Configuration Value
00158         rcode = pUsb->setConf(bAddress, 0, bConfNum);
00159 
00160         if(rcode)
00161                 goto FailSetConfDescr;
00162 
00163 #ifdef PL2303_COMPAT
00164         /* Shamanic dance - sending Prolific init data as-is */
00165         vendorRead( 0x84, 0x84, 0, buf );
00166         vendorWrite( 0x04, 0x04, 0 );
00167         vendorRead( 0x84, 0x84, 0, buf );
00168         vendorRead( 0x83, 0x83, 0, buf );
00169         vendorRead( 0x84, 0x84, 0, buf );
00170         vendorWrite( 0x04, 0x04, 1 );
00171         vendorRead( 0x84, 0x84, 0, buf);
00172         vendorRead( 0x83, 0x83, 0, buf);
00173         vendorWrite( 0, 0, 1 );
00174         vendorWrite( 1, 0, 0 );
00175         if( pltype == rev_HX ) {
00176                 vendorWrite( 2, 0, 0x44 );
00177                 vendorWrite( 0x06, 0x06, 0 ); // From W7 init
00178         }
00179         else {
00180                 vendorWrite( 2, 0, 0x24 );
00181         }
00182         /* Shamanic dance end */
00183 #endif
00184         /* Calling post-init callback */
00185         rcode = pAsync->OnInit(this);
00186 
00187         if(rcode)
00188                 goto FailOnInit;
00189 
00190         USBTRACE("PL configured\r\n");
00191 
00192         //bPollEnable = true;
00193         ready = true;
00194         return 0;
00195 
00196 FailGetDevDescr:
00197 #ifdef DEBUG_USB_HOST
00198         NotifyFailGetDevDescr();
00199         goto Fail;
00200 #endif
00201 
00202 FailSetDevTblEntry:
00203 #ifdef DEBUG_USB_HOST
00204         NotifyFailSetDevTblEntry();
00205         goto Fail;
00206 #endif
00207 
00208 FailGetConfDescr:
00209 #ifdef DEBUG_USB_HOST
00210         NotifyFailGetConfDescr();
00211         goto Fail;
00212 #endif
00213 
00214 FailSetConfDescr:
00215 #ifdef DEBUG_USB_HOST
00216         NotifyFailSetConfDescr();
00217         goto Fail;
00218 #endif
00219 
00220 FailOnInit:
00221 #ifdef DEBUG_USB_HOST
00222         USBTRACE("OnInit:");
00223 #endif
00224 
00225 #ifdef DEBUG_USB_HOST
00226 Fail:
00227         NotifyFail(rcode);
00228 #endif
00229         Release();
00230         return rcode;
00231 }
00232 
00233 //uint8_t PL::Poll()
00234 //{
00235 //      uint8_t rcode = 0;
00236 //
00237 //      //if (!bPollEnable)
00238 //      //      return 0;
00239 //
00240 //      //if (qNextPollTime <= (uint32_t)millis())
00241 //      //{
00242 //      //      USB_HOST_SERIAL.println(bAddress, HEX);
00243 //
00244 //      //      qNextPollTime = (uint32_t)millis() + 100;
00245 //      //}
00246 //      return rcode;
00247 //}