<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>i.MX RT Crossover MCUs中的主题 Re: Potential bug in USB_DeviceGetDescriptor() function used in many usb_examples projects...</title>
    <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1284022#M14444</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your feedback. I reported the potential issue to the software team.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
    <pubDate>Fri, 28 May 2021 08:36:40 GMT</pubDate>
    <dc:creator>danielchen</dc:creator>
    <dc:date>2021-05-28T08:36:40Z</dc:date>
    <item>
      <title>Potential bug in USB_DeviceGetDescriptor() function used in many usb_examples projects...</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1278368#M14279</link>
      <description>&lt;P&gt;I was looking at the function USB_DeviceGetDescriptor() in the file usb_examlpes/usb_device_composite_hid_audio_unified_lite/usb_device_descriptor.c...&lt;/P&gt;&lt;P&gt;(note that this probably applies to all the projects that have this function)&lt;/P&gt;&lt;P&gt;The code that is used in the switch case USB_DESCRIPTOR_TYPE_STRING seems at best to be confusing, and potentially hazardous if the device supports more than one language...&lt;/P&gt;&lt;P&gt;As best I can tell, the host passes the device its language ID in setup-&amp;gt;wIndex. The device looks for a match in the languageList, and if found, it returns the requested string. If this is not the purpose of this snippet of code then everything that I say below is bogus so stop here (&lt;EM&gt;&lt;STRONG&gt;and reply with an explanation please, and feel free to call me an idiot&lt;/STRONG&gt;&lt;/EM&gt;)...&lt;/P&gt;&lt;P&gt;The loop uses USB_DEVICE_STRING_COUNT for no real reason, but worse than that, could potentially be hazardous.&amp;nbsp; Following is the code from the SDK...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    usb_status_t error      = kStatus_USB_Success;
    uint8_t descriptorType  = (uint8_t)((setup-&amp;gt;wValue &amp;amp; 0xFF00U) &amp;gt;&amp;gt; 8U);
    uint8_t descriptorIndex = (uint8_t)((setup-&amp;gt;wValue &amp;amp; 0x00FFU));

    if (USB_REQUEST_STANDARD_GET_DESCRIPTOR != setup-&amp;gt;bRequest)
    {
        return kStatus_USB_InvalidRequest;
    }

    switch (descriptorType)
    {
        case USB_DESCRIPTOR_TYPE_STRING:
            /* Get string descriptor */
            if (0U == descriptorIndex)
            {
                *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
                *length = g_UsbDeviceLanguageList.stringLength;
            }
            else
            {
                uint8_t languageIndex = 0U;
                uint8_t stringIndex   = USB_DEVICE_STRING_COUNT;

                for (; languageIndex &amp;lt; USB_DEVICE_LANGUAGE_COUNT; languageIndex++)
                {
                    if (setup-&amp;gt;wIndex == g_UsbDeviceLanguageList.languageList[languageIndex].languageId)
                    {
                        if (descriptorIndex &amp;lt; USB_DEVICE_STRING_COUNT)
                        {
                            stringIndex = descriptorIndex;
                        }
                        break;
                    }
                }

                if (USB_DEVICE_STRING_COUNT == stringIndex)
                {
                    return kStatus_USB_InvalidRequest;
                }
                *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[languageIndex].string[stringIndex];
                *length = g_UsbDeviceLanguageList.languageList[languageIndex].length[stringIndex];
            }
            break;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error condition is hit if the incoming language index (setup-&amp;gt;wIndex) does not find a match with the targets' set of supported languages. This naturally occurs if the loop increments through to its max value; but instead of testing for that, the code tests for...&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;STRONG&gt;(USB_DEVICE_STRING_COUNT == languageIndex)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Several confusing points IMHO here:&lt;BR /&gt;1. Its confusing (just test for languageIndex &amp;gt;= USB_DEVICE_LANGUAGE_COUNT)&lt;BR /&gt;2. If the target supports several languages but only a few strings, then the loop could legitimately have languageIndex == USB_DEVICE_STRING_COUNT.&lt;BR /&gt;&lt;BR /&gt;For anyone that agrees, here's what I use in place of the above code (I believe this fixes the error case only encountered if multiple languages are defined):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    uint8_t descriptorType  = (uint8_t)((setup-&amp;gt;wValue &amp;amp; 0xFF00U) &amp;gt;&amp;gt; 8U);
    uint8_t stringIdx       = (uint8_t)((setup-&amp;gt;wValue &amp;amp; 0x00FFU));
    usb_status_t ret        = kStatus_USB_Success;

    if (USB_REQUEST_STANDARD_GET_DESCRIPTOR != setup-&amp;gt;bRequest)
    {
        return kStatus_USB_InvalidRequest;
    }
    switch (descriptorType)
    {
        case USB_DESCRIPTOR_TYPE_STRING:
            /* Get string descriptor */
            if (0U == stringIdx)
            {
                *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
                *length = g_UsbDeviceLanguageList.stringLength;
            }
            else
            {
                uint8_t languageIdx;

                if (stringIdx &amp;gt;= USB_DEVICE_STRING_COUNT)
                    return kStatus_USB_InvalidRequest;

                for (languageIdx=0; languageIdx &amp;lt; USB_DEVICE_LANGUAGE_COUNT; languageIdx++)
                {
                    if (setup-&amp;gt;wIndex == g_UsbDeviceLanguageList.languageList[languageIdx].languageId)
                        break;
                }

                if (languageIdx &amp;gt;= USB_DEVICE_LANGUAGE_COUNT)
                    return kStatus_USB_InvalidRequest;
					
                *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[languageIndex].string[stringIndex];
                *length = g_UsbDeviceLanguageList.languageList[languageIndex].length[stringIndex];
            }
            break;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 16:18:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1278368#M14279</guid>
      <dc:creator>EdSutter</dc:creator>
      <dc:date>2021-05-18T16:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Potential bug in USB_DeviceGetDescriptor() function used in many usb_examples projects...</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1284022#M14444</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your feedback. I reported the potential issue to the software team.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 08:36:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1284022#M14444</guid>
      <dc:creator>danielchen</dc:creator>
      <dc:date>2021-05-28T08:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Potential bug in USB_DeviceGetDescriptor() function used in many usb_examples projects...</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1284542#M14467</link>
      <description>&lt;P&gt;It seems your code is based on an old version.&amp;nbsp; The latest version is SDK 2.9.2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Daniel&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 06:18:11 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1284542#M14467</guid>
      <dc:creator>danielchen</dc:creator>
      <dc:date>2021-05-31T06:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Potential bug in USB_DeviceGetDescriptor() function used in many usb_examples projects...</title>
      <link>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1286332#M14513</link>
      <description>&lt;P&gt;The only difference between what I show and the latest SDK (actually its 2.9.3) is a variable name change.&lt;/P&gt;&lt;P&gt;There is no change to the logic, so I believe the issue I mention still applies.&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 20:27:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-RT-Crossover-MCUs/Potential-bug-in-USB-DeviceGetDescriptor-function-used-in-many/m-p/1286332#M14513</guid>
      <dc:creator>EdSutter</dc:creator>
      <dc:date>2021-06-02T20:27:50Z</dc:date>
    </item>
  </channel>
</rss>

