现在的位置: 首页 > 综合 > 正文

windows wdf 驱动开发总结(2)–usb驱动

2013年09月20日 ⁄ 综合 ⁄ 共 10234字 ⁄ 字号 评论关闭

武安河:Usbsample

(14) WDF_REQUEST_SEND_OPTIONS_INIT

函数功能:initializes a driver's
WDF_REQUEST_SEND_OPTIONS structure

VOID
  WDF_REQUEST_SEND_OPTIONS_INIT(
    OUT PWDF_REQUEST_SEND_OPTIONS  Options,
    IN ULONG  
Flags
    );

Options

A pointer to a caller-supplied
WDF_REQUEST_SEND_OPTIONS structure.

Flags

A bitwise OR of
WDF_REQUEST_SEND_OPTIONS_FLAGS-typed flags.

 

15WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT

函数功能:sets a time-out value in a driver's
WDF_REQUEST_SEND_OPTIONS structure.

VOID WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(

  __out 
PWDF_REQUEST_SEND_OPTIONS Options,

  __in  
LONGLONG Timeout

);

Timeout
如果为负数,那么是相对于系统时间而言的,如果timeout为正数,那么是一个绝对的时间,为0就表示没有超时。

Options [out]

A pointer to the driver's
WDF_REQUEST_SEND_OPTIONS structure.

Timeout [in]

An absolute or relative time-out value. For more information, see the
Timeout member of the
WDF_REQUEST_SEND_OPTIONS structure.

 

typedef struct _WDF_REQUEST_SEND_OPTIONS {

  ULONG   
Size;

  ULONG   
Flags;

  LONGLONG Timeout;

} WDF_REQUEST_SEND_OPTIONS, *PWDF_REQUEST_SEND_OPTIONS;

 

Members

Size

The size, in bytes, of this structure.

Flags

A bitwise OR of
WDF_REQUEST_SEND_OPTIONS_FLAGS-typed flags.

Timeout

A time-out value, in system time units (100-nanosecond intervals). If the driver has set the WDF_REQUEST_SEND_OPTION_TIMEOUT flag, the framework cancels the associated I/O request if
it is not completed within the specified time-out period. The time-out value can be negative, positive, or zero, as follows:

· 
If the value is negative, the expiration time is relative to the current system time.

· 
If the value is positive, the expiration time is specified as an absolute time (which is actually relative to January 1, 1601).

· 
If the value is zero, the framework does not time out the request.

Relative expiration times are not affected by any changes to the system time that might occur within the specified time-out period. Absolute expiration times do reflect system time changes.

The framework provides
time conversion functions that convert time values into system time units.

If the framework cancels an I/O request because the specified time-out period has elapsed, the framework provides a completion status of STATUS_IO_TIMEOUT for the I/O request. However,
after the time-out period elapses, the I/O target might complete the I/O request before the framework is able to cancel it. In that case, the I/O request's completion status will not be STATUS_IO_TIMEOUT.

 

评论:

The
WDF_REQUEST_SEND_OPTIONS structure is passed to object methods that send an I/O request to an I/O target, such as the
WdfRequestSend method. The structure must be initialized by calling the
WDF_REQUEST_SEND_OPTIONS_INIT and
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT functions.

 

(16) WDF_USB_CONTROL_SETUP_PACKET_INIT_VENDOR      
//vendor,class

函数功能:初始化控制传输包,设置要传递的批量传输字节数

    
WDF_USB_CONTROL_SETUP_PACKET_INIT_VENDOR(

                                              
&controlSetupPacket,

                      
                       BmRequestHostToDevice,

                           
                   BmRequestToDevice,

    
                                         
0,

                                    
          dwBytes,

                                         
      0

                                              
 );

VOID WDF_USB_CONTROL_SETUP_PACKET_INIT_VENDOR(

  __out 
PWDF_USB_CONTROL_SETUP_PACKET Packet,    //setup_packet

  __in  
WDF_USB_BMREQUEST_DIRECTION Direction, //
主机to设备或者设备to主机

  __in  
WDF_USB_BMREQUEST_RECIPIENT Recipient,//
请求to设备,请求to接口,请求to端点

  __in  
BYTE Request,              //
请求类型F1

  __in  
USHORT Value,             

  __in  
USHORT Index

);

 

 

