?? usbsetup.c
字號:
/**************************************************************
NAME: usbsetup.c
DESC: process the USB setup stage operations.
HISTORY:
MAR.25.2002:purnnamu: S3C2400X usbsetup.c is ported for S3C2440A
AUG.20.2002:purnnamu: rEP0_CSR should be used instead of rOUT_CSR1_REG for EP0 macros.
**************************************************************/
#include "def.h"
#include "2440addr.h"
#include "2440usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "usbin.h"
#include "usb.h"
#define USB_DOWNLOAD_SUPPORT
#ifdef USB_DOWNLOAD_SUPPORT
// *** End point information ***
// EP0: control
// EP1: bulk in end point
// EP2: not used
// EP3: bulk out end point
// EP4: not used
// *** VERY IMPORTANT NOTE ***
// Every descriptor size of EP0 should be 8n+m(m=1~7).
// Otherwise, USB will not operate normally because the program
// doesn't prepare the case that the descriptor size is 8n+0.
// If the size of a descriptor is 8n, the 0 length packit should be sent.
// Special thanks to E.S.Choi for reminding me of this USB specification.
// ===================================================================
// All following commands will operate only in case
// - ep0_csr is valid.
// ===================================================================
#define CLR_EP0_OUT_PKT_RDY() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
EP0_SERVICED_OUT_PKT_RDY )
#define CLR_EP0_OUTPKTRDY_DATAEND() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_SERVICED_OUT_PKT_RDY|EP0_DATA_END) )
#define SET_EP0_IN_PKT_RDY() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_IN_PKT_READY) )
#define SET_EP0_INPKTRDY_DATAEND() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_IN_PKT_READY|EP0_DATA_END) )
#define CLR_EP0_SETUP_END() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)| \
(EP0_SERVICED_SETUP_END) )
#define CLR_EP0_SENT_STALL() rEP0_CSR=( ep0_csr & (~EP0_WR_BITS)& \
(~EP0_SENT_STALL) )
#define FLUSH_EP0_FIFO() {register i;while(rOUT_FIFO_CNT1_REG)i=rEP0_FIFO;}
U32 ep0State;
U32 ep0SubState;
extern volatile int isUsbdSetConfiguration;
struct USB_SETUP_DATA descSetup;
struct USB_DEVICE_DESCRIPTOR descDev;
struct USB_CONFIGURATION_DESCRIPTOR descConf;
struct USB_INTERFACE_DESCRIPTOR descIf;
struct USB_ENDPOINT_DESCRIPTOR descEndpt0;
struct USB_ENDPOINT_DESCRIPTOR descEndpt1;
static const U8 descStr0[]={
4,STRING_TYPE,LANGID_US_L,LANGID_US_H, //codes representing languages
};
static const U8 descStr1[]={ //Manufacturer
(0x14+2),STRING_TYPE,
'S',0x0,'y',0x0,'s',0x0,'t',0x0,'e',0x0,'m',0x0,' ',0x0,'M',0x0,
'C',0x0,'U',0x0,
};
static const U8 descStr2[]={ //Product
(0x2a+2),STRING_TYPE,
'S',0x0,'E',0x0,'C',0x0,' ',0x0,'S',0x0,'3',0x0,'C',0x0,'2',0x0,
'4',0x0,'1',0x0,'0',0x0,'X',0x0,' ',0x0,'T',0x0,'e',0x0,'s',0x0,
't',0x0,' ',0x0,'B',0x0,'/',0x0,'D',0x0
};
/*****************************************/
/* Ep0中斷處理開始 */
/* 控制傳輸中斷處理 */
/*****************************************/
void Ep0Handler(void)
{
static int ep0SubState;
// int i;
U8 ep0_csr;
rINDEX_REG=0; //訪問端點0控制狀態寄存器EP0_CSR前,必須給索引寄存器INDEX_REG賦0
ep0_csr=rEP0_CSR; //端點0控制狀態寄存器的值賦給變量ep0_csr
//DATAEND interrupt(ep0_csr==0x0) will be ignored
//because ep0State==EP0_STATE_INIT when the DATAEND interrupt is issued.
if(ep0_csr & EP0_SETUP_END) //當SETUP_END為1時,即一個控制傳輸結束
{
// Host may end GET_DESCRIPTOR operation without completing the IN data stage.
// If host does that, SETUP_END bit will be set.
// OUT_PKT_RDY has to be also cleared because status stage sets OUT_PKT_RDY to 1.
CLR_EP0_SETUP_END(); //清除SETUP_END位
if(ep0_csr & EP0_OUT_PKT_READY) //一個有效的令牌包寫入EP0 FIFO,USB對OUT_PKT_READY位置1
{
FLUSH_EP0_FIFO(); //沖掉EP0_FIFO中的數據
//I think this isn't needed because EP0 flush is done automatically.
CLR_EP0_OUT_PKT_RDY(); //清除OUT_PKT_READY
}
ep0State=EP0_STATE_INIT; //ep0State狀態為0
return;
}
//I think that EP0_SENT_STALL will not be set to 1.
if(ep0_csr & EP0_SENT_STALL) //控制事務由于協議沖突而停止,USB對SENT_STALL位置1
{
// DbgOut("[STALL]");
CLR_EP0_SENT_STALL(); //清除SENT_STALL位
if(ep0_csr & EP0_OUT_PKT_READY) //如果輸出包標志置位
{
CLR_EP0_OUT_PKT_RDY(); //清除輸出包有效標志
}
ep0State=EP0_STATE_INIT;
return;
}
//一個有效的令牌包寫入EP0 FIFO,并且ep0State=0
if((ep0_csr & EP0_OUT_PKT_READY) && (ep0State==EP0_STATE_INIT))
{
RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE);//把8字節SETUP數據包讀入結構體descSetup
switch(descSetup.bRequest)//根據請求命令代碼分支跳轉
{
case GET_DESCRIPTOR://請求描述符:代碼=6
switch(descSetup.bValueH) //wValue字段的高字節為描述符類型編號
{
case DEVICE_TYPE://設備類:編號為1
// DbgOut("[GDD]");
CLR_EP0_OUT_PKT_RDY();//清除OUT_PKT_READY
ep0State=EP0_STATE_GD_DEV_0;//EP0的狀態為請求描述符設備0階段
break;
case CONFIGURATION_TYPE://配置類:編號為2
// DbgOut("[GDC]");
CLR_EP0_OUT_PKT_RDY();
if((descSetup.bLengthL+(descSetup.bLengthH<<8))>0x9)//數據包大于0x9時,設置EP0的狀態
//bLengthH should be used for bLength=0x209 at WIN2K.
ep0State=EP0_STATE_GD_CFG_0; //for WIN98,WIN2K
else
ep0State=EP0_STATE_GD_CFG_ONLY_0; //for WIN2K
break;
case STRING_TYPE://字符串類:編號為3
// DbgOut("[GDS]");
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL)//wValue字段的低字節為描述符字符串索引
{
case 0:
ep0State=EP0_STATE_GD_STR_I0;//獲得字符串0階段
break;
case 1:
ep0State=EP0_STATE_GD_STR_I1;//獲得字符串1階段
break;
case 2:
ep0State=EP0_STATE_GD_STR_I2;//獲得字符串2階段
break;
default:
// DbgOut("[UE:STRI?]");
break;
}
ep0SubState=0;
break;
case INTERFACE_TYPE://接口類:編號為4
// DbgOut("[GDI]");
CLR_EP0_OUT_PKT_RDY();
ep0State=EP0_STATE_GD_IF_ONLY_0; //for WIN98
break;
case ENDPOINT_TYPE: //端點類:編號為5
// DbgOut("[GDE]");
CLR_EP0_OUT_PKT_RDY();
switch(descSetup.bValueL&0xf)//wValue字段的低字節為描述符字符串索引
{
case 0:
ep0State=EP0_STATE_GD_EP0_ONLY_0;
break;
case 1:
ep0State=EP0_STATE_GD_EP1_ONLY_0;
break;
default:
// DbgOut("[UE:GDE?]");
break;
}
break;
default:
// DbgOut("[UE:GD?]");
break;
}
break;//wValue字段的低字節為描述符字符串索引
case SET_ADDRESS://設置地址:代碼=5
// DbgOut("[SA:%d]",descSetup.bValueL);
rFUNC_ADDR_REG=descSetup.bValueL | 0x80;//地址值放在wValue字段的低字節,更新ADDR_UPDATA
CLR_EP0_OUTPKTRDY_DATAEND(); //沒有數據控制傳輸,DATA_END位置1,數據傳輸結束.
ep0State=EP0_STATE_INIT;
break;
case SET_CONFIGURATION://設置配置:代碼=9
// DbgOut("[SC]");
CLR_EP0_OUTPKTRDY_DATAEND(); //沒有數據控制傳輸,DATA_END位置1.
ep0State=EP0_STATE_INIT;
isUsbdSetConfiguration=1;
break;
default:
// DbgOut("[UE:SETUP=%x]",descSetup.bRequest);
CLR_EP0_OUTPKTRDY_DATAEND(); //沒有數據控制傳輸,DATA_END位置1
ep0State=EP0_STATE_INIT;
break;
}
}//一個有效的令牌包寫入EP0 FIFO,并且ep0State=0處理結束
switch(ep0State)//根據EP0的階段狀態標志ep0State分支跳轉
{
case EP0_STATE_INIT:
break;
//=== GET_DESCRIPTOR:DEVICE ===
case EP0_STATE_GD_DEV_0://GET_DESCRIPTOR:DEVICE-->發送設備描述符
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -