/*
 * uid_hid_desc.c
 * 
 * Alternate USB descriptor for crash recovery mode
 *
 * Created: 10/5/2023 10:43:02 PM
 *  Author: david
 */ 

#include "conf_usb.h"
#include "udd.h"
#include "udc_desc.h"
#include "udi_hid.h"
#include "udi_hid_generic.h"

COMPILER_WORD_ALIGNED
UDC_DESC_STORAGE usb_dev_desc_t crashdump_udc_device_desc = {
	.bLength                   = sizeof(usb_dev_desc_t),
	.bDescriptorType           = USB_DT_DEVICE,
	.bcdUSB                    = LE16(USB_V2_0),
	.bDeviceClass              = 0, 
	.bDeviceSubClass           = 0,
	.bDeviceProtocol           = 0,
	.bMaxPacketSize0           = USB_DEVICE_EP_CTRL_SIZE,
	.idVendor                  = LE16(USB_DEVICE_VENDOR_ID),
	.idProduct                 = LE16(CRASHDUMP_USB_DEVICE_PRODUCT_ID),
	.bcdDevice                 = LE16((USB_DEVICE_MAJOR_VERSION << 8)
	| USB_DEVICE_MINOR_VERSION),
	#ifdef USB_DEVICE_MANUFACTURE_NAME
	.iManufacturer             = 1,
	#else
	.iManufacturer             = 0,  // No manufacture string
	#endif
	#ifdef USB_DEVICE_PRODUCT_NAME
	.iProduct                  = 2,
	#else
	.iProduct                  = 0,  // No product string
	#endif
	#if (defined USB_DEVICE_SERIAL_NAME || defined USB_DEVICE_GET_SERIAL_NAME_POINTER)
	.iSerialNumber             = 3,
	#else
	.iSerialNumber             = 0,  // No serial string
	#endif
	.bNumConfigurations        = 1
};

#ifdef USB_DEVICE_HS_SUPPORT
//! USB Device Qualifier Descriptor for HS
COMPILER_WORD_ALIGNED
UDC_DESC_STORAGE usb_dev_qual_desc_t udc_device_qual = {
	.bLength                   = sizeof(usb_dev_qual_desc_t),
	.bDescriptorType           = USB_DT_DEVICE_QUALIFIER,
	.bcdUSB                    = LE16(USB_V2_0),
	.bDeviceClass              = 0,
	.bDeviceSubClass           = 0,
	.bDeviceProtocol           = 0,
	.bMaxPacketSize0           = USB_DEVICE_EP_CTRL_SIZE,
	.bNumConfigurations        = 1
};
#endif

COMPILER_PACK_SET(1)
typedef struct{
	usb_conf_desc_t conf;
	udi_hid_desc_t hid_generic;
} crashdump_udc_desc_t;
COMPILER_PACK_RESET()

//! USB Device Configuration Descriptor filled for FS and HS
COMPILER_WORD_ALIGNED
UDC_DESC_STORAGE crashdump_udc_desc_t crashdump_udc_desc = {
	.conf.bLength              = sizeof(usb_conf_desc_t),
	.conf.bDescriptorType      = USB_DT_CONFIGURATION,
	.conf.wTotalLength         = LE16(sizeof(crashdump_udc_desc_t)),
	.conf.bNumInterfaces       = CRASHDUMP_USB_DEVICE_NB_INTERFACE,
	.conf.bConfigurationValue  = 1,
	.conf.iConfiguration       = 0,
	.conf.bmAttributes         = USB_CONFIG_ATTR_MUST_SET | USB_DEVICE_ATTR,
	.conf.bMaxPower            = USB_CONFIG_MAX_POWER(USB_DEVICE_POWER),
	.hid_generic               = CRASHDUMP_UDI_HID_DESC,
};

/**
 * \name UDC structures which contains all USB Device definitions
 */
//@{

//! Associate an UDI for each USB interface
UDC_DESC_STORAGE udi_api_t *crashdump_udi_apis[CRASHDUMP_USB_DEVICE_NB_INTERFACE] = {
	&udi_api_hid,
};

//! Add UDI with USB Descriptors FS & HS
UDC_DESC_STORAGE udc_config_speed_t udc_config_fshs[1] = { {
	.desc          = (usb_conf_desc_t UDC_DESC_STORAGE*)&crashdump_udc_desc,
	.udi_apis = crashdump_udi_apis,
}};

//! Add all information about USB Device in global structure for UDC
UDC_DESC_STORAGE udc_config_t crashdump_udc_config = {
	.confdev_lsfs = &crashdump_udc_device_desc,
	.conf_lsfs = udc_config_fshs,
#ifdef USB_DEVICE_HS_SUPPORT
	.confdev_hs = &crashdump_udc_device_desc,
	.qualifier = &udc_device_qual,
	.conf_hs = udc_config_fshs,
#endif
};