(17)WdfUsbTargetDeviceSendControlTransferSynchronously

函数功能:builds a USB control transfer request and sends it synchronously
to an I/O target.

NTSTATUS WdfUsbTargetDeviceSendControlTransferSynchronously(

  [in]            
WDFUSBDEVICE UsbDevice,

  [in, optional]  
WDFREQUEST Request,

  [in, optional]  
PWDF_REQUEST_SEND_OPTIONS RequestOptions,

  [in]            
PWDF_USB_CONTROL_SETUP_PACKET SetupPacket,

  [in, optional]  
PWDF_MEMORY_DESCRIPTOR MemoryDescriptor,

  [out, optional] 
PULONG BytesTransferred

);

UsbDevice [in]

A handle to a USB device object that was obtained from a previous call to
WdfUsbTargetDeviceCreate.

Request [in,
optional]

A handle to a framework request object. This parameter is optional and can be NULL. For more information, see the following Remarks section.

RequestOptions
[in, optional]

A pointer to a caller-allocated
WDF_REQUEST_SEND_OPTIONS structure that specifies options for the request. This pointer is optional and can be NULL. For more information, see the following Remarks
section.

SetupPacket [in]

A pointer to a caller-allocated
WDF_USB_CONTROL_SETUP_PACKET structure that describes the control transfer.

MemoryDescriptor
[in, optional]

A pointer to a caller-allocated
WDF_MEMORY_DESCRIPTOR structure that describes either an input or an output buffer, depending on the device-specific command. This pointer is optional and can be NULL.
For more information, see the following Remarks section.

BytesTransferred
[out, optional]

A pointer to a location that receives the number of bytes that are transferred. This parameter is optional and can be NULL.

Return Value
STATUS_IO_TIMEOUT STATUS_INVALID_PARAMETER

STATUS_INSUFFICIENT_RESOURCES,
STATUS_INVALID_DEVICE_REQUEST

    
成功返回

//控制传输VENDOR命令

    
status = WdfUsbTargetDeviceSendControlTransferSynchronously(

                        
pDeviceContext->UsbDevice,

                   
     WDF_NO_HANDLE,

                      
 &syncReqOptions,

                        
&controlSetupPacket,

                        
NULL,

                        
NULL

                        
);

 

Msdn:例子

WDF_USB_CONTROL_SETUP_PACKET  controlSetupPacket;

WDF_USB_CONTROL_SETUP_PACKET_INIT_VENDOR(

                                        
&controlSetupPacket,

                                        
BmRequestHostToDevice,

                                        
BmRequestToDevice,

                                        
USBFX2LK_REENUMERATE,

                                        
0,

                                        
0

                                        
);

 

status = WdfUsbTargetDeviceSendControlTransferSynchronously(

                                        
UsbDevice,

                                        
WDF_NO_HANDLE,

                                        
NULL,

                                        
&controlSetupPacket,

                                        
NULL,

                                      
  NULL

                                        
);

return status;

 

 

(18) WdfUsbTargetDeviceResetPortSynchronously

函数功能:resets the USB port that is associated with the specified USB
device.

NTSTATUS WdfUsbTargetDeviceResetPortSynchronously(

  [in]  WDFUSBDEVICE UsbDevice

);

参数:UsbDevice:usb设备对象句柄

在设置I/O target’s usb port以前,取消保持在I/O
target’s queue
里面的所有I/O请求,驱动程序不能发送其他的I/O请求,直到WdfUsbTargetDeviceResetPortSynchronously返回。

在发送WdfUsbTargetDeviceResetPortSynchronously函数之前,必须调用WdfIoTargetStop在调用该函数后,驱动can
call WdfIoTargetStart
函数。

 

Msdn
例子:

NTSTATUS status;

status = WdfUsbTargetDeviceResetPortSynchronously(UsbDevice);

 

 

(19) WDF_MEMORY_DESCRIPTOR_INIT_BUFFER

函数功能:initializes a
WDF_MEMORY_DESCRIPTOR structure so that it describes a specified buffer.

VOID WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(

  __out 
PWDF_MEMORY_DESCRIPTOR Descriptor,

  __in  
PVOID Buffer,

  __in  
ULONG BufferLength

);

 

