📄 usb.c
字号:
}
void USB_Register_EP0_Class_CmdHdlr(kal_uint8 cmd, usb_ep0_cmd_ptr handler)
{
gUsbDevice.ep0_class_cmd_handler.cmd = cmd;
gUsbDevice.ep0_class_cmd_handler.ep0_cmd_hdlr = handler;
gUsbDevice.ep0_class_cmd_handler.b_enable = KAL_TRUE;
}
/* prepare TX data infomration for pep0state, data is actually sent out in TX state handler */
void USB_Generate_EP0Data(Usb_Ep0_Status*pep0state, Usb_Command*pcmd, void *data, kal_int32 len)
{
pep0state->nBytesLeft = len;
pep0state->pData = (void*)data;
/* only transmit at most command request */
if (pcmd->wLength < pep0state->nBytesLeft)
pep0state->nBytesLeft = pcmd->wLength;
/* EP0 go to TX state */
gUsbDevice.ep0_state = USB_EP0_TX;
}
/************************************************************
system ctrl functions
*************************************************************/
/* init function, called when user select usb type,
entry function for task , B_eanble is D+ detection enable */
void USB_Init(USB_DEVICE_TYPE type, kal_bool b_enable)
{
#ifndef __USB_ENABLE__
EXT_ASSERT(0, 0, 0, 0);
#endif
#if ( defined(MT6218) || defined(MT6218B) || defined(MT6219) )
#ifndef MCU_52M
EXT_ASSERT(0, 0, 0, 0); /* Must 52M */
#endif /* MCU_52M */
#endif /* MT6218||MT6218B||MT6219 */
USB_Init_Drv_Info(); /*initialize driver parameters*/
USB_Init_Device_Status(); /*initialize global variable gUsbDevice*/
if(type == USB_MASS_STORAGE)
{
USB_Init_Ms_Status();
}
else if(type == USB_CDC_ACM)
{
USB_Init_Acm_Status();
}
#ifdef WEBCAM_SUPPORT
else if(type == USB_VIDEO)
{
USB_Init_Video_Status();
}
#endif
else if(type == USB_COMPOSITE)
{
USB_Init_Ms_Status();
USB_Init_Acm_Status();
}
else
{
EXT_ASSERT(0, type, 0, 0);
}
gUsbDevice.device_type = type;
/* register reset and ep0 interrupt handler to driver info*/
USB_Register_Drv_Info(USB_DRV_HDLR_RESET, 0, USB_Reset);
USB_Register_Drv_Info(USB_DRV_HDLR_EP0, 0, USB_Endpoint0_Hdlr);
/* create descriptors */
USB_Software_Create();
#ifdef __USB_RAMDISK__
if (type != USB_CDC_ACM)
{
FAT_Init();
}
#endif /*__USB_RAMDISK__*/
if(b_enable==KAL_TRUE)
{
#ifdef __OTG_ENABLE__
OTG_Enable_Device();
#else
/* enable USB power, host can detect USB device*/
USB_PDNmode(KAL_FALSE);
#endif
}
/* reset and initialize system initial value and registers*/
USB_Reset_Drv(); /* reset hw power register */
USB_Initialize_Drv();
/* initailize according to application*/
USB_Initialize();
/* if __OTG_ENABLE__, only activate OTG ISR*/
#ifndef __OTG_ENABLE__
/* LISR should be unmask after all initialize and enable any USB interrupt */
USB_Drv_Activate_ISR();
#endif /* __OTG_ENABLE__ */
}
void USB_Release(void)
{
USB_Release_Drv();
}
/* reset device, called when receive reset interrupt*/
static void USB_Reset(void)
{
kal_uint8 ep_num;
gUsbDevice.is_configured_now = KAL_FALSE;
g_usb_reset_times++;
// initialize driver info and system interrupt
USB_ResetDrv_Info();
USB_Initialize_Drv();
USB_Initialize(); /* call class reset function */
for(ep_num=0;ep_num<gUsbDevice.resource_ep_bulkin_number;ep_num++)
{
if(gUsbDevice.ep_bulkin_info[ep_num].ep_reset)
gUsbDevice.ep_bulkin_info[ep_num].ep_reset();
}
for(ep_num=0;ep_num<gUsbDevice.resource_ep_bulkout_number;ep_num++)
{
if(gUsbDevice.ep_bulkout_info[ep_num].ep_reset)
gUsbDevice.ep_bulkout_info[ep_num].ep_reset();
}
for(ep_num=0;ep_num<gUsbDevice.resource_ep_intr_number;ep_num++)
{
if(gUsbDevice.ep_intr_info[ep_num].ep_reset)
gUsbDevice.ep_intr_info[ep_num].ep_reset();
}
#ifdef __OTG_ENABLE__
OTG_B_EnDis_HNP(KAL_FALSE);
#endif
}
/* initialize system */
static void USB_Initialize(void)
{
kal_uint8 index_ep;
gUsbDevice.nDevState = DEVSTATE_DEFAULT;
for( index_ep = 0; index_ep < gUsbDevice.resource_ep_bulkin_number; index_ep++)
{
USB_InEPInit((gUsbDevice.ep_bulkin_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)),
gUsbDevice.ep_bulkin_info[index_ep].epdesc.stdep.wMaxPacketSize[0], USB_ENDPT_BULK);
gUsbDevice.ep_bulkin_info[index_ep].ep_status.epin_status.nBytesLeft = USB_EP_NODATA;
}
for( index_ep = 0; index_ep < gUsbDevice.resource_ep_bulkout_number; index_ep++)
{
USB_OutEPInit((gUsbDevice.ep_bulkout_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)),
gUsbDevice.ep_bulkout_info[index_ep].epdesc.stdep.wMaxPacketSize[0], USB_ENDPT_BULK);
gUsbDevice.ep_bulkout_info[index_ep].ep_status.epout_status.nBuffLen = 0;
gUsbDevice.ep_bulkout_info[index_ep].ep_status.epout_status.nBytesRecv = 0;
}
for( index_ep = 0; index_ep < gUsbDevice.resource_ep_intr_number; index_ep++)
{
USB_InEPInit((gUsbDevice.ep_intr_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)),
gUsbDevice.ep_intr_info[index_ep].epdesc.stdep.wMaxPacketSize[0], USB_ENDPT_INTR);
gUsbDevice.ep_intr_info[index_ep].ep_status.epin_status.nBytesLeft = USB_EP_NODATA;
}
/* Clear current configuration pointer */
//gUsbDevice.pCurCfg = &gUsbDevice.cfg_info[0].stdcfg; /* pointer point to configuration descriptor */
gUsbDevice.self_powered = KAL_FALSE;
gUsbDevice.remoteWk = KAL_FALSE;
gUsbDevice.config_num = 0; /* set configuration command value */
gUsbDevice.interface_num = 0; /* set inferface command value */
gUsbDevice.ep0_state = USB_EP0_IDLE;
gUsbDevice.ep0info.byFAddr = 0xff; /* device (function) address, no use, at HW still set 0x00 */
/* initial class specific interface functions*/
USB_Software_Init();
}
/************************************************************
EP0 functions
*************************************************************/
/* parse command Set Address */
static kal_bool USB_Cmd_SetAddress(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
kal_bool bError = KAL_FALSE;
/* Store device function address until status stage of request */
if (pcmd->bmRequestType != USB_CMD_STDDEVOUT)
{
bError = KAL_TRUE;
}
else
{
if (gUsbDevice.nDevState <= DEVSTATE_ADDRESS)
{
pep0state->byFAddr = (kal_uint8)pcmd->wValue;
if ((gUsbDevice.nDevState == DEVSTATE_DEFAULT) && (pep0state->byFAddr<=127))
{
gUsbDevice.nDevState = DEVSTATE_SET_ADDRESS;
USB_SetAddress(pep0state->byFAddr, USB_SET_ADDR_DATA);
}
else
{
gUsbDevice.nDevState = DEVSTATE_DEFAULT;
}
}
else
{
bError = KAL_TRUE;
}
}
return bError;
}
/* parse command Get Descriptor */
static kal_bool USB_Cmd_GetDescriptor(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
kal_bool bError = KAL_FALSE;
kal_uint8 byConfig;
kal_uint8 bystr;
Usb_Cfg_Dscr *pcfg;
/* Decode the required descriptor from the command */
if (pcmd->bmRequestType != USB_CMD_STDDEVIN)
{
bError = KAL_TRUE;
}
else
{
switch (pcmd->wValue & USB_CMD_DESCMASK)
{
case USB_CMD_DEVICE:
/* Prepare to return Standard Device Descriptor */
USB_Generate_EP0Data(pep0state, pcmd, &gUsbDevice.devdscr, sizeof(Usb_Dev_Dscr));
break;
case USB_CMD_CONFIG:
byConfig = (kal_uint8)(pcmd->wValue & 0x00FF);
if (byConfig >= gUsbDevice.devdscr.bNumConfigurations)
{
bError = KAL_TRUE;
}
else
{
/* Get pointer to requested configuration descriptor */
pcfg = (Usb_Cfg_Dscr*)gUsbDevice.conf;
#ifdef __USB_MULTI_CHARGE_CURRENT__
#if ( defined(MT6305) || defined(MT6318) )
/* memory copy to modify descriptor */
kal_mem_cpy( (kal_uint8*) &(pcfg->bConfigurationValue),
(kal_uint8*) &gUsbDevice.multi_configuration_value[byConfig] ,
1);
kal_mem_cpy( (kal_uint8*) &(pcfg->bMaxPower),
(kal_uint8*) &gUsbDevice.multi_Max_Power[byConfig] ,
1);
#endif
#endif
/* Prepare to return Configuration Descriptors */
USB_Generate_EP0Data(pep0state, pcmd, pcfg, pcfg->wTotalLength);
}
break;
case USB_CMD_STRING:
bystr = (kal_uint8)(pcmd->wValue & 0x00FF);
if (bystr >= gUsbDevice.resource_string_number)
{
bError = KAL_TRUE;
}
else
{
/* Get pointer to requested string descriptor */
USB_Generate_EP0Data(pep0state, pcmd, (void *)gUsbDevice.resource_string[bystr],
(*(kal_uint8 *)gUsbDevice.resource_string[bystr]));
}
break;
default:
bError = KAL_TRUE;
break;
}
}
return bError;
}
/* parse command Set Configuration */
static kal_bool USB_Cmd_SetConfiguration(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
kal_bool bError = KAL_FALSE;
kal_uint8 byConfig;
ilm_struct *ilm;
/* add for usb charging */
ilm_struct *ilm_charge;
bmt_usb_ind_struct *ind;
#ifndef __USB_MULTI_CHARGE_CURRENT__
kal_bool bConfirm = KAL_FALSE;
#endif
/* uart plug in ind*/
uart_plugin_ind_struct *ilm_uart_plug_in;
byConfig = (kal_uint8)(pcmd->wValue & 0x00FF);
if (gUsbDevice.nDevState == DEVSTATE_DEFAULT)
{
bError = KAL_TRUE;
}
/* Assumes configurations are numbered from 1 to NumConfigurations */
else
{
if (byConfig > gUsbDevice.devdscr.bNumConfigurations)
{
bError = KAL_TRUE;
}
else if (byConfig == 0)
{
gUsbDevice.nDevState = DEVSTATE_ADDRESS;
gUsbDevice.config_num = byConfig;
}
else
{
gUsbDevice.nDevState = DEVSTATE_CONFIG;
gUsbDevice.is_configured_now = KAL_TRUE;
gUsbDevice.config_num = byConfig;
#ifndef __USB_MULTI_CHARGE_CURRENT__
bConfirm = KAL_TRUE;
#endif
}
}
if (INT_BootMode() != MTK_FACTORY_MODE)
{
/* add for usb charging */
ind = (bmt_usb_ind_struct*)construct_local_para(sizeof(bmt_usb_ind_struct), TD_CTRL);
#ifdef __USB_MULTI_CHARGE_CURRENT__
#if ( defined(MT6305) || defined(MT6318) )
if (byConfig ==0)
ind->usb_ind = (kal_uint8) (USB_DEVDSC_CONFIGS+1); /*usb not charging */
else
{
ind->usb_ind = (kal_uint8) (byConfig-1);
//ind->usb_ind = (USB_DEVDSC_CONFIGS+1); /* test with PC */
}
#else
ind->usb_ind = (byConfig ==0)?(USB_DEVDSC_CONFIGS+1):(byConfig-1);
#endif
#else
ind->usb_ind = (bConfirm == KAL_TRUE)?(BMT_USB_500):(BMT_USB_100);
#endif
DRV_BuildPrimitive( ilm_charge, MOD_DRV_HISR, MOD_BMT, MSG_ID_BMT_USB_IND, ind);
msg_send_ext_queue(ilm_charge);
}
else
{
/* meta mode, notify ft task */
DRV_BuildPrimitive(ilm, MOD_DRV_HISR, MOD_FT, MSG_ID_USB_FT_IND, NULL);
msg_send_ext_queue(ilm);
}
/* notify UART owner that virtual UART is plug in*/
if((gUsbDevice.nDevState == DEVSTATE_CONFIG) && (gUsbDevice.device_type == USB_CDC_ACM))
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner == USB_ACM_OWNER_DSP)
{
/* notify usb task to start IRDBG*/
DRV_BuildPrimitive(ilm, MOD_DRV_HISR, MOD_USB, MSG_ID_USB_IRDGB_START_IND, NULL);
msg_send_ext_queue(ilm);
}
else
#endif /* __DSPIRDBG__ */
{
if(USB2UARTPort.ownerid != MOD_DRV_HISR)
{
ilm_uart_plug_in = (uart_plugin_ind_struct*)construct_local_para(sizeof(uart_plugin_ind_struct), TD_CTRL);
ilm_uart_plug_in->port = USB2UARTPort.port_no;
DRV_BuildPrimitive(ilm,
MOD_DRV_HISR,
USB2UARTPort.ownerid,
MSG_ID_UART_PLUGIN_IND,
ilm_uart_plug_in);
msg_send_ext_queue(ilm);
g_UsbACM.send_UARTilm = KAL_TRUE;
if (g_UsbACM.config_send_Txilm == KAL_TRUE )
{
g_UsbACM.send_Txilm = KAL_TRUE;
/* if someone PutBytes once, then issue ready-to-write message */
if(USB2UARTPort.tx_cb!=NULL)
USB2UARTPort.tx_cb(uart_port_usb);
}
else
{
g_UsbACM.send_Txilm = KAL_FALSE;
}
}
}
}
return bError;
}
/* parse command Get Configuration */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -