
#include <windows.h>
//#include <ceddk.h>
#include <nkintr.h>
#include <oalintr.h>
#include <pm.h>
#include "pmplatform.h"
#include "Pkfuncs.h"
#include "s2440.h"


#define IO_CTL_LED_1_ON 0x01
#define IO_CTL_LED_2_ON 0x02
#define IO_CTL_LED_3_ON 0x03
#define IO_CTL_LED_4_ON 0x04
#define IO_CTL_LED_ALL_ON 0x05
#define IO_CTL_LED_1_OFF 0x06
#define IO_CTL_LED_2_OFF 0x07
#define IO_CTL_LED_3_OFF 0x08
#define IO_CTL_LED_4_OFF 0x09
#define IO_CTL_LED_ALL_OFF 0x0a


volatile IOPreg 	*s2440IOP = (IOPreg *)IOP_BASE;
volatile INTreg 	*s2440INT = (INTreg *)INT_BASE;



BOOL mInitialized;
void Virtual_Alloc();						// Virtual allocation



void Virtual_Alloc()
{

    // GPIO Virtual alloc
	s2440IOP = (volatile IOPreg *) VirtualAlloc(0,sizeof(IOPreg),MEM_RESERVE, PAGE_NOACCESS);
	if(s2440IOP == NULL) {
		RETAILMSG(1,(TEXT("For s2440IOP: VirtualAlloc faiLED!\r\n")));
	}
	else {
		if(!VirtualCopy((PVOID)s2440IOP,(PVOID)(IOP_BASE),sizeof(IOPreg),PAGE_READWRITE | PAGE_NOCACHE )) {
			RETAILMSG(1,(TEXT("For s2440IOP: VirtualCopy faiLED!\r\n")));
		}
	}
 
	
}


BOOL WINAPI  
DllEntry(HANDLE	hinstDLL, 
			DWORD dwReason, 
			LPVOID /* lpvReserved */)
{
	switch(dwReason)
	{
	case DLL_PROCESS_ATTACH:
		DEBUGREGISTER((HINSTANCE)hinstDLL);
		return TRUE;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
#ifdef UNDER_CE
	case DLL_PROCESS_EXITING:
		break;
	case DLL_SYSTEM_STARTED:
		break;
#endif
	}

	return TRUE;
}


BOOL LED_Deinit(DWORD hDeviceContext)
{
	BOOL bRet = TRUE;
	
	RETAILMSG(1,(TEXT("USERLED: LED_Deinit\r\n")));

	return TRUE;
} 




BOOL LEDGpioInit()
{
	RETAILMSG(1,(TEXT("LED_Gpio_Setting----\r\n")));
	s2440IOP->rGPBCON  = (s2440IOP->rGPBCON  &~(3 << 10)) | (1<< 10);	// GPB5 == OUTPUT.
	s2440IOP->rGPBCON  = (s2440IOP->rGPBCON  &~(3 << 12)) | (1<< 12);	// GPB6 == OUTPUT.
	s2440IOP->rGPBCON  = (s2440IOP->rGPBCON  &~(3 << 14)) | (1<< 14);	// GPB7 == OUTPUT.
	s2440IOP->rGPBCON  = (s2440IOP->rGPBCON  &~(3 << 16)) | (1<< 16);	// GPB8 == OUTPUT.
	return TRUE;
}




DWORD LED_Init(DWORD dwContext)
{
	
	RETAILMSG(1,(TEXT("LED_Init----\r\n")));

	// 1. Virtual Alloc
	Virtual_Alloc();
	
	LEDGpioInit();
	
	
	mInitialized = TRUE;
	return TRUE;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL LED_IOControl(DWORD hOpenContext, 
				   DWORD dwCode, 
				   PBYTE pBufIn, 
				   DWORD dwLenIn, 
				   PBYTE pBufOut, 
				   DWORD dwLenOut, 
				   PDWORD pdwActualOut)
{
	switch(dwCode)
	{
	case IO_CTL_LED_1_ON:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&~(0x1<<5);
	case IO_CTL_LED_2_ON:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&~(0x1<<6);
		break;
	case IO_CTL_LED_3_ON:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&~(0x1<<7);
		break;
	case IO_CTL_LED_4_ON:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&~(0x1<<8);
		break;
	case IO_CTL_LED_ALL_ON:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&~(0xF<<5);
		break;
	case IO_CTL_LED_1_OFF:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT|(0x1<<5);
		break;
	case IO_CTL_LED_2_OFF:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT|(0x1<<6);
		break;
	case IO_CTL_LED_3_OFF:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT|(0x1<<7);
		break;
	case IO_CTL_LED_4_OFF:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT|(0x1<<8);
		break;
	case IO_CTL_LED_ALL_OFF:
		s2440IOP->rGPBDAT=s2440IOP->rGPBDAT|(0xF<<5);
		break;
	default:
		break;		
	}
    
	RETAILMSG(1,(TEXT("LED:Ioctl code = 0x%x\r\n"), dwCode));
	return TRUE;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD LED_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
	RETAILMSG(1,(TEXT("USERLED: LED_Open\r\n")));
	return TRUE;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL LED_Close(DWORD hOpenContext)
{
	s2440IOP->rGPBDAT=s2440IOP->rGPBDAT&(0xF<<5);
	RETAILMSG(1,(TEXT("USERLED: LED_Close\r\n")));
	return TRUE;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void LED_PowerDown(DWORD hDeviceContext)
{
	RETAILMSG(1,(TEXT("USERLED: LED_PowerDown\r\n")));
	
	//RETAILMSG(1,(TEXT("CAMERA: LED_PowerDown, m_Dx = D%u, init %d \r\n"), m_Dx, mInitialized));
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void LED_PowerUp(DWORD hDeviceContext)
{
	RETAILMSG(1,(TEXT("USERLED: LED_PowerUp\r\n")));

} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD LED_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
	RETAILMSG(1,(TEXT("USERLED: LED_Read\r\n")));
	return TRUE;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD LED_Seek(DWORD hOpenContext, long Amount, DWORD Type)
{
	RETAILMSG(1,(TEXT("USERLED: LED_Seek\r\n")));
	return 0;
} 

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD LED_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
	RETAILMSG(1,(TEXT("USERLED: LED_Write\r\n")));
	return 0;
}




