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

📄 usb.c

📁 本程序为ST公司开发的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
				local_usb_downstream_cmd.pData, 				local_usb_downstream_cmd.DataLength, 				local_usb_downstream_cmd.bEndFlag);			if (send_status != MUSB_CIS_BUSY)			{				if (send_status == MUSB_CIS_SUCCESS)				{					M_SET_FLAG(pUsbIpodVolume->flag_data_out);	/* to inform USB TASK */				}				else				{					M_SET_FLAGS(gUsbDevice.flag_error, E_USB_DOWNSTREAM);	/* to inform USB TASK */				}			}		}		else		{			M_SET_FLAGS(gUsbDevice.flag_error, E_USB_DOWNSTREAM);	/* to inform USB TASK */		}	}	if (EVENT_USB_IS_UPSTREAM_CMD)	{        /* set ack status and send it */		local_usb_upstream_cmd = EVENT_USB_UPSTREAM_CMD;#ifdef _USB_DEBUG		DBG_PRINTF("U:Ucmd\r\n");#endif#ifdef _B32_BACK_COMPATIBILITY		pUsbIpodVolume = (tUsbIpodVolume*)USB_GetVolume(S_USB_TYPE_IPOD, S_USB_GET_FIRST, &zero);#else /*_B32_BACK_COMPATIBILITY*/		pUsbIpodVolume = (tUsbIpodVolume*)USB_GetVolume(S_USB_TYPE_IPOD, S_USB_GET_FIRST, local_usb_downstream_cmd.bVolNum);#endif /*_B32_BACK_COMPATIBILITY*/		if (pUsbIpodVolume)		{			pUsbPassthrough = &(pUsbIpodVolume->Mode.UsbPassthrough);			if (M_IS_SET(pUsbPassthrough->bUpstreamPending))	//new			{	//new				M_CLEAR_FLAG(pUsbPassthrough->bUpstreamPending);				M_USB_UPSTREAM_COUNT_POSITION(pUsbPassthrough->wReadPosition);				pUsbPassthrough->wFreePositions++; 			}	//new			USB_PassthroughUpstreamTransition(pUsbPassthrough);		}		else		{			M_SET_FLAGS(gUsbDevice.flag_error, E_USB_UPSTREAM);	/* to inform USB TASK */			DBG_PRINTF("usb: *E - preevents: iPod volume doesn't exist");		}	}#endif /*IPOD_PASSTHROUGH || IPOD_AP*/  if ((EVENT_USB_IS_CMD) || (local_cmd.command))  {        if ((local_cmd.command) && (&EVENT_USB_CMD == NO_EVENT))    {      local_usb_cmd_event = local_cmd;      local_cmd_present = b_FALSE;  /* clear cmd because it's going to be processed */    }    else if (EVENT_USB_IS_CMD)  	/* command events coming from controller */  	{  		local_usb_cmd_event = EVENT_USB_CMD;  	}				pout_event = (t_usb_status_event*) pevent_get_out(USB_STATUS_EVENT);		switch(local_usb_cmd_event.command)		{		case CHILD_CMD_INIT:			if(gUsbOn == FALSE)			{				GCR1.field.usb_pad_en = 1;				USB_Init();	//TBD - result?			}			else			{				USB_ReinitializeUsb();			}			/* copy to event_out */			pout_event->USB_state = USB_INITIALIZED;			event_set_out(USB_STATUS_EVENT);			/* task state handling */			usb_fsm_background.state = FSM_DONE; /* (be aware of the task switching) */			return S_USB_FSM_END; /* to cancel/(postpone) other processig */		case CHILD_CMD_CLOSE:			if(gUsbOn == TRUE)			{				USB_Destroy();				GCR1.field.usb_pad_en = 0;			}			/* copy to event_out */			pout_event->USB_state = USB_CLOSED;			event_set_out(USB_STATUS_EVENT);			/* task state handling */			usb_fsm_background.state = FSM_DONE; /* (be aware of the task switching) */			return S_USB_FSM_END; /* to cancel/(postpone) other processig */		case CHILD_CMD_XFER_DATA:		{#ifdef _USB_DEBUG			DBG_PRINTF("usb: \tCHILD_CMD_XFER_DATA\r\n");#endif /*_USB_DEBUG*/#ifdef _B32_BACK_COMPATIBILITY			if(!(gUsbDevice.bAutoMCheck))				USB_StopCheckMediumTimer();	/* to save MIPS - don't check if transfer is pending; drawback: no media change is recognized */ //TBD#endif /*_B32_BACK_COMPATIBILITY*/			if(gUsbDevice.bProcessing)			{	/* driver is occupied */			  local_cmd = local_usb_cmd_event;  /* temporarily store command which is not possible to process */			  local_cmd_present = b_TRUE;#ifdef _USB_DEBUG				dbg_count++;#endif /*_USB_DEBUG*/				break;			}						#ifdef _USB_DEBUG			else if(dbg_count)			{				DBG_PRINTF("usb: *W - preevents: CMD re-request no.:%d\r\n", dbg_count);				dbg_count = 0;			}#endif /*_USB_DEBUG*/			FSM_activate(&usb_fsm_background, 0, 0);				/* activate USB_BACKGROUND transition */			FSM_activate(&usb_fsm_xfer, S_USB_GET_DATA_FSM_IDLE, 1);	/* activate USB_XFER transition, notify parent */			break;		}													case CHILD_CMD_STOP:		{		  #ifdef _USB_DEBUG			DBG_PRINTF("usb: \tCHILD_CMD_STOP\r\n");#endif /*_USB_DEBUG*/#ifdef _B32_BACK_COMPATIBILITY			if(!(gUsbDevice.bAutoMCheck))				USB_StartCheckMediumTimer(50);	/* save MIPS strategy -> start to check medium immediatelly */ //TBD - optimal delay vs. swiftness#endif /*_B32_BACK_COMPATIBILITY*/			FSM_activate(&usb_fsm_stop, 0, 1);			/* activate USB_STOP transition */			break;		}#if (1 == HAVE_WMDRM)		case CHILD_CMD_MTP:		{			if(gUsbDevice.bProcessing)			{	/* driver is occupied */			  local_cmd = EVENT_USB_CMD;  /* temporarily store command which is not possible to process */			  local_cmd_present = b_TRUE;				break;			}			FSM_activate(&usb_fsm_background, 0, 0);				/* activate USB_BACKGROUND transition */			FSM_activate(&usb_fsm_mtp, S_USB_MTP_TREAT_COMMAND, 1);	/* activate USB_MTP transition */			break;		}#endif		}	}	if(UsbISRcounter > 0)	/* events coming from USB interrupt */	{		DISABLE_INTERRUPTS();		UsbISRcounter--;		ENABLE_INTERRUPTS(); 	 	   						if((UsbISRcounter) > 0)		{ /* wake up USB task again */		  event_usb_set_irq(READY);		}				FSM_activate(&usb_fsm_background, 0, 0);		/* activate USB_BACKGROUND transition */	}		return S_USB_FSM_MORE;}#if (0 == HAVE_WMDRM)/******************************************************************************//* Function:  usb_stop_transition                                             *//*                                                                            *//*! \brief    try to stop any pending MSD data transfer*             *  \param    t_usb_passthrough_cmd_event * usb_passthrough_cmd*  \return   teUsbFsmCompl - completion flag*  \remark*******************************************************************************/teUsbFsmCompl usb_stop_transition(void){#ifdef _USB_DEBUG	DBG_PRINTF("usb: \tusb_stop_transition\r\n");#endif /*_USB_DEBUG*/	switch(usb_fsm_xfer.state)	{	case FSM_RUN:	{		usb_fsm_xfer.state = FSM_ERROR;		usb_fsm_xfer.error_reason = E_FORCED_TERMINATION;		break;	}	case FSM_DONE:	case FSM_IDLE:	{		usb_fsm_xfer.state = FSM_IDLE;		if(usb_fsm_stop.parent_notify)		{		  event_usb_set_stop(READY);		}		usb_fsm_stop.state = FSM_DONE;		break;	}	case FSM_ERROR:	{	  if(usb_fsm_xfer.error_reason == E_FORCED_TERMINATION)	  { /* if usb_fsm_xfer in error due to stop command while xfer active */		if(usb_fsm_background.state == FSM_DONE)		{ /* last partial xfer finished */			usb_fsm_xfer.state = FSM_IDLE;			if(usb_fsm_stop.parent_notify)			{  		        event_usb_set_stop(READY);			}			usb_fsm_stop.state = FSM_DONE;			break;		}		else /* wait for finish of partial xfer */			return S_USB_FSM_END;	  }	  else if((usb_fsm_xfer.error_reason == E_USB_FORCED_UNPLUG)	        ||(usb_fsm_xfer.error_reason == E_USB_READ))		//M.Ch.	  { /* if usb_fsm_xfer in error due to USB key unplugged and stop command was send after */	    /* if usb_fsm_xfer in error due to USB read error and stop command was send after */		usb_fsm_xfer.state = FSM_IDLE;		if(usb_fsm_stop.parent_notify)		{		  event_usb_set_stop(READY);		}		usb_fsm_stop.state = FSM_DONE;		break;	  }	}	default: 		return S_USB_FSM_END;    }	return S_USB_FSM_MORE;}#else/******************************************************************************//* Function:  usb_stop_transition                                             *//*                                                                            *//*! \brief    try to stop any pending MSD or MTP data transfer*             *  \param    *  \return   teUsbFsmCompl - completion flag*  \remark*******************************************************************************/teUsbFsmCompl usb_stop_transition(void){t_fsm *local_usb_fsm = &usb_fsm_xfer;	if (USB_is_currnt_device_mtp())	{		local_usb_fsm = &usb_fsm_mtp;	}	switch(local_usb_fsm->state)	{	case FSM_RUN:	{		local_usb_fsm->state = FSM_ERROR;		local_usb_fsm->error_reason = E_FORCED_TERMINATION;		break;	}	case FSM_DONE:	case FSM_IDLE:	{		local_usb_fsm->state = FSM_IDLE;		if(usb_fsm_stop.parent_notify)		{		  event_usb_set_stop(READY);		}		usb_fsm_stop.state = FSM_DONE;		break;	}	case FSM_ERROR:	{	  if(local_usb_fsm->error_reason == E_FORCED_TERMINATION)	  { /* if usb_fsm_xfer in error due to stop command while xfer active */		if(usb_fsm_background.state == FSM_DONE)		{ /* last partial xfer finished */			local_usb_fsm->state = FSM_IDLE;			if(usb_fsm_stop.parent_notify)			{			             	  event_usb_set_stop(READY);			}			usb_fsm_stop.state = FSM_DONE;			break;		}		else /* wait for finish of partial xfer */			return S_USB_FSM_END;	  }	  else if((local_usb_fsm->error_reason == E_USB_FORCED_UNPLUG)	        ||(local_usb_fsm->error_reason == E_USB_READ))		//M.Ch.	  { /* if usb_fsm_xfer in error due to USB key unplugged and stop command was send after */	    /* if usb_fsm_xfer in error due to USB read error and stop command was send after */		local_usb_fsm->state = FSM_IDLE;		if(usb_fsm_stop.parent_notify)		{            event_usb_set_stop(READY);		}		usb_fsm_stop.state = FSM_DONE;		break;	  }	}	default: 		return S_USB_FSM_END;	}	return S_USB_FSM_MORE;}/* Report current device is MTP or other */t_bool USB_is_currnt_device_mtp(void){#ifdef _B32_BACK_COMPATIBILITY	tUsbGenVolume* pUsbGenVolume = (tUsbGenVolume*)(gUsbDevice.apUsbVolumes[0]);	//!!! be carefull !!!#else /*_B32_BACK_COMPATIBILITY*/	tUsbGenVolume* pUsbGenVolume = USB_GetVolume(S_USB_TYPE_NONE, S_USB_GET_POSITION, <pevent->usb_vol_ID>)		//TBD - usb_vol_ID from cmd event#endif /*_B32_BACK_COMPATIBILITY*/	if(pUsbGenVolume->Type == S_USB_TYPE_MTP)	{ 	    return TRUE; //1;   // MTP device	}	return FALSE; //0;       // Other device}/******************************************************************************//* Function:  usb_mtp_transision                                 *//*                                                                            *//*! \brief    Handling to read/write MTP data transfer*             *  \param*  \return    teUsbFsmCompl - completion flag*  \remark*******************************************************************************/teUsbFsmCompl usb_mtp_transition(void){	uint32 local_data_size;	uint8 **pbMessageReceived;#ifdef _B32_BACK_COMPATIBILITY	tUsbMtpVolume* pUsbMtpVolume = (tUsbMtpVolume*)(gUsbDevice.apUsbVolumes[0]);	//!!! be carefull !!!#else /*_B32_BACK_COMPATIBILITY*/	tUsbMtpVolume* pUsbMtpVolume = USB_GetVolume(S_USB_TYPE_MTP, S_USB_GET_POSITION, <pevent->usb_vol_ID>)		//TBD - usb_vol_ID from cmd event#endif /*_B32_BACK_COMPATIBILITY*/	if(!pUsbMtpVolume)	{		USB_SetErrorForController(&usb_fsm_mtp, E_USB_READ_FATAL);		return S_USB_FSM_END;	}	switch (usb_fsm_mtp.transition)	{	case S_USB_MTP_TREAT_COMMAND:		memcpy (&local_data_size, local_usb_cmd_event.command_params.mtp_params.cbMessageReceived, 4); 		if (local_usb_cmd_event.command_params.mtp_params.cbMessageToSend) /* any write data ? */		{			/* Upper layer prepared "data + Header space (=12 Bytes)" */			pUSB_temp_buf = local_usb_cmd_event.command_params.mtp_params.pbMessageToSend;		}		else if ((local_data_size != 0xFFFFFFFF)			  && (local_data_size > (MTP_TEMP_BUF_SIZE - 12)))		/* Read case & read size is over than USB temp buffer */		{			pUSB_temp_buf = (uint8*)MALLOC(local_data_size + 12);			if(pUSB_temp_buf == NULL)			{				USB_SetErrorForController(&usb_fsm_mtp, E_USB_READ);   // no buffer memory				return S_USB_FSM_END;			}		}		else		{			pUSB_temp_buf = USB_temp_buf;		}		/* to do read/write */		if (USB_DataReadWrite(pUsbMtpVolume) != USB_OK)		/* Report execution error from driver */		{			USB_SetErrorForController(&usb_fsm_mtp, E_USB_FORCED_UNPLUG);			return S_USB_FSM_END;		}		usb_fsm_mtp.transition = S_USB_MTP_WAIT_RESULT;		return S_USB_FSM_END;        case S_USB_MTP_WAIT_RESULT:		if ( usb_fsm_background.state == FSM_RUN )

⌨️ 快捷键说明

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