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

📄 usb_task.c

📁 MTK平台绝密核心代码之 USB驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
*************************************************************************/
kal_bool usb_create(comptask_handler_struct **handle)
{
#ifndef __USB_ENABLE__
	EXT_ASSERT(0, 0, 0, 0);
#endif

	static const comptask_handler_struct usb_handler_info = 
	{
		usb_task_main,			/* task entry function */
		usb_task_init,		  	/* task initialization function */
		NULL, 				/* task configuration function */
		usb_task_reset,			/* task reset handler */
		usb_task_end			/* task termination handler */
	};

	g_usb_eint_no = custom_eint_get_channel(usb_eint_chann);
	// remove to avoid affecting touch panel function
	//clear_ext_intr_init(g_usb_eint_no);

	*handle = (comptask_handler_struct *)&usb_handler_info;
	return KAL_TRUE;
}

/*************************************************************************
* FUNCTION                                                            
*	usb_task_init
*
* DESCRIPTION
*	This function implements xyz's init handler
*
* PARAMETERS 
*	task_index	-	task's index
*
* RETURNS
*
* GLOBALS AFFECTED
*
*************************************************************************/
static kal_bool usb_task_init(task_indx_type task_indx)
{
#ifndef __USB_ENABLE__
	EXT_ASSERT(0, 0, 0, 0);
#endif

	kal_uint32 buff_size = 0;
#ifdef __P_PROPRIETARY_COPYRIGHT__
	kal_uint32 protect_num, non_protect_num;
	kal_uint32 protect_index = 0;
#endif
#ifdef WEBCAM_SUPPORT
	kal_uint32 index;
#endif
	
#ifdef __OTG_ENABLE__
	OTG_Drv_Create_ISR();
#else
	USB_Drv_Create_ISR();   /*   Register USB_HISR, not active, ensure we have enough memory space  */
#endif

  	/* Do task's init here.*/
  	if (INT_USBBoot() == KAL_TRUE)   /* MS mode */
  	{
#ifdef __P_PROPRIETARY_COPYRIGHT__
		FS_GetCopyrightConfig(&protect_num, &non_protect_num, NULL, NULL,
    	                     &g_UsbMS.read_protect_items, &g_UsbMS.read_protect_second_items);
		g_UsbMS.read_protect_num = protect_num+non_protect_num;
		buff_size = (sizeof(Usb_Disk_Buffer)>(g_UsbMS.read_protect_num*sizeof(kal_uint32))) ?
					sizeof(Usb_Disk_Buffer):(g_UsbMS.read_protect_num*sizeof(kal_uint32));
		/* for index */
		protect_index = buff_size;
		buff_size+=g_UsbMS.read_protect_num*sizeof(kal_uint32)*2;
#else
		buff_size = sizeof(Usb_Disk_Buffer);
#endif
 		
  		/* get the buffer at initialize time for USB boot mode*/	
  		g_UsbMS.disk_buffer = NULL;	
  		g_UsbMS.disk_buffer = kal_tmp_mem_alloc(buff_size);
  		if(g_UsbMS.disk_buffer == NULL)
			EXT_ASSERT(0, buff_size, 0, 0);	
#ifdef __P_PROPRIETARY_COPYRIGHT__
		g_UsbMS.read_protect_index = (kal_uint32*)(((kal_uint8*)g_UsbMS.disk_buffer) +protect_index);
		g_UsbMS.read_protect_second_index = g_UsbMS.read_protect_index+g_UsbMS.read_protect_num;
#endif			
  	}
  	
  	/* get customize parameters*/
	gUsbDevice.device_param = USB_GetCustomFunc()->get_device_param_func();
	g_UsbMS.ms_param = USB_GetCustomFunc()->get_ms_param_func();
	g_UsbACM.acm_param = USB_GetCustomFunc()->get_acm_param_func();
#ifdef WEBCAM_SUPPORT	
	g_USBVideo.custom_param = USB_GetCustomFunc()->get_video_param_func();
	g_USBVideo.camera_param = USBVideo_Get_Camera_Param();
	for(index = 0; index < USBVIDEO_VIDEO_SIZE_MAX; index++)
	{
		g_USBVideo.video_control.default_jpeg_max_size[index] = 0;
		g_USBVideo.video_control.b_set_default_jpeg_max_size[index] = KAL_FALSE;
	}
#endif
	return KAL_TRUE;
}


