Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1273686
  • 博文数量: 482
  • 博客积分: 13297
  • 博客等级: 上将
  • 技术积分: 2890
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 16:25
文章分类

全部博文(482)

文章存档

2012年(9)

2011年(407)

2010年(66)

分类: WINDOWS

2011-12-06 11:51:04

 近几天想学习使用USB的通信协议,看了几天中文规范,没有任何进展,所以就先从现成的USB驱动着手,USB驱动使用Windriver (Jungo.WinDriver.v10.10.Incl.Keymaker-EMBRACE)完成框架,在对其进行相应修改,不过还是首先要看懂USB驱动协议才能够修改。

    对生成的驱动程序进行分析,使用Windriver生成的USB驱动框架中包含了:diag_lib.c、usb_diag_lib.c、usbdrvier_diag.c三个源文件,其中usbdrvier为自己建的驱动工程名。

    就先对usbdrvier_diag.c文件进行理解:

   1.  使用windriver的库文件,包括、 "wdu_lib.h"、"status_strings.h"、 "utils.h"、"usb_diag_lib.h"、

"windrvr.h"、"diag_lib.h"、"wd_ver.h"、"windrvr_usb.h"。在这里也需要实现自己的驱动函数功能,所以还要调用usbdriver_diag.h对自定义的函数进行定义。

//设备上下文
typedef struct DEVICE_CONTEXT
{
    struct DEVICE_CONTEXT *pNext;
    WDU_DEVICE_HANDLE hDevice;
    DWORD dwVendorId;
    DWORD dwProductId;
    DWORD dwInterfaceNum;
    DWORD dwAlternateSetting;
} DEVICE_CONTEXT;

//驱动上下文
typedef struct DRIVER_CONTEXT
{
    HANDLE hEvent;
    HANDLE hMutex;
    DWORD dwDeviceCount;
    DEVICE_CONTEXT *deviceContextList;
    DEVICE_CONTEXT *pActiveDev;
    HANDLE hDeviceUnusedEvent;
} DRIVER_CONTEXT;

首先是usb设备连接功能函数

static BOOL DLLCALLCONV DeviceAttach(WDU_DEVICE_HANDLE hDevice, WDU_DEVICE *pDeviceInfo, PVOID pUserData)

枚举设备的接入、拔出以及上电状态表结构体

typedef struct
{
    WDU_ATTACH_CALLBACK  pfDeviceAttach;
    WDU_DETACH_CALLBACK  pfDeviceDetach;
    WDU_POWER_CHANGE_CALLBACK pfPowerChange;
    PVOID pUserData;  /* pointer to pass in each callback */
} WDU_EVENT_TABLE;

 对WDU_ATTACH_CALLBACK 定义在

typedef BOOL (DLLCALLCONV *WDU_ATTACH_CALLBACK)(WDU_DEVICE_HANDLE hDevice, WDU_DEVICE *pDeviceInfo,

                           PVOID pUserData);

定义DLLCALLCONV    #define DLLCALLCONV __stdcall 查询_stdcall作用:Win32的API函数都遵循_stdcall调用约定,是Pascal方式清理C方式压栈,通常用于Win32 Api中,函数采用从右到左的压栈方式,自己在退出时清空堆栈。__stdcall、__cdecl和__fastcall可以直接加在要输出的函数前。

 

根据Windriver的帮助文件获取USB驱动设计信息,在生成的驱动文件框架中如何添加自己的代码:

1. Call WDU_Init()  at the beginning of your program to initialize WinDriver for your USB device and wait for the device-attach callback. The relevant device information will be provided in the attach callback.

2. Once the attach callback is received, you can start using one of the WDU_Transfer()  functions family to send and receive data.

3. To finish, call WDU_Uninit() to un-register from the device.

WDU_Init()  、WDU_Transfer() 、 WDU_Uninit() 为Windriver USB的API函数,在WDU_xxx中定义了USB用户应用和USB设备之间的事件驱动传输。

WDU_ATTACH_CALLBACK()、 WDU_DETACH_CALLBACK()、WDU_POWER_CHANGE_CALLBACK()用于通知用户程序设备相关状态,例如USB设备的插入、拔出操作。

操作流程如下程序所示:

main()
{
    WDU_Init(...);
    ...
    while (...)
    {
        /* wait for new devices */
         ...
       /* issue transfers */
         ...
    }
    ...
    WDU_Uninit();
}
下表对应WDU_XXX文件中的Windrvier USB的API函数表,可以方便后面用户调用API函数功能。

Problem Solution
              High Level API
This function... has been replaced by...
WD_Open()
WD_Version()
WD_UsbScanDevice()
WDU_Init() 
WD_UsbDeviceRegister() WDU_SetInterface() 
WD_UsbGetConfiguration() WDU_GetDeviceInfo() 
WD_UsbDeviceUnregister() WDU_Uninit() 
              Low Level API
This function... has been replaced by...
WD_UsbTransfer()

WDU_Transfer()
WDU_TransferDefaultPipe()  WDU_TransferBulk() 
WDU_TransferIsoch()  WDU_TransferInterrupt() 

USB_TRANSFER_HALT option WDU_HaltTransfer()
WD_UsbResetPipe() WDU_ResetPipe()
WD_UsbResetDevice()
WD_UsbResetDeviceEx()
WDU_ResetDevice()

wdu_lib.h头文件中定义如下函数:

WDU_Init() :监听对应输入协议的设备,并为设备产生回调通知,定义如下

DWORD WDU_Init( WDU_DRIVER_HANDLE *phDriver, // output 协议事件句柄 WDU_MATCH_TABLE *pMatchTables, // input 设备标准协议表 DWORD dwNumMatchTables, // input WDU_EVENT_TABLE *pEventTable, // input const char *sLicense, // input DWORD dwOptions); // input
 
 
----------------------------------------------------------------------------------------------
WDU_Init() 取得设备所有的相关信息(各种描述符等),并调用回调函数typedef BOOL (DLLCALLCONV *WDU_ATTACH_CALLBACK)( WDU_DEVICE_HANDLE hDevice, WDU_DEVICE *pDeviceInfo, PVOID pUserData); 其中通过pDeviceInfo指针可以访问设备的各种信息,原文这样描述的:Pointer to a USB device information structure Valid until the end of the function。
WDU_GetDeviceInfo()实现了同样的功能,可能WDU_Init()调用了此函数或存在实现相同功能的代码。
阅读(4042) | 评论(0) | 转发(0) |
0

上一篇:VS2008技巧收集

下一篇:vi(vim)自动更新Ctags

给主人留下些什么吧!~~