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

📄 usbdriver.h

📁 USB2.0编程的代码
💻 H
字号:
//#ifdef DRIVER和ENDIF的部分是开发设备驱动程序所专用的,他们不能在Win32应用程序中使用,所以加上了该屏蔽。 
//也就是说,该头文件也可在Win32应用程序中直接使用,而且也的确使用了。当然,也可以在Win32应用程序中把不
//需要的部分直接去除。

#ifndef USBDRIVER_H
#define USBDRIVER_H
#endif

#ifdef DRIVER
#include "wdm.h"
#include "usbdi.h"
#include "usbdlib.h"
#include <resource.h>
#endif

#ifndef _BYTE_DEFINED
#define _BYTE_DEFINED
typedef unsigned char BYTE;
#endif // !_BYTE_DEFINED

#ifndef _WORD_DEFINED
#define _WORD_DEFINED
typedef unsigned short WORD;
#endif // !_WORD_DEFINED

typedef struct _GET_STRING_DESCRIPTOR_IN
{
   UCHAR    Index;
   USHORT   LanguageId;
} GET_STRING_DESCRIPTOR_IN, *PGET_STRING_DESCRIPTOR_IN;

typedef struct _ISO_TRANSFER_CONTROL
{
   // pipe number to perform the ISO transfer to/from.  Direction is
   // implied by the pipe number.
   ULONG PipeNum;

   // ISO packet size.  Determines how much data is transferred each
   // frame.  Should be less than or equal to the maxpacketsize for
   // the endpoint.
   ULONG PacketSize;

   // Total number of ISO packets to transfer.
   ULONG PacketCount;

   // The following two parameters detmine how buffers are managed for
   // an ISO transfer.  In order to maintain an ISO stream, the driver
   // must create at least 2 transfer buffers and ping pong between them.
   // BufferCount determines how many buffers the driver creates to ping
   // pong between.  FramesPerBuffer specifies how many USB frames of data
   // are transferred by each buffer.
   ULONG FramesPerBuffer;     // 10 is a good value
   ULONG BufferCount;         // 2 is a good value
} ISO_TRANSFER_CONTROL, *PISO_TRANSFER_CONTROL;


#ifdef DRIVER

typedef struct _RING_BUFFER
{
   PUCHAR      inPtr;
   PUCHAR      outPtr;
   ULONG       totalSize;
   ULONG       currentSize;
   KSPIN_LOCK	spinLock;
   PUCHAR      buffer;
} RING_BUFFER, *PRING_BUFFER;

typedef struct _ISO_STREAM_OBJECT ISO_STREAM_OBJECT, *PISO_STREAM_OBJECT;

typedef struct _ISO_TRANSFER_OBJECT
{
   ULONG Frame;
   PISO_STREAM_OBJECT StreamObject;
   PURB Urb;
   PIRP Irp;
   KEVENT Done;
} ISO_TRANSFER_OBJECT, *PISO_TRANSFER_OBJECT;

typedef struct _ISO_STREAM_OBJECT
{
   PDEVICE_OBJECT DeviceObject;
   ULONG PacketSize;
   ULONG NumPackets;
   PUSBD_PIPE_INFORMATION PipeInfo;
   PVOID TransferBuffer;
   ULONG TransferBufferLength;
   PVOID IsoDescriptorBuffer;
   ULONG FramesPerBuffer;
   ULONG BufferCount;
   ULONG PendingTransfers;
   PRING_BUFFER DataRingBuffer;
   PRING_BUFFER DescriptorRingBuffer;
   PISO_TRANSFER_OBJECT TransferObject;
} ISO_STREAM_OBJECT, *PISO_STREAM_OBJECT;

// Device extension structure
typedef struct tagDEVICE_EXTENSION
{
   PDEVICE_OBJECT PhysicalDeviceObject;   // physical device object   
   PDEVICE_OBJECT LowerDeviceObject;		// next lower driver in same stack
   BOOLEAN Stop;     // Indicates that we have recieved a STOP message

   // configuration handle for the configuration the
   // device is currently in
   USBD_CONFIGURATION_HANDLE ConfigurationHandle;

   // pointer to the USB device descriptor for this device
   PUSB_DEVICE_DESCRIPTOR DeviceDescriptor;

   // we support up to one interface
   PUSBD_INTERFACE_INFORMATION Interface;

   // the number of device handles currently open to the device object.
   // Gets incremented by Create and decremented by Close
   //打开的设备句柄。与用户应用程序相对应的。即createfile和closehandle。
   ULONG OpenHandles;

   // Name buffer for our named Functional device object link
   //设备连接名,注意它的长度为64。不要太小了,如32,不好。
   WCHAR DeviceLinkName[64];

   // use counter for the device.  Gets incremented when the driver receives
   // a request and gets decremented when a request s completed.
   //记录有多少个I/O请求正在处理中。
   LONG Usages;

   // this event gets set when it is ok to remove the device
   KEVENT evRemove;

   // TRUE if we're trying to remove this device
   BOOLEAN Removing;
   
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

NTSTATUS TestAddDevice(IN PDRIVER_OBJECT DriverObject,
                       IN PDEVICE_OBJECT PhysicalDeviceObject);
VOID TestDrvUnload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS TestPnpIrp(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS TestPowerIrp(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS TestCreate(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS TestClose(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS TestIOCTL(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
BOOLEAN LockDevice(IN PDEVICE_OBJECT fdo);
void UnlockDevice(PDEVICE_OBJECT fdo);
NTSTATUS CompleteRequest(IN PIRP Irp,IN NTSTATUS status,IN ULONG info);
NTSTATUS UsbCallUSBDI(IN PDEVICE_OBJECT fdo,IN PURB Urb);
NTSTATUS UsbResetPipe(IN PDEVICE_OBJECT DeviceObject,IN ULONG PipeNum);

#endif      //DRIVER section


#define Ezusb_IOCTL_INDEX  0x0800

//FILE_DEVICE_UNKNOWN在adddevice时标志的。

#define IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+0,\
                                                   METHOD_BUFFERED,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+1,\
                                                   METHOD_BUFFERED,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_Ezusb_GET_STRING_DESCRIPTOR CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+2,\
                                                   METHOD_BUFFERED,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_EZUSB_ISO_WRITE             CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+3,\
                                                   METHOD_IN_DIRECT,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_EZUSB_ISO_READ              CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+4,\
                                                   METHOD_OUT_DIRECT,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_EZUSB_GET_CURRENT_FRAME_NUMBER  CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+5,\
                                                   METHOD_BUFFERED,  \
                                                   FILE_ANY_ACCESS)

#define IOCTL_Ezusb_RESETPIPE             CTL_CODE(FILE_DEVICE_UNKNOWN,  \
                                                   Ezusb_IOCTL_INDEX+6,\
                                                   METHOD_IN_DIRECT,  \
                                                   FILE_ANY_ACCESS)

⌨️ 快捷键说明

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