/*************************************************************************
* FUNCTION                                                            
*	usb_task_reset
*
* DESCRIPTION
*	This function implements xyz's reset handler
*
* PARAMETERS 
*	task_index	-	task's index
*
* RETURNS
*
* GLOBALS AFFECTED
*
*************************************************************************/
static kal_bool usb_task_reset(task_indx_type task_indx)
{
  	/* Do task's reset here.
	 * Notice that: shouldn't execute modules reset handler since 
	 * stack_task_reset() will do. */
	return KAL_TRUE;
}



/*************************************************************************
* FUNCTION                                                            
*	usb_task_end
*
* DESCRIPTION
*	This function implements xyz's termination handler
*
* PARAMETERS 
*	task_index	-	task's index
*
* RETURNS
*
* GLOBALS AFFECTED
*
*************************************************************************/
static kal_bool usb_task_end(task_indx_type task_indx)
{
  	/* Do task's termination here.
	 * Notice that: shouldn't execute modules reset handler since 
	 * stack_task_end() will do. */
	return KAL_TRUE;
}



/*************************************************************************
* FUNCTION
*	usb_task_main
*
* DESCRIPTION
*	This function implements xyz task's entry function
*
* PARAMETERS
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
static void usb_task_main( task_entry_struct * task_entry_ptr )
{
	ilm_struct current_ilm;
	kal_uint32 my_index;
	drvuem_usbcfg_req_struct *usbcfg;

#ifndef __USB_ENABLE__
	EXT_ASSERT(0, 0, 0, 0);
#endif

	/* for adding module id*/
   	kal_get_my_task_index( &my_index );     
   	stack_set_active_module_id( my_index, MOD_USB);

	gUsbDevice.device_type = USB_UNKOWN;
	g_UsbMS.b_ms_first_plugin = KAL_TRUE;

	/* set external interrupt polarity and register it */
	g_usb_cable_state = gUsbDevice.device_param->cable_plugin_level;
	
#ifdef __OTG_ENABLE__	

#ifndef __USB_IN_NORMAL_MODE__
	/* OTG enable must be with normal mode compile option */
	EXT_ASSERT(0, 0, 0, 0);
#endif

    #ifndef __CUST_NEW__
	//set GPIO mode as OTG function
	GPIO_ModeSetup(7, 2);
	GPIO_ModeSetup(8, 2);
    #endif /* __CUST_NEW__ */

	USB_Init_Mode();
	OTG_Init_A_Plug_Detect();
	
