Ilya I / Mbed 2 deprecated iva2k_NetHttpServerTcpSockets

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbserialif.cpp Source File

usbserialif.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 /*
00025   USB-Serial device driver
00026   Donatien Garnier 2010
00027 */
00028 
00029 #include "usbserialif.h"
00030 #include "drv/usb/UsbEndpoint.h"
00031 
00032 #include "netCfg.h"
00033 #if NET_USB_SERIAL
00034 
00035 UsbEndpoint* pEpIn;
00036 UsbEndpoint* pEpOut;
00037 
00038 USB_INT08U ser_int_found;
00039 USB_INT32S SerialInit()
00040 {
00041   Host_Init();               // Initialize the  host controller
00042   USB_INT32S rc = Host_EnumDev();       // Enumerate the device connected
00043   if (rc != OK)
00044   {
00045       fprintf(stderr, "Could not enumerate device: %d\n", rc);
00046       return rc;
00047   }
00048   return OK;
00049 }
00050 
00051 bool SerialHasToSwitch()
00052 {
00053   return (ser_int_found == 3);
00054 }
00055 
00056 uint16_t m_vid;
00057 uint16_t m_pid;
00058 
00059 USB_INT32S SerialSendMagic()
00060 {
00061   bool scsi = false;
00062   //Size 31
00063   const unsigned char magicHuawei[] = { 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78, 0,    0, 0, 0, 0,    0, 0,    0x11, 0x6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00064   const unsigned char magicVoda[] = {   0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12, 0x01, 0, 0, 0, 0x80, 0, 0x06, 0x01, 0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00065   char* magic;
00066   USB_INT32S rc;
00067   if((m_vid == 0x12d1) && (m_pid == 0x1446))
00068   {
00069     PRINT_Log("\r\nHuawei magic packet sent.\r\n");
00070     magic = (char*) magicHuawei;
00071     scsi = true;
00072   }
00073   else if((m_vid == 0x12d1) && (m_pid == 0x1003))
00074   {
00075     PRINT_Log("\r\nHuawei magic control packet sent.\r\n");
00076     rc = Host_CtrlSend( 0 /*USB_TYPE_STANDARD | USB_RECIP_DEVICE*/, 0x03 /*USB_REQ_SET_FEATURE*/, 00000001, 0, 0, NULL);
00077   }
00078   else if(m_vid == 0x0af0)
00079   {
00080     PRINT_Log("\r\nVoda magic packet sent.\r\n");
00081     magic = (char*) magicVoda;  
00082     scsi = true;
00083   }
00084   else
00085   {
00086    return -1;
00087   }
00088   
00089   
00090   if(scsi)
00091   {
00092     //rc = Host_ProcessTD(EDBulkOut, TD_OUT, (volatile USB_INT08U*)magic, 31);
00093     rc = pEpOut->transfer((volatile USB_INT08U*)magic, 31);
00094     while(rc == PROCESSING)
00095     {
00096      // __WFI();
00097       rc = pEpOut->status();
00098     }
00099   
00100   }
00101   
00102   
00103   delete pEpOut;
00104   pEpOut = NULL;
00105   return rc;
00106 }
00107 
00108 USB_INT32S SerialCheckVidPid()
00109 {
00110   volatile  USB_INT08U  *desc_ptr;
00111   desc_ptr     = TDBuffer;
00112   
00113   ser_int_found = 0;
00114   
00115   m_vid = *((USB_INT16U*)&desc_ptr[8]);
00116   m_pid = *((USB_INT16U*)&desc_ptr[10]);
00117   
00118   if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_DEVICE) {   
00119       PRINT_Log("\r\nLen = %02x\r\n",desc_ptr[0]);
00120       PRINT_Log("\r\nDesc code %02x\r\n",desc_ptr[1]);
00121       return (ERR_BAD_CONFIGURATION);
00122   }
00123   
00124   if( m_vid == 0x12d1 &&//Huawei : Change
00125       m_pid == 0x1446 )
00126   {
00127     PRINT_Log("\r\nHuawei device found in CDFS mode\r\n");
00128     ser_int_found=3;
00129   }
00130   else if(  m_vid == 0x12d1 &&//Huawei : Change
00131             m_pid == 0x1001 )
00132   {
00133     PRINT_Log("\r\nHuawei device found in Serial mode\r\n");
00134     ser_int_found=1;
00135   }
00136   else if(  m_vid  == 0x0af0 &&//Voda?Qualcomm? : Change
00137             m_pid == 0x7501 )
00138   {
00139     PRINT_Log("\r\nVodafone K3760 found, checking mode...\r\n");
00140     ser_int_found=3;
00141   }
00142   else if(  m_vid  == 0x12d1 &&//Voda?Qualcomm? : Change
00143             m_pid == 0x1003 )
00144   {
00145     PRINT_Log("\r\nHuawei device found, checking mode...\r\n");
00146     ser_int_found=3;
00147   }
00148   else
00149   {
00150     PRINT_Log("\r\nDevice %04x : %04x found.\r\n",m_vid,m_pid);                
00151   }
00152   return OK;
00153 }
00154 
00155 USB_INT32S SerialParseConfig()
00156 {
00157     volatile  USB_INT08U  *desc_ptr;
00158 
00159     desc_ptr     = TDBuffer;
00160 
00161     if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION) {    
00162         return (ERR_BAD_CONFIGURATION);
00163     }
00164     desc_ptr += desc_ptr[0];
00165     
00166     int epOut = 0;
00167     int epIn = 0;
00168 
00169     pEpOut = NULL;
00170     pEpIn = NULL;
00171     while (desc_ptr != TDBuffer + ReadLE16U(&TDBuffer[2])) {
00172 
00173         switch (desc_ptr[1]) {
00174             case USB_DESCRIPTOR_TYPE_INTERFACE: 
00175                  PRINT_Log("\r\nIf %02x:%02x:%02x.\r\n",desc_ptr[5],desc_ptr[6],desc_ptr[7]);
00176                  if (desc_ptr[5] == 0xFF &&             
00177                      desc_ptr[6] == 0xFF &&     
00178                      desc_ptr[7] == 0xFF ) {       
00179                      //if(ser_int_found==1)
00180                      ser_int_found=2;
00181                  }
00182                  desc_ptr    += desc_ptr[0];                      /* Move to next descriptor start      */
00183                  break;
00184 
00185             case USB_DESCRIPTOR_TYPE_ENDPOINT:                        /* If it is an endpoint descriptor    */
00186                  PRINT_Log("\r\nEp %02x of size %d.\r\n", desc_ptr[2], (ReadLE16U(&desc_ptr[4]) ));
00187                  if ( SerialHasToSwitch() )
00188                  {
00189                    if( pEpOut == NULL /*desc_ptr[2] == 1*/) //EP 1
00190                    {
00191                    #if 0
00192                          EDBulkOut->Control = 1                             |      /* USB address           */
00193                                               ((desc_ptr[2] & 0x7F) << 7)   |      /* Endpoint address      */
00194                                               (1 << 11)                     |      /* direction             */
00195                                               (ReadLE16U(&desc_ptr[4]) << 16);     /* MaxPkt Size           */
00196                    #endif
00197                      pEpOut = new UsbEndpoint((desc_ptr[2] & 0x7F), false, ReadLE16U(&desc_ptr[4]));
00198                    }
00199                    desc_ptr += desc_ptr[0];                     /* Move to next descriptor start      */
00200                  }
00201                  else if ((desc_ptr[3] & 0x03) == 0x02) {                  /* If it is Bulk endpoint             */
00202                      if (desc_ptr[2] & 0x80) {                        /* If it is In endpoint               */
00203                        if(epIn == 0)
00204                        {
00205                          PRINT_Log("\r\nEp %02x is in ep.\r\n", desc_ptr[2]);
00206                        #if 0
00207                          EDBulkIn->Control =  1                             |      /* USB address           */
00208                                               ((desc_ptr[2] & 0x7F) << 7)   |      /* Endpoint address      */
00209                                               (2 << 11)                     |      /* direction             */
00210                                               (ReadLE16U(&desc_ptr[4]) << 16);     /* MaxPkt Size           */
00211                        #endif
00212                        // pEpIn = new UsbEndpoint((desc_ptr[2] & 0x7F), true, ReadLE16U(&desc_ptr[4]));
00213                        }
00214                        epIn++;
00215                        desc_ptr += desc_ptr[0];                     /* Move to next descriptor start      */
00216                      } else {                                         /* If it is Out endpoint              */
00217                        if(epOut == 0)
00218                        {
00219                          PRINT_Log("\r\nEp %02x is out ep.\r\n", desc_ptr[2]);
00220                        #if 0
00221                          EDBulkOut->Control = 1                             |      /* USB address           */
00222                                               ((desc_ptr[2] & 0x7F) << 7)   |      /* Endpoint address      */
00223                                               (1 << 11)                     |      /* direction             */
00224                                               (ReadLE16U(&desc_ptr[4]) << 16);     /* MaxPkt Size           */
00225                        #endif
00226                         // pEpOut = new UsbEndpoint((desc_ptr[2] & 0x7F), false, ReadLE16U(&desc_ptr[4]));
00227                        }
00228                        epOut++;
00229                        desc_ptr += desc_ptr[0];                     /* Move to next descriptor start      */
00230                      }
00231                  } else {                                             /* If it is not bulk end point        */
00232                      desc_ptr += desc_ptr[0];                         /* Move to next descriptor start      */
00233                  }
00234                  break;
00235 
00236             default:                                 /* If the descriptor is neither interface nor endpoint */
00237                  desc_ptr += desc_ptr[0];                             /* Move to next descriptor start      */
00238                  break;
00239         }
00240     }
00241     if (ser_int_found==2) {
00242         PRINT_Log("Virtual Serial Port device %04x:%04x connected, vid=%d, pid=%d, E220=%d\n", m_vid, m_pid, ( m_vid  == 0x12d1 ), ( m_pid == 0x1003 ), (( m_vid  == 0x12d1 ) && ( m_pid == 0x1003 )));
00243       if(m_vid==0x0af0) //Voda
00244       {
00245         pEpOut = new UsbEndpoint((0x0a & 0x7F), false, 64);
00246         pEpIn = new UsbEndpoint((0x8b & 0x7F), true, 64);
00247         PRINT_Log("Voda K3760\r\n");
00248       }
00249       else //if ( ( m_vid  == 0x12d1 ) && ( m_pid == 0x1003 ) )
00250       /*{*/
00251       if ( ( m_vid  == 0x12d1 ) && ( m_pid == 0x1003 ) )
00252       {
00253         pEpOut = new UsbEndpoint((0x02 & 0x7F), false, 64);
00254         pEpIn = new UsbEndpoint((0x82 & 0x7F), true, 64);
00255         PRINT_Log("Huawei E220\r\n");
00256       }
00257       /*else
00258       {
00259         if(( m_vid  == 0x12d1 ))
00260            PRINT_Log("VID OK\r\n");
00261         else
00262            PRINT_Log("VID NOK\r\n");
00263         if(( m_pid  == 0x1003 ))
00264            PRINT_Log("VID OK\r\n");
00265         else
00266            PRINT_Log("VID NOK\r\n");
00267         PRINT_Log("\r\n....\r\n\r\n");
00268         error("\r\n\r\n");
00269       }*/
00270       //} 
00271       #if 1
00272       else /*if (  m_vid  == 0x12d1 &&
00273             m_pid == 0x1001 )*/
00274       {
00275         pEpOut = new UsbEndpoint((0x01 & 0x7F), false, 64);
00276         pEpIn = new UsbEndpoint((0x82 & 0x7F), true, 64); 
00277         PRINT_Log("Huawei E1550\r\n");
00278       }   
00279       #endif
00280         
00281         PRINT_Log("Virtual Serial Port device %04x:%04x connected\n", m_vid, m_pid);
00282         return (OK);
00283     }
00284     else if (ser_int_found==3) {
00285         PRINT_Log("CDFS dongle connected, reset\n");
00286         return (OK);
00287     } else {
00288         PRINT_Log("Not a Virtual Serial Port device\n");
00289         return (ERR_NO_MS_INTERFACE);
00290     }
00291 }
00292 
00293 
00294 USB_INT32S SerialRx( volatile USB_INT08U* buf, USB_INT32U len )
00295 {
00296     USB_INT32S rc;
00297     rc = pEpIn->transfer(buf, len);
00298     return rc;
00299 }
00300 
00301 USB_INT32S SerialReceived()
00302 {
00303   return pEpIn->status();
00304 }
00305 
00306 USB_INT32S SerialTx( volatile USB_INT08U* buf, USB_INT32U len )
00307 {
00308     USB_INT32S rc;
00309     rc = pEpOut->transfer(buf, len);
00310   //  PRINT_Log("\r\nOut rc = %d\r\n",len);
00311     return rc;
00312 }
00313 
00314 USB_INT32S SerialTransmitted()
00315 {
00316   USB_INT32S rc = pEpOut->status();
00317 /*  if(rc>=0)
00318   {
00319       PRINT_Log("\r\nTransmitted %d\r\n",rc);
00320   }*/
00321   return rc;
00322 }
00323 
00324 USB_INT32S SerialReg(USB_INT16U vid, USB_INT16U pid) {return 0;}
00325 
00326 #endif