U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Philip Oberfichtner <pro@denx.de>
To: u-boot@lists.denx.de
Cc: marex@denx.de, trini@konsulko.com, patrice.chotard@foss.st.com,
	caleb.connolly@linaro.org, neil.armstrong@linaro.org,
	sumit.garg@linaro.org, michal.simek@amd.com, bmeng.cn@gmail.com,
	ryder.lee@mediatek.com, weijie.gao@mediatek.com,
	chunfeng.yun@mediatek.com, GSS_MTK_Uboot_upstream@mediatek.com,
	iwamatsu@nigauri.org, lukma@denx.de, mkorpershoek@baylibre.com,
	eddie.cai.linux@gmail.com, neal@gompa.dev, pro@denx.de,
	j@jannau.net, sjg@chromium.org, teik.heng.chong@intel.com,
	jonas@kwiboo.se, nm@ti.com, seanga2@gmail.com,
	festevam@gmail.com, tharvey@gateworks.com, othacehe@gnu.org,
	fabrice.gasnier@foss.st.com, xdrudis@tinet.cat, marcan@marcan.st,
	richard.habeeb@gmail.com, artur@conclusive.pl, simon@holesch.de,
	clamor95@gmail.com, ion@agorria.com, miquel.raynal@bootlin.com
Subject: [PATCH 1/1] usb: Assimilate usb_get_descriptor() to linux
Date: Fri, 17 May 2024 11:18:20 +0200	[thread overview]
Message-ID: <20240517091820.3992-1-pro@denx.de> (raw)

Before this commit, usb_get_descriptor() failed for some flakey USB
devices. We hereby adopt the more robust linux implementation [1].

Signed-off-by: Philip Oberfichtner <pro@denx.de>

[1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/drivers/usb/core/message.c?h=v6.9#n781
---
 common/usb.c | 37 ++++++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/common/usb.c b/common/usb.c
index 84b10f5c7d..f5b21c883f 100644
--- a/common/usb.c
+++ b/common/usb.c
@@ -214,8 +214,9 @@ int usb_int_msg(struct usb_device *dev, unsigned long pipe,
  * clear keyboards LEDs). For data transfers, (storage transfers) we don't
  * allow control messages with 0 timeout, by previousely resetting the flag
  * asynch_allowed (usb_disable_asynch(1)).
- * returns the transferred length if OK or -1 if error. The transferred length
- * and the current status are stored in the dev->act_len and dev->status.
+ * returns the transferred length if OK, otherwise a negative error code. The
+ * transferred length and the current status are stored in the dev->act_len and
+ * dev->status.
  */
 int usb_control_msg(struct usb_device *dev, unsigned int pipe,
 			unsigned char request, unsigned char requesttype,
@@ -257,11 +258,14 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
 			break;
 		mdelay(1);
 	}
+
+	if (timeout == 0)
+		return -ETIMEDOUT;
+
 	if (dev->status)
 		return -1;
 
 	return dev->act_len;
-
 }
 
 /*-------------------------------------------------------------------
@@ -562,10 +566,29 @@ int usb_clear_halt(struct usb_device *dev, int pipe)
 static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
 			unsigned char index, void *buf, int size)
 {
-	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
-			       USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
-			       (type << 8) + index, 0, buf, size,
-			       USB_CNTL_TIMEOUT);
+	int i;
+	int result;
+
+	if (size <= 0)		/* No point in asking for no data */
+		return -EINVAL;
+
+	memset(buf, 0, size);	/* Make sure we parse really received data */
+
+	for (i = 0; i < 3; ++i) {
+		/* retry on length 0 or error; some devices are flakey */
+		result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
+					 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
+					 (type << 8) + index, 0, buf, size,
+					 USB_CNTL_TIMEOUT);
+		if (result <= 0 && result != -ETIMEDOUT)
+			continue;
+		if (result > 1 && ((u8 *)buf)[1] != type) {
+			result = -ENODATA;
+			continue;
+		}
+		break;
+	}
+	return result;
 }
 
 /**********************************************************************
-- 
2.39.2


             reply	other threads:[~2024-05-17 12:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17  9:18 Philip Oberfichtner [this message]
2024-05-17 21:16 ` [PATCH 1/1] usb: Assimilate usb_get_descriptor() to linux Marek Vasut
2024-06-04 10:19   ` Philip Oberfichtner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240517091820.3992-1-pro@denx.de \
    --to=pro@denx.de \
    --cc=GSS_MTK_Uboot_upstream@mediatek.com \
    --cc=artur@conclusive.pl \
    --cc=bmeng.cn@gmail.com \
    --cc=caleb.connolly@linaro.org \
    --cc=chunfeng.yun@mediatek.com \
    --cc=clamor95@gmail.com \
    --cc=eddie.cai.linux@gmail.com \
    --cc=fabrice.gasnier@foss.st.com \
    --cc=festevam@gmail.com \
    --cc=ion@agorria.com \
    --cc=iwamatsu@nigauri.org \
    --cc=j@jannau.net \
    --cc=jonas@kwiboo.se \
    --cc=lukma@denx.de \
    --cc=marcan@marcan.st \
    --cc=marex@denx.de \
    --cc=michal.simek@amd.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=mkorpershoek@baylibre.com \
    --cc=neal@gompa.dev \
    --cc=neil.armstrong@linaro.org \
    --cc=nm@ti.com \
    --cc=othacehe@gnu.org \
    --cc=patrice.chotard@foss.st.com \
    --cc=richard.habeeb@gmail.com \
    --cc=ryder.lee@mediatek.com \
    --cc=seanga2@gmail.com \
    --cc=simon@holesch.de \
    --cc=sjg@chromium.org \
    --cc=sumit.garg@linaro.org \
    --cc=teik.heng.chong@intel.com \
    --cc=tharvey@gateworks.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=weijie.gao@mediatek.com \
    --cc=xdrudis@tinet.cat \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox