⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m3usbc.cpp.svn-base

📁 vc环境下编译的一个对USB设备进行烧录的驱动程序
💻 SVN-BASE
字号:
/*
   //*******************************************************************
   //
   //                 A n y k a     P r o p r i e t a r y
   //
   //                 COPYRIGHT (c)   2004 BY ANYKA CHINA
   //                      --   ALL RIGHT RESERVE  --          
   //
   //FileName:     m3usbc.cpp
   //Author:       skyxiang 
   //Creat time:   12 Fre 2004
   //Modified:
   //Revision:     1.0

   //*******************************************************************
*/


//Includes
#include <stdafx.h>
//#include <afxwin.h>
//#include <afxcmn.h>
//#include <iostream.h>
#include <afxtempl.h>
#include <objbase.h>
//#include "devioctl.h"

#include <initguid.h>
#include "M3USBFUN.h"
#include "USBCRC16.h"
#include "devioctl.h"
#include "Resource.h"
#include "burn.h"


DEFINE_GUID(M3USB_GUID, 
0x6e7ac6a7, 0x1a4c, 0x4a72, 0x83, 0x54, 0x89, 0x95, 0x11, 0xf8, 0x13, 0x51);

//DEFINE_GUID(M3USB_GUID,
//0xe4c3a4bc, 0xa77a, 0x40d4, 0x80, 0xa0, 0xeb, 0xde, 0xf4, 0x6, 0xf7, 0xe2);

#define IOCTL_MYDEV_GET_FIRMWARE_REV \
		CTL_CODE(FILE_DEVICE_UNKNOWN,0,METHOD_OUT_DIRECT,FILE_ANY_ACCESS)




CM3USBC::CM3USBC()
{
	M3USB_HD = INVALID_HANDLE_VALUE;
}

CM3USBC::~CM3USBC()
{
	M3USBCLOSE();
}

HANDLE CM3USBC::Get_M3USBHandle()
{
	return M3USB_HD;
}

extern char *usb_name_index;
extern char usb_name[MAX_DEVICE_NUM][255];

bool CM3USBC::M3USBOPEN()
{
	SP_DEVINFO_DATA spdd, spddTmp;
	CString m3devicepath,m3devicepathTmp;
	bool bError = false;

	if(M3USBOPENQUICK(spdd, m3devicepath))
	{
		M3USB_HD =	CreateFile(
					   m3devicepath,
					   GENERIC_READ | GENERIC_WRITE,
					   0,
					   NULL,
					   OPEN_EXISTING,
					   0,
					   NULL
					   );
		if(M3USB_HD == INVALID_HANDLE_VALUE)
		{
			return false;
		}

		DevInst = spdd.DevInst;
		strcpy(usb_name_index, m3devicepath);
	}
	else
	{
		return false;
	}
    
	while(M3USBOPENQUICK(spddTmp, m3devicepathTmp))
	{
		bError = true;
		M3USBCLOSEQUICK(spddTmp);
	}

	if(bError)
	{
		M3USBCLOSE();
		usb_name_index[0] = '\0';
		return false;
	}

	return(true);
	
}

