예제 #1
0
파일: usb.c 프로젝트: bluefix/picsdr
// ----------------------------------------------------------------------
// Inspect an incoming packet.
// ----------------------------------------------------------------------
static	void	usb_receive ( byte_t* data, byte_t rx_len )
{
	byte_t	len;
	byte_t	type;
	byte_t	limit;

	usb_tx_state = TX_STATE_RAM;
	len = 0;
	if	( usb_rx_token == USB_PID_SETUP )
	{
		limit = data[6];
		if	( data[7] )
		{
			limit = 255;
		}
		type = data[0] & 0x60;
		if	( type == 0x00 )
		{	// Standard request
			if	( data[1] == 0 )	// GET_STATUS
			{
				len = 2;
#if	USBTINY_MAX_POWER == 0
				data[0] = (data[0] == 0x80);
#else
				data[0] = 0;
#endif
				data[1] = 0;
			}
			else if	( data[1] == 5 )	// SET_ADDRESS
			{
				new_address = data[2];
			}
			else if	( data[1] == 6 )	// GET_DESCRIPTOR
			{
				usb_tx_state = TX_STATE_ROM;
				if	( data[3] == 1 )
				{	// DEVICE
					data = (byte_t*) &descr_device;
					len = sizeof(descr_device);
				}
				else if	( data[3] == 2 )
				{	// CONFIGURATION
					data = (byte_t*) &descr_config;
					len = sizeof(descr_config);
					// If we got this far, there's a good chance everything is OK with enumeration so turn on the OK led
					PORTD = _BV(5);

				}
#if	VENDOR_NAME_ID || DEVICE_NAME_ID || SERIAL_ID
				else if	( data[3] == 3 )
				{	// STRING
					if	( data[2] == 0 )
					{
						data = (byte_t*) &string_langid;
						len = sizeof(string_langid);
					}
#if	VENDOR_NAME_ID
					else if	( data[2] == VENDOR_NAME_ID )
					{
						data = (byte_t*) &string_vendor;
						len = sizeof(string_vendor);
					}
#endif
#if	DEVICE_NAME_ID
					else if ( data[2] == DEVICE_NAME_ID )
					{
						data = (byte_t*) &string_device;
						len = sizeof(string_device);
					}
#endif
#if	SERIAL_ID
					else if ( data[2] == SERIAL_ID )
					{
						data = (byte_t*) &string_serial;
						len = sizeof(string_serial);
					}
#endif
				}
#endif
			}
			else if	( data[1] == 8 )	// GET_CONFIGURATION
			{
				data[0] = 1;		// return bConfigurationValue
				len = 1;
			}
			else if	( data[1] == 10 )	// GET_INTERFACE
			{
				data[0] = 0;
				len = 1;
			}
		}
		else
		{	// Class or Vendor request
			len = usb_setup( data );
#if	USBTINY_CALLBACK_IN
			if	( len == 0xff )
			{
				usb_tx_state = TX_STATE_CALLBACK;
			}
#endif
		}
		if	( len > limit )
		{
			len = limit;
		}
		usb_tx_data = data;
	}
#if	USBTINY_CALLBACK_OUT
	else if	( rx_len > 0 )
	{	// usb_rx_token == USB_PID_OUT
		usb_out( data, rx_len );
	}
#endif
	usb_tx_total  = len;
	usb_tx_buf[0] = USB_PID_DATA0;	// next data packet will be DATA1
}
예제 #2
0
파일: usb.c 프로젝트: cyborg-x1/OpenServo
// ----------------------------------------------------------------------
// Inspect an incoming packet.
// ----------------------------------------------------------------------
static	void	usb_receive ( byte_t* data, byte_t rx_len )
{
	byte_t	len;
	byte_t	type;
	txlen_t	limit;

	usb_tx_state = TX_STATE_RAM;
	len = 0;
	limit = 0;
	if	( usb_rx_token == USB_PID_SETUP )
	{
#if	USBTINY_CALLBACK_IN == 2
		limit = * (uint_t*) & data[6];
#else
		limit = data[6];
		if	( data[7] )
		{
			limit = 255;
		}
#endif
		type = data[0] & 0x60;
		if	( type == 0x00 )
		{	// Standard request
			if	( data[1] == 0 )	// GET_STATUS
			{
				len = 2;
#if	USBTINY_MAX_POWER == 0
				data[0] = (data[0] == 0x80);
#else
				data[0] = 0;
#endif
				data[1] = 0;
			}
			else if	( data[1] == 5 )	// SET_ADDRESS
			{
				usb_new_address = data[2];
#ifdef	USBTINY_USB_OK_LED
				SET(USBTINY_USB_OK_LED);// LED on
#endif
			}
			else if	( data[1] == 6 )	// GET_DESCRIPTOR
			{
				usb_tx_state = TX_STATE_ROM;
				if	( data[3] == 1 )
				{	// DEVICE
					data = (byte_t*) &descr_device;
					len = sizeof(descr_device);
				}
				else if	( data[3] == 2 )
				{	// CONFIGURATION
					data = (byte_t*) &descr_config;
					len = sizeof(descr_config);
				}
#if	VENDOR_NAME_ID || DEVICE_NAME_ID || SERIAL_ID
				else if	( data[3] == 3 )
				{	// STRING
					if	( data[2] == 0 )
					{
						data = (byte_t*) &string_langid;
						len = sizeof(string_langid);
					}
#if	VENDOR_NAME_ID
					else if	( data[2] == VENDOR_NAME_ID )
					{
						data = (byte_t*) &string_vendor;
						len = sizeof(string_vendor);
					}
#endif
#if	DEVICE_NAME_ID
					else if ( data[2] == DEVICE_NAME_ID )
					{
						data = (byte_t*) &string_device;
						len = sizeof(string_device);
					}
#endif
#if	SERIAL_ID
					else if ( data[2] == SERIAL_ID )
					{
						data = (byte_t*) &string_serial;
						len = sizeof(string_serial);
					}
#endif
				}
#endif
			}
			else if	( data[1] == 8 )	// GET_CONFIGURATION
			{
				data[0] = 1;		// return bConfigurationValue
				len = 1;
			}
			else if	( data[1] == 10 )	// GET_INTERFACE
			{
				data[0] = 0;
				len = 1;
			}
		}
		else
		{	// Class or Vendor request
			len = usb_setup( data );
#if	USBTINY_CALLBACK_IN
			if	( len == 0xff )
			{
				usb_tx_state = TX_STATE_CALLBACK;
			}
#endif
		}
		if	(  len < limit
#if	USBTINY_CALLBACK_IN == 2
			&& len != 0xff
#endif
			)
		{
			limit = len;
		}
		usb_tx_data = data;
	}
#if	USBTINY_CALLBACK_OUT
	else if	( rx_len > 0 )
	{	// usb_rx_token == USB_PID_OUT
		usb_out( data, rx_len );
	}
#endif
	usb_tx_total  = limit;
	usb_tx_buf[0] = USB_PID_DATA0;	// next data packet will be DATA1
}