/**
*  This function will initialize Cam Driver.
*                       To Initialize to default settings, HAL_CAM_Init(NULL);
*                                                       To change default settings use ACTION_CAM_GetCameraSettings to get a copy of default camera settings,
*                                                       Modify default settings in HAL_CAM_Action_settings_st_tand pass back in HAL_CAM_Init(HAL_LCD_config_st* HAL_LCD_config)
*                                                           driver will copy new settings to local and initialize Camera.
*/
HAL_CAM_Result_en_t HAL_CAM_Init(
        CamSensorSelect_t           sensor_select,                      ///< (in) Select Camera Sensor
        HAL_CAM_config_st_t*        config_st                           ///< (out) device configuration structure
        )
{
//    Result_t result = RESULT_OK;
    HAL_CAM_Result_en_t hal_result = HAL_CAM_SUCCESS;

    pr_debug("HAL_CAM_Init \r\n");

#if 0
// Obtain Hal Cam Action Semaphore
    if( sHalCamActionSema == NULL )
    {
        sHalCamActionSema = OSSEMAPHORE_Create( 1, OSSUSPEND_PRIORITY );
        OSSEMAPHORE_ChangeName(sHalCamActionSema, "HalCamAction");
        pr_debug("sHalCamActionSema Created \r\n");
    }
    OSSEMAPHORE_Obtain(sHalCamActionSema,TICKS_FOREVER);

    if (config_st != NULL)
    {
    //  Get Current Camera Default Settings
        CAM_GetCameraSettings(config_st->cam_config_st, CamPowerOff, sensor_select);
    }

//  Initialize Camera Driver and Sensor
    result = CAM_Init(sensor_select);
    switch (result)
    {
        case CAM_UNSUPPORTED:
            hal_result = HAL_CAM_ERROR_ACTION_NOT_SUPPORTED;
            break;
        case CAM_FAILED:
            hal_result = HAL_CAM_ERROR_INTERNAL_ERROR;
            break;
        case RESULT_DONE:
            hal_result = HAL_CAM_SUCCESS;
            break;
        case RESULT_OK:
            hal_result = HAL_CAM_SUCCESS;
            break;
        default:
            hal_result = HAL_CAM_ERROR_ACTION_NOT_SUPPORTED;
            break;
    }

// Release Hal Cam Action Semaphore
    OSSEMAPHORE_Release(sHalCamActionSema);
#endif
    return hal_result;
}
/*
 *
 * Function Name:  cslDsiOsInit
 *
 * Description:    DSI COntroller OS Interface Init
 *
 */
Boolean cslDsiOsInit(DSI_HANDLE dsiH)
{
	Boolean res = TRUE;

#ifdef UNDER_LINUX
	int ret;
#endif

	/* Update Request Queue */
	dsiH->updReqQ = OSQUEUE_Create(FLUSH_Q_SIZE,
			sizeof(DSI_UPD_REQ_MSG_T),
			OSSUSPEND_PRIORITY);
	if (!dsiH->updReqQ) {
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: OSQUEUE_Create failed\n",
			__func__);
		res = FALSE;
	} else {
		OSQUEUE_ChangeName(dsiH->updReqQ,
				   dsiH->bus ? "Dsi1Q" : "Dsi0Q");
	}

	/* Update Request Task */
	dsiH->updReqT = OSTASK_Create(dsiH->task,
			dsiH->
			bus ? (TName_t) "Dsi1T" : (TName_t)
			"Dsi0T", TASKPRI_DSI, STACKSIZE_DSI);
	if (!dsiH->updReqT) {
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: Create Task failure\n",
			__func__);
		res = FALSE;
	}
	/* DSI Interface Semaphore */
	dsiH->semaDsi = OSSEMAPHORE_Create(1, OSSUSPEND_PRIORITY);
	if (!dsiH->semaDsi) {
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: ERR Sema Creation!\n",
			__func__);
		res = FALSE;
	} else {
		OSSEMAPHORE_ChangeName(dsiH->semaDsi,
				dsiH->bus ? "Dsi1" : "Dsi0");
	}

	/* DSI Interrupt Event Semaphore */
	dsiH->semaInt = OSSEMAPHORE_Create(0, OSSUSPEND_PRIORITY);
	if (!dsiH->semaInt) {
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: ERR Sema Creation!\n",
			__func__);
		res = FALSE;
	} else {
		OSSEMAPHORE_ChangeName(dsiH->semaInt,
				dsiH->bus ? "Dsi1Int" : "Dsi0Int");
	}

	/* EndOfDma Semaphore */
	dsiH->semaDma = OSSEMAPHORE_Create(0, OSSUSPEND_PRIORITY);
	if (!dsiH->semaDma) {
		LCD_DBG(LCD_DBG_ERR_ID, "[CSL DSI] %s: ERR Sema Creation!\n",
			__func__);
		res = FALSE;
	} else {
		OSSEMAPHORE_ChangeName(dsiH->semaDma,
				dsiH->bus ? "Dsi1Dma" : "Dsi0Dma");
	}

#ifndef __KERNEL__
	/* DSI Controller Interrupt */
	dsiH->iHisr = OSINTERRUPT_Create((IEntry_t)dsiH->hisr,
			dsiH->bus ? (IName_t)"Dsi1" : (IName_t)"Dsi0",
			IPRIORITY_MIDDLE, HISRSTACKSIZE_DSISTAT);
#endif

#ifdef __KERNEL__
	ret = request_irq(dsiH->interruptId, dsiH->lisr, IRQF_DISABLED |
			  IRQF_NO_SUSPEND, "BRCM DSI CSL", NULL);
	if (ret < 0) {
		pr_err("%s(%s:%u)::request_irq failed IRQ %d\n",
		       __func__, __FILE__, __LINE__, dsiH->interruptId);
		goto free_irq;
	}
#else
	IRQ_Register(dsiH->interruptId, dsiH->lisr);
#endif

	return res;

#ifdef __KERNEL__
free_irq:
	free_irq(dsiH->interruptId, NULL);
	return res;
#endif
}
/**
*  This function will perform specific action from defined list, copy of parameters passed thru parm structure.
*
*/
HAL_CAM_Result_en_t HAL_CAM_Ctrl(
        HAL_CAM_Action_en_t action,                 ///< (in) device action
        void*               data,                   ///< (in/out) structure pointer to additional parameters for Set, Get, and IS
        void*               callback                ///< (in) callback function device driver calls once action complete, NULL if not needed
        )
{
    Result_t result = RESULT_OK;
    HAL_CAM_Result_en_t hal_result = HAL_CAM_SUCCESS;
#if 0
    CamIntfConfig_st_t *pSensorIntfConfig;

    pr_debug("HAL_CAM_Ctrl():  action=%d \r\n", action);

// Obtain Hal Cam Action Semaphore
    if( sHalCamActionSema == NULL )
    {
        sHalCamActionSema = OSSEMAPHORE_Create( 1, OSSUSPEND_PRIORITY );
        OSSEMAPHORE_ChangeName(sHalCamActionSema, "HalCamAction");
        pr_debug("sHalCamActionSema Created \r\n");
    }
    OSSEMAPHORE_Obtain(sHalCamActionSema,TICKS_FOREVER);
#endif

    switch (action)
    {
// *** Actions handled by hal camera driver
    // Camera Configuration
#if 0
        case ACTION_CAM_SetPowerMode:                           ///< CamPowerOff/CamPowerOn interface and sensor, use HAL_CAM_Action_power_mode_st_t to set
            result = CAM_SetPowerMode( ((HAL_CAM_Action_power_mode_st_t *)data)->sensor, ((HAL_CAM_Action_power_mode_st_t *)data)->mode );
            break;
        case ACTION_CAM_GetCameraSettings:              ///< Get Interface and Nominal/Min/Max camera settings, use HAL_CAM_Action_settings_st_t to get
            result = CAM_GetCameraSettings(((HAL_CAM_Action_settings_st_t *)data)->cam_config_st, ((HAL_CAM_Action_settings_st_t *)data)->mode, ((HAL_CAM_Action_settings_st_t *)data)->sensor);
            break;
        case ACTION_CAM_GetCameraInterface:             ///< Get I2C and Camera Controller interface settings, use HAL_CAM_Action_cam_interface_st_t to get
            pSensorIntfConfig = CAM_GetCameraInterface( ((HAL_CAM_Action_cam_interface_st_t *)data)->sensor );
            ((HAL_CAM_Action_cam_interface_st_t *)data)->i2c_dev_id = pSensorIntfConfig->sensor_config_i2c->i2c_device_id;
            ((HAL_CAM_Action_cam_interface_st_t *)data)->i2c_max_page = pSensorIntfConfig->sensor_config_i2c->i2c_max_page;
            ((HAL_CAM_Action_cam_interface_st_t *)data)->i2c_sub_addr_op = pSensorIntfConfig->sensor_config_i2c->i2c_sub_adr_op;
//            ((HAL_CAM_Action_cam_interface_st_t *)data)->controller_config = CAM_CNTRL_GetConfig();
            break;
    // Image Capture Modes
        case ACTION_CAM_CaptureImage:                           ///< Capture camera image, use HAL_CAM_Action_capture_image_st_t to enable
            result = CAM_CaptureImage( ((HAL_CAM_Action_capture_image_st_t *)data)->size, ((HAL_CAM_Action_capture_image_st_t *)data)->format,
                                                        ((HAL_CAM_Action_capture_image_st_t *)data)->sensor, ((HAL_CAM_Action_capture_image_st_t *)data)->buf, callback );
            break;
        case ACTION_CAM_CaptureVideo:                           ///< Start camera video capture, use HAL_CAM_Action_capture_video_st_t to enable
            result = CAM_CaptureVideo( ((HAL_CAM_Action_capture_video_st_t *)data)->size, ((HAL_CAM_Action_capture_video_st_t *)data)->format,
                                                        ((HAL_CAM_Action_capture_video_st_t *)data)->sensor, ((HAL_CAM_Action_capture_video_st_t *)data)->buf0,
                                                        ((HAL_CAM_Action_capture_video_st_t *)data)->buf1, callback );
            break;
        case ACTION_CAM_PreviewPPzoom:                          ///< Start camera preview/video capture, use HAL_CAM_Action_preview_PPzoom_st_t to enable
            result = CAM_PreviewPPzoom( ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->window_scale,
                                        ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->format, ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->sensor,
                                        ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->flash_mode, ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->mode,
                                        ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->buf0, ((HAL_CAM_Action_preview_PPzoom_st_t *)data)->buf1, callback );
            break;
        case ACTION_CAM_StillsPPzoom:                          ///< Start camera preview/video capture, use HAL_CAM_Action_preview_PPzoom_st_t to enable
            result = CAM_StillsPPzoom( ((HAL_CAM_Action_stills_PPzoom_st_t *)data)->window_scale, ((HAL_CAM_Action_stills_PPzoom_st_t *)data)->format,
                                        ((HAL_CAM_Action_stills_PPzoom_st_t *)data)->sensor, ((HAL_CAM_Action_stills_PPzoom_st_t *)data)->flash_mode,
                                        ((HAL_CAM_Action_stills_PPzoom_st_t *)data)->buf, callback );
            break;
        case ACTION_CAM_CapImageWithThumbNail:                  ///< Capture camera image with thumbnail, use HAL_CAM_Action_capture_image_with_thumbnail_st_t to enable
            result = CAM_CapImageWithThumbNail( ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->size, ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->format,
                                        ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->sensor, ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->buf,
                                        ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->thumbnail_width, ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->thumbnail_height,
                                        ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->thumbnail_format,
                                        ((HAL_CAM_Action_capture_image_with_thumbnail_st_t *)data)->thumbnail_buf, callback );
            break;
        case ACTION_CAM_CapVideoWithViewFinder:                 ///< Start camera video capture with viewfinder, use HAL_CAM_Action_capture_video_with_viewfinder_st_t to enable
            result = CAM_CapVideoWithViewfinder( ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->size, ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->format,
                                        ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->sensor, ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->buf0,
                                        ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->buf1,((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->viewfinder_size,
                                        ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->viewfinder_format, ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->viewfinder_buf0,
                                        ((HAL_CAM_Action_capture_video_with_viewfinder_st_t *)data)->viewfinder_buf1, callback );
            break;
        case ACTION_CAM_StopCapture:                    ///< Stop video or image capture, no additional parameters
            result = CAM_StopCapture();
            break;
		case ACTION_CAM_PauseCapture:					///< Pause viewfinder, video capture, no additional parameters
			result = CAM_PauseCapture();
			break;

		case ACTION_CAM_ResumeCapture:					///< Resume viewfinder, video capture, no additional parameters
			result = CAM_ResumeCapture();
			break;

    // Operation Control Settings
        case ACTION_CAM_SetWindowScale:             ///< Set the Camera window size and scale factors, use HAL_CAM_Action_window_scale_st_t to set
            result = CAM_SetWindowScale( (CamImageSize_t)((HAL_CAM_Action_window_scale_st_t *)data)->size,
                                                        (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->start_pixel, (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->end_pixel,
                                                        (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->start_line, (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->end_line,
                                                        (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->horizontal_SDF, (UInt32)((HAL_CAM_Action_window_scale_st_t *)data)->vertical_SDF );
            break;
        case ACTION_CAM_SetZoom:                                    ///< Set required zoom, use (UInt16) HAL_CAM_Action_param_st_t to set
            result = CAM_SetZoom( (UInt16)(((HAL_CAM_Action_param_st_t *)data)->param) );
            break;
        case ACTION_CAM_GetMaxWindowSize:                   ///< Camera Maximum Pixel Window size, use HAL_CAM_Action_window_st_t to get
            pSensorIntfConfig = CAM_GetCameraInterface( ((HAL_CAM_Action_window_st_t *)data)->sensor );
                                ((HAL_CAM_Action_window_st_t *)data)->width = pSensorIntfConfig->sensor_config_caps->output_st.max_width;
                                ((HAL_CAM_Action_window_st_t *)data)->height = pSensorIntfConfig->sensor_config_caps->output_st.max_height;
            break;
        case ACTION_CAM_GetMaxZoom:                             ///< Get maximum zoom allowed, use (UInt16) HAL_CAM_Action_param_st_t to get
#if defined(SS_2153) && defined(BROOKLYN_HW)
            ((HAL_CAM_Action_param_st_t *)data)->param = (UInt16)CAM_GetMaxZoom();
#else
            ((HAL_CAM_Action_param_st_t *)data)->param = (UInt16)CAM_GetMaxZoom(((HAL_CAM_Action_param_st_t *)data)->sensor);
#endif
            break;

    // Flash Settings
        case ACTION_CAM_SetFlashEnable:                     ///< Set flash enable TRUE/FALSE, use (Boolean) HAL_CAM_Action_param_st_t to set
            result = CAM_SetFlashEnable(*((Boolean*)((HAL_CAM_Action_param_st_t *)data)->param) );
            break;
    // Register Callback
		case	ACTION_CAM_SetESD_CB:						///< ESD callback setting
			result = CAM_SetEsdCb((((HAL_CAM_Action_param_st_t *)data)->param), ((HAL_CAM_Action_param_st_t *)data)->sensor );
			break;
#endif

    // Resolution not handled by all camera device drivers
#if 0
        case ACTION_CAM_GetSensorResolution:            ///< Get matching image size from passed pixel width and height, use HAL_CAM_Action_image_size_st_t to get CamImageSize_t
            result  = CAMDRV_GetResolution(((HAL_CAM_Action_resolution_st_t *)data)->size,
                                                    ((HAL_CAM_Action_resolution_st_t *)data)->mode,
                                                    ((HAL_CAM_Action_resolution_st_t *)data)->sensor,
                                                    ((HAL_CAM_Action_resolution_st_t *)data)->sensor_size);
            break;
#endif

// *** Actions for Camera Sensor Driver Only ***  (No baseband control, handled by camera device driver)
// Default: CAMDRV_xxx() Functions
// *********************************************************************************************************
// *********************************************************************************************************

// *** All other Customer Defined Actions:  Handled by camera device driver only
        default:
            pr_debug("HAL_CAM_Ctrl(): HAL_CAMDRV_Ctrl(): \r\n");
            result = CAMDRV_ActionCtrl(action, data, callback);
            if (result != RESULT_OK)
            {
                pr_debug("HAL_CAM_Ctrl(): HAL_CAMDRV_Ctrl(): ERROR: Invalid Action=%d \r\n", action);
            }
            break;
    }
    switch (result)
    {
        case CAM_UNSUPPORTED:
            hal_result = HAL_CAM_ERROR_ACTION_NOT_SUPPORTED;
            break;
        case CAM_FAILED:
            hal_result = HAL_CAM_ERROR_INTERNAL_ERROR;
            break;
        case RESULT_DONE:
            hal_result = HAL_CAM_SUCCESS;
            break;
        case RESULT_OK:
            hal_result = HAL_CAM_SUCCESS;
            break;
        default:
            hal_result = HAL_CAM_ERROR_ACTION_NOT_SUPPORTED;
            break;
    }

#if 0
// Release Hal Cam Action Semaphore
    OSSEMAPHORE_Release(sHalCamActionSema);

#endif
    return hal_result;
}