bool CM3USBC::M3USBOPEN(CString dbcc_name)
{
    
    GUID                              *m3usb_guid; 
	HDEVINFO                          m3info;
    SP_INTERFACE_DEVICE_DATA		  m3InterfaceInfo;
	PSP_INTERFACE_DEVICE_DETAIL_DATA  m3InterfaceDetail;
    ULONG						      m3requiredsize;
	ULONG							  m3predictedsize;
    CString							  m3devicepath;	
	
	int								  loop = 0;
	bool							  init = false;

    SP_DEVINFO_DATA                   spdd;

    if(dbcc_name.IsEmpty())
        return false;

    dbcc_name.MakeLower();

    M3USB_HD =	CreateFile(
		           dbcc_name,
                   GENERIC_READ | GENERIC_WRITE,
                   0,
				   NULL,
				   OPEN_EXISTING,
				   0,
				   NULL
				   );
    if(M3USB_HD == INVALID_HANDLE_VALUE)
	{
		return false;
	}

    m3usb_guid =(LPGUID)&M3USB_GUID;
	m3info     = SetupDiGetClassDevs(						//get the device info
		m3usb_guid,
		NULL,
		NULL,
		DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
	
	if( m3info == INVALID_HANDLE_VALUE )
		return false;

    m3InterfaceInfo.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    for (int i=0;; i++)				//8 usb interface  that can more than 8
	{

		if( !SetupDiEnumInterfaceDevice(					//get the interface info
			m3info,
			0,
			m3usb_guid,
			i,
			&m3InterfaceInfo) )
		{
			SetupDiDestroyDeviceInfoList(m3info);
			return false;
		}
		
		
		SetupDiGetDeviceInterfaceDetail(                  //get the device interface detail
			m3info,
			&m3InterfaceInfo,
			NULL,
			0,
			&m3requiredsize,
			NULL);
		
		m3predictedsize           = m3requiredsize;
		m3InterfaceDetail         = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(m3predictedsize);
		m3InterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
        
        ZeroMemory(&spdd, sizeof(spdd));
		spdd.cbSize = sizeof(spdd);
        
		if(! SetupDiGetDeviceInterfaceDetail(
			m3info,
			&m3InterfaceInfo,
			m3InterfaceDetail,
			m3predictedsize,
			&m3requiredsize,
			&spdd) )
		{
			free((PVOID) m3InterfaceDetail);
			SetupDiDestroyDeviceInfoList(m3info);
			return false;
		}

        CString DevicePath;

        DevicePath = m3InterfaceDetail->DevicePath;

        DevicePath.MakeLower();
		
		if(DevicePath == dbcc_name)
		{
            DevInst = spdd.DevInst;
            free((PVOID) m3InterfaceDetail);
			break;
		}   

        free((PVOID) m3InterfaceDetail);
	}

    return true;
}

bool CM3USBC::M3USBREAD(BYTE *rbuf, int rbufcount, int *read_count)
{
	ULONG nReaden;
	if(rbufcount>0)
	{
		if( M3USB_HD !=INVALID_HANDLE_VALUE )
		{
			if(!ReadFile(M3USB_HD,rbuf,rbufcount,&nReaden,NULL))
			{
				return false;
			}
			*read_count = nReaden;
			return true;
		}
		
	}
	else
		return false;
   return false;
}
    
  
bool CM3USBC::M3USB_UPLOAD( BYTE* upload_buf, UINT upload_count, UINT* uploaded_count)
{
	UINT  i;
	//BYTE  temp[4096];
	BYTE  *temp_store = new BYTE[upload_count];
	INT  readed_count;
	UINT  count_0_time = 0;
	UINT  already_read = 0;
	UINT   left_count = upload_count;
	while(1)
	{
		if( !M3USBREAD( temp_store,left_count, &readed_count) )
		{
			delete[] temp_store;
			temp_store = NULL;
			return false;
		}
		if( readed_count == 0 )
		{
			Sleep(10);
			count_0_time++;
			if( count_0_time == 5 )
			{
				*uploaded_count = already_read;
				delete[] temp_store;
				temp_store = NULL;
			//	::PostMessage((AfxGetMainWnd()->m_hWnd), WM_MY_PROGRESS_CLOSE_U, 0, 0 );
				return true;
			}
		}
		else
		{
			count_0_time = 0;
		}

		for(i = 0; i < readed_count; i++ )
		{
			upload_buf[ i+ already_read] = temp_store[i];
		}
		already_read = already_read + readed_count;
//		::PostMessage(AfxGetMainWnd()->m_hWnd, WM_MY_PROGRESS_STEP, already_read, 0);
		left_count = left_count - readed_count;
		if( left_count == 0 )
		{
			*uploaded_count = already_read;
			delete[] temp_store;
			temp_store = NULL;
//			::PostMessage((AfxGetMainWnd()->m_hWnd), WM_MY_PROGRESS_CLOSE_U, 0, 0 );
			return true;
		}
		

	}
	

	
	

	/*
	for( i = 0; i < count_4k; i++ )
	{
		if( !M3USBREAD( temp,4096, &readed_count) )
		{
			*uploaded_count = i*4096;
			return false;
		}
		if(readed_count == 0)
		{
			Sleep(1);
			return false;
		}
		for( j = 0; j < 4096; j++ )
		{
			upload_buf[ i*4096+j] = temp[j];
		}

		::PostMessage(AfxGetMainWnd()->m_hWnd, WM_MY_PROGRESS_STEP, i*4096, 0);

	}

	if( upload_count % 4096 != 0 )
	{
		if( !M3USBREAD( temp, (upload_count % 4096), &readed_count) )
		{
			*uploaded_count = count_4k * 4096;
			return false;
		}

		for( i = 0; i < upload_count%4096; i++ )
		{
			upload_buf[ count_4k*4096 + i ] = temp[i];
		}
	}
	while(1)
	{
		if( !M3USBREAD( temp, 64, &readed_count) )
		{
			*uploaded_count = upload_count;
			return false;
		}
		if( readed_count == 0 )
		{
		    break;
		}
	}
	
	*uploaded_count = upload_count;

	::PostMessage((AfxGetMainWnd()->m_hWnd), WM_MY_PROGRESS_CLOSE_U, 0, 0 );

	*/
}


bool CM3USBC::M3USB_DOWNLOAD(BYTE *down_buf, UINT down_count, UINT *downed_count )
{

	UINT i,j;
	UINT count_4k;
	UINT fill_64;
	BYTE temp[4096];
	*downed_count = 0;
	count_4k = down_count/4096;
	//CWnd *pMainWnd = AfxGetMainWnd();
//	CDownload *pCDownload = (CDownload*)AfxGetMainWnd();
//	CM3USBAPPDlg *pMainWnd = (CM3USBAPPDlg*)AfxGetMainWnd();
	for(i=0; i<count_4k; i++)
	{
		 for( j=0; j<4096; j++)
		 {
			 temp[j]=*(down_buf+i*4096+j);
		 }

	     if( !M3USBWRITE(temp,4096) )
		 {
			*downed_count = i*4096;
			return false;
		 }	

//		 Cdldlg.m_progress.SetPos(i*4096);
		
		
		
//		 ::PostMessage(AfxGetMainWnd()->m_hWnd, WM_MY_PROGRESS_STEP, (i*4096),0);

	}

	if( down_count % 4096 != 0 )
	{
		fill_64 = down_count % 4096;
		if( down_count % 64 != 0 )
		{
			fill_64 = down_count%4096 + 64 - (down_count % 4096) % 64 ;
		}
		for( i = 0; i < fill_64; i++)
		{
			temp[i] = 0;
			if( i < down_count%4096 )
			{
				temp[i] = * ( down_buf + count_4k*4096 + i );
			}
		}

		if( !M3USBWRITE( temp, fill_64 ) )
		{
			*downed_count = count_4k*4096;
			return false;
		}
	}

	*downed_count =  count_4k * 4096 + down_count % 4096;
	


//	::PostMessage(AfxGetMainWnd()->m_hWnd, WM_MY_PROGRESS_CLOSE_D, 0, 0);

	
	//::SendMessage(AfxGetMainWnd()->m_hWnd, WM_MY_PROGRESS_CLOSE_D, 0, 0);

	return true;

}



bool CM3USBC::M3USBWRITE(BYTE *wbuf, int wbufcount)
{	
	ULONG nWritten;
	if( wbufcount > 0 && ( wbufcount % 64 == 0 ) )
	{
		if( M3USB_HD !=INVALID_HANDLE_VALUE )
		{
			if(!WriteFile(M3USB_HD,wbuf,wbufcount,&nWritten,NULL))
			{
				return false;
			}
			return true;
		}
	}
	return false;

}



bool CM3USBC::M3USBDeviceIo(BYTE *obuf, int obufcount)
{
	DWORD nBytes;   
	BOOLEAN bResult=0;
	if( M3USB_HD !=INVALID_HANDLE_VALUE )
	{
		bResult = DeviceIoControl(M3USB_HD,    
        IOCTL_MYDEV_GET_FIRMWARE_REV,    
                    NULL, 0,               // no input buffer
                    obuf, obufcount,     // output buffer
                    &nBytes, NULL);               // # bytes returned
	}

    if(bResult)
	{
		return true;
	}

	return false;
	
}


bool CM3USBC::M3USBCLOSE()
{
    
    if(M3USB_HD != INVALID_HANDLE_VALUE)
    {


        if( !CloseHandle(M3USB_HD) )
        {
            MessageBox(NULL,"Failed to close m3chip usb interace","failed to close", MB_OK);  
            return false;
        }
       
       
        M3USB_HD = INVALID_HANDLE_VALUE;

        long res;


        //DEVINST DevInstParent = 0;
        //res = CM_Get_Parent(&DevInstParent, DevInst, 0); 

        for ( long tries=1; tries<=3; tries++ )
        { // sometimes we need some tries...

            WCHAR VetoNameW[MAX_PATH];
            PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown;
            bool bSuccess = false;

            VetoNameW[0] = 0;

            //res = CM_Request_Device_EjectW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, 0); 

            res = CM_Request_Device_EjectW(DevInst, &VetoType, VetoNameW, MAX_PATH, 0); 

/*
            res = CM_Query_And_Remove_SubTreeW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, 0); // CM_Query_And_Remove_SubTreeA is not implemented under W2K!

            if ( res == CR_ACCESS_DENIED ) 
            { // no admin rights
                res = CM_Request_Device_EjectW(DevInstParent, &VetoType, VetoNameW, MAX_PATH, 0); 
            }
*/
            bSuccess = (res==CR_SUCCESS && VetoType == PNP_VetoTypeUnknown);
            
            if ( bSuccess )  
            { 
                break;
            }

            Sleep(500); // required to give the next tries a chance!
        }
        
        return true;
    }

    return false;

}



bool CM3USBC::M3USBOPENQUICK(SP_DEVINFO_DATA &spdd, CString &m3devicepath)
{
	ULONG					          i;								
    GUID                              *m3usb_guid; 
	HDEVINFO                          m3info;
    SP_INTERFACE_DEVICE_DATA		  m3InterfaceInfo;
	PSP_INTERFACE_DEVICE_DETAIL_DATA  m3InterfaceDetail;
    ULONG						      m3requiredsize;
	ULONG							  m3predictedsize;
	char							  name[260];
	
	int								  loop = 0;
	bool							  init = false;	

	for (i=0;; i++)				//8 usb interface  that can more than 8
	{ 
		m3usb_guid =(LPGUID)&M3USB_GUID;
		m3info     = SetupDiGetClassDevs(						//get the device info
			m3usb_guid,
			NULL,
			NULL,
			DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
		
		if( m3info == INVALID_HANDLE_VALUE )
			return false;
		
		m3InterfaceInfo.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

		if( !SetupDiEnumInterfaceDevice(					//get the interface info
			m3info,
			0,
			m3usb_guid,
			i,
			&m3InterfaceInfo) )
		{
			SetupDiDestroyDeviceInfoList(m3info);
			return false;
		}
		
		
		SetupDiGetDeviceInterfaceDetail(                  //get the device interface detail
			m3info,
			&m3InterfaceInfo,
			NULL,
			0,
			&m3requiredsize,
			NULL);
		
		m3predictedsize           = m3requiredsize;
		m3InterfaceDetail         = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(m3predictedsize);
		m3InterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    
		ZeroMemory(&spdd, sizeof(spdd));
		spdd.cbSize = sizeof(spdd);
    
		if(! SetupDiGetDeviceInterfaceDetail(
			m3info,
			&m3InterfaceInfo,
			m3InterfaceDetail,
			m3predictedsize,
			&m3requiredsize,
			&spdd) )
		{
			free((PVOID) m3InterfaceDetail);
			SetupDiDestroyDeviceInfoList(m3info);
			return false;
		}
		strncpy(name,m3InterfaceDetail->DevicePath,sizeof(name));
		m3devicepath = name;
		free((PVOID) m3InterfaceDetail);
		
		for(int j = 0; j < MAX_DEVICE_NUM; j++)
		{
			if(strcmp((const char *)m3devicepath, (const char *)usb_name[j]) == 0)
			{
				break;
			}
		}

		if(j == MAX_DEVICE_NUM)
		{
			return true;
		}
	}

	return(true);
}

bool CM3USBC::M3USBCLOSEQUICK(SP_DEVINFO_DATA spdd)
{
    long res;

    for ( long tries=1; tries<=3; tries++ )
    { // sometimes we need some tries...

        WCHAR VetoNameW[MAX_PATH];
        PNP_VETO_TYPE VetoType = PNP_VetoTypeUnknown;
        bool bSuccess = false;

        VetoNameW[0] = 0;

        res = CM_Request_Device_EjectW(spdd.DevInst, &VetoType, VetoNameW, MAX_PATH, 0); 

        bSuccess = (res==CR_SUCCESS && VetoType == PNP_VetoTypeUnknown);
        
        if ( bSuccess )  
        { 
            return true;
        }

        Sleep(500); // required to give the next tries a chance!
    }

    return false;

}




/**********************future Revision: *********************************************
	1. to suport more than one same device:   change the SetupDiEnumInterfaceDevice() 
	                                          to see the USB_WIN APPLICATION
    2.  
**************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -