📄 u_request.c
字号:
/******************************************************************************
* File name : request.c
* Module name : USB request module
* Author : Hiromichi.Kondo
*----------------------------------------------------------------------------
* $Id: request.c,v 1.2 2003/04/16 10:18:10 9551619 Exp $
*****************************************************************************/
#include "u_value.h"
#include "u_ram_data.h"
#include "u_rom_data.h"
#include "u_regNikka.h"
/*--- Function Prototype ---------------------------------------------------*/
int StandardRequest(void);
int ClassRequest(void);
int VendorRequest(void);
int GetStatus(void);
int ClearFeature(void);
int SetFeature(void);
int SetAddress(void);
int GetDescriptor(void);
int GetConfiguration(void);
int SetConfiguration(void);
int SetInterface(void);
int GetInterface(void);
int RequestError(void);
void SetAlternateInterface(BYTE alternate_valid);
int BulkOnlyMassStorageReset(void);
int GetMaxLUN(void);
int AcceptDeviceSpecificCmd(void);
int TestEP0Out(void);
int TestEP0In(void);
/*****************************************************************************
*
*****************************************************************************/
int (*const standard_req_tbl[])(void) = {
GetStatus, /* 00h Get Status */
ClearFeature, /* 01h Clear Feature */
RequestError, /* 02h Reserved for future use */
SetFeature, /* 03h Set Feature */
RequestError, /* 04h Reserved for future use */
SetAddress, /* 05h Set Address */
GetDescriptor, /* 06h Get Descriptor */
RequestError, /* 07h Set Descriptor */
GetConfiguration, /* 08h Get Configuration */
SetConfiguration, /* 09h Set Configuration */
GetInterface, /* 0Ah Get Interface */
SetInterface, /* 0Bh Set Interface */
RequestError}; /* 0Ch Synch Frame */
int StandardRequest(void)
{
int ret;
if (rEP0SETUP_1 < 0x0D)
{
ret = standard_req_tbl[rEP0SETUP_1]();
}
else
return REQUEST_ERROR;
return ret;
}
/*****************************************************************************
* ClassRequest
*****************************************************************************/
int ClassRequest(void)
{
int ret;
switch(rEP0SETUP_1)
{
case ACCEPT_DEV_SPC_CMD:
ret = AcceptDeviceSpecificCmd();
break;
case B_ONLY_STORAGE_RST:
ret = BulkOnlyMassStorageReset();
break;
case GET_MAX_LUN:
ret = GetMaxLUN();
break;
default:
ret = REQUEST_ERROR;
break;
}
return ret;
}
/*****************************************************************************
* VendorRequest
*****************************************************************************/
int VendorRequest(void)
{
int ret;
switch(rEP0SETUP_1){
case TEST_EP0_OUT:
ret = TestEP0Out();
break;
case TEST_EP0_IN:
ret = TestEP0In();
break;
default:
ret = REQUEST_ERROR;
}
return ret;
}
/*****************************************************************************
* GetStatus
*****************************************************************************/
int GetStatus(void)
{
int i, ret = REQUEST_ERROR;
if(usb_state != DEFAULT_STATE)
{ /* Default */
if((rEP0SETUP_0 & 0x80) == 0x80)
{
switch(rEP0SETUP_0 & 0x1F){
case RECIPIENT_DEVICE:
if(rEP0SETUP_3 || rEP0SETUP_2) /* wValue */
break;
if(rEP0SETUP_5 || rEP0SETUP_4) /* wIndex */
break;
if(rEP0SETUP_7 || rEP0SETUP_6 != 0x02) /* wLength */
break;
/* */
usb_wk_data[0] = (usb_flag.EnbRemoteWakeup << 1) + 0x01;
/* Self Powerd(0x01) */
usb_wk_data[1] = 0x00;
total_tran_cnt = actual_tran_cnt = 0x02;
tran_start_addr = &usb_wk_data[0];
ret = REQUEST_OK;
break;
case RECIPIENT_INTERFACE:
if(usb_state == ADDRESS_STATE)
break;
if(rEP0SETUP_3 || rEP0SETUP_2) /* wValue */
break;
if(rEP0SETUP_5 || rEP0SETUP_4) /* wIndex */
break;
if(rEP0SETUP_7 || rEP0SETUP_6 != 0x02) /* wLength */
break;
usb_wk_data[0] = usb_wk_data[1] = 0x00;
total_tran_cnt = actual_tran_cnt = 0x02;
tran_start_addr = &usb_wk_data[0];
ret = REQUEST_OK;
break;
case RECIPIENT_ENDPOINT:
if(rEP0SETUP_3 || rEP0SETUP_2) /* wValue */
break;
if(rEP0SETUP_5) /* wIndex.h */
break;
if(rEP0SETUP_7 || rEP0SETUP_6 != 0x02) /* wLength */
break;
if(rEP0SETUP_4 == 0)
{
/* EndPoint 0 */
if(rEP0ControlIN_BP.ForceSTALL || rEP0ControlOUT_BP.ForceSTALL)
{
usb_wk_data[0] = 0x01;
}
else
{
usb_wk_data[0] = 0x00;
}
usb_wk_data[1] = 0x00;
total_tran_cnt = actual_tran_cnt = 0x02;
tran_start_addr = &usb_wk_data[0];
ret = REQUEST_OK;
}
else
{
if(usb_state == ADDRESS_STATE)
break;
for( i=0; i<NUM_ENDPOINT; i++ )
{
if( rEP0SETUP_4 == ep_number[i] )
{
if( i == 0 )
{
/* EndPoint a */
usb_wk_data[0] = ((rEPaControl_BP.ForceSTALL)? 0x01 : 0x00);
}
else
{
if( i == 1 )
{
/* EndPoint b */
usb_wk_data[0] = ((rEPbControl_BP.ForceSTALL)? 0x01 : 0x00);
}
else
{
if( i == 2 )
{
/* EndPoint c */
usb_wk_data[0] = ((rEPcControl_BP.ForceSTALL)? 0x01 : 0x00);
}
}
}
usb_wk_data[1] = 0x00;
total_tran_cnt = actual_tran_cnt = 0x02;
tran_start_addr = &usb_wk_data[0];
ret = REQUEST_OK;
}
}
}
}
}
}
return ret;
}
/*****************************************************************************
* Clear Feature
*****************************************************************************/
int ClearFeature(void)
{
int i, ret = REQUEST_ERROR;
if(usb_state != DEFAULT_STATE){ /* */
if((rEP0SETUP_0 & 0x80) == 0x00){ /* */
switch(rEP0SETUP_0 & 0x1F){
case RECIPIENT_DEVICE:
if(rEP0SETUP_3){ /* wValue.h */
break;
}
if(rEP0SETUP_2 == DEVICE_REMOTE_WAKEUP){
if(rEP0SETUP_5 || rEP0SETUP_4){ /* wIndex */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
if((fs_config_desc.bmAttributes & REMOTEWAKEUP) == 0){
break;
}
usb_flag.EnbRemoteWakeup = 0; /* RemoteWakeup */
ret = REQUEST_OK;
}else{
if(rEP0SETUP_2 == TEST_MODE){
#ifndef TEST_MODE_ON
break;
#endif /* TEST_MODE_ON */
if(rEP0SETUP_5 || rEP0SETUP_4){ /* wIndex */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
/* TestMode */
ret = REQUEST_OK;
}else{
ret = REQUEST_ERROR;
}
}
break;
case RECIPIENT_ENDPOINT:
if(rEP0SETUP_3){ /* wValue.h */
break;
}
if(rEP0SETUP_2 != ENDPOINT_HALT){
break;
}
if(rEP0SETUP_5){ /* wIndex.h */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
if(rEP0SETUP_4 != 0){
/* EndPoint 0 */
if(usb_state == ADDRESS_STATE){
break;
}
for( i=0; i<NUM_ENDPOINT; i++ ){
if( rEP0SETUP_4 == ep_number[i] ){
if( i == 0 ){
/* EndPoint a */
rEPaControl_BP.ToggleClr = 1;
if(rEPaControl_BP.ForceSTALL){
rEPrFIFO_Clr_BP.EPaFIFO_Clr = 1;
if(protocol_phase != RESET_WAIT){
rEPaControl_BP.ForceSTALL = 0;
bulk_in_state = BULK_IN_IDLE;
if(protocol_phase == DATA_PHASE){
protocol_phase = STATUS_PHASE;
tran_flag.XferCmp = 1;
}
}
}
}else{
if( i == 1 ){
/* EndPoint b */
rEPbControl_BP.ToggleClr = 1;
if(rEPbControl_BP.ForceSTALL){
rEPrFIFO_Clr_BP.EPbFIFO_Clr = 1;
if(protocol_phase != RESET_WAIT){
rEPbControl_BP.ForceSTALL = 0;
bulk_out_state = BULK_OUT_IDLE;
if(protocol_phase == DATA_PHASE){
protocol_phase = STATUS_PHASE;
tran_flag.XferCmp = 1;
}
}
}
}else{
if( i == 2 ){
/* EndPoint c */
rEPcControl_BP.ToggleClr = 1;
if(rEPcControl_BP.ForceSTALL){
rEPrFIFO_Clr_BP.EPcFIFO_Clr = 1;
if(protocol_phase != RESET_WAIT){
rEPcControl_BP.ForceSTALL = 0;
int_in_state = INT_IN_IDLE;
}
}
}
}
}
usb_wk_data[1] = 0x00;
total_tran_cnt = actual_tran_cnt = 0x02;
tran_start_addr = &usb_wk_data[0];
ret = REQUEST_OK;
}
}
}
}
}
}
return ret;
}
/*****************************************************************************
* SetFeature
*****************************************************************************/
int SetFeature(void)
{
int i,ret = REQUEST_ERROR;
if((rEP0SETUP_0 & 0x80) == 0x00){ /* */
switch(rEP0SETUP_0 & 0x1F){
case RECIPIENT_DEVICE:
if(rEP0SETUP_3){ /* wValue */
break;
}
if(rEP0SETUP_2 == DEVICE_REMOTE_WAKEUP){
if(usb_state == DEFAULT_STATE){
break;
}
if(rEP0SETUP_5 || rEP0SETUP_4){ /* wIndex */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
if((fs_config_desc.bmAttributes & REMOTEWAKEUP) == 0){
break;
}
usb_flag.EnbRemoteWakeup = 1;
ret = REQUEST_OK;
}else{
if(rEP0SETUP_2 == TEST_MODE){
#ifndef TEST_MODE_ON
break;
#endif /* TEST_MODE_ON */
if(rEP0SETUP_4){ /* wIndex.l */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
switch(rEP0SETUP_5){
case TEST_J_MODE:
rUSB_Test_BP.Test_J = 1;
usb_flag.TestMode = 1; /* TestMode */
ret = REQUEST_OK;
break;
case TEST_K_MODE:
rUSB_Test_BP.Test_K = 1;
usb_flag.TestMode = 1; /* TestMode */
ret = REQUEST_OK;
break;
case TEST_SE0_NAK_MODE:
rUSB_Test_BP.Test_SE0_NAK = 1;
usb_flag.TestMode = 1; /* TestMode */
ret = REQUEST_OK;
break;
case TEST_PACKET_MODE:
/* TestPacket */
rEPcMaxSize_H = 0x00; /* MaxPacketSize = 64Byte */
rEPcMaxSize_L = 0x40;
rEPcConfig_0_BP.INxOUT = 1;
rEPaConfig_0_BP.EnEndPoint = 0;
rEPbConfig_0_BP.EnEndPoint = 0;
rEPcConfig_0_BP.EnEndPoint = 1;
rEPcConfig_0_BP.EndPointNumber = 0x0F;
rEPcStartAdrs_H = 0x03; /* - 64 */
rEPcStartAdrs_L = 0xC0;
rEPdStartAdrs_H = 0x04; /* */
rEPdStartAdrs_L = 0x00;
rEPeStartAdrs_H = 0x04; /* */
rEPeStartAdrs_L = 0x00;
rEPfStartAdrs_H = 0x04; /* */
rEPfStartAdrs_L = 0x00;
rEPgStartAdrs_H = 0x04; /* */
rEPgStartAdrs_L = 0x00;
rEPhStartAdrs_H = 0x04; /* */
rEPhStartAdrs_L = 0x00;
rEPcIntEnb_BP.EnIN_TranErr = 0;
rEPnControl_BP.AllFIFO_Clr = 1;
rCPU_JoinWr_BP.JoinEPcWr = 1;
rEnEPnFIFO_Access_BP.EnEPnFIFO_Wr = 1;
for(i=0; i<53; i++){
rEPnFIFOforCPU = test_packet[i];
}
rEnEPnFIFO_Access_BP.EnEPnFIFO_Wr = 0;
rCPU_JoinWr_BP.JoinEPcWr = 0;
rUSB_Test_BP.Test_Packet = 1;
usb_flag.TestMode = 1; /* TestMode */
ret = REQUEST_OK;
break;
default:
break;
}
}else{
ret = REQUEST_ERROR;
}
}
break;
case RECIPIENT_ENDPOINT:
if(usb_state == DEFAULT_STATE){
break;
}
if(rEP0SETUP_3){ /* wValue.h */
break;
}
if(rEP0SETUP_2 != ENDPOINT_HALT){
break;
}
if(rEP0SETUP_5){ /* wIndex.h */
break;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
break;
}
if(rEP0SETUP_4 != 0){
/* EndPoint 0 */
if(usb_state == ADDRESS_STATE){
break;
}
for( i=0; i<NUM_ENDPOINT; i++ ){
if( rEP0SETUP_4 == ep_number[i] ){
if( i == 0 ){
/* EndPoint a */
if(rEPaControl_BP.ForceSTALL == 0){
rEPaControl_BP.ForceSTALL = 1;
bulk_in_state = BULK_IN_STALL;
}
}else{
if( i == 1 ){
/* EndPoint b */
if(rEPbControl_BP.ForceSTALL == 0){
rEPbControl_BP.ForceSTALL = 1;
bulk_out_state = BULK_OUT_STALL;
}
}else{
if( i == 2 ){
/* EndPoint c */
if(rEPcControl_BP.ForceSTALL == 0){
rEPcControl_BP.ForceSTALL = 1;
int_in_state = INT_IN_STALL;
}
}
}
}
ret = REQUEST_OK;
}
}
}
break;
default:
ret = REQUEST_ERROR;
}
}
return ret;
}
/*****************************************************************************
* Set Address
*****************************************************************************/
int SetAddress(void)
{
int ret;
if(usb_state == CONFIGURED_STATE){
return REQUEST_ERROR;
}
if((rEP0SETUP_0 & 0x80) != 0x00){ /* */
return REQUEST_ERROR;
}
if((rEP0SETUP_0 & 0x1F) != RECIPIENT_DEVICE){
return REQUEST_ERROR;
}
if(rEP0SETUP_3){ /* wValue.h */
return REQUEST_ERROR;
}
if(rEP0SETUP_5 || rEP0SETUP_4){ /* wIndex */
return REQUEST_ERROR;
}
if(rEP0SETUP_7 || rEP0SETUP_6){ /* wLength */
return REQUEST_ERROR;
}
if(rEP0SETUP_2 > 0x7F){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -