📄 usb_mode.c
字号:
USB_Init(USB_CDC_ACM, b_enable);
}
else if (type == USB_MASS_STORAGE)
{
#ifdef __USB_IN_NORMAL_MODE__
if(kal_if_hisr()==KAL_FALSE)
{
USB_Init_Nomal_Mode_Ms(parameter, b_enable);
}
else
{
#ifdef __OTG_ENABLE__
OTG_Hdlr_Send_Msg(OTG_HDLR_TASK_INIT_NOMAL_MS, parameter, b_enable);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
#endif
}
#ifdef WEBCAM_SUPPORT
else if ((type == USB_VIDEO) && (gUsbDevice.device_type != USB_VIDEO))
{
USB_Register_CreateFunc("USBVIDEO VC", USBVideo_VC_If_Create,USBVideo_VC_If_Reset);
USB_Register_CreateFunc("USBVIDEO VS", USBVideo_VS_If_Create,USBVideo_VS_If_Reset);
USB_Register_Device_Code(USBVIDEO_DEVICE_CODE, USBVIDEO_SUBCLASS_CODE,
USBVIDEO_PROTOCOL_CODE, g_USBVideo.custom_param->desc_product);
USB_Init(USB_VIDEO, b_enable);
}
#endif /* WEBCAM_SUPPORT */
else if (type==USB_STOP_MS)
{
#ifdef __NVRAM_IN_USB_MS__
/* This can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
/* Prevent MS mode still communicate with PC before handset power down */
gUsbDevice.device_type = USB_UNKOWN;
USB_PDNmode(KAL_TRUE); /* power down USB, disable D+ pull high */
USB_Free_DMA_Channel(g_UsbMS.txpipe->byEP);
USB_Release_Ms_Status();
USB_End_Ms();
#endif
}
else
{
EXT_ASSERT(0, type, gUsbDevice.device_type, 0);
}
}
/* The enable parameter decides whether to turn off D+ at this time */
void USB_Release_Type(kal_bool b_enable, kal_bool b_plugout)
{
USB_DEVICE_TYPE device_type;
device_type = gUsbDevice.device_type;
/* note that device type should be set to unknown here,
incase uart owner use other uart functions*/
gUsbDevice.device_type = USB_UNKOWN;
if(b_enable==KAL_TRUE)
{
/* power down USB */
USB_PDNmode(KAL_TRUE); /* disable D+ pull high */
}
/* stop and release resource*/
if (device_type == USB_MASS_STORAGE)
{
USB_Free_DMA_Channel(g_UsbMS.txpipe->byEP);
USB_Release_Ms_Status();
if(INT_USBBoot()==KAL_FALSE) /* normal mode */
{
#ifdef __USB_IN_NORMAL_MODE__
if(kal_if_hisr()==KAL_FALSE)
{
USB_End_Nomal_Mode_Ms();
}
else
{
#ifdef __OTG_ENABLE__
OTG_Hdlr_Send_Msg(OTG_HDLR_TASK_END_NORMAL_MS, 0, KAL_FALSE);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
else
{
#ifdef __NVRAM_IN_USB_MS__
if(kal_if_hisr()==KAL_FALSE)
{
USB_End_Ms();
}
else
{
#ifdef __OTG_ENABLE__
OTG_Hdlr_Send_Msg(OTG_HDLR_TASK_END_MS, 0, KAL_FALSE);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
#endif
}
}
else if(device_type == USB_CDC_ACM)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner == USB_ACM_OWNER_DSP)
{
USB_DSPIRDBG_Stop();
}
else
#endif /* __DSPIRDBG__*/
{
/* notify UART owner that virtual UART cable is plug out*/
/* Note that this should be called before release ACM status */
if((g_UsbACM.send_UARTilm == KAL_TRUE) && (USB2UARTPort.ownerid != MOD_DRV_HISR))
{
USB_Send_Msg(USB_SEND_MSG_UART_PLUG_OUT, 0, KAL_TRUE);
}
}
USB_Free_DMA_Channel(g_UsbACM.txpipe->byEP);
/* release global variable g_UsbACM resource*/
USB_Release_Acm_Status();
}
#ifdef WEBCAM_SUPPORT
else if(device_type == USB_VIDEO)
{
if(g_UsbMode.cable_type==USB_MODE_CABLE_PLUGOUT)/*real plug out, not these two messages result in*/
{
/* Update video state machine */
USBVideo_Plug_Out_Hdlr();
}
USB_Free_DMA_Channel(g_USBVideo.txpipe->byEP);
USB_Release_Video_Status();
}
#endif /* WEBCAM_SUPPORT */
if(device_type!=USB_UNKOWN)
{
/* If cable is plug out second time in mass storage mode, do not send these msgs */
USB_DeRegister_CreateFunc();
USB_DeRegister_Device_Code();
/* release global variable gUsbDevice resource*/
USB_Release_Device_Status();
}
if(b_plugout==KAL_TRUE)
{
USB_Release();
/*Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_OUT, 0, KAL_TRUE);
}
}
#ifdef __OTG_ENABLE__
static void OTG_Host_Stop_Hdlr(void)
{
USBD_Detach(USB_HCD_DETATCH, 0);
if(g_UsbMode.b_start_hnp == KAL_TRUE)
{
/*Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_OUT, 0, KAL_TRUE);
}
return;
}
void OTG_Hdlr_Select(OTG_HDLR_TASK_TYPE type, kal_uint32 *parameter, kal_bool b_param)
{
switch(type)
{
case OTG_HDLR_TASK_INIT_MS:
USB_Init_Ms(g_UsbMS.b_ms_first_plugin);
g_UsbMS.b_ms_first_plugin = KAL_FALSE;
break;
case OTG_HDLR_TASK_END_MS:
USB_End_Ms();
break;
case OTG_HDLR_TASK_INIT_NOMAL_MS:
USB_Init_Nomal_Mode_Ms(parameter, b_param);
break;
case OTG_HDLR_TASK_END_NORMAL_MS:
USB_End_Nomal_Mode_Ms();
break;
default:
break;
}
}
#endif
/************************************************************
mass storage mode functions
*************************************************************/
#ifdef __USB_IN_NORMAL_MODE__
static FS_DEVICE_TYPE_ENUM USB_Ms_Dev_To_Fs_Type(USB_STORAGE_DEV_TYPE type)
{
FS_DEVICE_TYPE_ENUM fs_type;
switch(type)
{
case USB_STORAGE_DEV_NOR:
fs_type = FS_DEVICE_TYPE_NOR;
break;
case USB_STORAGE_DEV_NAND:
fs_type = FS_DEVICE_TYPE_NAND;
break;
case USB_STORAGE_DEV_CARD:
fs_type = FS_DEVICE_TYPE_CARD;
break;
default:
EXT_ASSERT(0, type, 0, 0);
break;
}
return fs_type;
}
#endif
void USB_Init_Nomal_Mode_Ms(kal_uint32* mem_addr, kal_bool b_enable)
{
#ifdef __USB_IN_NORMAL_MODE__
kal_int32 index;
USB_STORAGE_DEV_TYPE dev_type;
FS_DEVICE_TYPE_ENUM fs_type;
kal_int32 result;
kal_uint8 max_lun = USB_Ms_Get_Max_LUN();
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
/* get disk buffer */
g_UsbMS.disk_buffer = (void*)mem_addr;
/* lock file system */
FS_LockFAT(FS_EXPORT_ENUM);
/* query whether each device can be exported or not (system driver disk)*/
for(index = max_lun; index >= 0; index--)
{
/* get each device type and translate to file system type */
dev_type = USB_Ms_Dev_Type(index);
fs_type = USB_Ms_Dev_To_Fs_Type(dev_type);
/* query whether it can be exported or not */
result = FS_GetDevStatus(fs_type, FS_EXPORT_STATE_ENUM);
if(result==FS_NO_ERROR)
{
/* Means this is system drive */
USB_Ms_DeRegister_DiskDriver(index);
}
else if(result != FS_DEVICE_EXPORTED_ERROR)
{
EXT_ASSERT(0, result, dev_type, fs_type);
}
}
/* Initialize storage function */
USB_Register_CreateFunc("MASS STORAGE", USB_Ms_If_Create, USB_Ms_If_Reset);
USB_Register_Device_Code(USB_MS_DEVICE_CODE, USB_MS_SUBCLASS_CODE,
USB_MS_PROTOCOL_CODE, g_UsbMS.ms_param->desc_product);
/* initialize MASS STORAGE MODE */
USB_Init(USB_MASS_STORAGE, b_enable);
#else /* __USB_IN_NORMAL_MODE__ */
EXT_ASSERT(0, 0, 0, 0);
#endif
}
void USB_End_Nomal_Mode_Ms(void)
{
#ifdef __USB_IN_NORMAL_MODE__
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
FS_LockFAT(FS_REMOUNT_ENUM);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
void USB_Init_Ms(kal_bool bFirst)
{
#ifdef __P_PROPRIETARY_COPYRIGHT__
kal_int32 fs_ret = 0;
#endif
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
#ifdef __NVRAM_IN_USB_MS__
/* Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_IN, 0, KAL_TRUE);
#else
if(bFirst)
{
/* Notify UEM*/
USB_Send_Msg(USB_SEND_MSG_PLUG_IN, 0, KAL_TRUE);
}
#endif
/* If not first time plug in, only notify UEM*/
if(bFirst)
{
/* get unique string from nvram and construct serial string*/
USB_Get_Serial_Value();
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* Note that all NVRAM actions should be done before the following checking*/
/* delete protected files in not audio directly*/
fs_ret = FS_SweepCopyrightFile();
/* In these errors, it means protect folder can not be created*/
if((fs_ret==FS_FAT_ALLOC_ERROR)||(fs_ret==FS_ROOT_DIR_FULL)||(fs_ret==FS_DISK_FULL))
{
g_UsbMS.read_protect_dev_type = FS_DEVICE_TYPE_NAND;
g_UsbMS.read_protect_items[0] = 0;
}
else
{
/* USB mass sotrage mode. Get read protect array from file system */
fs_ret = FS_GetCopyrightList();
if((fs_ret<=0)||(g_UsbMS.read_protect_items==NULL)||(g_UsbMS.read_protect_second_items==NULL))
EXT_ASSERT(0, fs_ret, (kal_uint32)g_UsbMS.read_protect_items, (kal_uint32)g_UsbMS.read_protect_second_items);
g_UsbMS.read_protect_dev_type = USB_Ms_Trans_Fs_Type((FS_DEVICE_TYPE_ENUM)fs_ret);
/* Check protect items can not be more than sorting buffer (read and write buffer)*/
if(g_UsbMS.read_protect_items[0] > g_UsbMS.read_protect_num)
EXT_ASSERT(0, g_UsbMS.read_protect_items[0], g_UsbMS.read_protect_num, 0);
}
USB_Ms_Sort_Protect_Items(g_UsbMS.read_protect_items, g_UsbMS.read_protect_second_items);
USB_Send_Msg(USB_SEND_MSG_MS_INIT_DONE, 0, KAL_TRUE);
#endif
/* must lock file system before turn on USB power */
FS_LockFAT(FS_LOCK_USB_ENUM); //lock file system in ms mode
USB_Register_CreateFunc("MASS STORAGE", USB_Ms_If_Create, USB_Ms_If_Reset);
USB_Register_Device_Code(USB_MS_DEVICE_CODE, USB_MS_SUBCLASS_CODE,
USB_MS_PROTOCOL_CODE, g_UsbMS.ms_param->desc_product);
/* initialize MASS STORAGE MODE */
USB_Init(USB_MASS_STORAGE, KAL_TRUE);
}
}
void USB_End_Ms(void)
{
#ifdef __NVRAM_IN_USB_MS__
/* This function can not be executed in HISR*/
if(kal_if_hisr()==KAL_TRUE)
EXT_ASSERT(0, 0, 0, 0);
FS_LockFAT(FS_UNLOCK_USB_ENUM);
#else
EXT_ASSERT(0, 0, 0, 0);
#endif
}
void USB_Init_ACM_In_Meta(void)
{
static kal_bool b_init_acm_meta = KAL_FALSE;
if(b_init_acm_meta==KAL_FALSE)
{
b_init_acm_meta = KAL_TRUE;
/* init ft acm parameters*/
USB_Acm_FT_Init();
/* initailize CDC_ACM */
USB_Register_CreateFunc("ACM DATA", USB_Acm_DataIf_Create,USB_Acm_DataIf_Reset);
USB_Register_CreateFunc("ACM COMMU.", USB_Acm_CommIf_Create,USB_Acm_CommIf_Reset);
USB_Register_Device_Code(USB_ACM_DEVICE_CODE, USB_ACM_SUBCLASS_CODE,
USB_ACM_PROTOCOL_CODE, g_UsbACM.acm_param->desc_product);
USB_Init(USB_CDC_ACM, KAL_TRUE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -