
#ifndef _USB_TMC_DESCRIPTORS_H_
#define _USB_TMC_DESCRIPTORS_H_

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

#include "config.h"
#include "modules/usb/device_chap9/usb_standard_request.h"
#include "conf_usb.h"

//_____ M A C R O S ________________________________________________________

#define Usb_unicode(a)         ((U16)(a))
#define Usb_get_dev_desc_pointer()        (&(usb_dev_desc.bLength))
#define Usb_get_dev_desc_length()         (sizeof (usb_dev_desc))
#define Usb_get_conf_desc_pointer()       (&(usb_conf_desc.cfg_tmc.bLength))
#define Usb_get_conf_desc_length()        (sizeof (usb_conf_desc))
#define Usb_get_intf_capab_pointer()      (&(usb_int_capab.bUSBTMC_status))
#define Usb_get_intf_capab_length()       (sizeof (usb_int_capab))

//_____ U S B    D E F I N E _______________________________________________

                  // USB Device descriptor
#define USB_SPECIFICATION			0x0200
#define DEVICE_CLASS				0			//! each configuration has its own class
#define DEVICE_SUB_CLASS			0			//! each configuration has its own sub-class
#define DEVICE_PROTOCOL				0			//! each configuration has its own protocol
#define EP_CONTROL_LENGTH			64
#define VENDOR_ID					VID_ATMEL
#define PRODUCT_ID					0x1234
#define RELEASE_NUMBER				0x1000
#define MAN_INDEX					0x01
#define PROD_INDEX					0x02   
#if (USB_DEVICE_SN_USE==ENABLE)
	#define SN_INDEX				0x03
#else
	#define SN_INDEX				0x00		// No serial number field
#endif
#define FW_INDEX					0x04
#define NB_CONFIGURATION			1

// CONFIGURATION
#define NB_INTERFACE				1								// Number of interfaces
#define CONF_NB						1								//! Number of this configuration
#define CONF_INDEX					0
#define CONF_ATTRIBUTES				USB_CONFIG_BUSPOWERED
#define MAX_POWER					100								// 200 mA

// USB Interface descriptor gen
#define INTERFACE_NB_TMC			0								//! The number of this interface
#define ALTERNATE_TMC				0								//! The alt settting nb of this interface
#define NB_ENDPOINT_TMC				3								//! The number of endpoints this this interface have
#define INTERFACE_CLASS_TMC			APPLICATION_CLASS				//! Class		Application-Class
#define INTERFACE_SUB_CLASS_TMC		TMC_SUBCLASS					//! Sub Class
#define INTERFACE_PROTOCOL_TMC		PROTOCOL_USBTMC_ONLY			//! Protocol 0-USBTMC, 1-USBTMC_USB488
#define INTERFACE_INDEX_TMC			0

// USB Endpoint 1 descriptor FS (EP_BULK_IN)
#define ENDPOINT_NB_BULK_IN			(EP_BULK_IN | USB_ENDPOINT_IN)
#define EP_ATTRIBUTES_BULK_IN		0x02							// BULK = 0x02, INTERUPT = 0x03
#define EP_IN_LENGTH_BULK_IN		64
#define EP_SIZE_BULK_IN				EP_IN_LENGTH_BULK_IN
#define EP_INTERVAL_BULK_IN			0x00							// Interrupt polling interval from host

// USB Endpoint 2 descriptor FS (EP_BULK_OUT)
#define ENDPOINT_NB_BULK_OUT		EP_BULK_OUT
#define EP_ATTRIBUTES_BULK_OUT		0x02							// BULK = 0x02, INTERUPT = 0x03
#define EP_IN_LENGTH_BULK_OUT		64
#define EP_SIZE_BULK_OUT			EP_IN_LENGTH_BULK_OUT
#define EP_INTERVAL_BULK_OUT		0x00							// Interrupt polling interval from host

// USB Endpoint 3 descriptor FS (EP_INTERRUPT_IN) 
#define ENDPOINT_NB_INT_IN			(EP_INT_IN | USB_ENDPOINT_IN)
#define EP_ATTRIBUTES_INT_IN		0x03							// BULK = 0x02, INTERUPT = 0x03
#define EP_IN_LENGTH_INT_IN			64
#define EP_SIZE_INT_IN				EP_IN_LENGTH_INT_IN
#define EP_INTERVAL_INT_IN			20								// Interrupt polling interval from host


#define DEVICE_STATUS				USB_DEVICE_STATUS_BUS_POWERED

#define LANG_ID						0x00


#define USB_MN_LENGTH         16
#define USB_MANUFACTURER_NAME_CHARS \
  Usb_unicode('L') \
, Usb_unicode('u') \
, Usb_unicode('b') \
, Usb_unicode('o') \
, Usb_unicode('m') \
, Usb_unicode('i') \
, Usb_unicode('r') \
, Usb_unicode(' ') \
, Usb_unicode('S') \
, Usb_unicode('e') \
, Usb_unicode('h') \
, Usb_unicode('n') \
, Usb_unicode('a') \
, Usb_unicode('l') \
, Usb_unicode('e') \
, Usb_unicode('k') \


#define USB_MANUFACTURER_NAME \
{ USB_MANUFACTURER_NAME_CHARS \
}

#define USB_PN_LENGTH         9
#define USB_PRODUCT_NAME_CHARS \
 Usb_unicode('L') \
,Usb_unicode('a') \
,Usb_unicode('b') \
,Usb_unicode(' ') \
,Usb_unicode('D') \
,Usb_unicode('C') \
,Usb_unicode(' ') \
,Usb_unicode('P') \
,Usb_unicode('S') \



#define USB_PRODUCT_NAME \
{ USB_PRODUCT_NAME_CHARS \
}


// Serial Number should be at least 12 characters long
#define USB_SN_LENGTH         0x0D
#define USB_SERIAL_NUMBER_CHARS \
Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \
,Usb_unicode('0') \

			  
#define USB_SERIAL_NUMBER \
{ USB_SERIAL_NUMBER_CHARS \
}

#define USB_FW_LENGTH         7
#define USB_FW_REVISION_CHARS \
 Usb_unicode(V_major) \
,Usb_unicode('.') \
,Usb_unicode(V_minor) \
,Usb_unicode('.') \
,Usb_unicode(V_build100) \
,Usb_unicode(V_build10) \
,Usb_unicode(V_build1) \


#define USB_FIRMWARE_REVISION \
{ USB_FW_REVISION_CHARS \
}

#define LANGUAGE_ID           0x0409


//! Usb Request
typedef struct
{
   U8      bmRequestType;       //!< Characteristics of the request
   U8      bRequest;            //!< Specific request
   U16     wValue;              //!< field that varies according to request
   U16     wIndex;              //!< field that varies according to request
   U16     wLength;             //!< Number of bytes to transfer if Data
}  S_UsbRequest;

//! Usb Device Descriptor
typedef struct {
   U8      bLength;             //!< Size of this descriptor in bytes
   U8      bDescriptorType;     //!< DEVICE descriptor type
   U16     bscUSB;              //!< Binay Coded Decimal Spec. release
   U8      bDeviceClass;        //!< Class code assigned by the USB
   U8      bDeviceSubClass;     //!< Sub-class code assigned by the USB
   U8      bDeviceProtocol;     //!< Protocol code assigned by the USB
   U8      bMaxPacketSize0;     //!< Max packet size for EP0
   U16     idVendor;            //!< Vendor ID. ATMEL = 0x03EB
   U16     idProduct;           //!< Product ID assigned by the manufacturer
   U16     bcdDevice;           //!< Device release number
   U8      iManufacturer;       //!< Index of manu. string descriptor
   U8      iProduct;            //!< Index of prod. string descriptor
   U8      iSerialNumber;       //!< Index of S.N.  string descriptor
   //U8      iFirmwareVersion;     //!< Index of FW  string descriptor  ---------------------------
   U8      bNumConfigurations;  //!< Number of possible configurations
}  S_usb_device_descriptor;

//! Usb Device Qualifier Descriptor - if only full speed, then disable
 /*
 typedef struct {
	 U8      bLength;              //!< Size of this descriptor in bytes
	 U8      bDescriptorType;      //!< DEVICE descriptor type
	 U16     bscUSB;               //!< Binay Coded Decimal Spec. release
	 U8      bDeviceClass;         //!< Class code assigned by the USB
	 U8      bDeviceSubClass;      //!< Sub-class code assigned by the USB
	 U8      bDeviceProtocol;      //!< Protocol code assigned by the USB
	 U8      bMaxPacketSize0;      //!< Max packet size for EP0
	 U8      bNumConfigurations;   //!< Number of possible configurations
 }  S_usb_device_Qualifier_descriptor;
*/ 

//! Usb Configuration Descriptor
typedef struct {
   U8      bLength;             //!< size of this descriptor in bytes
   U8      bDescriptorType;     //!< CONFIGURATION descriptor type
   U16     wTotalLength;        //!< total length of data returned
   U8      bNumInterfaces;      //!< number of interfaces for this conf.
   U8      bConfigurationValue; //!< value for SetConfiguration resquest
   U8      iConfiguration;      //!< index of string descriptor
   U8      bmAttibutes;         //!< Configuration characteristics
   U8      MaxPower;			//!< maximum power consumption
}  S_usb_configuration_descriptor;


//! Usb Interface Descriptor
typedef struct {
   U8      bLength;             //!< size of this descriptor in bytes
   U8      bDescriptorType;     //!< INTERFACE descriptor type
   U8      bInterfaceNumber;    //!< Number of interface
   U8      bAlternateSetting;   //!< value to select alternate setting
   U8      bNumEndpoints;       //!< Number of EP except EP 0
   U8      bInterfaceClass;     //!< Class code assigned by the USB		USBTMC class = 0xFE
   U8      bInterfaceSubClass;  //!< Sub-class code assigned by the USB	USBTMC subclass = 0x03
   U8      bInterfaceProtocol;  //!< Protocol code assigned by the USB	protocol = 0-USBTMC, 1-USBTMC-USB488
   U8      iInterface;			//!< Index of string descriptor
}  S_usb_interface_descriptor;


//! Usb Endpoint Descriptor
typedef struct {
   U8      bLength;             //!< Size of this descriptor in bytes
   U8      bDescriptorType;     //!< ENDPOINT descriptor type
   U8      bEndpointAddress;    //!< Address of the endpoint
   U8      bmAttributes;        //!< Endpoint's attributes
   U16     wMaxPacketSize;      //!< Maximum packet size for this EP
   U8      bInterval;			//!< Interval for polling EP in ms
} S_usb_endpoint_descriptor;


//! Usb Language Descriptor
typedef struct {
   U8      bLength;             //!< size of this descriptor in bytes
   U8      bDescriptorType;     //!< STRING descriptor type
   U16     wlangid;				//!< language id
}  S_usb_language_id;


//_____ U S B   M A N U F A C T U R E R   D E S C R I P T O R _______________


//struct usb_st_manufacturer
typedef struct {
   U8  bLength;					// size of this descriptor in bytes
   U8  bDescriptorType;			// STRING descriptor type
   U16 wstring[USB_MN_LENGTH];	// Unicode characters
} S_usb_manufacturer_string_descriptor;


//_____ U S B   P R O D U C T   D E S C R I P T O R _________________________


//struct usb_st_product
typedef struct {
   U8  bLength;					// size of this descriptor in bytes
   U8  bDescriptorType;			// STRING descriptor type
   U16 wstring[USB_PN_LENGTH];	// Unicode characters
} S_usb_product_string_descriptor;


//_____ U S B   S E R I A L   N U M B E R   D E S C R I P T O R _____________


#if (USB_DEVICE_SN_USE==ENABLE)
//struct usb_st_serial_number
typedef struct {
   U8  bLength;					// size of this descriptor in bytes
   U8  bDescriptorType;			// STRING descriptor type
#if (USE_DEVICE_SN_UNIQUE==ENABLE)

#else   
   U16 wstring[USB_SN_LENGTH];	// Unicode characters
#endif
} S_usb_serial_number;
#endif


//_____ U S B  F I R M W A R E    R E V I S I O N  _______________


//struct usb_st_firmaware
typedef struct {
	U8  bLength;				// size of this descriptor in bytes
	U8  bDescriptorType;		// STRING descriptor type
	U16 wstring[USB_FW_LENGTH];	// Unicode characters
} S_usb_firmware_string_descriptor;


// Configuration descriptor template
// The device has two interfaces
// - First interface has 2 bulk endpoints
// - Second interface has 1 interrupt IN endpoint
typedef struct {
   S_usb_configuration_descriptor cfg_tmc;
   S_usb_interface_descriptor     ifc_tmc;
   S_usb_endpoint_descriptor      BuIn_tmc;
   S_usb_endpoint_descriptor      BuOut_tmc;
   //S_usb_interface_descriptor     ifc_second_temp;
   S_usb_endpoint_descriptor      IntIn_tmc;
} S_usb_user_configuration_descriptor;



// additional attributes and capabilities of a USBTMC interface  
typedef struct {
	U8      bUSBTMC_status;			// Status indication for this request
	U8      bZeroValue1;			// reserved must be 0x00
	U16     bcdUSBTMC;				// BCD version number of the relevant USBTMC specification for this USBTMC interface.
	U8      bUSBTMCItfCapab;		// USBTMC Interface Capabilities
	U8      bUSBTMCDevCapab;		// USBTMC Device Capabilities
	U16     wZeroValue2;			// reserved must be 0x0000
	U16     wZeroValue3;			// reserved must be 0x0000
	U16     wZeroValue4;            // reserved must be 0x0000
	U16     wZeroValue5;            // reserved must be 0x0000
	U16     wZeroValue6;            // reserved must be 0x0000
	U16     wZeroValue7;            // reserved must be 0x0000
	U16     wZeroValue8;            // reserved must be 0x0000
	U16     wZeroValue9;            // reserved must be 0x0000
	U16     wZeroValue10;           // reserved must be 0x0000
}  S_usb_interface_capabilities;



#endif

