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

📄 usb2com.h

📁 usb to rs232 虚拟RS232串口驱动程序
💻 H
字号:
/*++

Copyright (c) 2005-2006  E0 Technology,Inc.

Module Name:

	usb2com.h

Abstract:

	Virtual Com Port Driver for USB to RS232 Converter of E0 Technology,Inc.

Environment:

	Kernel mode

Notes:

Revision History:

    2006/3/1 : 	Adapted from the BulkUsb DDK sample.
--*/

#ifndef USB2COM_INCD
#define USB2COM_INCD

#include "wdm.h"
#include "stdarg.h"
#include "stdio.h"

#include "usbdi.h"
#include "usbdlib.h"

#include "dbg.h"
#include "buf.h"
#include "usb.h"
#include "serial.h"




// used to track information on pipes in use;
//  currently just to flag if opened or closed
typedef struct USB2COM_PIPEINFO {

    BOOLEAN fPipeOpened;

} USB2COM_PIPEINFO, *PUSB2COM_PIPEINFO;


typedef struct _INT_URBS {
	PIRP	Irp;
	PURB	Urb;
	BOOLEAN	Idle;
	PUSBD_PIPE_INFORMATION   PipeInfo;
	PDEVICE_OBJECT deviceObject;
	UCHAR	TransferBuffer[8];
} INT_URBS, *PINT_URBS;

#define MAX_READ_INT_URBS 1
//
// A structure representing the instance information associated with
// this particular device.
//

