Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdc_XR21B1411.cpp Source File

cdc_XR21B1411.cpp

00001 /* Copyright (C) 2015 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 "cdc_XR21B1411.h"
00018 
00019 XR21B1411::XR21B1411(Usb *p, CDCAsyncOper *pasync) :
00020 ACM(p, pasync) {
00021         // Is this needed??
00022         _enhanced_status = enhanced_features(); // Set up features
00023 }
00024 
00025 uint8_t XR21B1411::Init(uint8_t parent, uint8_t port, bool lowspeed) {
00026         const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
00027 
00028         uint8_t buf[constBufSize];
00029         USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
00030 
00031         uint8_t rcode;
00032         UsbDevice *p = NULL;
00033         EpInfo *oldep_ptr = NULL;
00034         uint8_t num_of_conf; // number of configurations
00035 
00036         AddressPool &addrPool = pUsb->GetAddressPool();
00037 
00038         USBTRACE("XR Init\r\n");
00039 
00040         if(bAddress)
00041                 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
00042 
00043         // Get pointer to pseudo device with address 0 assigned
00044         p = addrPool.GetUsbDevicePtr(0);
00045 
00046         if(!p)
00047                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
00048 
00049         if(!p->epinfo) {
00050                 USBTRACE("epinfo\r\n");
00051                 return USB_ERROR_EPINFO_IS_NULL;
00052         }
00053 
00054         // Save old pointer to EP_RECORD of address 0
00055         oldep_ptr = p->epinfo;
00056 
00057         // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
00058         p->epinfo = epInfo;
00059 
00060         p->lowspeed = lowspeed;
00061 
00062         // Get device descriptor
00063         rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf);
00064 
00065         // Restore p->epinfo
00066         p->epinfo = oldep_ptr;
00067 
00068         if(rcode)
00069                 goto FailGetDevDescr;
00070 
00071         // Allocate new address according to device class
00072         bAddress = addrPool.AllocAddress(parent, false, port);
00073 
00074         if(!bAddress)
00075                 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
00076 
00077         // Extract Max Packet Size from the device descriptor
00078         epInfo[0].maxPktSize = udd->bMaxPacketSize0;
00079 
00080         // Assign new address to the device
00081         rcode = pUsb->setAddr(0, 0, bAddress);
00082 
00083         if(rcode) {
00084                 p->lowspeed = false;
00085                 addrPool.FreeAddress(bAddress);
00086                 bAddress = 0;
00087                 USBTRACE2("setAddr:", rcode);
00088                 return rcode;
00089         }
00090 
00091         USBTRACE2("Addr:", bAddress);
00092 
00093         p->lowspeed = false;
00094 
00095         p = addrPool.GetUsbDevicePtr(bAddress);
00096 
00097         if(!p)
00098                 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
00099 
00100         p->lowspeed = lowspeed;
00101 
00102         num_of_conf = udd->bNumConfigurations;
00103 
00104         if((((udd->idVendor != 0x2890U) || (udd->idProduct != 0x0201U)) && ((udd->idVendor != 0x04e2U) || (udd->idProduct != 0x1411U))))
00105                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
00106 
00107         // Assign epInfo to epinfo pointer
00108         rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
00109 
00110         if(rcode)
00111                 goto FailSetDevTblEntry;
00112 
00113         USBTRACE2("NC:", num_of_conf);
00114 
00115         for(uint8_t i = 0; i < num_of_conf; i++) {
00116                 ConfigDescParser< USB_CLASS_COM_AND_CDC_CTRL,
00117                         CDC_SUBCLASS_ACM,
00118                         CDC_PROTOCOL_ITU_T_V_250,
00119                         CP_MASK_COMPARE_CLASS |
00120                         CP_MASK_COMPARE_SUBCLASS |
00121                         CP_MASK_COMPARE_PROTOCOL > CdcControlParser(this);
00122 
00123                 ConfigDescParser<USB_CLASS_CDC_DATA, 0, 0,
00124                         CP_MASK_COMPARE_CLASS> CdcDataParser(this);
00125 
00126                 rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcControlParser);
00127 
00128                 if(rcode)
00129                         goto FailGetConfDescr;
00130 
00131                 rcode = pUsb->getConfDescr(bAddress, 0, i, &CdcDataParser);
00132 
00133                 if(rcode)
00134                         goto FailGetConfDescr;
00135 
00136                 if(bNumEP > 1)
00137                         break;
00138         } // for
00139 
00140         if(bNumEP < 4)
00141                 return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
00142 
00143         // Assign epInfo to epinfo pointer
00144         rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo);
00145 
00146         USBTRACE2("Conf:", bConfNum);
00147 
00148         // Set Configuration Value
00149         rcode = pUsb->setConf(bAddress, 0, bConfNum);
00150 
00151         if(rcode)
00152                 goto FailSetConfDescr;
00153 
00154         // Set up features status
00155         _enhanced_status = enhanced_features();
00156         half_duplex(false);
00157         autoflowRTS(false);
00158         autoflowDSR(false);
00159         autoflowXON(false);
00160         wide(false); // Always false, because this is only available in custom mode.
00161 
00162         rcode = pAsync->OnInit(this);
00163 
00164         if(rcode)
00165                 goto FailOnInit;
00166 
00167         USBTRACE("XR configured\r\n");
00168 
00169         ready = true;
00170 
00171         //bPollEnable = true;
00172 
00173         //USBTRACE("Poll enabled\r\n");
00174         return 0;
00175 
00176 FailGetDevDescr:
00177 #ifdef DEBUG_USB_HOST
00178         NotifyFailGetDevDescr();
00179         goto Fail;
00180 #endif
00181 
00182 FailSetDevTblEntry:
00183 #ifdef DEBUG_USB_HOST
00184         NotifyFailSetDevTblEntry();
00185         goto Fail;
00186 #endif
00187 
00188 FailGetConfDescr:
00189 #ifdef DEBUG_USB_HOST
00190         NotifyFailGetConfDescr();
00191         goto Fail;
00192 #endif
00193 
00194 FailSetConfDescr:
00195 #ifdef DEBUG_USB_HOST
00196         NotifyFailSetConfDescr();
00197         goto Fail;
00198 #endif
00199 
00200 FailOnInit:
00201 #ifdef DEBUG_USB_HOST
00202         USBTRACE("OnInit:");
00203 #endif
00204 
00205 #ifdef DEBUG_USB_HOST
00206 Fail:
00207         NotifyFail(rcode);
00208 #endif
00209         Release();
00210         return rcode;
00211 }