mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jules Maselbas <jmaselbas@kalray.eu>
To: barebox@lists.infradead.org
Cc: Jules Maselbas <jmaselbas@kalray.eu>
Subject: [PATCH 6/6] usb: string: Minor changes
Date: Thu, 10 Oct 2019 10:15:03 +0200	[thread overview]
Message-ID: <20191010081503.15254-6-jmaselbas@kalray.eu> (raw)
In-Reply-To: <20191010081503.15254-1-jmaselbas@kalray.eu>

This is mostly cosmetic changes except it adds a test in case
dma_alloc failed, the boolean have_langid will will now take the
value 1 when set (instead of -1) and now characters with a value
above 0x7f (outside ASCII range) will also be converted to '?'.

Signed-off-by: Jules Maselbas <jmaselbas@kalray.eu>
---
 drivers/usb/core/usb.c | 51 +++++++++++++++++++++---------------------
 include/usb/usb.h      |  2 +-
 2 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index e34cb5bf2..cdcab6082 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -900,31 +900,32 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  * Get string index and translate it to ascii.
  * returns string length (> 0) or error (< 0)
  */
-int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
+int usb_string(struct usb_device *dev, int index, char *str, size_t size)
 {
-	unsigned char *tbuf;
-	int err;
-	unsigned int u, idx;
+	unsigned char *buf;
+	unsigned int i, u;
+	int ret;
+
+	buf = dma_alloc(USB_BUFSIZ);
 
-	if (!size || !buf || !index)
+	if (!size || !str || !buf || !index)
 		return -1;
-	buf[0] = 0;
-	tbuf = dma_alloc(USB_BUFSIZ);
+	str[0] = 0;
 
 	/* get langid for strings if it's not yet known */
 	if (!dev->have_langid) {
-		err = usb_string_sub(dev, 0, 0, tbuf);
-		if (err < 0) {
+		ret = usb_string_sub(dev, 0, 0, buf);
+		if (ret < 0) {
 			pr_debug("error getting string descriptor 0 " \
 				   "(error=%lx)\n", dev->status);
-			err = -1;
+			ret = -1;
 			goto error;
-		} else if (tbuf[0] < 4) {
+		} else if (buf[0] < 4) {
 			pr_debug("string descriptor 0 too short\n");
-			err = -1;
+			ret = -1;
 			goto error;
 		} else {
-			dev->have_langid = -1;
+			dev->have_langid = 1;
 			dev->string_langid = le16_to_cpu(*((__le16 *)&buf[2]));
 				/* always use the first langid listed */
 			pr_debug("USB device number %d default " \
@@ -933,26 +934,24 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
 		}
 	}
 
-	err = usb_string_sub(dev, dev->string_langid, index, tbuf);
-	if (err < 0)
+	ret = usb_string_sub(dev, dev->string_langid, index, buf);
+	if (ret < 0)
 		goto error;
 
-	size--;		/* leave room for trailing NULL char in output buffer */
-	for (idx = 0, u = 2; u < err; u += 2) {
-		if (idx >= size)
-			break;
-		if (tbuf[u+1])			/* high byte */
-			buf[idx++] = '?';  /* non-ASCII character */
+	size--;	/* leave room for trailing NULL char in output buffer */
+	for (i = 0, u = 2; u < ret && i < size; i++, u += 2) {
+		if (buf[u] & 0x80 || buf[u + 1]) /* non-ASCII character */
+			str[i] = '?';
 		else
-			buf[idx++] = tbuf[u];
+			str[i] = buf[u];
 	}
-	buf[idx] = 0;
-	err = idx;
 
+	str[i] = 0;
+	ret = i;
 error:
-	dma_free(tbuf);
+	dma_free(buf);
 
-	return err;
+	return ret;
 }
 
 int usb_driver_register(struct usb_driver *drv)
diff --git a/include/usb/usb.h b/include/usb/usb.h
index 4698308df..e9e708867 100644
--- a/include/usb/usb.h
+++ b/include/usb/usb.h
@@ -181,7 +181,7 @@ int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
 			unsigned char type, unsigned char id, void *buf,
 			int size);
 int usb_clear_halt(struct usb_device *dev, int pipe);
-int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
+int usb_string(struct usb_device *dev, int index, char *str, size_t size);
 int usb_set_interface(struct usb_device *dev, int interface, int alternate);
 
 void usb_rescan(void);
-- 
2.21.0.196.g041f5ea


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      parent reply	other threads:[~2019-10-10  8:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-10  8:14 [PATCH 1/6] usb: hub: Use more accurate struct name Jules Maselbas
2019-10-10  8:14 ` [PATCH 2/6] usb: hub: Only clear port reset status if set Jules Maselbas
2019-10-10  8:15 ` [PATCH 3/6] usb: string: Use dma_alloc instead of a local buffer Jules Maselbas
2019-10-10  8:15 ` [PATCH 4/6] usb: string: Use le16_to_cpu for langid Jules Maselbas
2019-10-10 20:41   ` Andrey Smirnov
2019-10-14 11:42   ` Sascha Hauer
2019-10-14 15:09     ` Jules Maselbas
2019-10-10  8:15 ` [PATCH 5/6] usb: string: size_t is unsigned Jules Maselbas
2019-10-10  8:15 ` Jules Maselbas [this message]

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=20191010081503.15254-6-jmaselbas@kalray.eu \
    --to=jmaselbas@kalray.eu \
    --cc=barebox@lists.infradead.org \
    /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