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

📄 usb_host_ms_state.c

📁 MTK平台绝密核心代码之 USB驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
		OTG_B_Stop_Host(); 
	}

	/* Notify FMT about media change */
	//if(g_UsbHostMs.b_state_change==KAL_TRUE)
	//{
	//	g_UsbHostMs.b_state_change = KAL_FALSE;
	//	USB_Host_Ms_SendInd(USB_HOST_MS_MSG_MEDIA_CHANGE);
	//}

	if(g_UsbHostMs.dev_attatch==KAL_TRUE)
	{
		if((g_UsbHostMs.media_state_change != 0) && (g_UsbHostMs.b_sending_change_ilm == KAL_FALSE))
		{
			for(index=0; index<g_UsbHostMs.total_lun; index++)
			{
				if((g_UsbHostMs.media_state_change&(1<<index))!=0)
					g_UsbHostMs.media_msg_array[index] = (kal_uint32)&g_UsbHostMs.media_handle[index];
				else
					g_UsbHostMs.media_msg_array[index] = NULL;
			}
			g_UsbHostMs.media_state_change = 0;
			USB_Host_Ms_SendInd(USB_HOST_MS_MSG_MEDIA_CHANGE);
		}
	
		OTG_Display_Message(OTG_DISPLAY_MSG_UN_SUPPORT);
	}	
}	

/************************************************************
	ms class state functions. Matintain the state of all LUN 
*************************************************************/

/* Get device LUN. It contains the total error handling. 
     If result is fail, the host function is turned off in this function */
static kal_bool USB_Host_Ms_Comp_Get_LUN(void)
{
	/* Send class specific GET_LUN EP0 command to device*/
	USB_Host_Ms_Get_LUN();
	if(g_UsbHostMs.dev_state==USB_HOST_MS_DEV_STATE_ERROR)
	{
		g_UsbHostMs.dev_error_count++;
		if(USB_Host_Ms_Error_Check()==KAL_TRUE)
		{
			/* It means reset is no use or error count exceed limit */
			USB_Host_Ms_Error_Handler();
		}
		else
		{
			/* reset is complete, so GET_LUN again */
			/* Error handling is in the recursive function */
			USB_Host_Ms_Comp_Get_LUN();
		}
	}


	if((g_UsbHostMs.dev_error_count>=3) ||(g_UsbHostMs.dev_attatch==KAL_FALSE))
	{
		/* Error count exceed limit, host function has been turned off in error handler */
		return KAL_FALSE;
	}
	else
	{
		/* Reset error count. Avoid error count propagete */
		g_UsbHostMs.dev_error_count = 0;
		return KAL_TRUE;
	}		
}


/* Query all LUN media. It contains the total error handling. 
     If result is fail, the host function is turned off in this function */
static kal_bool USB_Host_Ms_Query_All_Media(void)
{
	kal_uint32 index;
	kal_uint32 index2;
	for(index = 0; index < g_UsbHostMs.total_lun; index++)
	{
		/* query the dedicated LUN*/
		USB_Host_MS_Query_Media(index);
		if(g_UsbHostMs.dev_state==USB_HOST_MS_DEV_STATE_ERROR)
		{
			g_UsbHostMs.dev_error_count++;

			/* reset all media state */
			for(index2 = 0; index2 < g_UsbHostMs.total_lun; index2++)
				g_UsbHostMs.media_info[index2].state = USB_HOST_MS_MEDIA_STATE_ABSENT;
			
			if(USB_Host_Ms_Error_Check()==KAL_TRUE)
			{
				/* It means reset is no use or error count exceed limit */
				USB_Host_Ms_Error_Handler();
				break;
			}
			else
			{
				/* reset is complete, so Query all media again */
				/* Error handling is in the recursive function */				
				USB_Host_Ms_Query_All_Media();
				break;
			}
		}
	}

	if((g_UsbHostMs.dev_error_count>=3)||(g_UsbHostMs.dev_attatch==KAL_FALSE))
	{
		/* Error count exceed limit, host function has been turned off in error handler */
		return KAL_FALSE;
	}
	else
	{
		/* Reset error count. Avoid error count propagete */
		g_UsbHostMs.dev_error_count = 0;
		return KAL_TRUE;
	}	
}

/* Query all LUN media. It contains the total error handling. 
     If result is fail, the host function is turned off in this function */
static kal_bool USB_Host_Ms_Check_All_Media(void)
{
	kal_uint32 index;
	kal_uint32 index2; 
	for(index = 0; index < g_UsbHostMs.total_lun; index++)
	{
		/* Note that task and give the mutex in lun level. To reduce the latency in application read and write */
		kal_take_mutex(g_UsbHostMs.mutex_id);
		/* check the dedicated LUN*/
		USB_Host_MS_Check_Media(index);
		if(g_UsbHostMs.dev_state==USB_HOST_MS_DEV_STATE_ERROR)
		{
			g_UsbHostMs.dev_error_count++;

			/* reset all media state */
			for(index2 = 0; index2 < g_UsbHostMs.total_lun; index2++)
				g_UsbHostMs.media_info[index2].state = USB_HOST_MS_MEDIA_STATE_ABSENT;

			/* should modify to dev error, reset device */
			if(USB_Host_Ms_Error_Check()==KAL_TRUE)
			{
				/* It means reset is no use or error count exceed limit */
				USB_Host_Ms_Error_Handler();
				kal_give_mutex(g_UsbHostMs.mutex_id);
				break;
			}
			else
			{
				/* reset is complete, so check all media again */
				/* Error handling is in the recursive function */	
				kal_give_mutex(g_UsbHostMs.mutex_id);
				USB_Host_Ms_Check_All_Media();
				break;
			}
		}
		else
			kal_give_mutex(g_UsbHostMs.mutex_id);
	}	

	if((g_UsbHostMs.dev_error_count>=3) ||(g_UsbHostMs.dev_attatch==KAL_FALSE) )
	{
		/* Error count exceed limit, host function has been turned off in error handler */
		return KAL_FALSE;
	}
	else
	{
		/* Reset error count. Avoid error count propagete */
		g_UsbHostMs.dev_error_count = 0;
		return KAL_TRUE;
	}	
}


/************************************************************
	state machine entry functions 
*************************************************************/

/* GPT timeout function*/
/* This GPT timer is used to periodically checking media status*/
static void USB_Host_Ms_State_Timeout(void *parameter)
{
	ilm_struct *usb_ilm;
	
	GPTI_StopItem(g_UsbHostMs.gpt_handle);
	
	/* Send msg to USB task, in order to start ms state machine */
	DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,
					MOD_USB, MSG_ID_USB_MSHOST_QUERY_IND, NULL);
	msg_send_ext_queue(usb_ilm);
}


/* Start ms state. Initialize state info and query each slot info */
/* Note that this function should be called when first detect ms device*/
void USB_Host_Ms_State_Start(void)
{
	kal_uint32 index;
	kal_bool result;
	
	/* Initailize the information */
	if(g_UsbHostMs.event_id==0)
		g_UsbHostMs.event_id = kal_create_event_group("USB_MS_EVEVT");

	if(g_UsbHostMs.mutex_id==0)
		g_UsbHostMs.mutex_id = kal_create_mutex("USB_MS_MUTEX");
			
	if (g_UsbHostMs.gpt_handle == 0)
		GPTI_GetHandle(&g_UsbHostMs.gpt_handle);

	g_UsbHostMs.total_lun=0;

	/* Get LUN, it contains the error handling */
	result = USB_Host_Ms_Comp_Get_LUN();
	/* If result is fail, ms has been detached*/
	if(result==KAL_TRUE)
	{
		/* The got LUN is total lun - 1 */
		g_UsbHostMs.total_lun++;
		/* If LUN number is larger than we can support, onlt the first N can be detected */
		if(g_UsbHostMs.total_lun>g_UsbHostMs.support_lun)
			g_UsbHostMs.total_lun = g_UsbHostMs.support_lun;
		/* Set all the LUN state to absent state */
		for(index = 0; index<g_UsbHostMs.total_lun; index++)
			g_UsbHostMs.media_info[index].state = USB_HOST_MS_MEDIA_STATE_ABSENT;
	}	
	
	if(result==KAL_TRUE)
	{
		/* Get LUN is succuss, continue to query each LUN info*/
		result = USB_Host_Ms_Query_All_Media();
		/* If result is fail, ms has been detached*/
	}	
	
	if(result==KAL_TRUE)
	{
		/* Query each LUN is succuss, continue to check whether each LUN is in card slot or not*/
		result = USB_Host_Ms_Check_All_Media();
		/* If result is fail, ms has been detached*/
	}	

	if( (result==KAL_TRUE) && (g_UsbHostMs.media_state_change != 0) 
		&& (g_UsbHostMs.b_sending_change_ilm == KAL_FALSE))
	{
		/* Send message to FMT task about card state change */
		for(index=0; index<g_UsbHostMs.total_lun; index++)
		{
			if((g_UsbHostMs.media_state_change&(1<<index))!=0)
				g_UsbHostMs.media_msg_array[index] = (kal_uint32)&g_UsbHostMs.media_handle[index];
			else
				g_UsbHostMs.media_msg_array[index] = NULL;
		}
		g_UsbHostMs.media_state_change = 0;
		USB_Host_Ms_SendInd(USB_HOST_MS_MSG_MEDIA_CHANGE);
	}


	if(result==KAL_TRUE)
	{
		/* start timer to query device state */
		GPTI_StartItem(g_UsbHostMs.gpt_handle, USB_HOST_MS_QUERY_PERIOD,
					USB_Host_Ms_State_Timeout, &g_UsbHostMs);
	}	

}

/* ms class main state machine of each LUN */
/* Note that this is called periodically when ms is active */
void USB_Host_Ms_State_Main(void)
{
	kal_uint8 index;
	kal_bool result;

	result = USB_Host_Ms_Check_All_Media();	
	/* If result is fail, ms has been detached*/
		
	if( (result==KAL_TRUE) && (g_UsbHostMs.media_state_change != 0) 
		&& (g_UsbHostMs.b_sending_change_ilm == KAL_FALSE))
	{
		/* Send message to FMT task about card state change */
		for(index=0; index<g_UsbHostMs.total_lun; index++)
		{
			if((g_UsbHostMs.media_state_change&(1<<index))!=0)
				g_UsbHostMs.media_msg_array[index] = (kal_uint32)&g_UsbHostMs.media_handle[index];
			else
				g_UsbHostMs.media_msg_array[index] = NULL;
		}
		g_UsbHostMs.media_state_change = 0;
		USB_Host_Ms_SendInd(USB_HOST_MS_MSG_MEDIA_CHANGE);
	}

	if(result==KAL_TRUE)
	{
		/* start timer to start query device state */
		GPTI_StartItem(g_UsbHostMs.gpt_handle, USB_HOST_MS_QUERY_PERIOD,
					USB_Host_Ms_State_Timeout, &g_UsbHostMs);
	}	
}

#endif  /* __OTG_ENABLE__ */
 

⌨️ 快捷键说明

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