typedef struct _DEVICE_EXTENSION {

    // Device object we call when submitting Urbs
    PDEVICE_OBJECT TopOfStackDeviceObject;

	// The bus driver object
    PDEVICE_OBJECT PhysicalDeviceObject;

    DEVICE_POWER_STATE CurrentDevicePowerState;

    // USB configuration handle and ptr for the configuration the
    // device is currently in
    USBD_CONFIGURATION_HANDLE UsbConfigurationHandle;
	PUSB_CONFIGURATION_DESCRIPTOR UsbConfigurationDescriptor;


    // ptr to the USB device descriptor
    // for this device
    PUSB_DEVICE_DESCRIPTOR UsbDeviceDescriptor;

    // we support one interface
    // this is a copy of the info structure
    // returned from select_configuration or
    // select_interface
    PUSBD_INTERFACE_INFORMATION UsbInterface;

	//Bus drivers set the appropriate values in this structure in response
	//to an IRP_MN_QUERY_CAPABILITIES IRP. Function and filter drivers might
	//alter the capabilities set by the bus driver.
    DEVICE_CAPABILITIES DeviceCapabilities;

	// used to save the currently-being-handled system-requested power irp request
    PIRP PowerIrp;

	// count of self-staged irps pending
    ULONG StagedPendingIrpCount;

	// set when PendingIoCount goes to 0; flags device can be removed
    KEVENT RemoveEvent;

	// set when PendingIoCount goes to 1 ( 1st increment was on add device )
	// this indicates no IO requests outstanding, either user, system, or self-staged
    KEVENT NoPendingIoEvent;

	// set to signal driver-generated power request is finished
    KEVENT SelfRequestedPowerIrpEvent;

	// spinlock used to protect inc/dec iocount logic
	KSPIN_LOCK	IoCountSpinLock;

	// incremented when device is added and any IO request is received;
	// decremented when any io request is completed or passed on, and when device is removed
    ULONG PendingIoCount;

	// count of open pipes
	ULONG OpenPipeCount;

    // ptr to array of structs to track pipeinfo;
    //  in this basic sample it's only used to track if open/closed;
    PUSB2COM_PIPEINFO PipeInfo;

    //
    // Records whether we actually created the symbolic link name
    // at driver load time.  If we didn't create it, we won't try
    // to destroy it when we unload.
    //
    BOOLEAN CreatedSymbolicLink;
    //
    // Records whether we actually created an entry in SERIALCOMM
    // at driver load time.  If we didn't create it, we won't try
    // to destroy it when the device is removed.
    //
    BOOLEAN CreatedSerialCommEntry;
    //
    // String where we keep the symbolic link that is returned to us when we
    // register our device under the COMM class with the Plug and Play manager.
    //
    UNICODE_STRING DeviceClassSymbolicName;
    UNICODE_STRING SymbolicLinkName;
    UNICODE_STRING DeviceName;
    //
    // This points to the pure "COMx" name
    //
    UNICODE_STRING DosName;
    ULONG SkipNaming;
    PDEVICE_OBJECT DeviceObject;
    	ULONG BaudRate;
	SERIAL_LINE_CONTROL SerialLineControl;
	SERIAL_CHARS SpecialChars;
	SERIAL_HANDFLOW HandFlow;
	SERIAL_TIMEOUTS SerialTimeOuts;
	ULONG	WaitMask;
	ULONG BreakInterruptErrorCount;
	SERIALPERF_STATS SerialPerfStats;
	CIRCULAR_BUFFER InputBuffer;
	KSPIN_LOCK InputBufferLock;
	CIRCULAR_BUFFER OutputBuffer;
	KSPIN_LOCK OutputBufferLock;
	LIST_ENTRY ReadQueue;
	PIRP CurrentReadIrp;
	KSPIN_LOCK ReadQueueSpinLock;
	LIST_ENTRY WriteQueue;
	KSPIN_LOCK WriteQueueSpinLock;
	ULONG	TotalCharsQueued;
	PIRP CurrentWriteIrp;
	UCHAR *CurrentWriteIrpDataPtr;
	ULONG CurrentWriteIrpDataLen;
	ULONG CurrentWriteIrpRequestLen;
	PIRP CurrentWaitIrp;
	ULONG HistoryMask;
	LIST_ENTRY PurgeQueue;
	PIRP CurrentPurgeIrp;
	KSPIN_LOCK PurgeQueueSpinLock;
	INT_URBS ReadIntUrbs[MAX_READ_INT_URBS];
	INT_URBS WriteIntUrb;
	//flag set when processing IRP_MN_REMOVE_DEVICE
    BOOLEAN DeviceRemoved;

 	// flag set when driver has answered success to IRP_MN_QUERY_REMOVE_DEVICE
    BOOLEAN RemoveDeviceRequested;

	// flag set when driver has answered success to IRP_MN_QUERY_STOP_DEVICE
    BOOLEAN StopDeviceRequested;

	// flag set when device has been successfully started
	BOOLEAN DeviceStarted;

    // flag set when IRP_MN_WAIT_WAKE is received and we're in a power state
    // where we can signal a wait
    BOOLEAN EnabledForWakeup;

	// used to flag that we're currently handling a self-generated power request
    BOOLEAN SelfPowerIrp;

	// default power state to power down to on self-suspend 
	ULONG PowerDownLevel; 

} DEVICE_EXTENSION, *PDEVICE_EXTENSION;


// function prototypes

NTSTATUS
USB2COM_ProcessPnPIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP           Irp
    );

NTSTATUS
USB2COM_ProcessSysControlIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP           Irp
    );

VOID
USB2COM_Unload(
    IN PDRIVER_OBJECT DriverObject
    );

NTSTATUS
USB2COM_StartDevice(
    IN  PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_StopDevice(
    IN  PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_RemoveDevice(
    IN  PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_CallUSBD(
    IN PDEVICE_OBJECT DeviceObject,
    IN PURB Urb
    );
    
NTSTATUS
UsbDoControlTransfer(
	IN PDEVICE_OBJECT DeviceObject,
	IN USHORT  Function,
	IN ULONG TransferFlags,
	IN UCHAR  bRequest,
  	IN USHORT  wValue,
   	IN USHORT  wIndex,
   	IN USHORT wLength,
	IN PVOID UserBuffer,
	ULONG *UserBufferSize);
	
NTSTATUS
USB2COM_PnPAddDevice(
    IN PDRIVER_OBJECT DriverObject,
    IN PDEVICE_OBJECT PhysicalDeviceObject
    );

NTSTATUS
USB2COM_CreateDeviceObject(
    IN PDRIVER_OBJECT DriverObject,
    IN PDEVICE_OBJECT PhysicalDeviceObject,
    IN PDEVICE_OBJECT *DeviceObject
    );

NTSTATUS
USB2COM_ConfigureDevice(
    IN  PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_IrpCompletionRoutine(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PVOID Context
    );

NTSTATUS
USB2COM_PoRequestCompletion(
    IN PDEVICE_OBJECT       DeviceObject,
    IN UCHAR                MinorFunction,
    IN POWER_STATE          PowerState,
    IN PVOID                Context,
    IN PIO_STATUS_BLOCK     IoStatus
    );

NTSTATUS
USB2COM_PoSelfRequestCompletion(
    IN PDEVICE_OBJECT       DeviceObject,
    IN UCHAR                MinorFunction,
    IN POWER_STATE          PowerState,
    IN PVOID                Context,
    IN PIO_STATUS_BLOCK     IoStatus
    );

PURB
USB2COM_BuildAsyncRequest(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PUSBD_PIPE_INFORMATION PipeHandle,
    IN BOOLEAN Read
    );

NTSTATUS
USB2COM_GetPortStatus(
    IN PDEVICE_OBJECT DeviceObject,
    IN PULONG PortStatus
    );

NTSTATUS
USB2COM_ResetParentPort(
    IN IN PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_SelfRequestPowerIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN POWER_STATE PowerState
    );

BOOLEAN
USB2COM_SetDevicePowerState(
    IN PDEVICE_OBJECT DeviceObject,
    IN DEVICE_POWER_STATE DeviceState
    );

NTSTATUS
USB2COM_AsyncReadWrite_Complete(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PVOID Context
    );

NTSTATUS
USB2COM_SimpleReadWrite_Complete(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PVOID Context
    );


NTSTATUS
USB2COM_PowerIrp_Complete(
    IN PDEVICE_OBJECT NullDeviceObject,
    IN PIRP Irp,
    IN PVOID Context
    );

NTSTATUS
USB2COM_QueryCapabilities(
    IN PDEVICE_OBJECT PdoDeviceObject,
    IN PDEVICE_CAPABILITIES DeviceCapabilities
    );


NTSTATUS
USB2COM_Write(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );

NTSTATUS
USB2COM_Create(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );


NTSTATUS
USB2COM_Read(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );

NTSTATUS
PrepareWriteIntUrb(
	IN PDEVICE_OBJECT DeviceObject,
	IN PUSBD_PIPE_INFORMATION PipeInfo
	);

void
FreeWriteIntUrb(
	IN PDEVICE_EXTENSION deviceExtension
	);

NTSTATUS
USB2COM_AbortPipes(
    IN PDEVICE_OBJECT DeviceObject
    );


NTSTATUS
USB2COM_ProcessIOCTL(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );

NTSTATUS
USB2COM_SelectInterface(
    IN PDEVICE_OBJECT DeviceObject,
    IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor
    );



NTSTATUS
USB2COM_ResetDevice(
    IN PDEVICE_OBJECT DeviceObject
    );

NTSTATUS
USB2COM_Close(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    );

NTSTATUS
USB2COM_ResetPipe(
    IN PDEVICE_OBJECT DeviceObject,
    IN PUSBD_PIPE_INFORMATION PipeInfo
    );


VOID
USB2COM_IncrementIoCount(
    IN PDEVICE_OBJECT DeviceObject
    );

LONG
USB2COM_DecrementIoCount(
    IN PDEVICE_OBJECT DeviceObject
    );   


NTSTATUS
USB2COM_ProcessPowerIrp(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP           Irp
    );    


NTSTATUS
USB2COM_StagedReadWrite(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN BOOLEAN Read
    );

NTSTATUS
USB2COM_SelfSuspendOrActivate(
    IN PDEVICE_OBJECT DeviceObject,
	IN BOOLEAN fSuspend
    );


BOOLEAN
USB2COM_CancelPendingIo(
    IN PDEVICE_OBJECT DeviceObject
    );

BOOLEAN
USB2COM_CanAcceptIoRequests(
    IN PDEVICE_OBJECT DeviceObject
    );

PUSB2COM_PIPEINFO USB2COM_PipeWithName( 
    IN PDEVICE_OBJECT DeviceObject,
    IN PUNICODE_STRING FileName
);

#endif // already included







⌨️ 快捷键说明

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