📄 usb_host_ms_state.c
字号:
/*****************************************************************************
* 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) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* usb_host_ms_state.c
*
* Project:
* --------
* Maui_Software
*
* Description:
* ------------
* This file implements host mass storage class state machine
*
* 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!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#include "drv_comm.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h" /* Task message communiction */
#include "gpt_sw.h"
#include "usb_comm.h"
#include "usb.h"
#include "usbd.h"
#include "otg_drv.h"
#include "otg.h"
#include "usbms_state.h"
#include "usb_host_ms_drv.h"
#include "usb_host_ms_state.h"
#ifdef __OTG_ENABLE__
static void USB_Host_Ms_State_Timeout(void *parameter);
static void USB_Host_MS_Query_Media(kal_uint8 lun);
static void USB_Host_MS_Check_Media(kal_uint8 lun);
static kal_bool USB_Host_Ms_Error_Check(void);
static void USB_Host_Ms_Error_Handler(void);
static kal_bool USB_Host_Ms_Comp_Get_LUN(void);
static kal_bool USB_Host_Ms_Query_All_Media(void);
static kal_bool USB_Host_Ms_Check_All_Media(void);
/************************************************************
ms class state functions. Matintain the state of each LUN
*************************************************************/
/* Query media info for lun parameter */
/* This query function should be called when first detect the lun plug in */
static void USB_Host_MS_Query_Media(kal_uint8 lun)
{
USB_HOST_MS_RESULT result;
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_READY;
/* Send INQUIRY request to device */
result = USB_Host_Ms_Inquiry(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
/* Send READ FORMAT CAPACITY request to device */
result = USB_Host_Ms_Read_Format_Capacity(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
/* Send READ CAPACITY request to device */
result = USB_Host_Ms_Read_Capacity(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
}
/* Check media info for lun parameter */
/* This check function should be called periodically when slot is still attched */
static void USB_Host_MS_Check_Media(kal_uint8 lun)
{
USB_HOST_MS_RESULT result;
kal_bool b_state_change = KAL_FALSE;
USB_HOST_MS_MEDIA_STATE media_state = g_UsbHostMs.media_info[lun].state;
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_READY;
/* Send TEST UNIT READY request to device */
result = USB_Host_Ms_Test_Unit_Ready(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
/* state may change from READY->CHANGED at this step, and CHANGED->READY at next step */
if(media_state!=g_UsbHostMs.media_info[lun].state)
b_state_change = KAL_TRUE;
if(g_UsbHostMs.media_info[lun].state==USB_HOST_MS_MEDIA_STATE_CHANGED)
{
/* If card is plug in in caed reader, the previous state would be state changed, so query state again*/
/* If this request finds media state cahnge to READY, then capacity can be queried in the following step directly */
result = USB_Host_Ms_Test_Unit_Ready(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
}
if(media_state!=g_UsbHostMs.media_info[lun].state)
b_state_change = KAL_TRUE;
if((b_state_change==KAL_TRUE) && (g_UsbHostMs.media_info[lun].state==USB_HOST_MS_MEDIA_STATE_READY))
{
/* state cahnge from NON_READY to READY*/
/* Send READ CAPACITY request to device */
result = USB_Host_Ms_Read_Capacity(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
if(result!=USB_HOST_MS_RESULT_FORMAT_ERROR)
{
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
}
return;
}
/* Send MODE SENSE6 request to device. Query whether the device is write protected or not */
result = USB_Host_Ms_Mode_Sense6(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
if(g_UsbHostMs.media_info[lun].state == USB_HOST_MS_MEDIA_STATE_ERROR)
{
/* Send READ CAPACITY request to device */
result = USB_Host_Ms_Read_Capacity(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
if(result!=USB_HOST_MS_RESULT_FORMAT_ERROR)
{
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
}
return;
}
/* Try all mode again */
result = USB_Host_Ms_Mode_Sense6_All_Mode(lun);
if(result!=USB_HOST_MS_RESULT_OK)
{
/* result not OK means CBW, DATA, CSW transfer is error after its internal error handling */
g_UsbHostMs.dev_state = USB_HOST_MS_DEV_STATE_ERROR;
return;
}
}
}
}
/* Return true means error happens*/
/* In this function, try to issue ms class specific reset command and see if error count exceed limit */
static kal_bool USB_Host_Ms_Error_Check(void)
{
while(g_UsbHostMs.dev_error_count<3)
{
/* At most reset 3 times */
USB_Host_Ms_Reset();
if(g_UsbHostMs.dev_state==USB_HOST_MS_DEV_STATE_ERROR)
g_UsbHostMs.dev_error_count++;
else
break;
}
if((g_UsbHostMs.dev_error_count>=3) ||(g_UsbHostMs.dev_attatch==KAL_FALSE))
return KAL_TRUE;
else
return KAL_FALSE;
}
/* Serious error happens and it can not be recovered from error handling mechanism in ms class */
/* Stop the MS action and turn off OTG power */
static void USB_Host_Ms_Error_Handler(void)
{
kal_uint32 index;
GPTI_StopItem(g_UsbHostMs.gpt_handle);
for(index=0; index<g_UsbHostMs.total_lun; index++)
{
/* If original state is READY, set state change as true */
if(((g_UsbHostMs.media_info[index].state==USB_HOST_MS_MEDIA_STATE_WR_PROTECT)
||(g_UsbHostMs.media_info[index].state==USB_HOST_MS_MEDIA_STATE_READY))
&& (g_UsbHostMs.media_info[index].sec_size!=0))
{
//g_UsbHostMs.b_state_change = KAL_TRUE;
g_UsbHostMs.media_state_change |= (1<<index);
}
/* Set all the media state to error */
g_UsbHostMs.media_info[index].state = USB_HOST_MS_MEDIA_STATE_ERROR;
}
/* Stop OTG action */
if(OTG_Get_Plug_Type()==OTG_PLUG_A)
{
OTG_A_Stop_Host();
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -