[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <1591939967-29943-1-git-send-email-macpaul.lin@mediatek.com>
Date: Fri, 12 Jun 2020 13:32:47 +0800
From: Macpaul Lin <macpaul.lin@...iatek.com>
To: Felipe Balbi <balbi@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Matthias Brugger <matthias.bgg@...il.com>,
<linux-usb@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<linux-arm-kernel@...ts.infradead.org>,
<linux-mediatek@...ts.infradead.org>, Jim Lin <jilin@...dia.com>,
Siqi Lin <siqilin@...gle.com>
CC: Mediatek WSD Upstream <wsd_upstream@...iatek.com>,
Macpaul Lin <macpaul.lin@...iatek.com>,
Macpaul Lin <macpaul.lin@...il.com>
Subject: [PATCH] usb: replace hardcoded maximum usb string length by definition
Replace hardcoded maximum usb string length (126 bytes) by definition
"MAX_USB_STRING_LEN".
Signed-off-by: Macpaul Lin <macpaul.lin@...iatek.com>
---
drivers/usb/gadget/composite.c | 4 ++--
drivers/usb/gadget/configfs.c | 3 ++-
drivers/usb/gadget/usbstring.c | 5 +++--
include/linux/usb.h | 2 ++
4 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index cb4950c..d0de016 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -1041,7 +1041,7 @@ static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
while (*sp) {
s = *sp;
language = cpu_to_le16(s->language);
- for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
+ for (tmp = buf; *tmp && tmp < &buf[MAX_USB_STRING_LEN]; tmp++) {
if (*tmp == language)
goto repeat;
}
@@ -1116,7 +1116,7 @@ static int get_string(struct usb_composite_dev *cdev,
collect_langs(sp, s->wData);
}
- for (len = 0; len <= 126 && s->wData[len]; len++)
+ for (len = 0; len <= MAX_USB_STRING_LEN && s->wData[len]; len++)
continue;
if (!len)
return -EINVAL;
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 32b637e..c9d61ac 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -4,6 +4,7 @@
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/nls.h>
+#include <linux/usb.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget_configfs.h>
#include "configfs.h"
@@ -115,7 +116,7 @@ static int usb_string_copy(const char *s, char **s_copy)
char *str;
char *copy = *s_copy;
ret = strlen(s);
- if (ret > 126)
+ if (ret > MAX_USB_STRING_LEN)
return -EOVERFLOW;
str = kstrdup(s, GFP_KERNEL);
diff --git a/drivers/usb/gadget/usbstring.c b/drivers/usb/gadget/usbstring.c
index 7c24d1c..c125d59 100644
--- a/drivers/usb/gadget/usbstring.c
+++ b/drivers/usb/gadget/usbstring.c
@@ -11,6 +11,7 @@
#include <linux/device.h>
#include <linux/nls.h>
+#include <linux/usb.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
@@ -55,9 +56,9 @@
return -EINVAL;
/* string descriptors have length, tag, then UTF16-LE text */
- len = min ((size_t) 126, strlen (s->s));
+ len = min((size_t)MAX_USB_STRING_LEN, strlen(s->s));
len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
- (wchar_t *) &buf[2], 126);
+ (wchar_t *) &buf[2], MAX_USB_STRING_LEN);
if (len < 0)
return -EINVAL;
buf [0] = (len + 1) * 2;
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 9f3c721..df4a9cb 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1815,6 +1815,8 @@ static inline int usb_get_ptm_status(struct usb_device *dev, void *data)
0, data);
}
+/* USB String descriptors can contain at most 126 characters. */
+#define MAX_USB_STRING_LEN 126
extern int usb_string(struct usb_device *dev, int index,
char *buf, size_t size);
--
1.7.9.5
Powered by blists - more mailing lists