/* * ------------------------------------------------------------------ Copyright (c) 2023 by Lattice Semiconductor Corporation ALL RIGHTS RESERVED ------------------------------------------------------------------ DISCLAIMER: LATTICE MAKES NO WARRANTIES ON THIS FILE OR ITS CONTENTS, WHETHER EXPRESSED, IMPLIED, STATUTORY, OR IN ANY PROVISION OF THE LATTICE PROPEL LICENSE AGREEMENT OR COMMUNICATION WITH LICENSEE, AND LATTICE SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. LATTICE DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED HEREIN WILL MEET LICENSEE 'S REQUIREMENTS, OR THAT LICENSEE' S OPERATION OF ANY DEVICE, SOFTWARE OR SYSTEM USING THIS FILE OR ITS CONTENTS WILL BE UNINTERRUPTED OR ERROR FREE, OR THAT DEFECTS HEREIN WILL BE CORRECTED. LICENSEE ASSUMES RESPONSIBILITY FOR SELECTION OF MATERIALS TO ACHIEVE ITS INTENDED RESULTS, AND FOR THE PROPER INSTALLATION, USE, AND RESULTS OBTAINED THEREFROM. LICENSEE ASSUMES THE ENTIRE RISK OF THE FILE AND ITS CONTENTS PROVING DEFECTIVE OR FAILING TO PERFORM PROPERLY AND IN SUCH EVENT, LICENSEE SHALL ASSUME THE ENTIRE COST AND RISK OF ANY REPAIR, SERVICE, CORRECTION, OR ANY OTHER LIABILITIES OR DAMAGES CAUSED BY OR ASSOCIATED WITH THE SOFTWARE. IN NO EVENT SHALL LATTICE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS FILE OR ITS CONTENTS, EVEN IF LATTICE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. LATTICE 'S SOLE LIABILITY, AND LICENSEE' S SOLE REMEDY, IS SET FORTH ABOVE. LATTICE DOES NOT WARRANT OR REPRESENT THAT THIS FILE, ITS CONTENTS OR USE THEREOF DOES NOT INFRINGE ON THIRD PARTIES' INTELLECTUAL PROPERTY RIGHTS, INCLUDING ANY PATENT. IT IS THE USER' S RESPONSIBILITY TO VERIFY THE USER SOFTWARE DESIGN FOR CONSISTENCY AND FUNCTIONALITY THROUGH THE USE OF FORMAL SOFTWARE VALIDATION METHODS. ------------------------------------------------------------------ * */ #include #include #include "descriptor.h" #include "descriptor_fifo.h" int descriptor_fifo_write(uint32_t descriptor_data_size, uint32_t* descrtiptor_data) { if(descriptor_data_size == 0x00) { return -2; } for(uint32_t i=0;i<(descriptor_data_size);i++) { if(ltcusb_fifo_write(DESC_FIFO_ADDR_RV, descrtiptor_data[i]) != 0) { return -1; } } return 0; } static int ascii2unicode(uint8_t* ascii_str, uint32_t ascii_str_len, uint8_t* unicode_str, uint32_t* unicode_str_len) { *unicode_str_len = (ascii_str_len << 1); for(uint32_t i=0;i<*unicode_str_len;i++) { unicode_str[i] = (i&0x1)?0:(uint8_t)ascii_str[i/2]; } return 0; } int create_string_descriptor(uint8_t* str, uint32_t str_len, uint32_t* str_desc, uint32_t* str_desc_len) { const uint32_t unicode_max_len = 252; uint8_t unicode_str[unicode_max_len]; memset(unicode_str,0x00,unicode_max_len); if(str_len > unicode_max_len/2) { return -1; } ascii2unicode(str, str_len, unicode_str, str_desc_len); *str_desc_len += 2; /**@todo descriptor type with enum or macro.*/ *str_desc = (uint32_t)*str_desc_len | (uint32_t)(0x03 << 8); memcpy((uint8_t*)str_desc+2, unicode_str, *str_desc_len-2); if(*str_desc_len%4) { *str_desc_len = (*str_desc_len >> 2); ++*str_desc_len; } else { *str_desc_len = (*str_desc_len >> 2); } return 0; } int descriptor_init() { /* Device Descriptor */ device_descriptor_data.bLength = 0x12; device_descriptor_data.bDescriptorType = 0x01; if(g_usb_speed == super_speed_usb) { if(g_connect_speed == super_speed_usb) { device_descriptor_data.bcdUSB = 0x0320; } else { device_descriptor_data.bcdUSB = 0x0210; } } else { device_descriptor_data.bcdUSB = 0x0200; } device_descriptor_data.bDeviceClass = 0xFF; device_descriptor_data.bDeviceSubClass = 0xFF; device_descriptor_data.bDeviceProtocol = 0xFF; if(g_connect_speed == super_speed_usb) { device_descriptor_data.bMaxPacketSize0 = 0x09; } else { device_descriptor_data.bMaxPacketSize0 = 0x40; } device_descriptor_data.idVendor = 0x2AC1; device_descriptor_data.idProduct = 0x20C8; device_descriptor_data.bcdDevice = 0x0600; device_descriptor_data.iManufacturer = 0x01; device_descriptor_data.iProduct = 0x02; device_descriptor_data.iSerialNumber = 0x03; device_descriptor_data.bNumConfigurations = 0x01; /*BOS Descriptor */ bos_descriptor_data.bLength = 0x05; bos_descriptor_data.bDescriptorType = 0x0f; bos_descriptor_data.wTotalLength = 0x0016; bos_descriptor_data.bNumDeviceCaps = 0x02; /* BOS Device Capability : USB 2.0 Extension descriptor */ usb_2_0_extension_descriptor_data.bLength = 0x07; usb_2_0_extension_descriptor_data.bDescriptorType = 0x10; usb_2_0_extension_descriptor_data.bDevCapabilityType = 0x02; usb_2_0_extension_descriptor_data.bmAttributes = 0x00000002; /* BOS Device Capability : SuperSpeed USB Device Capability Descriptor */ ss_usb_device_capability_descriptor_data.bLength = 0x0a; ss_usb_device_capability_descriptor_data.bDescriptorType = 0x10; ss_usb_device_capability_descriptor_data.bDevCapabilityType = 0x03; ss_usb_device_capability_descriptor_data.bmAttributes = 0x0; ss_usb_device_capability_descriptor_data.wSpeedsSupported = 0x000e; ss_usb_device_capability_descriptor_data.bFunctionalitySupport = 0x02; ss_usb_device_capability_descriptor_data.bU1DevExitLat = 0x0a; ss_usb_device_capability_descriptor_data.wU2DevExitLat = 0x07ff; /* device_qualifier_descriptor */ device_qualifier_descriptor_data.bLength = 0x0A; device_qualifier_descriptor_data.bDescriptorType= 0x06; device_qualifier_descriptor_data.bcdUSB = 0x0200; device_qualifier_descriptor_data.bDeviceClass = 0xff; device_qualifier_descriptor_data.bDeviceSubClass = 0xff; device_qualifier_descriptor_data.bDeviceProtocol = 0xff; device_qualifier_descriptor_data.bMaxPacketSize0 = 0x40; device_qualifier_descriptor_data.bNumConfigurations = 0x01; device_qualifier_descriptor_data.bReserved = 0x00; /*Other_Speed_Configuration Descriptor*/ other_speed_configuration_descriptor_data.bLength = 0x09; other_speed_configuration_descriptor_data.bDescriptorType = 0x07; other_speed_configuration_descriptor_data.wTotalLength = 0x002e; other_speed_configuration_descriptor_data.bNumInterfaces = 0x01; other_speed_configuration_descriptor_data.bConfigurationValue = 0x01; other_speed_configuration_descriptor_data.iConfiguration = 0x00; other_speed_configuration_descriptor_data.bmAttributes = 0xc0; other_speed_configuration_descriptor_data.bMaxPower = 0xfa; /* Configuration Descriptor */ config_descriptor_data.bLength = 0x09; config_descriptor_data.bDescriptorType = 0x02; if(g_connect_speed == super_speed_usb) { config_descriptor_data.wTotalLength = 0x0046; } else { config_descriptor_data.wTotalLength = 0x002e; } config_descriptor_data.bNumInterfaces = 0x01; config_descriptor_data.bConfigurationValue = 0x01; config_descriptor_data.iConfiguration = 0x00; config_descriptor_data.bmAttributes = 0xc0; config_descriptor_data.bMaxPower = 0xfa; //Interface Descriptor interface_descriptor_data.bLength = 0x09; interface_descriptor_data.bDescriptorType = 0x04; interface_descriptor_data.bInterfaceNumber = 0x00; interface_descriptor_data.bAlternateSetting = 0x00; interface_descriptor_data.bNumEndpoints = 0x04; interface_descriptor_data.bInterfaceClass = 0xff; interface_descriptor_data.bInterfaceSubClass = 0xff; interface_descriptor_data.bInterfaceProtocol = 0xff; interface_descriptor_data.iInterface = 0x00; //Endpoint Descriptor endpoint_descriptor_bin_1_data.bLength = 0x07; endpoint_descriptor_bin_1_data.bDescriptorType = 0x05; endpoint_descriptor_bin_1_data.bEndpointAddress = 0x81; endpoint_descriptor_bin_1_data.bmAttributes = 0x02; if(g_connect_speed == high_speed_usb) { endpoint_descriptor_bin_1_data.wMaxPacketSize = 0x0200; } else if(g_connect_speed == super_speed_usb) { endpoint_descriptor_bin_1_data.wMaxPacketSize = 0x0400; } else { endpoint_descriptor_bin_1_data.wMaxPacketSize = 0x0040; } endpoint_descriptor_bin_1_data.bInterval = 0x00; endpoint_descriptor_bout_1_data.bLength = 0x07; endpoint_descriptor_bout_1_data.bDescriptorType = 0x05; endpoint_descriptor_bout_1_data.bEndpointAddress = 0x01; endpoint_descriptor_bout_1_data.bmAttributes = 0x02; if(g_connect_speed == high_speed_usb) { endpoint_descriptor_bout_1_data.wMaxPacketSize = 0x0200; } else if(g_connect_speed == super_speed_usb) { endpoint_descriptor_bout_1_data.wMaxPacketSize = 0x0400; } else { endpoint_descriptor_bout_1_data.wMaxPacketSize = 0x0040; } endpoint_descriptor_bout_1_data.bInterval = 0x00; endpoint_descriptor_iin_5_data.bLength = 0x07; endpoint_descriptor_iin_5_data.bDescriptorType = 0x05; endpoint_descriptor_iin_5_data.bEndpointAddress = 0x85; endpoint_descriptor_iin_5_data.bmAttributes = 0x03; if(g_connect_speed == high_speed_usb) { endpoint_descriptor_iin_5_data.wMaxPacketSize = 0x0400; } else if(g_connect_speed == super_speed_usb) { endpoint_descriptor_iin_5_data.wMaxPacketSize = 0x0400; } else { endpoint_descriptor_iin_5_data.wMaxPacketSize = 0x0040; } endpoint_descriptor_iin_5_data.bInterval = 0x01; endpoint_descriptor_iout_5_data.bLength = 0x07; endpoint_descriptor_iout_5_data.bDescriptorType = 0x05; endpoint_descriptor_iout_5_data.bEndpointAddress = 0x05; endpoint_descriptor_iout_5_data.bmAttributes = 0x03; if(g_connect_speed == high_speed_usb) { endpoint_descriptor_iout_5_data.wMaxPacketSize = 0x0400; } else if(g_connect_speed == super_speed_usb) { endpoint_descriptor_iout_5_data.wMaxPacketSize = 0x0400; } else { endpoint_descriptor_iout_5_data.wMaxPacketSize = 0x0040; } endpoint_descriptor_iout_5_data.bInterval = 0x01; //ss_endpoint_companion_descriptor for bulk ep ss_endpoint_companion_descriptor_bulk_data.bLength = 0x06; ss_endpoint_companion_descriptor_bulk_data.bDescriptorType = 0x30; ss_endpoint_companion_descriptor_bulk_data.bMaxBurst = 0x03; ss_endpoint_companion_descriptor_bulk_data.bmAttributes = 0x0; ss_endpoint_companion_descriptor_bulk_data.wBytesPerInterval = 0x0; //ss_endpoint_companion_descriptor for interrupt ep ss_endpoint_companion_descriptor_interrupt_data.bLength = 0x06; ss_endpoint_companion_descriptor_interrupt_data.bDescriptorType = 0x30; ss_endpoint_companion_descriptor_interrupt_data.bMaxBurst = 0x00; ss_endpoint_companion_descriptor_interrupt_data.bmAttributes = 0x0; ss_endpoint_companion_descriptor_interrupt_data.wBytesPerInterval = 0x0; //string_language_descriptor string_language_descriptor_data.bLength = 0x04; string_language_descriptor_data.bDescriptorType = 0x03; string_language_descriptor_data.wLANGID_0 = 0x0409; return 0; }