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

📄 usbsetup.c

📁 S3C2410 USB Mass storage 源码.
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************
 NAME: usbsetup.c
 DESC: process the USB setup stage operations.
 HISTORY:
 MAR.25.2002:purnnamu: S3C2400X usbsetup.c is ported for S3C2410X.
 AUG.20.2002:purnnamu: rEP0_CSR should be used instead of rOUT_CSR1_REG for EP0 macros.
***************************************************************/

#include <string.h>
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"


#include "2410usb.h"
#include "usbmain.h"
#include "usb.h"
#include "usblib.h"
#include "usbsetup.h"

// *** 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 SET_EP0_IN_PKT_RDY() 		rEP0_CSR=( ep0_csr & EP0_IN_PKT_READY)
 
//#define SET_EP0_INPKTRDY_DATAEND() 	rEP0_CSR=( ep0_csr & (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() 		{while(rOUT_FIFO_CNT1_REG)rEP0_FIFO;}

U32 ep0State; //U32=Unsigned int (32bit)
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_HID_DESCRIPTOR descHid;
struct USB_ENDPOINT_DESCRIPTOR descEndpt0;
struct USB_ENDPOINT_DESCRIPTOR descEndpt[2]={0};

static const U8 descStr0[]={
	4,STRING_TYPE,LANGID_US_L,LANGID_US_H,  //codes representing languages
    };//Total 4 string

U8 DeviceStatus=0, InterfaceStatus=0, EndPoint0Status=0;
U8 EndPoint1Status=0, EndPoint3Status=0, DeviceProtocolStatus=0;
U8 DeviceIdleStatus=0, InterfaceRegister=0;


unsigned char LANGUAGE_ID[4]={0x04,0x03,0x09,0x04}; 		 //字符串描述符所用的语言种类		 
				 
unsigned char device_serial_number[18]= 					  //设备序列号
{0x12,0x03,0x32,0x00,0x30,0x00,0x37,0x00,0x31,0x00,0x30,0x00,0x39,0x00,0x38,0x00,0x32,0x00};


//厂商字符串
U8 ManufacturerString[80]=
{80,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x84,0x76,0xB6,0x5B,
 0x53,0x5F,'-',0x00,'-',0x00,'-',0x00,'-',0x00,'@',0,0x20,0x00,'H',0,'t',0,'t',0,
 'p',0,':',0,'/',0,'/',0,'C',0,'o',0,'m',0,'p',0,'u',0,'t',0,'e',0,'r',0,'0',0,
 '0',0,'.',0,'2',0,'1',0,'i',0,'c',0,'.',0,'o',0,'r',0,'g',0};

//产品字符串
U8 ProducterString[80]=
{80,STRING_DESCRIPTOR,0x35,0x75,0x11,0x81,0x08,0x57,0x08,0x57,0x5A,0x50,0x84,0x76,
 'U',0,'S',0,'B',0,0x2E,0x95,0xD8,0x76,'@',0,0x20,0x00,'H',0,'t',0,'t',0,
 'p',0,':',0,'/',0,'/',0,'C',0,'o',0,'m',0,'p',0,'u',0,'t',0,'e',0,'r',0,'0',0,
 '0',0,'.',0,'2',0,'1',0,'i',0,'c',0,'.',0,'o',0,'r',0,'g',0};

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,
    };//Total 22 string
    
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
    };//Total 44 string


//Struct definition at "USB.H"
void InitDescriptorTable(void)
{	
    //Standard device descriptor
		descDev.bLength=0x12;//EP0_DEV_DESC_SIZE=0x12 bytes    
		descDev.bDescriptorType=DEVICE_TYPE;		 
		descDev.bcdUSBL=0x10;
		descDev.bcdUSBH=0x01;//Indicate USB Ver 1.10
		descDev.bDeviceClass=0x0;//Vendor specific
		descDev.bDeviceSubClass=0x0;		  
		descDev.bDeviceProtocol=0x0;//?? should be 0xff 		 
		descDev.bMaxPacketSize0=16;//EndPoint_0's max Packet Size
		descDev.idVendorL=0x45;//Vendor ID_h
		descDev.idVendorH=0x53;//Vendor ID_l
		descDev.idProductL=0x00;//Product ID_h
		descDev.idProductH=0x12;//Product ID_l
		descDev.bcdDeviceL=0x00;//Product version_h
		descDev.bcdDeviceH=0x01;//Product version_l
		descDev.iManufacturer=0x00;//index of string descriptor index
		descDev.iProduct=0x00;//index of string descriptor index
		descDev.iSerialNumber=0x02;//Product serial number string descriptor index
		descDev.bNumConfigurations=0x01;//Device descriptor number
	
	//Standard configuration descriptor
		descConf.bLength=0x09;	  
		descConf.bDescriptorType=CONFIGURATION_TYPE;//0x02
		descConf.wTotalLengthL=0x20; //<cfg 9>+<if 9>+<endp0 7>||+<endp1 7>
		descConf.wTotalLengthH=0x00;
		descConf.bNumInterfaces=0x01;
		descConf.bConfigurationValue=0x01;	
		descConf.iConfiguration=0x00;
		descConf.bmAttributes=0x80;//bus powered only.
		descConf.maxPower=0xC8; //draws 50mA current from the USB bus.			
	
	//Standard interface descriptor
		descIf.bLength=0x09;
		descIf.bDescriptorType=INTERFACE_TYPE;//0x04
		descIf.bInterfaceNumber=0x00;
		descIf.bAlternateSetting=0x00; //?
		descIf.bNumEndpoints=0x02;	//# of endpoints except EP0
		descIf.bInterfaceClass=0x08; //0x0 ?
		descIf.bInterfaceSubClass=0x06;
		descIf.bInterfaceProtocol=0x50;
		descIf.iInterface=0x00;
	
	//Standard endpoint0 descriptor
		descEndpt[0].bLength=0x07;
		descEndpt[0].bDescriptorType=ENDPOINT_TYPE;//0x05 		
		descEndpt[0].bEndpointAddress=1|EP_ADDR_IN;//0x80|0x01=0x81	// 2400Xendpoint 1 is IN endpoint.
		descEndpt[0].bmAttributes=EP_ATTR_BULK;//0x02,transmition type = Bulk
		descEndpt[0].wMaxPacketSizeL=0x40; //64
		descEndpt[0].wMaxPacketSizeH=0x00;
		descEndpt[0].bInterval=0x00; //10 mS interval
	
	//Standard endpoint1 descriptor
		descEndpt[1].bLength=0x7;    
		descEndpt[1].bDescriptorType=ENDPOINT_TYPE;//0x05 		
		descEndpt[1].bEndpointAddress=3|EP_ADDR_OUT;//0x00|0x03=0x03	 // 2400X endpoint 3 is OUT endpoint.
		descEndpt[1].bmAttributes=EP_ATTR_BULK;//0x02,transmition type = Bulk
		descEndpt[1].wMaxPacketSizeL=0x40;//EP3_PKT_SIZE; //64
		descEndpt[1].wMaxPacketSizeH=0x00;
		descEndpt[1].bInterval=0x00; //not used 


}


void Ep0Handler(void)
{
	static int ep0SubState=0;
	int i;
	U8 ep0_csr;

	rINDEX_REG=0;
	ep0_csr=rEP0_CSR;//Push to buffer
//	DbgPrintf("Ep0\n");
	if(ep0_csr & EP0_SETUP_END)//define EP0_SETUP_END=0x10
	{
//		DbgPrintf("Ep0_1\n");
		//Uart_Printf("-----------\n");
		CLR_EP0_SETUP_END();
		if(ep0_csr & EP0_OUT_PKT_READY) 
		{
			FLUSH_EP0_FIFO(); //(???)
			CLR_EP0_OUT_PKT_RDY();
		}
		ep0State=EP0_STATE_INIT;//#define EP0_STATE_INIT=0
		return;
	}	

	//I think that EP0_SENT_STALL will not be set to 1.
	if(ep0_csr & EP0_SENT_STALL)
	{
//		DbgPrintf("Ep0_2\n");
		CLR_EP0_SENT_STALL();
		if(ep0_csr & EP0_OUT_PKT_READY) 
		{
			CLR_EP0_OUT_PKT_RDY();
		}
		ep0State=EP0_STATE_INIT;//#define EP0_STATE_INIT=0
		return;
	}


	if((ep0_csr & EP0_OUT_PKT_READY) && (ep0State==EP0_STATE_INIT))
	{	
		RdPktEp0((U8 *)&descSetup,EP0_PKT_SIZE);//Read data from EP0_FIFO
		CLR_EP0_OUT_PKT_RDY();
		
		if((descSetup.bmRequestType==0xA1)&&(descSetup.bRequest==0xFE))//get_max_LUN
		{
			rEP0_FIFO = MAX_LUN_NUMBER;
			SET_EP0_IN_PKT_RDY();
			//SET_EP0_INPKTRDY_DATAEND();
		}
		
		if((descSetup.bmRequestType==0x21)&&(descSetup.bRequest==0xFF))//mass_storage_reset
		{
			CLR_EP0_OUTPKTRDY_DATAEND();
		}
			
		switch(descSetup.bRequest){
			case GET_STATUS:
				switch(descSetup.bmRequestType){
					rEP0_FIFO=EndPoint0;
					case 0x80:
						rEP0_FIFO=DeviceStatus;
						break;
					case 0x81:
						rEP0_FIFO=InterfaceStatus;
						break;
					case 0x82:
						switch(descSetup.bIndexL){
							case 0x01:
								rEP0_FIFO=EndPoint1Status;
								break;
							case 0x03:
								rEP0_FIFO=EndPoint3Status;
								break;
							}
						break;
					default:
						CLR_EP0_OUTPKTRDY_DATAEND();
						break;
					}
				SET_EP0_IN_PKT_RDY();
				break;
			case CLEAR_FEATURE: // HID lass Command 0x01, Get Report
//				CLR_EP0_OUT_PKT_RDY();
				switch(descSetup.bmRequestType){
					case 0:
						DeviceStatus=0;
						break;
					case 0x01:
						InterfaceStatus=0;
						break;
					case 0x02:
						switch(descSetup.bIndexL){
							case 0x01:
								EndPoint0Status=0;
								break;
							case 0x03:
								EndPoint3Status=0;
								break;
							}
						break;
					case 0xA1:
						switch(descSetup.bValueL){
							case 0x01:
								break;
							case 0x02:
								break;
							case 0x03:
								break;
							default:
								break;
							}
						break;
					}
				break;
			case GET_IDLE: // HID Class command 0x02, Get Idle
				rEP0_FIFO = DeviceIdleStatus;
				SET_EP0_IN_PKT_RDY();
				ep0State = EP0_STATE_INIT; 
				break;

⌨️ 快捷键说明

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