dcl_pxs.c 13.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2001
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   dcl_pxs.c
 *
 * Project:
 * --------
 *   Maui
 *
 * Description:
 * ------------
 *   This Module defines DCL (Driver Common Layer) of the PXS driver.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/


// MMI request, even if there is no PXS device, the interfaces should still exist and return ERROR code
#if !defined(__PXS_ENABLE__)
#include "dcl.h"
DCL_STATUS DclPXS_Initialize(void)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_HANDLE DclPXS_Open(DCL_DEV dev, DCL_FLAGS flags)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_ReadData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_WriteData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_Configure(DCL_HANDLE handle, DCL_CONFIGURE_T *configure)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_RegisterCallback(DCL_HANDLE handle, DCL_EVENT event, PFN_DCL_CALLBACK callback)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_Control(DCL_HANDLE handle, DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)
{
	return STATUS_DEVICE_NOT_EXIST;
}
DCL_STATUS DclPXS_Close(DCL_HANDLE handle)
{
	return STATUS_DEVICE_NOT_EXIST;
}
#endif // #if !defined(__PXS_ENABLE__)

#if defined(__PXS_ENABLE__)
#include "kal_general_types.h"
#include "intrCtrl.h"
#include "dcl.h"



#define GET_DRIVER_SEMAPHORE(x)      x = SaveAndSetIRQMask()
#define RELEASE_DRIVER_SEMAPHORE(x)      RestoreIRQMask(x)

#define PXS_MAX_OPEN_COUNT   1

#define DCL_PXS_DEV_MAGIC_NUM    (0x60000000)
#define DCL_PXS_DEV_MAGIC_NUM_MASK    (0xFFFF0000)

kal_uint32 pxs_open_count;
PXS_CONFIG_T PXSConfig;  // The configuration from PXS users

kal_bool pxs_adv_algo_windowing_flag;
PXS_CTRL_GET_ADV_ALGO_WINDOWING_T pxs_algo_windowing;
kal_bool pxs_adv_algo_thread_flag;
PXS_CTRL_GET_ADV_ALGO_THRESHOLD_T pxs_algo_threshold;
DCL_BOOL pxs_detect_flag = DCL_FALSE;   // DCL_TRUE: Detected; DCL_FALSE: Not detected

kal_bool pxs_exist_flag = KAL_FALSE;


// The callback function registered to HPXS level driver
// PXS driver can decide to have some process or NOT then callback to user registered callback
static void HPXS_Callback(DCL_BOOL fgObjectDetected)
{
	pxs_detect_flag = fgObjectDetected;
	if (PXSConfig.fgNotify == DCL_TRUE)
	{
		if (PXSConfig.NotifyCallback)
		{
			PXSConfig.NotifyCallback(fgObjectDetected);
		}
		else
		{
			ASSERT(0);
		}
	}
}



/*************************************************************************
* FUNCTION
*  DclPXS_Initialize
*
* DESCRIPTION
*  This function is to initialize PXS module
*
* PARAMETERS
*  None
*
* RETURNS
*  STATUS_OK
*
*************************************************************************/
DCL_STATUS DclPXS_Initialize(void)
{

	DCL_CTRL_DATA_T ctrl;
	
	if (STATUS_OK != DclHPXS_Initialize())
	{
		//ASSERT(0);
		return STATUS_FAIL;
	}

	if (STATUS_OK == DclHPXS_Control(DCL_PXS_DEV_MAGIC_NUM, PXS_CMD_GET_ADV_ALGO_WINDOWING, (DCL_CTRL_DATA_T *)(&ctrl)))
	{
		pxs_adv_algo_windowing_flag = KAL_TRUE;
		pxs_algo_windowing.u2Count = ctrl.rPXSAdvAlgoWindowing.u2Count;
	}
	else
	{
		pxs_adv_algo_windowing_flag = KAL_FALSE;
	}

	if (STATUS_OK == DclHPXS_Control(DCL_PXS_DEV_MAGIC_NUM, PXS_CMD_GET_ADV_ALGO_THRESHOLD, (DCL_CTRL_DATA_T *)(&ctrl)))
	{
		pxs_adv_algo_thread_flag = KAL_TRUE;
		pxs_algo_threshold.u2HiThreshold = ctrl.rPXSAdvAlgoThreshold.u2HiThreshold;
		pxs_algo_threshold.u2LoThreshold = ctrl.rPXSAdvAlgoThreshold.u2LoThreshold;
	}
	else
	{
		pxs_adv_algo_thread_flag = KAL_FALSE;
	}

	pxs_open_count = 0;
	pxs_exist_flag = KAL_TRUE;

	return STATUS_OK;
}

/*************************************************************************
* FUNCTION
*  DclPXS_Open
*
* DESCRIPTION
*  This function is to open the PXS module and return a handle
*
* PARAMETERS
*  dev: only valid for DCL_PXS
*  flags: no sepcial flags is needed. Please use FLAGS_NONE
*
* RETURNS
*  DCL_HANDLE_INVALID: Open failed
*  Other value: A valid handle
*
*************************************************************************/
DCL_HANDLE DclPXS_Open(DCL_DEV dev, DCL_FLAGS flags)
{
	kal_uint32 handle;
	kal_uint32 savedMask;

	// Check device type
	if (dev != DCL_PXS){
		//ASSERT(0);
		return DCL_HANDLE_INVALID;		// Incorrecr device ID
	}

	// If device NOT exist, return STATUS_DEVICE_NOT_EXIST
	if (pxs_exist_flag == KAL_FALSE)
	{
		return STATUS_DEVICE_NOT_EXIST;
	}

	// Check allowed open number
	GET_DRIVER_SEMAPHORE(savedMask);
	if (pxs_open_count == PXS_MAX_OPEN_COUNT)
	{
		//ASSERT(0); // Only allow one user
		return DCL_HANDLE_OCCUPIED;
	}
	pxs_open_count ++;
	RELEASE_DRIVER_SEMAPHORE(savedMask);


	handle = (DCL_PXS_DEV_MAGIC_NUM | pxs_open_count);

	
	// After DclPXS_Open(), 
	{
		PXS_CONFIG_T HPXSConfig;
		HPXSConfig.fgNotify = DCL_TRUE;
		HPXSConfig.NotifyCallback = HPXS_Callback;
		DclHPXS_Configure(handle, (DCL_CONFIGURE_T *)(&HPXSConfig));
	}

	return handle;

}

/*************************************************************************
* FUNCTION
*  DclPXS_ReadData
*
* DESCRIPTION
*  This function is not supported for the PXS module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclPXS_ReadData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	return STATUS_UNSUPPORTED;
}

/*************************************************************************
* FUNCTION
*  DclPXS_WriteData
*
* DESCRIPTION
*  This function is not supported for the PXS module now.
*
* PARAMETERS
*	N/A
*
* RETURNS
*	STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclPXS_WriteData(DCL_HANDLE handle, DCL_BUFF *buff, DCL_BUFF_LEN buf_len, DCL_OPTIONS options)
{
	return STATUS_UNSUPPORTED;
}
/*************************************************************************
* FUNCTION
*  DclPXS_Configure
*
* DESCRIPTION
*  This function is to configure the PXS module.
*
* PARAMETERS
*  handle: the returned handle value of DclPXS_Open
*  configure: a structure which include the PXS configuration.
*
* RETURNS
*  STATUS_OK: Successfully configure PXS module.
*  STATUS_INVALID_DCL_HANDLE: It's a invalid handle.
*  STATUS_NOT_OPENED: The module has not been opened.
*  STATUS_INVALID_CONFIGURATION: The configuration is invalid.
*
*************************************************************************/
DCL_STATUS DclPXS_Configure(DCL_HANDLE handle, DCL_CONFIGURE_T *configure)
{

	// Check magic number
	if ((handle & DCL_PXS_DEV_MAGIC_NUM_MASK) != DCL_PXS_DEV_MAGIC_NUM){
	//if ((handle & DCL_PXS_DEV_MAGIC_NUM) != DCL_PXS_DEV_MAGIC_NUM){
		//ASSERT(0);
		return STATUS_INVALID_DCL_HANDLE;
	}

	// If device NOT exist, return STATUS_DEVICE_NOT_EXIST
	if (pxs_exist_flag == KAL_FALSE)
	{
		return STATUS_DEVICE_NOT_EXIST;
	}

	// Error check
	if (pxs_open_count == 0){
		//ASSERT(0);
		return STATUS_NOT_OPENED;
	}

	// Check pointer
	if (configure == NULL)
	{
		//ASSERT(0);
		return STATUS_INVALID_CONFIGURATION;
	}

	{
		PXS_CONFIG_T *prPXSConfig;
		PXS_CONFIG_T HPXSConfig;
		prPXSConfig = (PXS_CONFIG_T *)(configure);

		if (prPXSConfig->fgNotify == DCL_TRUE)
		{
			// Check callback function
			if (prPXSConfig->NotifyCallback == NULL)
			{
				ASSERT(0);
				return STATUS_INVALID_CONFIGURATION;
			}

			PXSConfig.fgNotify = DCL_TRUE;
			PXSConfig.NotifyCallback = prPXSConfig->NotifyCallback;
		}
		else
		{
			PXSConfig.fgNotify = DCL_FALSE;
			PXSConfig.NotifyCallback = NULL;
		}
		//HPXSConfig.fgNotify = PXSConfig.fgNotify;
		HPXSConfig.fgNotify = DCL_TRUE;  // Always ask low level driver to callback
		                                 // Whether to call back to upper layer(MMI), let DCLPXS layer to decide
		HPXSConfig.NotifyCallback = HPXS_Callback;
		return 	DclHPXS_Configure(handle, (DCL_CONFIGURE_T *)(&HPXSConfig));
	}

	// return STATUS_OK;

}

/*************************************************************************
* FUNCTION
*  DclPXS_RegisterCallback
*
* DESCRIPTION
*  This function is to set callback function for the PXS module.
*
* PARAMETERS
*  handle: the returned handle value of DclPXS_Open
*  event: Supported events:
*  callback: the callback function for registered events
*
* RETURNS
*  STATUS_UNSUPPORTED
*
*************************************************************************/
DCL_STATUS DclPXS_RegisterCallback(DCL_HANDLE handle, DCL_EVENT event, PFN_DCL_CALLBACK callback)
{
	return STATUS_UNSUPPORTED;
}

/*************************************************************************
* FUNCTION
*  DclPXS_Control
*
* DESCRIPTION
*  This function is to send command to control the PXS module.
*
* PARAMETERS
*  handle: The handle value returned from DclPXS_Open
*  cmd: A control command for PXS module
*          1. PXS_CMD_ENABLE: to enable/disable PXS function
*          2. PXS_CMD_GET_RAW_DATA: to get PXS sampled raw data
*          3. PXS_CMD_EM_READ: to read EM parameters
*          4. PXS_CMD_EM_WRITE: to write EM parameters
*          5. PXS_CMD_GET_DETECT_STATUS: to get PXS detected status
*  data: The data of the control command
*          1. PXS_CMD_ENABLE: pointer to a PXS_CTRL_ENABLE_T structure
*          2. PXS_CMD_GET_RAW_DATA: pointer to a PXS_CTRL_RAW_DATA_T structure
*          3. PXS_CMD_EM_READ: pointer to a PXS_CTRL_EM_PARAM_T structure
*          4. PXS_CMD_EM_WRITE: pointer to a PXS_CTRL_EM_PARAM_T structure
*          5. PXS_CMD_GET_DETECT_STATUS: pointer to a PXS_CTRL_GET_DETECT_STATUS_T structure
*
* RETURNS
*  STATUS_OK: command is executed successfully.
*  STATUS_FAIL: command is failed.
*  STATUS_INVALID_CMD: It's a invalid command.
*
*************************************************************************/
DCL_STATUS DclPXS_Control(DCL_HANDLE handle, DCL_CTRL_CMD cmd, DCL_CTRL_DATA_T *data)
{
	// Check magic number
	if ((handle & DCL_PXS_DEV_MAGIC_NUM_MASK) != DCL_PXS_DEV_MAGIC_NUM){
		//ASSERT(0);
		return STATUS_INVALID_DCL_HANDLE;
	}

	// If device NOT exist, return STATUS_DEVICE_NOT_EXIST
	if (pxs_exist_flag == KAL_FALSE)
	{
		return STATUS_DEVICE_NOT_EXIST;
	}

	// Error check
	if (pxs_open_count == 0){
		//ASSERT(0);
		return STATUS_NOT_OPENED;
	}

	// Check pointer
	if (data == NULL)
	{
		//ASSERT(0);
		return STATUS_INVALID_CTRL_DATA;
	}

	switch (cmd)
	{
		case PXS_CMD_ENABLE:
		case PXS_CMD_GET_RAW_DATA:
		case PXS_CMD_EM_READ:
		case PXS_CMD_EM_WRITE:
		case PXS_CMD_SET_DEBOUNCE:
		{
			return DclHPXS_Control(handle, cmd, data);
		}
		case PXS_CMD_MODIFY_CONFIG:
		{
			PXS_CTRL_CONFIG_T *prPXSConfig;
			PXS_CTRL_CONFIG_T HPXSConfig;
			prPXSConfig = (PXS_CTRL_CONFIG_T *)(data);
			if (prPXSConfig->fgNotify == DCL_TRUE)
			{
				// Check callback function
				if (prPXSConfig->NotifyCallback == NULL)
				{
					ASSERT(0);
					return STATUS_INVALID_ARGUMENT;
				}

				PXSConfig.fgNotify = DCL_TRUE;
				PXSConfig.NotifyCallback = prPXSConfig->NotifyCallback;
			}
			else
			{
				PXSConfig.fgNotify = DCL_FALSE;
				PXSConfig.NotifyCallback = NULL;
			}
			HPXSConfig.fgNotify = DCL_TRUE;
			HPXSConfig.NotifyCallback = HPXS_Callback;
			return DclHPXS_Control(handle, cmd, (DCL_CTRL_DATA_T *)&HPXSConfig);
		}
		//break;
		case PXS_CMD_GET_DETECT_STATUS:
		{
			PXS_CTRL_GET_DETECT_STATUS_T *prPXSGetStatus;
			prPXSGetStatus = (PXS_CTRL_GET_DETECT_STATUS_T *)(data);
			prPXSGetStatus->fgDetected = pxs_detect_flag;
			return STATUS_OK;
		}
		default:
		{
			//ASSERT(0);
			return STATUS_INVALID_CMD;
		}
		//break;
	}

	//return STATUS_OK;
}

/*************************************************************************
* FUNCTION
*  DclPXS_Close
*
* DESCRIPTION
*  This function is to close the PXS module.
*
* PARAMETERS
*  handle: the returned handle value of DclPXS_Open
*
* RETURNS
*  STATUS_OK
*
*************************************************************************/
DCL_STATUS DclPXS_Close(DCL_HANDLE handle)
{
	DCL_CTRL_DATA_T data;

	// Check magic number
	if ((handle & DCL_PXS_DEV_MAGIC_NUM_MASK) != DCL_PXS_DEV_MAGIC_NUM){
		//ASSERT(0);
		return STATUS_INVALID_DCL_HANDLE;
	}

	// If device NOT exist, return STATUS_DEVICE_NOT_EXIST
	if (pxs_exist_flag == KAL_FALSE)
	{
		return STATUS_DEVICE_NOT_EXIST;
	}

	// Error check
	if (pxs_open_count == 0){
		//ASSERT(0);
		return STATUS_NOT_OPENED;
	}

	PXSConfig.fgNotify = DCL_FALSE;
	PXSConfig.NotifyCallback = NULL;

	data.rPXSEnable.fgEnable = DCL_FALSE;

	DclHPXS_Control(handle, PXS_CMD_ENABLE, &data);

	pxs_open_count --;

	return STATUS_OK;

}


#endif // #if defined(__PXS_ENABLE__)