[PATCH 6/6] usb: string: Minor changes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux