Beispiel #1
0
DMP_INLINE(void) USB_Standard_Request(USB_Device *usb)
{
	switch (usb->Setup.bRequest)
	{
		// case 0x00: Get_Status(usb);        break;
		// case 0x01: Clear_Feature(usb);     break;
		// case 0x02: /* Reserved */          break;
		// case 0x03: Set_Feature(usb);       break;
		// case 0x04: /* Reserved */          break;
		case 0x05: Set_Address(usb);       break;
		case 0x06: Get_Descriptor(usb);    break;
		case 0x07: Set_Descriptor(usb);    break;
		case 0x08: Get_Configuration(usb); break;
		case 0x09: Set_Configuration(usb); break;
		case 0x0A: Get_Interface(usb);     break;
		case 0x0B: Set_Interface(usb);     break;
		case 0x0C: Synch_Frame(usb);       break;
		default: break;
	};
}
void handle_Control ( )
{
	unsigned int usb_cnt=0;
	if((EP_STATUS_OUT & 0x01)||(EP_STATUS_IN & 0x01))                                                                                          // 8 byte setup data received
	{     
		EP_STATUS_OUT = 0x01;                                                                                 // write 1 to clear
		EP_STATUS_IN = 0x01;

		EP_STATE[0] = EP_IDLE;    // force EP_STATE[0] to EP_IDLE
		system.usbp0_data.wait_out_report=0;

		if (CNT0 == 8)                // Make sure that EP 0 has an Out Packet of size 8 byte
		{                                
			// added on 2011.5.8 to fix usb set_report error.
			if (EP0_ADDR_DEF==0x21 && EP0_ADDR_DEF_1 == 0x09 )
			{
				USBINT0 = 0xc0;
				Set_Report();
				system.usbp0_data.report_id=EP0_ADDR_DEF_2;
				system.usbp0_data.report_type = EP0_ADDR_DEF_3;
		
				set_ep_rdy(EP_0);			// 1:Endpoint 0 ready for transfer USB data.
			}
			else if(EP0_ADDR_DEF==0x21 && EP0_ADDR_DEF_1 == 0x01 )
			{								// Fix SET_CUR request error
				SET_CUR();
			}
			else
			{
				fifo_Read (EP_0, 8, (UINT8 *) & setup);
				// Get setup Packet off of Fifo,
				// it is currently Big-Endian

				// Compiler Specific - these next three
				// statements swap the bytes of the
				// setup packet words to Big Endian so
				// they can be compared to other 16-bit
				// values elsewhere properly
//				USBINT0 = 0xc0;
//				set_ep_rdy(EP_0);			// 1:Endpoint 0 ready for transfer USB data.
				setup.wValue.i = setup.wValue.c[MSB] + ((int)setup.wValue.c[LSB] << 8);			// High byte and low byte exchange()
				setup.wIndex.i = setup.wIndex.c[MSB] + ( (int)setup.wIndex.c[LSB] << 8);
				setup.wLength.i = setup.wLength.c[MSB] + ( (int)setup.wLength.c[LSB] << 8);

				// Intercept HID class - specific requests
				// Class request.
//				if( (setup.bmRequestType & ~ 0x80) == DSC_HID) 
				if((setup.bmRequestType & cRequestType) == cClassReq)
				{
					#if	0
					switch (setup.bRequest) 
					{
						case GET_REPORT:
							Get_Report ( );
							break;
						case SET_REPORT:
							Set_Report();          
							break;
						case GET_IDLE:
							Get_Idle ( );
							break;
						case SET_IDLE:
							Set_Idle ( );
							break;
						case GET_PROTOCOL:
							Get_Protocol ( );
							break;
						case SET_PROTOCOL:
							Set_Protocol ( );
							break;
						default:
							force_Stall ( );      // Send stall to host if invalid
							break;                 // request
					}
					#endif
					switch(setup.bRequest)
					{
					// bmRequestType, bRequest, bChannelNum.(CN), bControlSelect(CS, include volume, mute etc.), bInterfaceNO, bTerminal/unit ID, wLength.
					// CTL: 21 01 00 02  00 05 02 00
					// DO:  00 00
						case cSET_CUR:
							if(setup.wIndex.c[1] >= 3)		// HID interface.
							{
								Get_Report();
							}
							else
								SET_CUR();
							break;

						case cSET_MIN:
							if(setup.wIndex.c[1] >= 3)		// HID interface.
							{
								Get_Idle();
							}
							else
								SET_MIN();
							break;

						case cSET_MAX:
							SET_MAX();
							break;

						case cSET_RES:
							SET_RES();
							break;

						case cSET_MEM:
							SET_MEM();
							break;

/*
						case GET_REPORT:
							Get_Report();
							break;
*/
						case SET_REPORT:
							Set_Report();          
							break;
/*								
						case GET_IDLE:
							Get_Idle ( );
							break;
*/	
						case SET_IDLE:
							Set_Idle ( );
							break;
								
						case cGET_CUR:
							GET_CUR();
							break;

						case cGET_MIN:
							GET_MIN();
							break;

						case cGET_MAX:
							GET_MAX();
							break;

						case cGET_RES:
							GET_RES();
							break;

						case cGET_MEM:
							GET_MEM();
							break;

						case cGET_STAT:
							GET_STAT();
							break;
							
						default:
							force_Stall ( );      // Send stall to host if invalid
							break;                 // request
					}
					
				} 
				else
				{
					// Standard request.
					switch (setup.bRequest)       // Call correct subroutine to handle
					{                             // each kind of standard request
						case GET_STATUS:
							Get_Status ( );
							break;
						case CLEAR_FEATURE:
							Clear_Feature ( );
							break;
						case SET_FEATURE:
							Set_Feature ( );
							break;
						case SET_ADDRESS:
							Set_Address ( );
							break;
						case GET_DESCRIPTOR:
							Get_Descriptor ( );
							break;
						case GET_CONFIGURATION:
							Get_Configuration ( );
							break;
						case SET_CONFIGURATION:
							Set_Configuration( );
							break;
						case GET_INTERFACE:
							Get_Interface ( );
							break;
						case SET_INTERFACE:		// 01 0b   00 00(wValue)   01 00(wIndex)   00 00
							Set_Interface ( );	// wValue:Interface alternate setting(0x0 is zero bandwith alternate setting, 0x01 is normal isochronous operate)
							break;				// wIndex: Interface number of one of the audiostreaming interface.
						default:
							force_Stall ( );                 // Send stall to host if invalid request
							break;
					}
				}
			}
		}      
		else 
		{
			force_Stall ( );                       // if rec setup data is not 8 byte , protacal stall
		}
	}   
	else if (EP_STATE[0] == EP_SET_ADDRESS)     // Handle Status Phase of Set Address
	{
		FUNCT_ADR = setup.wValue.c[LSB];         // set usb Address SFR
		EP_STATE[0] = EP_IDLE;


	}
	else if (EP_STATE[0] == EP_WAIT_STATUS)
	{
		EP_STATE[0] = EP_IDLE;

		wait_tx_status=0;
	}   
	else if (EP_STATE[0] == EP_RX)              // See if endpoint should transmit
	{  							// Out Token. PC->Device.
		if (DATASIZE >= EP0_PACKET_SIZE)
		{
			fifo_Read(EP_0, EP0_PACKET_SIZE, (unsigned char * )DATAPTR);
			// Advance data pointer
			DATAPTR  += EP0_PACKET_SIZE;
			// Decrement data size
			DATASIZE -= EP0_PACKET_SIZE;
			// Increment data sent counter
			DATASENT += EP0_PACKET_SIZE;            
		}
		else
		{
			// read bytes from FIFO
//			// Report type: 0x01 input, 0x02 output, 0x03 feature report.
			if( system.usbp0_data.wait_out_report && (system.usbp0_data.report_type==PC_SET_REPORT_2) && (system.usbp0_data.report_id==PC_SET_REPORT_1) )
			{
				system.usbp0_data.wait_out_report=0;
				DATAPTR = & EP0_ADDR_DEF;
			
				if(PC_SET_REPORT_1 == DATAPTR[0])
				{
					DATAPTR++;
					DATASIZE--;
				}

				for ( system.usbp0_data.report_cnt = 0;         // read  num = NO  fifo_data;   
					system.usbp0_data.report_cnt< DATASIZE;
					system.usbp0_data.report_cnt ++ )
				{
					system.usbp0_data.set_report_data[system.usbp0_data.report_cnt] = DATAPTR[system.usbp0_data.report_cnt];
				}
				system.usbp0_data.aceept = 1;  
				system.usbp0_data.reday_report_flag = 0;
				set_report_status_phace=1;
			}
			else
			{
				fifo_Read (EP_0, DATASIZE, (UINT8 * ) DATAPTR);
				// KEYBOARD_OUT_REPORT
				if (system.usbp0_data.wait_out_report)
				{
					numlock_sta = system.usbp0_data.out_report[0] & 0x01;
					system.usbp0_data.out_report_index = DATASIZE;
					system.usbp0_data.wait_out_report=0;
					set_report_status_phace=1;
				}

			}

			set_Wait_Status ( );                  // set Endpoint to EP_WAIT_STATUS
		}

		if ( (DATASENT == setup.wLength.i) && (set_report_status_phace==0) )
		{
			set_Wait_Status ( );
		}
		set_report_status_phace=0;
	}   
    
	if (EP_STATE[0] == EP_TX)                                        // See if endpoint should transmit
	{     									// In Token, device->PC

		if ((DATASIZE == 0))// || (DATASENT == setup.wLength.i))   // when all data has been sent over
		{

			set_Wait_Status ( );

		}
		else
		{   
			CFG_EP0_1 = 0xc0; 
			if (DATASIZE >= EP0_PACKET_SIZE)
			{
				// Break Data into multiple packets if larger than Max Packet
				fifo_Write (EP_0, EP0_PACKET_SIZE, (unsigned char*)DATAPTR);
				// Advance data pointer
				DATAPTR  += EP0_PACKET_SIZE;
				// Decrement data size
				DATASIZE -= EP0_PACKET_SIZE;
				// Increment data sent counter
				DATASENT += EP0_PACKET_SIZE;
			}

			else
			{

				// If data is less than Max Packet size or zero
				fifo_Write (EP_0, DATASIZE, (unsigned char*)DATAPTR);
				// Increment data sent counter
				DATASENT += DATASIZE;
				// Decrement data size
				DATASIZE = 0;
				USBINT0 = 0xc0;   
				EP_RDY = 0x01 ;

				// when usb_reset and send ok inttrupt (USBINT0&0x48)!=0 // when setup come (EP_STATUS & 0x01) will set
				 while(((USBINT0 & 0x4C) == 0) && ((EP_STATUS_OUT & 0x01) == 0));   //wait usb_reset, suspend or sending end interrupt  happend, wait setup come
				if((USBINT0&0x40) !=0) 
				{  
					CFG_EP0_1 =0x40;
					EP_RDY = 0x01 ;
					USBINT0 = 0xc0;
					EP_STATE[0]= EP_WAIT_STATUS;

				}
				wait_tx_status = 1; 
				system.usbp0_data.wait_out_report=0;
			} 
		}   
	}


	if (system.usbp0_data.wait_out_report==0 && wait_tx_status ==0)  
	{  
		USBINT0 = 0xc0;
		//   set_ep_rdy(EP_0);   // set ready to receive or send next packet
		EP_RDY = 0x01 ;  
	}
}
Beispiel #3
0
//*****************************************************************************************
// USB Device Enumeration Process
// Support 1 confguration and interface #0 and alternate setting #0 only
// Support up to 1 control endpoint + 4 data endpoint only
//*****************************************************************************************
  u_int8 EnumUsbDev(u_int8 usbaddr)
{  
	  u_int8 i;											// always reset USB transfer address 
	  u_int8 uAddr = 0;							// for enumeration to Address #0
	  u_int8 epLen;
	//  short strLang;
	
	pDevDesc  pDev;	
	pCfgDesc pCfg;
	pIntfDesc pIfc;
	pEPDesc pEnp;
	//------------------------------------------------
	// Reset only Slave device attached directly
	//------------------------------------------------
	uDev.wPayLoad[0] = 64;	// default 64-u_int8 payload of Endpoint 0, address #0
	if(usbaddr == 1)		// bus reset for the device attached to SL811HS only
		USBReset();		// that will always have the USB address = 0x01 (for a hub)
    	
//    task_time_sleep(25);
	
	pDev =(pDevDesc)DBUF;					// ask for 64 u_int8s on Addr #0
	
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=18;
	//usbstack.setup.wLength=sbstack.setup.wLength);
	usbstack.buffer=DBUF;
	
	if (!GetDesc())			// and determine the wPayload size
		return FALSE;								// get correct wPayload of Endpoint 0
	uDev.wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address

	//------------------------------------------------
	// Set Slave USB Device Address
	//------------------------------------------------
	if (!SetAddress(usbaddr)) 						// set to specific USB address
		return FALSE;								//
	uAddr = usbaddr;								// transfer using this new address

	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr X
	//------------------------------------------------
	pDev =(pDevDesc)DBUF;
	usbstack.usbaddr=uAddr;
	
	usbstack.setup.wLength=pDev->bLength;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	
	//usbstack.setup.wLength=0x12;//(  short)DBUF[0];//pDev->bLength;
	usbstack.buffer=DBUF;
	
	if (!GetDesc()) 	
		return FALSE;								// For this current device:
	uDev.wVID  = pDev->idVendor;			// save VID
	uDev.wPID  = pDev->idProduct;			// save PID
	uDev.iMfg  = pDev->iManufacturer;		// save Mfg Index
	uDev.iPdt  = pDev->iProduct;			// save Product Index

	//------------------------------------------------
	// Get Slave USB Configuration Descriptors
	//------------------------------------------------
	
	pCfg = (pCfgDesc)DBUF;	
	
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=CONFIGURATION;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=64;
	usbstack.buffer=DBUF;	
	if (!GetDesc()) 		
		return FALSE;	
	
	pIfc = (pIntfDesc)(DBUF + 9);					// point to Interface Descp
	uDev.bClass 	= pIfc->iClass;			// update to class type
	uDev.bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
	
	if(uDev.bClass==8) //mass storage device
		bXXGFlags.bMassDevice=TRUE;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
	//if (uDev[usbaddr].bClass!=HUBCLASS)				// enumerating a FS/LS non-hub device
		if (!Set_Configuration())		// connected directly to SL811HS
				return FALSE;

	//------------------------------------------------
	// For each slave endpoints, get its attributes
	// Excluding endpoint0, only data endpoints
	//------------------------------------------------
	
	epLen = 0;
	for (i=1; i<=uDev.bNumOfEPs; i++)				// For each data endpoint
	{
		pEnp = (pEPDesc)(DBUF + 9 + 9 + epLen);	   			// point to Endpoint Descp(non-HID)
		uDev.bEPAddr[i]  	= pEnp->bEPAdd;			// Ep address and direction
		uDev.bAttr[i]		= pEnp->bAttr;			// Attribute of Endpoint
		uDev.wPayLoad[i] 	= (pEnp->wPayLoad);		// Payload of Endpoint
		uDev.bInterval[i] 	= pEnp->bInterval;		// Polling interval
	    	uDev.bData1[i] = 0;			            // init data toggle
		epLen += 7;
		//////////////////////////////
		if(uDev.bAttr[i]==0x2)
		{
		    if(uDev.bEPAddr[i]&0x80)
		    	usbstack.epbulkin=uDev.bEPAddr[i];
		    else
		    	usbstack.epbulkout=uDev.bEPAddr[i];
		}
		//////////////////////////////
	}
	
	return TRUE;
}