/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * WinIoEx - Driver.h
 *
 * WinIoEx Copyright (C) 2009-2010 Doug Chen.
 * 
 *
 * You can use the binary files or source code, I DO NOT restrict you anything on anywhere even 
 * commercial purpose, but please send a mail or leave a message on MSN to me before releasing your 
 * project for encourage me keep going on the project, thank you.
 *
 *
 *                                                           Doug Chen, 2009/11/10 20:23 Taiwan Taipei
 *	
 *	Name		: Doug Chen  (³―Αδ·½)
 * 	Email		: dougchen850208@gmail.com
 * 	MSN			: dougchen850208@gmail.com
 *  Location	: Taiwan Taipei
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */







#ifndef __WINIOEX_DRIVER_H__
#define __WINIOEX_DRIVER_H__

#pragma warning(disable:4731)
#pragma warning(disable:4102)

#ifdef __cplusplus
extern "C"
{
#endif

#include <ntddk.h>
#include <windef.h>
#include "ioctl.h"

#ifdef __cplusplus
}
#endif


#define	DRVVERSION			"0.1"
#define DRVNAME             "WINIOEX: "
#define NT_DEVICE_NAME      L"\\Device\\WINIOEX"
#define DOS_DEVICE_NAME     L"\\DosDevices\\WINIOEX"


#pragma pack(1)

typedef struct _FIXEDBUFF_1IN1OUT
{	
	unsigned long InOutBuff;
	unsigned long InBuffSize;
	unsigned long OutBuffSize;
}FIXEDBUFF_1IN1OUT, *PFIXEDBUFF_1IN1OUT;


typedef struct _FIXEDBUFF_4IN4OUT
{	
	unsigned long InOutBuff[4];
	unsigned long InBuffSize;
	unsigned long OutBuffSize;
}FIXEDBUFF_4IN4OUT, *PFIXEDBUFF_4IN4OUT;



/*
#pragma pack(4)
typedef struct _CMD_ELEMENT
{	
	unsigned long Index;		// 4 Bytes
	unsigned char Data;			// 1 Byte
}CMD_ELEMENT, *PCMD_ELEMENT;

#pragma pack(4)
typedef struct _CMD_PACKAGE16
{	
	CMD_ELEMENT		CmdElement[15];	// 8 Bytes * 15
	unsigned char	CmdCount;		// 1 Byte for aglie meory.
}CMD_PACKAGE16, *PCMD_PACKAGE16;
*/




typedef struct _CMD_ELEMENT
{	
	unsigned long Index;			// 4 Bytes
	unsigned long Data;				// 4 Byte
}CMD_ELEMENT, *PCMD_ELEMENT;




typedef struct _CMD_PACKAGE16
{	
	CMD_ELEMENT		CmdElement[15];	// 8 Bytes * 15
	unsigned char	CmdCount;		// 1 Byte for aglie meory.
	unsigned char	Reserved[7];	// 7 Bytes for aglie meory.
}CMD_PACKAGE16, *PCMD_PACKAGE16;


typedef struct _MEM_BLOCK_1K
{	
    unsigned long   Index;
    unsigned long   Length;
    unsigned char	BufferBody[1024];   // 1K-4*2Bytes
}MEM_BLOCK_1K, *PMEM_BLOCK_1K;



typedef struct _CPU_REGS
{
	unsigned long EAX;
	unsigned long EBX;
	unsigned long ECX;
	unsigned long EDX;
}CPU_REGS, *PCPU_REGS;



typedef struct _DEVICE_EXTENSION {
	PDEVICE_OBJECT 			pdo;		        	
	PDEVICE_OBJECT 			LowerDeviceObject;		
    CMD_PACKAGE16           Buff_Package;
    MEM_BLOCK_1K            Buff_Mem_1K;
	CPU_REGS				Cpu_Regs;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
	


//===============================================================================================
// DriverEntry.cpp
void DriverUnload(PDRIVER_OBJECT DriverObject);
NTSTATUS CompleteIrp(PIRP Irp, NTSTATUS status, ULONG info);
NTSTATUS DriverAddDevice(PDRIVER_OBJECT DriverObject, PDEVICE_OBJECT pdo);
NTSTATUS CompleteRequest(PIRP Irp, NTSTATUS status, ULONG info);
//void SysDelay(ULONG delayTime);




//===============================================================================================
//PNP.C
NTSTATUS DispatchPnp(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS PnpDefaultHandler(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS ForwardAndWaitingIrp(IN PDEVICE_OBJECT fdo, IN PIRP Irp);
NTSTATUS IrpCompleteRoutine(PDEVICE_OBJECT fdo, PIRP Irp, PKEVENT event);

NTSTATUS HandleStartDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS StartDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleStopDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleRemoveDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleSurpriseRemoval(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleQueryStopDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleQueryRemoveDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleCancelStopDevice(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS HandleCancelRemoveDevice(PDEVICE_OBJECT fdo, PIRP Irp);



//===============================================================================================
// CreateClose.cpp
NTSTATUS DispatchCreate(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS DispatchClose(PDEVICE_OBJECT fdo, PIRP Irp);
NTSTATUS DispatchPower(PDEVICE_OBJECT fdo, PIRP Irp);


	
//===============================================================================================
// DeviceIoControl.cpp  
NTSTATUS DispatchDeviceIoControl(PDEVICE_OBJECT fdo, PIRP Irp);  
unsigned long IOPORT_GET_UCHAR(	unsigned char _index, 	unsigned char 	*_value);
unsigned long IOPORT_SET_UCHAR(	unsigned char _index, 	unsigned char 	_value);
unsigned long IOPORT_GET_USHORT(unsigned short _index, 	unsigned short 	*_value);
unsigned long IOPORT_SET_USHORT(unsigned short _index, 	unsigned short 	_value);
unsigned long IOPORT_GET_ULONG(	unsigned long _index, 	unsigned long 	*_value);
unsigned long IOPORT_SET_ULONG(	unsigned long _index, 	unsigned long 	_value);

unsigned long RWMSR_GET_VALUE(CPU_REGS *_CpuRegs);
unsigned long RWMSR_SET_VALUE(CPU_REGS _CpuRegs);

unsigned long RWMEM_GET_BLOCK(unsigned long _Index, unsigned long _Length, unsigned char *_Buffer);
unsigned long RWMEM_SET_BLOCK(unsigned long _Index, unsigned long _Length, unsigned char *_Buffer);



#endif
