diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c2862994..3276eada 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,7 +20,7 @@ lib_xua Change Log * CHANGE HID report descriptor to use generic events instead of GPI events, to report Key-phrase detection as AC Search, and to report end-call detection as AC Stop - * ADDED: Ability to read or modify vendor and product IDs + * ADDED: Ability to read or modify vendor and product IDs and strings * ADDED: Ability to read or modify bcdDevice * ADDED: Override USB descriptor with sampling frequency and bit-resolution set at boot time. diff --git a/lib_xua/api/xua_endpoint0.h b/lib_xua/api/xua_endpoint0.h index 13c00b61..60f68bf3 100644 --- a/lib_xua/api/xua_endpoint0.h +++ b/lib_xua/api/xua_endpoint0.h @@ -40,21 +40,73 @@ void XUA_Endpoint0_setVendorId(unsigned short vid); */ void XUA_Endpoint0_setProductId(unsigned short pid); -/** Function to get the Vendor ID value - * - * \return Vendor ID value -*/ -unsigned short XUA_Endpoint0_getVendorId(); -/** Function to get the Product ID value +/** Function to set the Vendor string * - * \return Product ID value + * \param vendor_str Vendor string to set */ -unsigned short XUA_Endpoint0_getProductId(); +#ifdef __XC__ +void XUA_Endpoint0_setVendorStr(char * unsafe vendor_str); +#else +void XUA_Endpoint0_setVendorStr(char * vendor_str); +#endif -unsigned short XUA_Endpoint0_getBcdDevice(); +/** Function to set the Product string + * + * \param product_str Product string to set +*/ + +#ifdef __XC__ +void XUA_Endpoint0_setProductStr(char * unsafe product_str); +#else +void XUA_Endpoint0_setProductStr(char * product_str); +#endif + +/** Function to set the BCD device + * + * \param bcdDevice BCD device to set + +*/ void XUA_Endpoint0_setBcdDevice(unsigned short bcdDevice); +/** Function to get the Vendor string + * + * \param vid vendor string +*/ +#ifdef __XC__ +char * unsafe XUA_Endpoint0_getVendorStr(); +#else +char * XUA_Endpoint0_getVendorStr(; +#endif + +/** Function to get the Product string + * + * \param pid Product string +*/ +#ifdef __XC__ +char * unsafe XUA_Endpoint0_getProductStr(); +#else +char * XUA_Endpoint0_getProductStr(; +#endif + +/** Function to get the Vendor string + * + * \return Vendor string +*/ +unsigned short XUA_Endpoint0_getVendorId(); + +/** Function to get the Product string + * + * \return Product string +*/ +unsigned short XUA_Endpoint0_getProductId(); + +/** Function to get the BCD device + * + * \return BCD device +*/ +unsigned short XUA_Endpoint0_getBcdDevice(); + #endif #endif diff --git a/lib_xua/src/core/endpoint0/xua_endpoint0.c b/lib_xua/src/core/endpoint0/xua_endpoint0.c index a75b0db3..b773a4d3 100755 --- a/lib_xua/src/core/endpoint0/xua_endpoint0.c +++ b/lib_xua/src/core/endpoint0/xua_endpoint0.c @@ -8,10 +8,10 @@ #include #include #include - +#include #include "xua.h" -#if XUA_USB_EN +#if XUA_USB_EN #include "xud_device.h" /* Standard descriptor requests */ #include "dfu_types.h" #include "usbaudio20.h" /* Defines from USB Audio 2.0 spec */ @@ -28,6 +28,11 @@ #if DSD_CHANS_DAC > 0 #include "dsd_support.h" #endif +#define DEBUG_UNIT XUA_EP0 +#ifndef DEBUG_PRINT_ENABLE_XUA_EP0 + #define DEBUG_PRINT_ENABLE_XUA_EP0 0 +#endif // DEBUG_PRINT_ENABLE_XUA_EP0 +#include "debug_print.h" #include "xua_usb_params_funcs.h" @@ -66,6 +71,9 @@ extern void device_reboot(void); #if( 0 < HID_CONTROLS ) #include "xua_hid.h" #endif +#ifndef MIN +#define MIN(a,b) (((a)<(b))?(a):(b)) +#endif unsigned int DFU_mode_active = 0; // 0 - App active, 1 - DFU active @@ -102,6 +110,13 @@ unsigned g_curStreamAlt_In = 0; /* Global variable for current USB bus speed (i.e. FS/HS) */ XUD_BusSpeed_t g_curUsbSpeed = 0; +/* Global variables for current USB Vendor and Product strings */ +char g_vendor_str[XUA_MAX_STR_LEN] = VENDOR_STR; +#if (AUDIO_CLASS == 2) +char g_product_str[XUA_MAX_STR_LEN] = PRODUCT_STR_A2; +#else +char g_product_str[XUA_MAX_STR_LEN] = PRODUCT_STR_A1; +#endif /* Subslot */ const unsigned g_subSlot_Out_HS[OUTPUT_FORMAT_COUNT] = {HS_STREAM_FORMAT_OUTPUT_1_SUBSLOT_BYTES, @@ -218,6 +233,82 @@ void XUA_Endpoint0_setVendorId(unsigned short vid) { #endif // AUDIO_CLASS == 1} } +void concatenateAndCopyStrings(char* string1, char* string2, char* string_buffer) { + memset(string_buffer, '\0', strlen(string_buffer)); + + uint32_t remaining_buffer_size = MIN(strlen(string1), XUA_MAX_STR_LEN-1); + strncpy(string_buffer, string1, remaining_buffer_size); + uint32_t total_string_size = MIN(strlen(string1)+strlen(string2), XUA_MAX_STR_LEN-1); + if (total_string_size==XUA_MAX_STR_LEN-1) { + remaining_buffer_size = XUA_MAX_STR_LEN-1-strlen(string1); + } else { + remaining_buffer_size = strlen(string1); + } + strncat(string_buffer, string2, remaining_buffer_size); +} + +void XUA_Endpoint0_setStrTable() { + + // update Vendor strings + concatenateAndCopyStrings(g_vendor_str, "", g_strTable.vendorStr); +#if (AUDIO_CLASS == 2) + concatenateAndCopyStrings(g_vendor_str, " Clock Selector", g_strTable.clockSelectorStr); + concatenateAndCopyStrings(g_vendor_str, " Internal Clock", g_strTable.internalClockSourceStr); +#endif +#if SPDIF_RX + concatenateAndCopyStrings(g_vendor_str, " S/PDIF Clock", g_strTable.spdifClockSourceStr); +#endif +#if ADAT_RX + concatenateAndCopyStrings(g_vendor_str, " ADAT Clock", g_strTable.adatClockSourceStr); +#endif +#if (XUA_DFU_EN == 1) + concatenateAndCopyStrings(g_vendor_str, " DFU", g_strTable.dfuStr); +#endif +#ifdef USB_CONTROL_DESCS + concatenateAndCopyStrings(g_vendor_str, " Control", g_strTable.ctrlStr); +#endif +#ifdef MIDI + concatenateAndCopyStrings(g_vendor_str, " MIDI Out", g_strTable.midiOutStr); + concatenateAndCopyStrings(g_vendor_str, " MIDI In", g_strTable.midiInStr); +#endif + // update product strings +#if (AUDIO_CLASS_FALLBACK) || (AUDIO_CLASS == 1) + concatenateAndCopyStrings(g_product_str, "", g_strTable.productStr_Audio1); + concatenateAndCopyStrings(g_product_str, "", g_strTable.outputInterfaceStr_Audio1); + concatenateAndCopyStrings(g_product_str, "", g_strTable.inputInterfaceStr_Audio1); + concatenateAndCopyStrings(g_product_str, "", g_strTable.usbInputTermStr_Audio1); + concatenateAndCopyStrings(g_product_str, "", g_strTable.usbOutputTermStr_Audio1); +#elif (AUDIO_CLASS == 2) + concatenateAndCopyStrings(g_product_str, "", g_strTable.productStr_Audio2); + concatenateAndCopyStrings(g_product_str, "", g_strTable.outputInterfaceStr_Audio2); + concatenateAndCopyStrings(g_product_str, "", g_strTable.inputInterfaceStr_Audio2); + concatenateAndCopyStrings(g_product_str, "", g_strTable.usbInputTermStr_Audio2); + concatenateAndCopyStrings(g_product_str, "", g_strTable.usbOutputTermStr_Audio2); +#endif +} + +void XUA_Endpoint0_setVendorStr(char* vendor_str) { + debug_printf("XUA_Endpoint0_setVendorStr() with string %s", vendor_str); + concatenateAndCopyStrings(vendor_str, "", g_vendor_str); +} + +void XUA_Endpoint0_setProductStr(char* product_str) { + debug_printf("XUA_Endpoint0_setProductStr() with string %s", product_str); + concatenateAndCopyStrings(product_str, "", g_product_str); +} + +char* XUA_Endpoint0_getVendorStr() { + return g_strTable.vendorStr; +} + +char* XUA_Endpoint0_getProductStr() { + #if (AUDIO_CLASS_FALLBACK) || (AUDIO_CLASS == 1) + return g_strTable.productStr_Audio1; + #elif (AUDIO_CLASS == 2) + return g_strTable.productStr_Audio2; + #endif +} + void XUA_Endpoint0_setProductId(unsigned short pid) { #if (AUDIO_CLASS == 1) devDesc_Audio1.idProduct = pid; @@ -270,6 +361,8 @@ void XUA_Endpoint0_init(chanend c_ep0_out, chanend c_ep0_in, chanend c_audioCont ep0_out = XUD_InitEp(c_ep0_out); ep0_in = XUD_InitEp(c_ep0_in); + XUA_Endpoint0_setStrTable(); + #if 0 /* Dont need to init globals.. */ /* Init tables for volumes (+ 1 for master) */ @@ -361,10 +454,10 @@ void XUA_Endpoint0_init(chanend c_ep0_out, chanend c_ep0_in, chanend c_audioCont cfgDesc_Audio1[USB_AS_IN_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME] = get_device_to_usb_bit_res() >> 3; //sub frame rate = bit rate /8 cfgDesc_Audio1[USB_AS_IN_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME + 1] = (get_device_to_usb_bit_res() & 0xff); //bit resolution - + cfgDesc_Audio1[USB_AS_OUT_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME] = get_usb_to_device_bit_res() >> 3; //sub frame rate = bit rate /8 cfgDesc_Audio1[USB_AS_OUT_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME + 1] = (get_usb_to_device_bit_res() & 0xff); //bit resolution - + const unsigned num_of_usb_descriptor_freq=3; //This should be =3 according to the comments "using a value of <=2 or > 7 for num_freqs_a1 causes enumeration issues on Windows" in xua_ep0_descriptors.h int i=0; for(i=0;i> 8; cfgDesc_Audio1[USB_AS_IN_INTERFACE_DESCRIPTOR_OFFSET_FREQ + 3*i + 2] = (get_device_to_usb_rate() & 0xff0000)>> 16; } - + for(i=0;i> 8; cfgDesc_Audio1[USB_AS_OUT_INTERFACE_DESCRIPTOR_OFFSET_FREQ + 3*i + 2] = (get_usb_to_device_rate() & 0xff0000)>> 16; } - + cfgDesc_Audio1[USB_AS_IN_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE] = ((get_device_to_usb_bit_res() >> 3) * MAX_PACKET_SIZE_MULT_IN_FS) & 0xff; //max packet size cfgDesc_Audio1[USB_AS_IN_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE + 1] = (((get_device_to_usb_bit_res() >> 3) * MAX_PACKET_SIZE_MULT_IN_FS) & 0xff00) >> 8; //max packet size - + cfgDesc_Audio1[USB_AS_OUT_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE] = ((get_usb_to_device_bit_res() >> 3) * MAX_PACKET_SIZE_MULT_OUT_FS) & 0xff; //max packet size cfgDesc_Audio1[USB_AS_OUT_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE + 1] = (((get_usb_to_device_bit_res() >> 3) * MAX_PACKET_SIZE_MULT_OUT_FS) & 0xff00) >> 8; //max packet size - + #endif } @@ -770,7 +863,7 @@ void XUA_Endpoint0_loop(XUD_Result_t result, USB_SetupPacket_t sp, chanend c_ep0 if(result == XUD_RES_ERR) { -#if (XUA_DFU_EN == 1) +#if (XUA_DFU_EN == 1) if (!DFU_mode_active) { #endif diff --git a/lib_xua/src/core/endpoint0/xua_ep0_descriptors.h b/lib_xua/src/core/endpoint0/xua_ep0_descriptors.h index 5e51b3dd..185575bb 100644 --- a/lib_xua/src/core/endpoint0/xua_ep0_descriptors.h +++ b/lib_xua/src/core/endpoint0/xua_ep0_descriptors.h @@ -26,7 +26,23 @@ #define APPEND_PRODUCT_STR_A1(x) PRODUCT_STR_A1 " "#x -#define STR_TABLE_ENTRY(name) char *name +#define STR_TABLE_ENTRY(name) char * name + +// The empty strings below are used in the g_strTable to set the maximum size of the table entries +// The last char of the strings are different, so that the compiler allocates separate memory spaces +#define XUA_VENDOR_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\01" +#define XUA_PRODUCT_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\02" +#define XUA_CLOCK_SELECTOR_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\03" +#define XUA_INTERNAL_CLOCK_SELECTOR_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\04" +#define XUA_SPDIF_CLOCK_SOURCE_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\05" +#define XUA_ADAT_CLOCK_SOURCE_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\06" +#define XUA_DFU_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\07" +#define XUA_CTRL_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\08" +#define XUA_MIDI_OUT_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\09" +#define XUA_MIDI_IN_EMPTY_STRING "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0a" + +// The value below must match the length of XUA_DESCR_EMPTY_STRING. +#define XUA_MAX_STR_LEN (32) #define ISO_EP_ATTRIBUTES_ASYNCH 0x05 //ISO, ASYNCH, DATA EP #define ISO_EP_ATTRIBUTES_ADAPTIVE 0x09 //ISO, ADAPTIVE, DATA EP @@ -317,41 +333,42 @@ typedef struct StringDescTable_t g_strTable = { .langID = "\x09\x04", /* US English */ - .vendorStr = VENDOR_STR, + .vendorStr = XUA_VENDOR_EMPTY_STRING, .serialStr = "", #if (AUDIO_CLASS == 2) - .productStr_Audio2 = PRODUCT_STR_A2, - .outputInterfaceStr_Audio2 = APPEND_PRODUCT_STR_A2(), - .inputInterfaceStr_Audio2 = APPEND_PRODUCT_STR_A2(), - .usbInputTermStr_Audio2 = APPEND_PRODUCT_STR_A2(), - .usbOutputTermStr_Audio2 = APPEND_PRODUCT_STR_A2(), + .productStr_Audio2 = XUA_PRODUCT_EMPTY_STRING, + .outputInterfaceStr_Audio2 = XUA_PRODUCT_EMPTY_STRING, + .inputInterfaceStr_Audio2 = XUA_PRODUCT_EMPTY_STRING, + .usbInputTermStr_Audio2 = XUA_PRODUCT_EMPTY_STRING, + .usbOutputTermStr_Audio2 = XUA_PRODUCT_EMPTY_STRING, #endif #if (AUDIO_CLASS_FALLBACK) || (AUDIO_CLASS == 1) - .productStr_Audio1 = PRODUCT_STR_A1, - .outputInterfaceStr_Audio1 = APPEND_PRODUCT_STR_A1(), - .inputInterfaceStr_Audio1 = APPEND_PRODUCT_STR_A1(), - .usbInputTermStr_Audio1 = APPEND_PRODUCT_STR_A1(), - .usbOutputTermStr_Audio1 = APPEND_PRODUCT_STR_A1(), + + .productStr_Audio1 = XUA_PRODUCT_EMPTY_STRING, + .outputInterfaceStr_Audio1 = XUA_PRODUCT_EMPTY_STRING, + .inputInterfaceStr_Audio1 = XUA_PRODUCT_EMPTY_STRING, + .usbInputTermStr_Audio1 = XUA_PRODUCT_EMPTY_STRING, + .usbOutputTermStr_Audio1 = XUA_PRODUCT_EMPTY_STRING, #endif #if (AUDIO_CLASS == 2) - .clockSelectorStr = APPEND_VENDOR_STR(Clock Selector), - .internalClockSourceStr = APPEND_VENDOR_STR(Internal Clock), + .clockSelectorStr = XUA_CLOCK_SELECTOR_EMPTY_STRING, + .internalClockSourceStr = XUA_INTERNAL_CLOCK_SELECTOR_EMPTY_STRING, #if SPDIF_RX - .spdifClockSourceStr = APPEND_VENDOR_STR(S/PDIF Clock), + .spdifClockSourceStr = XUA_SPDIF_CLOCK_SOURCE_EMPTY_STRING, #endif #if ADAT_RX - .adatClockSourceStr = APPEND_VENDOR_STR(ADAT Clock), + .adatClockSourceStr = XUA_ADAT_CLOCK_SOURCE_EMPTY_STRING, #endif #endif #if (XUA_DFU_EN == 1) - .dfuStr = APPEND_VENDOR_STR(DFU), + .dfuStr = XUA_DFU_EMPTY_STRING, #endif #ifdef USB_CONTROL_DESCS - .ctrlStr = APPEND_VENDOR_STR(Control), + .ctrlStr = XUA_CTRL_EMPTY_STRING, #endif #ifdef MIDI - .midiOutStr = APPEND_VENDOR_STR(MIDI Out), - .midiInStr = APPEND_VENDOR_STR(MIDI In), + .midiOutStr = XUA_MIDI_OUT_EMPTY_STRING, + .midiInStr = XUA_MIDI_IN_EMPTY_STRING, #endif #include "chanstrings.h" @@ -2293,6 +2310,8 @@ unsigned char cfgDesc_Null[] = * To work around this we repeat MAX_FREQ_FS multiple times in some cases */ #define MAX(a,b) (((a)>(b))?(a):(b)) +#define MIN(a,b) (((a)<(b))?(a):(b)) + const unsigned num_freqs_a1 = MAX(3, (0 #if(MIN_FREQ <= 8000) && (MAX_FREQ_FS >= 8000) + 1 @@ -2373,13 +2392,13 @@ const unsigned num_freqs_a1 = MAX(3, (0 #define AS_FORMAT_TYPE_BYTES (17) #define USB_AS_IN_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME (18 + AC_TOTAL_LENGTH + (OUTPUT_INTERFACES_A1 * (49 + num_freqs_a1 * 3)) + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + 5) #define USB_AS_OUT_INTERFACE_DESCRIPTOR_OFFSET_SUB_FRAME (18 + AC_TOTAL_LENGTH + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + 5) - + #define USB_AS_IN_INTERFACE_DESCRIPTOR_OFFSET_FREQ (18 + AC_TOTAL_LENGTH + (OUTPUT_INTERFACES_A1 * (49 + num_freqs_a1 * 3)) + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + 8) #define USB_AS_OUT_INTERFACE_DESCRIPTOR_OFFSET_FREQ (18 + AC_TOTAL_LENGTH + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + 8) - + #define USB_AS_IN_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE (18 + AC_TOTAL_LENGTH + (OUTPUT_INTERFACES_A1 * (49 + num_freqs_a1 * 3)) + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + (AS_FORMAT_TYPE_BYTES) + 4) #define USB_AS_OUT_EP_DESCRIPTOR_OFFSET_MAXPACKETSIZE (18 + AC_TOTAL_LENGTH + (2*INTERFACE_DESCRIPTOR_BYTES) + (AS_INTERFACE_BYTES) + (AS_FORMAT_TYPE_BYTES) + 4) - + #endif #define CHARIFY_SR(x) (x & 0xff),((x & 0xff00)>> 8),((x & 0xff0000)>> 16)