如何編寫Windows CE.net的usb驅(qū)動(dòng)程序(2)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
上述講了堆理論,可能讀者腦袋都已經(jīng)大了,為此,我們舉個(gè)簡單的例子來詳細(xì)說明一下驅(qū)動(dòng)程序的開發(fā)過程。
例如我們有個(gè)USBMouse設(shè)備,設(shè)備信息描述如下:
DeviceDescriptor:
bcdUSB:0x0100
bDeviceClass:0x00
bDeviceSubClass:0x00
bDeviceProtocol:0x00
bMaxPacketSize0:0x08(8)
idVendor:0x05E3(GenesysLogicInc.)
idProduct:0x0001
bcdDevice:0x0101
iManufacturer:0x00
iProduct:0x01
iSerialNumber:0x00
bNumConfigurations:0x01
ConnectionStatus:DeviceConnected
CurrentConfigValue:0x01
DeviceBusSpeed:Low
DeviceAddress:0x02
OpenPipes:1
EndpointDescriptor:
bEndpointAddress:0x81
TransferType:Interrupt
wMaxPacketSize:0x0003(3)
bInterval:0x0A
可以看出上述設(shè)備有一個(gè)中斷PIPE,包的最大值為3。可能有人問上述的值怎么得到的,win2k的DDK中有個(gè)usbview的例程,編譯一下,將你的USB設(shè)備插到PC機(jī)的USB口中,運(yùn)行usbview.exe即可看得相應(yīng)的設(shè)備信息。
有了這些基本信息,就可以編寫USB設(shè)備了,首先聲明一下,下面的代碼取自微軟的USB鼠標(biāo)樣本程序,版權(quán)歸微軟所有,此處僅僅借用來描述一下USB鼠標(biāo)驅(qū)動(dòng)的開發(fā)過程,讀者如需要引用此代碼,需要得到微軟的同意。
首先,必須輸出USBD要求調(diào)用的三個(gè)函數(shù),首先到設(shè)備插入到USB端口時(shí),USBD會(huì)調(diào)用USBDeviceAttach()函數(shù),相應(yīng)的代碼如下:
extern"C"BOOL
USBDeviceAttach(
USB_HANDLEhDevice,//USB設(shè)備句柄
LPCUSB_FUNCSlpUsbFuncs,//USBDI的函數(shù)集合
LPCUSB_INTERFACElpInterface,//設(shè)備接口描述信息
LPCWSTRszUniqueDriverId,//設(shè)備ID描述字符串。
LPBOOLfAcceptControl,//返回TRUE,標(biāo)識(shí)我們可以控制此設(shè)備,反之表示不能控制
DWORDdwUnused)
{
*fAcceptControl=FALSE;
//我們的鼠標(biāo)設(shè)備有特定的描述信息,要檢測是否是我們的設(shè)備。
if(lpInterface==NULL)
returnFALSE;
//打印相關(guān)的USB設(shè)備接口描述信息。
DEBUGMSG(ZONE_INIT,(TEXT("USBMouse:DeviceAttach,IF%u,#EP:%u,Class:%u,Sub:%u,Prot:%urn"),lpInterface->Descriptor.bInterfaceNumber,lpInterface->Descriptor.bNumEndpoints,lpInterface->Descriptor.bInterfaceClass,lpInterface->Descriptor.bInterfaceSubClass,lpInterface->Descriptor.bInterfaceProtocol));
//初試數(shù)據(jù)USB鼠標(biāo)類,產(chǎn)生一個(gè)接受USB鼠標(biāo)數(shù)據(jù)的線程
CMouse*pMouse=newCMouse(hDevice,lpUsbFuncs,lpInterface);
if(pMouse==NULL)
returnFALSE;
if(!pMouse->Initialize())
{
deletepMouse;
returnFALSE;
}
//注冊(cè)一個(gè)監(jiān)控USB設(shè)備事件的回調(diào)函數(shù),用于監(jiān)控USB設(shè)備是否已經(jīng)拔掉。
(*lpUsbFuncs->lpRegisterNotificationRoutine)(hDevice,
USBDeviceNotifications,pMouse);
*fAcceptControl=TRUE;
returnTRUE;
}
第二個(gè)函數(shù)是USBInstallDriver()函數(shù),
一些基本定義如下:
constWCHARgcszRegisterClientDriverId[]=L"RegisterClientDriverID";
constWCHARgcszRegisterClientSettings[]=L"RegisterClientSettings";
constWCHARgcszUnRegisterClientDriverId[]=L"UnRegisterClientDriverID";
constWCHARgcszUnRegisterClientSettings[]=L"UnRegisterClientSettings";
constWCHARgcszMouseDriverId[]=L"Generic_Sample_Mouse_Driver";
函數(shù)接口如下:
extern"C"BOOL
USBInstallDriver(
LPCWSTRszDriverLibFile)//@parm[IN]-ContainsclientdriverDLLname
{
BOOLfRet=FALSE;
HINSTANCEhInst=LoadLibrary(L"USBD.DLL");
//注冊(cè)USB設(shè)備信息
if(hInst)
{
LPREGISTER_CLIENT_DRIVER_IDpRegisterId=(LPREGISTER_CLIENT_DRIVER_ID)
GetProcAddress(hInst,gcszRegisterClientDriverId);
LPREGISTER_CLIENT_SETTINGSpRegisterSettings=
(LPREGISTER_CLIENT_SETTINGS)GetProcAddress(hInst,
gcszRegisterClientSettings);
if(pRegisterId&&pRegisterSettings)
{
USB_DRIVER_SETTINGSDriverSettings;
DriverSettings.dwCount=sizeof(DriverSettings);
//設(shè)置我們的特定的信息。
DriverSettings.dwVendorId=USB_NO_INFO;
DriverSettings.dwProductId=USB_NO_INFO;
DriverSettings.dwReleaseNumber=USB_NO_INFO;
DriverSettings.dwDeviceClass=USB_NO_INFO;
DriverSettings.dwDeviceSubClass=USB_NO_INFO;
DriverSettings.dwDeviceProtocol=USB_NO_INFO;
DriverSettings.dwInterfaceClass=0x03;//HID
DriverSettings.dwInterfaceSubClass=0x01;//bootdevice
DriverSettings.dwInterfaceProtocol=0x02;//mouse
fRet=(*pRegisterId)(gcszMouseDriverId);
if(fRet)
{
fRet=(*pRegisterSettings)(szDriverLibFile,
gcszMouseDriverId,NULL,&DriverSettings);
if(!fRet)
{
//BUGBUGunregistertheClientDriver’sID
}
}
}
else
{
RETAILMSG(1,(TEXT("!USBMouse:ErrorgettingUSBDfunctionpointersrn")));
}
FreeLibrary(hInst);
}
returnfRet;
}
上述代碼主要用于產(chǎn)生USB設(shè)備驅(qū)動(dòng)程序需要的注冊(cè)表信息,需要注意的是:USB設(shè)備驅(qū)動(dòng)程序不使用標(biāo)準(zhǔn)的注冊(cè)表函數(shù),而是使用RegisterClientDriverID()和RegisterClientSettings來注冊(cè)相應(yīng)的設(shè)備信息。[!--empirenews.page--]
另外一個(gè)函數(shù)是USBUninstallDriver()函數(shù),具體代碼如下:
extern"C"BOOL
USBUnInstallDriver()
{
BOOLfRet=FALSE;
HINSTANCEhInst=LoadLibrary(L"USBD.DLL");
if(hInst)
{
LPUN_REGISTER_CLIENT_DRIVER_IDpUnRegisterId=
(LPUN_REGISTER_CLIENT_DRIVER_ID)
GetProcAddress(hInst,gcszUnRegisterClientDriverId);
LPUN_REGISTER_CLIENT_SETTINGSpUnRegisterSettings=
(LPUN_REGISTER_CLIENT_SETTINGS)GetProcAddress(hInst,
gcszUnRegisterClientSettings);
if(pUnRegisterSettings)
{
USB_DRIVER_SETTINGSDriverSettings;
DriverSettings.dwCount=sizeof(DriverSettings);
//必須填入與注冊(cè)時(shí)相同的信息。
DriverSettings.dwVendorId=USB_NO_INFO;
DriverSettings.dwProductId=USB_NO_INFO;
DriverSettings.dwReleaseNumber=USB_NO_INFO;
DriverSettings.dwDeviceClass=USB_NO_INFO;
DriverSettings.dwDeviceSubClass=USB_NO_INFO;
DriverSettings.dwDeviceProtocol=USB_NO_INFO;
DriverSettings.dwInterfaceClass=0x03;//HID
DriverSettings.dwInterfaceSubClass=0x01;//bootdevice
DriverSettings.dwInterfaceProtocol=0x02;//mouse
fRet=(*pUnRegisterSettings)(gcszMouseDriverId,NULL,
&DriverSettings);
}
if(pUnRegisterId)
{
BOOLfRetTemp=(*pUnRegisterId)(gcszMouseDriverId);
fRet=fRet?fRetTemp:fRet;
}
FreeLibrary(hInst);
}
returnfRet;
}
此函數(shù)主要用于刪除USBInstallDriver()時(shí)創(chuàng)建的注冊(cè)表信息,同樣的它使用自己的函數(shù)接口UnRegisterClientDriverID()和UnRegisterClientSettings()來做相應(yīng)的處理。
另外一個(gè)需要處理的注冊(cè)的監(jiān)控通知函數(shù)USBDeviceNotifications():
extern"C"BOOLUSBDeviceNotifications(LPVOIDlpvNotifyParameter,DWORDdwCode,
LPDWORD*dwInfo1,LPDWORD*dwInfo2,LPDWORD*dwInfo3,
LPDWORD*dwInfo4)
{
CMouse*pMouse=(CMouse*)lpvNotifyParameter;
switch(dwCode)
{
caseUSB_CLOSE_DEVICE:
//刪除相關(guān)的資源。
deletepMouse;
returnTRUE;
}
returnFALSE;
}
USB鼠標(biāo)的類的定義如下:
classCMouse
{
public:
CMouse::CMouse(USB_HANDLEhDevice,LPCUSB_FUNCSlpUsbFuncs,
LPCUSB_INTERFACElpInterface);
~CMouse();
BOOLInitialize();
private:
//傳輸完畢調(diào)用的回調(diào)函數(shù)
staticDWORDCALLBACKMouseTransferCompleteStub(LPVOIDlpvNotifyParameter);
//中斷處理函數(shù)
staticULONGCALLBACKCMouse::MouseThreadStub(PVOIDcontext);
DWORDMouseTransferComplete();
DWORDMouseThread();
BOOLSubmitInterrupt();
BOOLHandleInterrupt();
BOOLm_fClosing;
BOOLm_fReadyForMouseEvents;
HANDLEm_hEvent;
HANDLEm_hThread;
USB_HANDLEm_hDevice;
USB_PIPEm_hInterruptPipe;
USB_TRANSFERm_hInterruptTransfer;
LPCUSB_FUNCSm_lpUsbFuncs;
LPCUSB_INTERFACEm_pInterface;
BOOLm_fPrevButton1;
BOOLm_fPrevButton2;
BOOLm_fPrevButton3;
//數(shù)據(jù)接受緩沖區(qū)。
BYTEm_pbDataBuffer[8];
};
具體實(shí)現(xiàn)如下:
//構(gòu)造函數(shù),初始化時(shí)調(diào)用
CMouse::CMouse(USB_HANDLEhDevice,LPCUSB_FUNCSlpUsbFuncs,
LPCUSB_INTERFACElpInterface)
{
m_fClosing=FALSE;
m_fReadyForMouseEvents=FALSE;
m_hEvent=NULL;
m_hThread=NULL;
m_hDevice=hDevice;
m_hInterruptPipe=NULL;
m_hInterruptTransfer=NULL;
m_lpUsbFuncs=lpUsbFuncs;
m_pInterface=lpInterface;
m_fPrevButton1=FALSE;
m_fPrevButton2=FALSE;
m_fPrevButton3=FALSE;
memset(m_pbDataBuffer,0,sizeof(m_pbDataBuffer));
}
//析構(gòu)函數(shù),用于清除申請(qǐng)的資源。
CMouse::~CMouse()
{
//通知系統(tǒng)去關(guān)閉相關(guān)的函數(shù)接口。
m_fClosing=TRUE;
//Wakeuptheconnectionthreadagainandgiveittimetodie.
if(m_hEvent!=NULL)
{
//通知關(guān)閉數(shù)據(jù)接受線程。
SetEvent(m_hEvent);
if(m_hThread!=NULL)
{
DWORDdwWaitReturn;
dwWaitReturn=WaitForSingleObject(m_hThread,1000);
if(dwWaitReturn!=WAIT_OBJECT_0)
{
TerminateThread(m_hThread,DWORD(-1));
}
CloseHandle(m_hThread);
m_hThread=NULL;
}
CloseHandle(m_hEvent);
m_hEvent=NULL;
}
if(m_hInterruptTransfer)
(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);
if(m_hInterruptPipe)
(*m_lpUsbFuncs->lpClosePipe)(m_hInterruptPipe);
}
//初始化USB鼠標(biāo)驅(qū)動(dòng)程序
BOOLCMouse::Initialize()
{
LPCUSB_DEVICElpDeviceInfo=(*m_lpUsbFuncs->lpGetDeviceInfo)(m_hDevice);
//檢測配置:USB鼠標(biāo)應(yīng)該只有一個(gè)中斷管道
if((m_pInterface->lpEndpoints[0].Descriptor.bmAttributes&USB_ENDPOINT_TYPE_MASK)!=USB_ENDPOINT_TYPE_INTERRUPT)
{
RETAILMSG(1,(TEXT("!USBMouse:EP0wrongtype(%u)!rn"),[!--empirenews.page--]
m_pInterface->lpEndpoints[0].Descriptor.bmAttributes));
returnFALSE;
}
DEBUGMSG(ZONE_INIT,(TEXT("USBMouse:EP0:MaxPacket:%u,Interval:%urn"),
m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,
m_pInterface->lpEndpoints[0].Descriptor.bInterval));
m_hInterruptPipe=(*m_lpUsbFuncs->lpOpenPipe)(m_hDevice,
&m_pInterface->lpEndpoints[0].Descriptor);
if(m_hInterruptPipe==NULL){
RETAILMSG(1,(TEXT("Mouse:Erroropeninginterruptpipern")));
return(FALSE);
}
m_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
if(m_hEvent==NULL)
{
RETAILMSG(1,(TEXT("USBMouse:ErroronCreateEventforconnecteventrn")));
return(FALSE);
}
//創(chuàng)建數(shù)據(jù)接受線程
m_hThread=CreateThread(0,0,MouseThreadStub,this,0,NULL);
if(m_hThread==NULL)
{
RETAILMSG(1,(TEXT("USBMouse:ErroronCreateThreadrn")));
return(FALSE);
}
return(TRUE);
}
//從USB鼠標(biāo)設(shè)備中讀出數(shù)據(jù),產(chǎn)生相應(yīng)的鼠標(biāo)事件。
BOOLCMouse::SubmitInterrupt()
{
if(m_hInterruptTransfer)
(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);
//從USB鼠標(biāo)PIPE中讀數(shù)據(jù)
m_hInterruptTransfer=(*m_lpUsbFuncs->lpIssueInterruptTransfer)
(m_hInterruptPipe,MouseTransferCompleteStub,this,
USB_IN_TRANSFER|USB_SHORT_TRANSFER_OK,//表示讀數(shù)據(jù)
min(m_pInterface->lpEndpoints[0].Descriptor.wMaxPacketSize,
sizeof(m_pbDataBuffer)),
m_pbDataBuffer,
NULL);
if(m_hInterruptTransfer==NULL)
{
DEBUGMSG(ZONE_ERROR,(L"!USBMouse:ErrorinIssueInterruptTransferrn"));
returnFALSE;
}
else
{
DEBUGMSG(ZONE_TRANSFER,(L"USBMouse::SubmitInterrupt,Transfer:0x%Xrn",
m_hInterruptTransfer));
}
returnTRUE;
}
//處理鼠標(biāo)中斷傳輸?shù)臄?shù)據(jù)
BOOLCMouse::HandleInterrupt()
{
DWORDdwError;
DWORDdwBytes;
DWORDdwFlags=0;
INTdx=(signedchar)m_pbDataBuffer[1];
INTdy=(signedchar)m_pbDataBuffer[2];
BOOLfButton1=m_pbDataBuffer[0]&0x01?TRUE:FALSE;
BOOLfButton2=m_pbDataBuffer[0]&0x02?TRUE:FALSE;
BOOLfButton3=m_pbDataBuffer[0]&0x04?TRUE:FALSE;
if(!(*m_lpUsbFuncs->lpGetTransferStatus)(m_hInterruptTransfer,&dwBytes,&dwError))
{
DEBUGMSG(ZONE_ERROR,(TEXT("!USBMouse:ErrorinGetTransferStatus(0x%X)rn"),
m_hInterruptTransfer));
returnFALSE;
}
else
{
DEBUGMSG(ZONE_TRANSFER,(TEXT("USBMouse::HandleInterrupt,hTransfer0x%Xcomplete(%ubytes,Error:%X)rn"),
m_hInterruptTransfer,dwBytes,dwError));
}
if(!SubmitInterrupt())
returnFALSE;
if(dwError!=USB_NO_ERROR)
{
DEBUGMSG(ZONE_ERROR,(TEXT("!USBMouse:Error0x%Xininterrupttransferrn"),dwError));
returnTRUE;
}
if(dwBytes<3)
{
DEBUGMSG(ZONE_ERROR,(TEXT("!USBMouse:Invalidbytecnt%ufrominterrupttransferrn"),dwBytes));
returnTRUE;
}
if(dx||dy)
dwFlags|=MOUSEEVENTF_MOVE;
if(fButton1!=m_fPrevButton1)
{
if(fButton1)
dwFlags|=MOUSEEVENTF_LEFTDOWN;
else
dwFlags|=MOUSEEVENTF_LEFTUP;
}
if(fButton2!=m_fPrevButton2)
{
if(fButton2)
dwFlags|=MOUSEEVENTF_RIGHTDOWN;
else
dwFlags|=MOUSEEVENTF_RIGHTUP;
}
if(fButton3!=m_fPrevButton3)
{
if(fButton3)
dwFlags|=MOUSEEVENTF_MIDDLEDOWN;
else
dwFlags|=MOUSEEVENTF_MIDDLEUP;
}
m_fPrevButton1=fButton1;
m_fPrevButton2=fButton2;
m_fPrevButton3=fButton3;
DEBUGMSG(ZONE_EVENTS,
(TEXT("USBMouseevent:dx:%d,dy:%d,dwFlags:0x%X(B1:%u,B2:%u,B3:%u)rn"),
dx,dy,dwFlags,fButton1,fButton2,fButton3));
//通知系統(tǒng)產(chǎn)生鼠標(biāo)事件
if(m_fReadyForMouseEvents)
mouse_event(dwFlags,dx,dy,0,0);
else
m_fReadyForMouseEvents=IsAPIReady(SH_WMGR);
returnTRUE;
}
DWORDCALLBACKCMouse::MouseTransferCompleteStub(LPVOIDlpvNotifyParameter)
{
CMouse*pMouse=(CMouse*)lpvNotifyParameter;
return(pMouse->MouseTransferComplete());
}
//數(shù)據(jù)傳輸完畢回調(diào)函數(shù)
DWORDCMouse::MouseTransferComplete()
{
if(m_hEvent)
SetEvent(m_hEvent);
return0;
}
ULONGCALLBACKCMouse::MouseThreadStub(PVOIDcontext)
{
CMouse*pMouse=(CMouse*)context;
return(pMouse->MouseThread());
}
//USB鼠標(biāo)線程
DWORDCMouse::MouseThread()
{
DEBUGMSG(ZONE_INIT,(TEXT("USBMouse:Workerthreadstartedrn")));
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);[!--empirenews.page--]
if(SubmitInterrupt())
{
while(!m_fClosing)
{
WaitForSingleObject(m_hEvent,INFINITE);
if(m_fClosing)
break;
if((*m_lpUsbFuncs->lpIsTransferComplete)(m_hInterruptTransfer))
{
if(!HandleInterrupt())
break;
}
else
{
RETAILMSG(1,(TEXT("!USBMouse:Eventsignalled,buttransfernotcompletern")));
//Theonlytimethisshouldhappenisifwegetanerroronthetransfer
ASSERT(m_fClosing||(m_hInterruptTransfer==NULL));
break;
}
}
}
RETAILMSG(1,(TEXT("USBMouse:Workerthreadexitingrn")));
return(0);
}
看到了沒有,其實(shí)USB的驅(qū)動(dòng)程序編寫就這么簡單,類似的其他設(shè)備,例如打印機(jī)設(shè)備,就有BulkOUTPIPE,需要Bulk傳輸,那就需要了解一下IssueBulkTransfer()的應(yīng)用。當(dāng)然如果是開發(fā)USBMassStorageDisk的驅(qū)動(dòng),那就需要了解更多的協(xié)議,例如Bulk-OnlyTransport協(xié)議等。
微軟的WindowsCE.NET的PlatformBuild中已經(jīng)帶有USBPrinter和USBMassStorageDisk的驅(qū)動(dòng)的源代碼了,好好研究一下,你一定回受益非淺的。