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

📄 usbd.c

📁 MTK平台绝密核心代码之 USB驱动
💻 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:
 * ---------
 *    usbd.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements usb1.1 host driver
 *
 * 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!
 *
 *------------------------------------------------------------------------------
 * 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 "syscomp_config.h"
#include "task_config.h"
#include "stacklib.h"
#include "reg_base.h"
#include "drvsignals.h"
  
#include "app_buff_alloc.h"
#include "drvpdn.h"
#include "intrCtrl.h"
#include "gpio_sw.h"

#include "usb_comm.h"
#include "usb_hcd.h"
#include "usb.h"
#include "usbd.h"

#ifdef __OTG_ENABLE__

USBD_Struct	gUsbHost;

static void USBD_Attatch(USB_HCD_STATUS result, kal_uint32 param);
static void USBD_Set_Address(kal_uint8 address);
static void USBD_Get_Descriptor(kal_uint16 type_index, kal_uint16 lang_id, kal_uint16 buflen, kal_uint8 *buffer);
static void USBD_Set_Configuration(kal_uint16 config_value);
static void USBD_Ch9_Callback(USB_HCD_STATUS result, kal_uint32 param);
static void USBD_EP0_Hdlr(USB_HCD_STATUS result, kal_uint32 param);

/************************************************************
	gUsbHost initialize and release functions
*************************************************************/

/* Initialize USBD information */
void USBD_Init_Status(void)
{
	kal_uint32 index;

	gUsbHost.desc_info.cfg_number = 0;
	for(index = 0; index < USBD_MAX_CFG_NUM; index++)
	{
		gUsbHost.desc_info.cfg_info[index].interface_number = 0;
		gUsbHost.desc_info.p_otg_desc[index] = NULL;
	}	
	for(index = 0; index < USBD_MAX_INTERFACE_NUM; index++)
		gUsbHost.desc_info.interface_info[index].ep_number= 0;

	gUsbHost.ep0_state = USBD_EP0_NONE;
	gUsbHost.ep0_status.max_packet_size = USBD_EP0_MAX_PKT_SIZE;
	gUsbHost.ch9_state = USBD_CH9_START;
		
	gUsbHost.current_class_driver = -1;
	gUsbHost.total_class_driver = 0;
}

/* Release USBD information */
void USBD_Release_Status(void)
{
	kal_uint32 index;

	/* release all resource in case no detatch interrupt before disable interrupt */
	USBD_Detach(USB_HCD_DETATCH, 0);
	
	gUsbHost.desc_info.cfg_number = 0;
	for(index = 0; index < USBD_MAX_CFG_NUM; index++)
	{
		gUsbHost.desc_info.cfg_info[index].interface_number = 0;
		gUsbHost.desc_info.p_otg_desc[index] = NULL;
	}
	for(index = 0; index < USBD_MAX_INTERFACE_NUM; index++)
		gUsbHost.desc_info.interface_info[index].ep_number= 0;

	gUsbHost.ch9_state = USBD_CH9_START;
	
	gUsbHost.current_class_driver = -1;
	gUsbHost.total_class_driver = 0;
}

/* Register class specific driver structre */
void USBD_Register_Class_Driver(USBD_ClassDriver_Struct *class_driver_ptr)
{
	if(gUsbHost.total_class_driver==USBD_MAX_CLASS_DRIVER)
		EXT_ASSERT(0, gUsbHost.total_class_driver, 0, 0);

	gUsbHost.class_driver_ptr[gUsbHost.total_class_driver] = class_driver_ptr;
	gUsbHost.total_class_driver++;
}

/* Deregister all class specific driver structre */
void USBD_Derigister_Class_Driver(void)
{
	gUsbHost.total_class_driver = 0;	
}

/* After class driver is startup, it should register its own EP0 handler.
    Class specific erquest and standard request are all transfered to class EP0 handler */
void USBD_Register_EP0_Callback(usbd_ep0_callback callback)
{
	gUsbHost.ep0_callback = callback;
}

/************************************************************
	system ctrl functions
*************************************************************/

/* Initialize USBD(Host) */
void USBD_Init(void)
{
	/* Init HCD driver */
	USB_HCD_Init_Drv_Info();
	USB_HCD_Register_Drv_Info(USB_HCD_HDLR_ATTACH, 0, USBD_Attatch);
	USB_HCD_Register_Drv_Info(USB_HCD_HDLR_DETACH, 0, USBD_Detach);
	USB_HCD_Register_Drv_Info(USB_HCD_HDLR_TOKEN_DONE, 0, USBD_EP0_Hdlr);

	USB_HCD_Initialize_Drv();
	USB_HCD_EP0En();
}

/* Release USBD(Host)*/
void USBD_Release(void)
{
	USB_HCD_Release_Drv();
}

/* Attatch interrupt handler*/
static void USBD_Attatch(USB_HCD_STATUS result, kal_uint32 param)
{
	kal_uint32 index;
	
	gUsbHost.ch9_state=USBD_CH9_START;
	for(index = 0; index < USBD_MAX_CFG_NUM; index++)
	{
		gUsbHost.desc_info.p_otg_desc[index] = NULL;
	}
	gUsbHost.support_hnp = KAL_FALSE;
	/* Register EP0 handler */
	USBD_Register_EP0_Callback(USBD_Ch9_Callback);
	/* Start CH9 state machine*/
	USBD_Ch9_Callback(result, param);
}

/* Detach interrupt handler*/
void USBD_Detach(USB_HCD_STATUS result, kal_uint32 param)
{
	kal_uint32 index;
	
	gUsbHost.ch9_state=USBD_CH9_START;
	if(gUsbHost.current_class_driver!=-1)
		gUsbHost.class_driver_ptr[gUsbHost.current_class_driver]->detach();
	gUsbHost.current_class_driver = -1;
	gUsbHost.ch9_error_count = 0;
	for(index = 0; index < USBD_MAX_CFG_NUM; index++)
	{
		if(gUsbHost.desc_info.p_conf_data[index]!=NULL)
			free_ctrl_buffer(gUsbHost.desc_info.p_conf_data[index]);
		gUsbHost.desc_info.p_conf_data[index] = NULL;
		gUsbHost.desc_info.p_otg_desc[index] = NULL;
	}	
	gUsbHost.support_hnp = KAL_FALSE;
}


/************************************************************
	EP0 functions
*************************************************************/
/* Set Address request */
static void USBD_Set_Address(kal_uint8 address)
{ 
	gUsbHost.ep0_status.setup_cmd.bmRequestType = USB_CMD_STDDEVOUT;
	gUsbHost.ep0_status.setup_cmd.bRequest = USB_SET_ADDRESS;
	gUsbHost.ep0_status.setup_cmd.wValue = address;
	gUsbHost.ep0_status.setup_cmd.wIndex = 0;
	gUsbHost.ep0_status.setup_cmd.wLength = 0;
	
	gUsbHost.ep0_state = USBD_EP0_SETUP;
	gUsbHost.ep0_status.data_ptr= NULL;
	gUsbHost.ep0_status.ep0_data_dir = USBD_EP0_DATA_SEND;
	gUsbHost.ep0_status.sofar = 0;
	gUsbHost.ep0_status.todo = 0;
	
	USB_HCD_EP0_Setup_Req(8, &gUsbHost.ep0_status.setup_cmd);
} 

/* Get Descriptor request */
static void USBD_Get_Descriptor(kal_uint16 type_index, kal_uint16 lang_id, kal_uint16 buflen, kal_uint8 *buffer)
{ 
	gUsbHost.ep0_status.setup_cmd.bmRequestType = USB_CMD_STDDEVIN;
	gUsbHost.ep0_status.setup_cmd.bRequest = USB_GET_DESCRIPTOR;
	gUsbHost.ep0_status.setup_cmd.wValue = type_index;
	gUsbHost.ep0_status.setup_cmd.wIndex = lang_id;
	gUsbHost.ep0_status.setup_cmd.wLength = buflen;
	
	gUsbHost.ep0_state = USBD_EP0_SETUP;
	gUsbHost.ep0_status.data_ptr= buffer;
	gUsbHost.ep0_status.ep0_data_dir = USBD_EP0_DATA_RECV;
	gUsbHost.ep0_status.sofar = 0;
	gUsbHost.ep0_status.todo = buflen;
	
	USB_HCD_EP0_Setup_Req(8, &gUsbHost.ep0_status.setup_cmd);
} 

/* Set Configuration request */
static void USBD_Set_Configuration(kal_uint16 config_value)
{ 
	gUsbHost.ep0_status.setup_cmd.bmRequestType = USB_CMD_STDDEVOUT;
	gUsbHost.ep0_status.setup_cmd.bRequest = USB_SET_CONFIGURATION;
	gUsbHost.ep0_status.setup_cmd.wValue = config_value;
	gUsbHost.ep0_status.setup_cmd.wIndex = 0;
	gUsbHost.ep0_status.setup_cmd.wLength = 0;
	
	gUsbHost.ep0_state = USBD_EP0_SETUP;
	gUsbHost.ep0_status.data_ptr= NULL;
	gUsbHost.ep0_status.ep0_data_dir = USBD_EP0_DATA_SEND;
	gUsbHost.ep0_status.sofar = 0;
	gUsbHost.ep0_status.todo = 0;

⌨️ 快捷键说明

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