/*This file is prepared for Doxygen automatic documentation generation.*/
//! \file *********************************************************************
//!
//! \brief user call-back functions
//!
//!  This file contains the user call-back functions corresponding to the
//!  application:
//!
//! - Compiler:           IAR EWAVR and GNU GCC for AVR
//! - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
//!
//! \author               Atmel Corporation: http://www.atmel.com \n
//!                       Support and FAQ: http://support.atmel.no/
//!
//! ***************************************************************************

/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an Atmel
 * AVR product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


//_____ I N C L U D E S ____________________________________________________

#include "config.h"
#include "conf_usb.h"
#include "lib_mcu/usb/usb_drv.h"
#include "modules/usb/descriptors/usb_tmc_descriptors.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "modules/usb/device_chap9/usb_specific_request.h"
#if ((USB_DEVICE_SN_USE==ENABLE) && (USE_DEVICE_SN_UNIQUE==ENABLE))
#include "lib_mcu/flash/flash_drv.h"
#endif

#define  F_CPU 16000000UL
#include <util/delay.h>

//_____ D E F I N I T I O N ________________________________________________

#ifdef __GNUC__
extern PGM_VOID_P pbuffer;
#else
extern U8   code *pbuffer;
#endif
extern U8   data_to_transfer;
Bool  usb_get_capabilities			(void);


//_____ D E C L A R A T I O N ______________________________________________

//! @breif This function checks the specific request and if known then processes it
//!
//! @param type      corresponding at bmRequestType (see USB specification)
//! @param request   corresponding at bRequest (see USB specification)
//!
//! @return TRUE,  when the request is processed
//! @return FALSE, if the request is'nt know (STALL handshake is managed by the main standard request function).
//!
Bool usb_user_read_request(U8 type, U8 request)
{
   U8  wValue_msb;
   U8  wValue_lsb;
   U8  s_lcd[32];

   // Read wValue
   wValue_lsb = Usb_read_byte();
   wValue_msb = Usb_read_byte();
   
   //** Specific request from Class
   if( USB_SETUP_GET_STAND_INTERFACE == type )
   {
      switch( request )
      {
         case SETUP_GET_DESCRIPTOR:
         switch( wValue_msb ) // Descriptor ID
         {
            default:
            // TODO
            break;
         }
         break;
      }
   }
   if( USB_SETUP_SET_CLASS_INTER == type )
   {
      switch( request )
      {
         default:
         break;
      }
   }
   if( USB_SETUP_GET_CLASS_INTER == type )
   {
      switch( request )
      {
         case GET_CAPABILITIES:
			//lcd_clrscr();
			//lcd_puts("Get capabilities request came");
			if( usb_get_capabilities() ) return;
			break;
			
		 default:
			break;
      }
   }
   
   //lcd_clrscr();
   //sprintf(s_lcd,"Req    : 0x%02X   Type   : 0x%02X   ",request,type);
   //lcd_puts(s_lcd);
   //_delay_ms(1000);
   return FALSE;  // No supported request
}


Bool usb_get_capabilities (void) {
	Bool zlp;
	U16  wLength;
	U8	 wInterface_msb;
	U8   wInterface_lsb;
	U8   byte_to_send;
	U8   nb_byte;
	
	zlp            = FALSE;
	wInterface_lsb = Usb_read_byte();
	wInterface_msb = Usb_read_byte();
	
	if (wInterface_lsb != INTERFACE_NB_TMC)
		return FALSE;
	else {
		data_to_transfer = Usb_get_intf_capab_length(); //!< sizeof (usb_user_configuration_descriptor);
		pbuffer          = Usb_get_intf_capab_pointer();
	}
		
	LSB(wLength) = Usb_read_byte();              //!< read wLength
	MSB(wLength) = Usb_read_byte();
	Usb_ack_receive_setup() ;
	
	if (wLength > data_to_transfer) 
	{
		if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
		else { zlp = FALSE; }                   //!< no need of zero length packet
	}
	else
	{
		data_to_transfer = (U8)wLength;         //!< send only requested number of data
	}
	
	Usb_ack_nak_out();
	
	byte_to_send=0;
	while((data_to_transfer != 0) && (!Is_usb_nak_out_sent()))
	{
		while(!Is_usb_read_control_enabled())
		{
			if (Is_usb_nak_out_sent())
				break;    // don't clear the flag now, it will be cleared after
			if (Is_usb_vbus_low())
				break;
		}
		
		nb_byte=0;
		while(data_to_transfer != 0)        //!< Send data until necessary
		{
			if(nb_byte++==EP_CONTROL_LENGTH) //!< Check endpoint 0 size
				break;
			Usb_write_PGM_byte(pbuffer++);
			data_to_transfer --;
		}
		
		if (Is_usb_nak_out_sent())
			break;
		if (Is_usb_vbus_low())
			break;
		Usb_send_control_in();
	}
	
	if((zlp == TRUE) && (!Is_usb_nak_out_sent()))
	{
		while(!Is_usb_read_control_enabled()) {if(Is_usb_vbus_low())break;}
		Usb_send_control_in();
	}

	while (!(Is_usb_nak_out_sent())) {if(Is_usb_vbus_low())break;}
	Usb_ack_nak_out();
	Usb_ack_control_out();
	return TRUE;			
}











//! @brief This function configures the endpoints
//!
//! @param conf_nb configuration number choosed by USB host
//!
void usb_user_endpoint_init(U8 conf_nb)
{
   usb_configure_endpoint(EP_BULK_IN,    \
                          TYPE_BULK,     \
                          DIRECTION_IN,  \
                          SIZE_64,       \
                          ONE_BANK,      \
                          NYET_ENABLED);
   usb_configure_endpoint(EP_BULK_OUT,   \
                          TYPE_BULK,     \
                          DIRECTION_OUT, \
                          SIZE_64,       \
                          ONE_BANK,      \
                          NYET_ENABLED);
   usb_configure_endpoint(EP_INT_IN,	 \
                          TYPE_INTERRUPT,\
                          DIRECTION_IN,	 \
                          SIZE_64,       \
                          ONE_BANK,      \
                          NYET_ENABLED);
}


//! @brief This function returns the interface alternate setting
//!
//! @param wInterface         Interface selected
//!
//! @return alternate setting configurated
//!
U8   usb_user_interface_get( U16 wInterface )
{
   return 0;  // Only one alternate setting possible for all interface
}


//! @brief This function selects (and resets) the interface alternate setting
//!
//! @param wInterface         Interface selected
//! @param alternate_setting  alternate setting selected
//!
void usb_user_interface_reset(U16 wInterface, U8 alternate_setting)
{  
   // default setting selected = reset data toggle
   if( INTERFACE_NB_TMC == wInterface )
   {
      // Interface reset
      Usb_select_endpoint(EP_BULK_IN);
      Usb_disable_stall_handshake();
      Usb_reset_endpoint(EP_BULK_IN);
      Usb_reset_data_toggle();
      Usb_select_endpoint(EP_BULK_OUT);
      Usb_disable_stall_handshake();
      Usb_reset_endpoint(EP_BULK_OUT);
      Usb_reset_data_toggle();
	  //-----ADDED BECAUSE OF TMC
	  Usb_select_endpoint(EP_INT_IN);
	  Usb_disable_stall_handshake();
	  Usb_reset_endpoint(EP_INT_IN);
	  Usb_reset_data_toggle();
   }
   /*
   if( INTERFACE_NB_SECOND_TEMP == wInterface )
   {
      // Interface reset
      Usb_select_endpoint(EP_TEMP_INT_IN);
      Usb_disable_stall_handshake();
      Usb_reset_endpoint(EP_TEMP_INT_IN);
      Usb_reset_data_toggle();
   }
   */
}

   
//! This function fills the global descriptor
//!
//! @param type      corresponding at MSB of wValue (see USB specification)
//! @param string    corresponding at LSB of wValue (see USB specification)
//!
//! @return FALSE, if the global descriptor no filled
//!
Bool usb_user_get_descriptor(U8 type, U8 string)
{
   switch(type)
   {
      case DESCRIPTOR_STRING:
      switch (string)
      {
         case LANG_ID:
         data_to_transfer = sizeof (usb_user_language_id);
         pbuffer = &(usb_user_language_id.bLength);
         return TRUE;
         break;
        
         case MAN_INDEX:
         data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
         pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
         return TRUE;
         break;
        
         case PROD_INDEX:
         data_to_transfer = sizeof (usb_user_product_string_descriptor);
         pbuffer = &(usb_user_product_string_descriptor.bLength);
         return TRUE;
         break;
           
#if (USB_DEVICE_SN_USE==ENABLE)              
         case SN_INDEX:
         data_to_transfer = sizeof (usb_user_serial_number);
         pbuffer = &(usb_user_serial_number.bLength);
	#if (USE_DEVICE_SN_UNIQUE==ENABLE)
         f_get_serial_string=TRUE;
         data_to_transfer += (SN_LENGTH*4);
	#endif
         return TRUE;
         break;
#endif
		case FW_INDEX:
		data_to_transfer = sizeof (usb_user_firmware_string_descriptor);
		pbuffer = &(usb_user_firmware_string_descriptor.bLength);
		return TRUE;
		break;
      }
      break;
   }
   return FALSE;
}