//	gUsbOtg.eint_no = g_usb_eint_no;	
//	eint_sw_debounce_time_delay = custom_config_eint_sw_debounce_time_delay();
//	gUsbOtg.eint_time_delay= eint_sw_debounce_time_delay[g_usb_eint_no];
#endif /* __OTG_ENABLE__ */
	
	if(INT_BootMode() != MTK_FACTORY_MODE)
	{		
	#ifdef MT6318	
	   /*XXXXX*/
	   #if (!defined(__CHARGER_USB_DETECT_WIHT_ONE_EINT__))
		PMIC_ChrDet_Registration(USB_CHR, USB_EINT_HISR);  /* called by USB to register its detection handler  */
		#endif
	#else	
		#ifdef _USB_VBUS_DETECT_WITH_NIRQ_  /* Only in EVB because EINT is not enough, NIRQ can be seen as EINT */
			USB_NIRQ_Registration(USB_EINT_HISR);
		#else
			#if !defined(__CHARGER_USB_DETECT_WIHT_ONE_EINT__) /*if defined, than registered at bmt_task_main.c */
				EINT_Registration(g_usb_eint_no, KAL_TRUE, g_usb_cable_state, USB_EINT_HISR, KAL_TRUE);
			#endif
		#endif
	#endif
	}	

	/* request for unique string to construct serial string*/
	if(INT_USBBoot() == KAL_FALSE)
	{
		USB_Send_Msg(USB_SEND_MSG_READ_UNIQUE_STRING, 0, KAL_TRUE);
	}
	
	while(1)
	{
		receive_msg_ext_q(task_info_g[task_entry_ptr->task_indx].task_ext_qid, &current_ilm);

		switch(current_ilm.msg_id)
		{
		case MSG_ID_USB_FT_START_REQ:
			g_UsbMode.mode_param |= USB_MODE_FT_START_DONE;
			#ifdef __OTG_ENABLE__
				OTG_Mode_Selection();
			#else
				USB_Mode_Selection();
			#endif	
			break;
		case MSG_ID_NVRAM_READ_CNF:
			g_UsbMode.mode_param |= USB_MODE_NVRAM_READ_DONE;	
			/* store the string value got from NVRAM */
			USB_Store_Serial_Value(&current_ilm); /* store in gUsbDevice.serial_string   */
			
				#ifdef __OTG_ENABLE__
				OTG_Mode_Selection();
			#else
				USB_Mode_Selection();
			#endif		
			break;
			
		case MSG_ID_BMT_USB_READ_CALI_DONE_IND:
			g_UsbMode.mode_param |= USB_MODE_BMT_READ_DONE;	
			
			#ifdef __OTG_ENABLE__
				OTG_Mode_Selection();
			#else
				USB_Mode_Selection();
			#endif		
			break;

		case MSG_ID_USB_A_PLUGIN_IND:
			#ifdef __OTG_ENABLE__
				if((OTG_Get_Plug_Type()==OTG_PLUG_A)&&(g_UsbMode.cable_type == USB_MODE_CABLE_PLUGOUT))
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_PLUGOUT;
					g_UsbMode.cable_type = USB_MODE_CABLE_A_PLUGIN;
					OTG_Mode_Selection();
				}	
			#else
				EXT_ASSERT(0, 0, 0, 0);
			#endif
			break;
			
		case MSG_ID_USB_B_PLUGIN_IND:
			#ifdef __OTG_ENABLE__
				if((OTG_Get_Plug_Type()==OTG_PLUG_B)&&(g_UsbMode.cable_type == USB_MODE_CABLE_PLUGOUT))
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_PLUGOUT;
					g_UsbMode.cable_type = USB_MODE_CABLE_B_PLUGIN;
					OTG_Mode_Selection();
				}
			#else
				if(g_UsbMode.cable_type == USB_MODE_CABLE_PLUGOUT)
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_PLUGOUT;
					g_UsbMode.cable_type = USB_MODE_CABLE_B_PLUGIN;
					USB_Mode_Selection();
				}
			#endif		
			break;

		case MSG_ID_USB_A_PLUGOUT_IND:
			#ifdef __OTG_ENABLE__	
				if(g_UsbMode.cable_type == USB_MODE_CABLE_A_PLUGIN)
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_A_PLUGIN;
					g_UsbMode.cable_type = USB_MODE_CABLE_PLUGOUT;
					OTG_Mode_Selection();
				}		
			#else
				EXT_ASSERT(0, 0, 0, 0);
			#endif
			break;

		case MSG_ID_USB_B_PLUGOUT_IND:
			#ifdef __OTG_ENABLE__	
				if(g_UsbMode.cable_type == USB_MODE_CABLE_B_PLUGIN)
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_B_PLUGIN;
					g_UsbMode.cable_type = USB_MODE_CABLE_PLUGOUT;
					OTG_B_Set_Session_Valid(KAL_FALSE);
					OTG_Mode_Selection();
				}	
			#else
				if(g_UsbMode.cable_type == USB_MODE_CABLE_B_PLUGIN)
				{
					g_UsbMode.previous_cable_type = USB_MODE_CABLE_B_PLUGIN;
					g_UsbMode.cable_type = USB_MODE_CABLE_PLUGOUT;
					USB_Mode_Selection();
				}
			#endif
			break;

		case MSG_ID_DRVUEM_USBCFG_REQ:
			usbcfg=(drvuem_usbcfg_req_struct *)current_ilm.local_para_ptr;
			#ifdef __OTG_ENABLE__
				if(g_UsbMode.b_start_hnp==KAL_TRUE)
				{
					OTG_Config_HNP(usbcfg->mode, usbcfg->parameter);
				}
				else if (g_UsbMode.b_start_srp==KAL_TRUE)
				{
					/* For SRP function all the software resource initialization should be configured before SRP,
					    After A device provide VBUS, B device should connect D+ within 1 sec*/
					OTG_Config_SRP(usbcfg->mode, usbcfg->parameter);
				}
				else
				{
					/* config request from MMI*/
					if(g_UsbMode.cable_type != USB_MODE_CABLE_PLUGOUT)
					{
						USB_Config_Type(usbcfg->mode, KAL_TRUE, usbcfg->parameter);			
						USB_Send_Msg(USB_SEND_MSG_CONFIG_CNF, usbcfg->mode, KAL_TRUE);						
					}
					else
						USB_Send_Msg(USB_SEND_MSG_CONFIG_CNF, usbcfg->mode, KAL_FALSE);
				}
			#else /* __OTG_ENABLE__ */				
				/* config request from MMI*/
				if(g_UsbMode.cable_type != USB_MODE_CABLE_PLUGOUT)
				{
					//USB_Config_Type(usbcfg->mode, KAL_TRUE, (kal_uint32*)usbcfg->reserved);
					USB_Config_Type(usbcfg->mode, KAL_TRUE, usbcfg->parameter);
					USB_Send_Msg(USB_SEND_MSG_CONFIG_CNF, usbcfg->mode, KAL_TRUE);						
				}
				else
					USB_Send_Msg(USB_SEND_MSG_CONFIG_CNF, usbcfg->mode, KAL_FALSE);
			#endif /* __OTG_ENABLE__ */	
			break;

		case MSG_ID_OTG_HDLR_IND:
			#ifdef __OTG_ENABLE__	
			{
				otg_hdr_ind_struct *hdlr_ind=(otg_hdr_ind_struct *)current_ilm.local_para_ptr;
				OTG_Hdlr_Select(hdlr_ind->type, hdlr_ind->parameter, hdlr_ind->b_param);
			}
			#else
				EXT_ASSERT(0, 0, 0, 0);
			#endif		
			break;

		case MSG_ID_USBD_START_HNP:
			#ifdef __OTG_ENABLE__	
				#ifdef OTG_SRP_HNP_APP_FIXED
					OTG_Config_HNP(USB_CDC_ACM, NULL);
				#else	
					g_UsbMode.b_start_hnp = KAL_TRUE;
					/* Notify UEM*/
					USB_Send_Msg(USB_SEND_MSG_PLUG_IN, 0, KAL_TRUE);
				#endif
			#else
				EXT_ASSERT(0, 0, 0, 0);
			#endif		
			break;

		// add for SRP test, In real case, this should check OTG status first
		case MSG_ID_SRP_INIT:	
			#ifdef __OTG_ENABLE__
				if(	(OTG_Get_Plug_Type()==OTG_PLUG_B) && 
					((OTG_Get_State()==OTG_STATE_START) || (OTG_Get_State()==OTG_STATE_B_IDLE)))

⌨️ 快捷键说明

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