Descriptor [out]

A pointer to a
WDF_MEMORY_DESCRIPTOR structure.

Buffer [in]

A pointer to a memory buffer.

BufferLength
[in]

The size, in bytes, of the memory buffer that
Buffer points to.

 

typedef struct _WDF_MEMORY_DESCRIPTOR {

  WDF_MEMORY_DESCRIPTOR_TYPE Type;

  union {

    struct {

      PVOID Buffer;

      ULONG Length;

    } BufferType;

    struct {

      PMDL 
Mdl;

      ULONG BufferLength;

    } MdlType;

    struct {

      WDFMEMORY        
Memory;

      PWDFMEMORY_OFFSET Offsets;

    } HandleType;

  } u;

} WDF_MEMORY_DESCRIPTOR, *PWDF_MEMORY_DESCRIPTOR;

 

    
//
批量传输数据块初始化

    
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&bufDesc,buffer,bufSize);

    
WDF_REQUEST_SEND_OPTIONS_INIT(&syncReqOptions, 0);

    
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&syncReqOptions, -1000000);

 

(20) WdfUsbTargetPipeWriteSynchronously

函数功能:builds a write request and sends it synchronously to a specified
USB output pipe.

NTSTATUS WdfUsbTargetPipeWriteSynchronously(

  [in]            
WDFUSBPIPE Pipe,

  [in, optional]  
WDFREQUEST Request,

  [in, optional]  
PWDF_REQUEST_SEND_OPTIONS RequestOptions,

  [in, optional]  
PWDF_MEMORY_DESCRIPTOR MemoryDescriptor,

  [out, optional] 
PULONG BytesWritten

);

Pipe [in]

A handle to a framework pipe object that was obtained by calling
WdfUsbInterfaceGetConfiguredPipe.

Request [in,
optional]

A handle to a framework request object. This parameter is optional and can be NULL. For more information, see the following Remarks section.

RequestOptions
[in, optional]

A pointer to a caller-allocated
WDF_REQUEST_SEND_OPTIONS structure that specifies options for the request. This pointer is optional and can be NULL. For more information, see the following Remarks
section.

MemoryDescriptor
[in, optional]

A pointer to a caller-allocated
WDF_MEMORY_DESCRIPTOR structure that describes the buffer that contains data that will be written to the device. For more information about this buffer, see the following
Remarks section.

BytesWritten
[out, optional] :

A pointer to a location that receives the number of bytes written, if the operation succeeds. This parameter is optional and can be NULL.

 

Supply a local buffer:

Because handles I/O requests synchronously, the driver can create request buffers that are local to the calling routine, as the following code example shows.

Copy Code

WDF_MEMORY_DESCRIPTOR  memoryDescriptor;

MY_BUFFER_TYPE  myBuffer;

WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&memoryDescriptor,

                                 
(PVOID) &myBuffer,

                                 
sizeof(myBuffer));

 

·                
WDF_MEMORY_DESCRIPTOR 
memoryDescriptor;

·                
WDFMEMORY 
memoryHandle = NULL;

·                
status = WdfMemoryCreate(NULL,

·                
                         NonPagedPool,

·                
                         POOL_TAG,

·                
                         MY_BUFFER_SIZE,

·                
                         &memoryHandle,

·                
                         NULL);

·                
WDF_MEMORY_DESCRIPTOR_INIT_HANDLE(&memoryDescriptor,

·                
                                  memoryHandle,

·                
                                  NULL);

WDF_MEMORY_DESCRIPTOR  writeBufDesc;

WDFMEMORY  wdfMemory;

ULONG  writeSize, bytesWritten;

size_t  bufferSize;

NTSTATUS status;

 

writeSize = SMALL_BUF_LEN;

status = WdfMemoryCreate(

                         WDF_NO_OBJECT_ATTRIBUTES,

                         NonPagedPool,

                         0,

                         writeSize,

                         &wdfMemory,

                         NULL

                         );

if (!NT_SUCCESS(status)){

    return status;

}

 

writeBuffer = WdfMemoryGetBuffer(

                                
wdfMemory,

                                
&bufferSize

                                
);

FillMyBuffer(

             writeBuffer,

             writeSize

             );

 

WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(

抱歉!评论已关闭.