📄 ezusbsys.h
字号:
#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 _FOURBYTE_CTRL_TRANSFER_
{
BYTE byte1;
BYTE byte2;
BYTE byte3;
BYTE byte4;
USHORT bTimeOut;
} FOURBYTE_CTRL_TRANSFER, *PFOURBYTE_CTRL_TRANSFER;
typedef struct _VENDOR_REQUEST_IN
{
BYTE bRequest;
WORD wValue;
WORD wIndex;
WORD wLength;
BYTE direction;
BYTE bData;
} VENDOR_REQUEST_IN, *PVENDOR_REQUEST_IN;
///////////////////////////////////////////////////////////
//
// control structure for bulk and interrupt data transfers
//
///////////////////////////////////////////////////////////
typedef struct _BULK_TRANSFER_CONTROL
{
ULONG pipeNum;
USHORT bTimeOut;
} BULK_TRANSFER_CONTROL, *PBULK_TRANSFER_CONTROL;
typedef struct _BULK_LATENCY_CONTROL
{
ULONG bulkPipeNum;
ULONG intPipeNum;
ULONG loops;
} BULK_LATENCY_CONTROL, *PBULK_LATENCY_CONTROL;
///////////////////////////////////////////////////////////
//
// control structure isochronous loopback test
//
///////////////////////////////////////////////////////////
typedef struct _ISO_LOOPBACK_CONTROL
{
// iso pipe to write to
ULONG outPipeNum;
// iso pipe to read from
ULONG inPipeNum;
// amount of data to read/write from/to the pipe each frame. If not
// specified, the MaxPacketSize of the out pipe is used.
ULONG packetSize;
} ISO_LOOPBACK_CONTROL, *PISO_LOOPBACK_CONTROL;
///////////////////////////////////////////////////////////
//
// control structure for sending vendor or class specific requests
// to the control endpoint.
//
///////////////////////////////////////////////////////////
typedef struct _VENDOR_OR_CLASS_REQUEST_CONTROL
{
// transfer direction (0=host to device, 1=device to host)
UCHAR direction;
// request type (1=class, 2=vendor)
UCHAR requestType;
// recipient (0=device,1=interface,2=endpoint,3=other)
UCHAR recepient;
//
// see the USB Specification for an explanation of the
// following paramaters.
//
UCHAR requestTypeReservedBits;
UCHAR request;
USHORT value;
USHORT index;
} VENDOR_OR_CLASS_REQUEST_CONTROL, *PVENDOR_OR_CLASS_REQUEST_CONTROL;
///////////////////////////////////////////////////////////
//
// control structure for isochronous data transfers
//
///////////////////////////////////////////////////////////
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;
///////////////////////////////////////////////////////////
//
// control structure for Anchor Downloads
//
///////////////////////////////////////////////////////////
typedef struct _ANCHOR_DOWNLOAD_CONTROL
{
WORD Offset;
} ANCHOR_DOWNLOAD_CONTROL, *PANCHOR_DOWNLOAD_CONTROL;
#define MAX_INTEL_HEX_RECORD_LENGTH 16
typedef struct _INTEL_HEX_RECORD
{
BYTE Length;
WORD Address;
BYTE Type;
BYTE Data[MAX_INTEL_HEX_RECORD_LENGTH];
} INTEL_HEX_RECORD, *PINTEL_HEX_RECORD;
typedef struct _SET_INTERFACE_IN
{
UCHAR interfaceNum;
UCHAR alternateSetting;
} SET_INTERFACE_IN, *PSET_INTERFACE_IN;
typedef struct _GET_STRING_DESCRIPTOR_IN
{
UCHAR Index;
USHORT LanguageId;
} GET_STRING_DESCRIPTOR_IN, *PGET_STRING_DESCRIPTOR_IN;
typedef struct _Myusb_DRIVER_VERSION
{
WORD MajorVersion;
WORD MinorVersion;
WORD BuildVersion;
} Myusb_DRIVER_VERSION, *PMyusb_DRIVER_VERSION;
#ifdef DRIVER
typedef struct _RING_BUFFER
{
PUCHAR inPtr;
PUCHAR outPtr;
ULONG totalSize;
ULONG currentSize;
KSPIN_LOCK spinLock;
PUCHAR buffer;
} RING_BUFFER, *PRING_BUFFER;
PRING_BUFFER
AllocRingBuffer(
ULONG Size
);
VOID
FreeRingBuffer(
PRING_BUFFER ringBuffer
);
ULONG
ReadRingBuffer(
PRING_BUFFER ringBuffer,
PUCHAR readBuffer,
ULONG numberOfBytesToRead
);
ULONG
WriteRingBuffer(
PRING_BUFFER ringBuffer,
PUCHAR writeBuffer,
ULONG numberOfBytesToWrite
);
typedef struct _Myusb_FIRMWARE
{
// tag contains a string to identify the start of the firmware
// image in the driver binary. Another utilty can then be used
// to replace the firmware image inthe driver without requiring
// a recompile
UCHAR tag[10];
ULONG size;
UCHAR firmware[];
} Myusb_FIRMWARE, *PMyusb_FIRMWARE;
//
// this is the default number of IRP's to queue for streaming ISO
// data.
//
#define DEFAULT_ISO_BUFFER_COUNT 2
//
// Default number of frames of ISO data transferred by a single ISO
// URB/IRP
//
#define DEFAULT_ISO_FRAMES_PER_BUFFER 10
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;
#define Myusb_NAME_MAX 64
//
// This is an unused structure in this driver, but is provided here
// so when you extend the driver to deal with USB pipes, you may wish
// to use this structure as an example or model.
//
typedef struct _Myusb_PIPE {
ULONG Mode;
ULONG Option;
ULONG Param1;
ULONG Param2;
WCHAR Name[Myusb_NAME_MAX];
PUSBD_PIPE_INFORMATION PipeInfo;
} Myusb_PIPE, *PMyusb_PIPE;
/*
// The interface number on this device that this driver expects to use
// This would be in the bInterfaceNumber field of the Interface Descriptor, hence
// this device driver would need to know this value.
*/
#define SAMPLE_INTERFACE_NBR 0x00
//
// A structure representing the instance information associated with
// this particular device.
//
typedef struct _DEVICE_EXTENSION
{
// physical device object
PDEVICE_OBJECT PhysicalDeviceObject;
// Device object we call when submitting Urbs/Irps to the USB stack
PDEVICE_OBJECT StackDeviceObject;
// Indicates that we have recieved a STOP message
BOOLEAN Stopped;
// Indicates that we are enumerated and configured. Used to hold
// of requests until we are ready for them
BOOLEAN Started;
// Indicates the device needs to be cleaned up (ie., some configuration
// has occurred and needs to be torn down).
BOOLEAN NeedCleanup;
// configuration handle for the configuration the
// device is currently in
USBD_CONFIGURATION_HANDLE ConfigurationHandle;
// ptr 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
ULONG OpenHandles;
// Name buffer for our named Functional device object link
WCHAR DeviceLinkNameBuffer[Myusb_NAME_MAX];
// This member is used to store the URB status of the
// most recently failed URB. If a USB transfer fails, a caller
// can use IOCTL_Myusb_GET_LAST_ERROR to retrieve this value.
// There's only room for one, so you better get it quick (or at
// least before the next URB failure occurs).
USBD_STATUS LastFailedUrbStatus;
// use counter for the device. Gets incremented when the driver receives
// a request and gets decremented when a request s completed.
LONG usage;
// this ev gets set when it is ok to remove the device
KEVENT evRemove;
// TRUE if we're trying to remove this device
BOOLEAN removing;
BOOLEAN StopIsoStream;
PRING_BUFFER DataRingBuffer;
PRING_BUFFER DescriptorRingBuffer;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
#if DBG
#define Myusb_KdPrint(_x_) DbgPrint("Ezusb.SYS: "); \
DbgPrint _x_ ;
#define TRAP() DbgBreakPoint()
#else
#define Myusb_KdPrint(_x_)
#define TRAP()
#endif
NTSTATUS
Myusb_Dispatch(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
VOID
Myusb_Unload(
IN PDRIVER_OBJECT DriverObject
);
NTSTATUS
Myusb_StartDevice(
IN PDEVICE_OBJECT DeviceObject
);
NTSTATUS
Myusb_StopDevice(
IN PDEVICE_OBJECT DeviceObject
);
NTSTATUS
Myusb_RemoveDevice(
IN PDEVICE_OBJECT DeviceObject
);
NTSTATUS
Myusb_CallUSBD(
IN PDEVICE_OBJECT DeviceObject,
IN PURB Urb
);
NTSTATUS
Myusb_PnPAddDevice(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject
);
NTSTATUS
Myusb_CreateDeviceObject(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT *DeviceObject,
LONG Instance
);
NTSTATUS
Myusb_ConfigureDevice(
IN PDEVICE_OBJECT DeviceObject
);
NTSTATUS
Myusb_Create(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Myusb_Close(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Myusb_Read_Write(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Myusb_ProcessIOCTL(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Myusb_SelectInterfaces(
IN PDEVICE_OBJECT DeviceObject,
IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
IN PUSBD_INTERFACE_INFORMATION Interface
);
NTSTATUS
Myusb_ResetPipe(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -