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

📄 usbdl_usb.c

📁 MTK 平台启动源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
*  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:
 * ---------
 *    usbdl_usb.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements USB driver for USB download
 *
 * 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!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

#ifdef  __USB_DOWNLOAD__
#include "drv_comm.h"
//#include "reg_base.h"

//#include "drvpdn.h"
//#include "intrCtrl.h"
//#include "gpio_sw.h"

#include "usb_comm.h"
#include "usb_drv.h"
#include "usbdl_usb.h"
#include "usbdl_usb_resource.h"
#include "usbdl_usbacm_drv.h"
#include "usb_custom.h"
#include "usbdl_debug_tool.h"

/* Device structure, handle usb level data */
Usb_Device gUsbDevice;
extern void USB_Initialize_Drv(void);
/* static fuctions */
static void USB_Reset(void);
static void USB_Initialize(void);
static kal_bool USB_Cmd_SetAddress(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static kal_bool USB_Cmd_GetDescriptor(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static kal_bool USB_Cmd_SetConfiguration(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static kal_bool USB_Cmd_GetConfiguration(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static kal_bool USB_Cmd_SetFeature(Usb_Command *pcmd, kal_bool bset);
static kal_bool USB_Cmd_GetStatus(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static void USB_Stdcmd(Usb_Ep0_Status *pep0state, Usb_Command *pcmd);
static void USB_Endpoint0_Idle (void);
static void USB_Endpoint0_Rx(void);
static void USB_Endpoint0_Tx(void);
static void USB_Endpoint0_Hdlr(void);


/************************************************************
	gUsbDevice initialize and release functions
*************************************************************/

/* initialize the  global variable gUsbDevice */
void USB_Init_Device_Status(void)
{
	kal_uint8 index = 0;

	kal_mem_set(&gUsbDevice.cfg_info, 0, sizeof(Usb_Config_Info));
 
	for( index = 0; index < USB_MAX_INTERFACE; index++)
	{
		kal_mem_set(&gUsbDevice.if_info[index], 0, sizeof(Usb_Interface_Info));
	}

	for( index = 0; index < USB_MAX_EP_BULK_IN; index++)
	{
		kal_mem_set(&gUsbDevice.ep_bulkin_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_EP_BULK_OUT; index++)
	{
		kal_mem_set(&gUsbDevice.ep_bulkout_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_EP_INTR; index++)
	{
		kal_mem_set(&gUsbDevice.ep_intr_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_STRING; index++)
	{
		gUsbDevice.resource_string[index] = NULL;
	}

	gUsbDevice.conf = NULL;
	gUsbDevice.device_type = USB_UNKOWN;
	gUsbDevice.nDevState = DEVSTATE_DEFAULT;
	gUsbDevice.remoteWk = KAL_FALSE;
	gUsbDevice.self_powered = KAL_FALSE;
	gUsbDevice.config_num = 0;
	gUsbDevice.interface_num = 0;
	gUsbDevice.ep0_rx_handler = NULL;
//	gUsbDevice.ep0_class_cmd_handler.b_enable = KAL_FALSE;

   	gUsbDevice.resource_ep_bulkin_number = 0;		
   	gUsbDevice.resource_ep_bulkout_number = 0;		
   	gUsbDevice.resource_ep_intr_number = 0;
   	gUsbDevice.resource_interface_number = 0;
   	gUsbDevice.resource_string_number = 0; 
}


/* release the already get memory, and reset some parameter,
    note that hisr should not be reset to NULL since it may already be created*/
/*
void USB_Release_Device_Status(void)
{
	kal_uint8 index = 0;

	kal_mem_set(&gUsbDevice.cfg_info, 0, sizeof(Usb_Config_Info));
	
	for( index = 0; index < USB_MAX_INTERFACE; index++)
	{
		kal_mem_set(&gUsbDevice.if_info[index], 0, sizeof(Usb_Interface_Info));
	}

	for( index = 0; index < USB_MAX_EP_BULK_IN; index++)
	{
		kal_mem_set(&gUsbDevice.ep_bulkin_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_EP_BULK_OUT; index++)
	{
		kal_mem_set(&gUsbDevice.ep_bulkout_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_EP_INTR; index++)
	{
		kal_mem_set(&gUsbDevice.ep_intr_info[index], 0, sizeof(Usb_Ep_Info));
	}

	for( index = 0; index < USB_MAX_STRING; index++)
	{
		gUsbDevice.resource_string[index] = NULL;
	}

	gUsbDevice.conf = NULL;
	
	gUsbDevice.nDevState = DEVSTATE_DEFAULT;
	gUsbDevice.remoteWk = KAL_FALSE;
	gUsbDevice.self_powered = KAL_FALSE;
	gUsbDevice.config_num = 0;
	gUsbDevice.interface_num = 0;
	gUsbDevice.device_type = USB_UNKOWN;
	gUsbDevice.ep0_rx_handler = NULL;
//	gUsbDevice.ep0_class_cmd_handler.b_enable = KAL_FALSE;

	gUsbDevice.resource_ep_bulkin_number = 0;		
	gUsbDevice.resource_ep_bulkout_number = 0;		
	gUsbDevice.resource_ep_intr_number = 0;
	gUsbDevice.resource_interface_number = 0;
	gUsbDevice.resource_string_number = 0; 
}
*/


/************************************************************
	EP0 functions
*************************************************************/

/* register EP0 rx OUT token data handler */
void USB_Register_EP0_RxHdlr(usb_ep0_rx_ptr handler)
{
	gUsbDevice.ep0_rx_handler = handler;
}

/*
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,kal_bool FT)
{
#if defined(DRV_USB_MCU_52M)
	#ifndef MCU_52M
		EXT_ASSERT(0, 0, 0, 0);  /* Must 52M */
	#endif   /* MCU_52M */
#endif 	/* MT6218||MT6218B||MT6219 */

	USB_Init_Drv_Info(KAL_FALSE);			/*initialize driver parameters*/
	USB_Init_Device_Status();	/*initialize global variable gUsbDevice*/

	USB_Init_Acm_Status();
	gUsbDevice.device_type = USB_CDC_ACM;

	/* 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();
	USB_Set_DP_Pull_High();

	/* reset and initialize system initial value and registers*/
	USB_Reset_Drv();  /* reset hw power register */
	USB_Initialize_Drv();

	/* initailize according to application*/
	USB_Initialize();
}

/*
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;

	// 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();
	}
}

/* initialize system */
static void USB_Initialize(void)
{
	kal_uint8 index_ep;
	kal_uint16 wMaxPacketSize;


	gUsbDevice.nDevState = DEVSTATE_DEFAULT;

	for( index_ep = 0; index_ep < gUsbDevice.resource_ep_bulkin_number; index_ep++)
	{
		wMaxPacketSize = (kal_uint8)gUsbDevice.ep_bulkin_info[index_ep].epdesc.stdep.wMaxPacketSize[1];
		wMaxPacketSize <<= 8;
		wMaxPacketSize |= (kal_uint8)gUsbDevice.ep_bulkin_info[index_ep].epdesc.stdep.wMaxPacketSize[0];
		
		USB_TxEPInit((gUsbDevice.ep_bulkin_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)), 
						wMaxPacketSize, 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++)
	{
		wMaxPacketSize = (kal_uint8)gUsbDevice.ep_bulkout_info[index_ep].epdesc.stdep.wMaxPacketSize[1];
		wMaxPacketSize <<= 8;
		wMaxPacketSize |= (kal_uint8)gUsbDevice.ep_bulkout_info[index_ep].epdesc.stdep.wMaxPacketSize[0];
			
		USB_RxEPInit((gUsbDevice.ep_bulkout_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)), 
						wMaxPacketSize, 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++)
	{
		wMaxPacketSize = 	(kal_uint8)gUsbDevice.ep_intr_info[index_ep].epdesc.stdep.wMaxPacketSize[1];
		wMaxPacketSize <<= 8;
		wMaxPacketSize |= (kal_uint8)gUsbDevice.ep_intr_info[index_ep].epdesc.stdep.wMaxPacketSize[0];
			
		USB_TxEPInit((gUsbDevice.ep_intr_info[index_ep].epdesc.stdep.bEndpointAddress&(~USB_EP_DIR)), 
						wMaxPacketSize, 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));

⌨️ 快捷键说明

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