From: rentao.bupt@gmail.com
To: Felipe Balbi <balbi@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Joel Stanley <joel@jms.id.au>, Andrew Jeffery <andrew@aj.id.au>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Chunfeng Yun <chunfeng.yun@mediatek.com>,
Colin Ian King <colin.king@canonical.com>,
Stephen Boyd <swboyd@chromium.org>,
Rob Herring <robh+dt@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
linux-usb@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-aspeed@lists.ozlabs.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, openbmc@lists.ozlabs.org,
taoren@fb.com
Cc: Tao Ren <rentao.bupt@gmail.com>
Subject: [PATCH v2 2/6] usb: gadget: add "usb_validate_langid" function
Date: Sun, 15 Mar 2020 12:16:28 -0700 [thread overview]
Message-ID: <20200315191632.12536-3-rentao.bupt@gmail.com> (raw)
In-Reply-To: <20200315191632.12536-1-rentao.bupt@gmail.com>
From: Tao Ren <rentao.bupt@gmail.com>
The USB LANGID validation code in "check_user_usb_string" function is
moved to "usb_validate_langid" function which can be used by other usb
gadget drivers.
Signed-off-by: Tao Ren <rentao.bupt@gmail.com>
---
No change in v2:
- the patch is added into the series since v2.
drivers/usb/gadget/configfs.c | 14 +-------------
drivers/usb/gadget/usbstring.c | 24 ++++++++++++++++++++++++
include/linux/usb/gadget.h | 3 +++
3 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
index 32b637e3e1fa..822ef0470c5f 100644
--- a/drivers/usb/gadget/configfs.c
+++ b/drivers/usb/gadget/configfs.c
@@ -13,8 +13,6 @@
int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
{
- unsigned primary_lang;
- unsigned sub_lang;
u16 num;
int ret;
@@ -22,17 +20,7 @@ int check_user_usb_string(const char *name,
if (ret)
return ret;
- primary_lang = num & 0x3ff;
- sub_lang = num >> 10;
-
- /* simple sanity check for valid langid */
- switch (primary_lang) {
- case 0:
- case 0x62 ... 0xfe:
- case 0x100 ... 0x3ff:
- return -EINVAL;
- }
- if (!sub_lang)
+ if (!usb_validate_langid(num))
return -EINVAL;
stringtab_dev->language = num;
diff --git a/drivers/usb/gadget/usbstring.c b/drivers/usb/gadget/usbstring.c
index 7c24d1ce1088..58a4d3325090 100644
--- a/drivers/usb/gadget/usbstring.c
+++ b/drivers/usb/gadget/usbstring.c
@@ -65,3 +65,27 @@ usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)
return buf [0];
}
EXPORT_SYMBOL_GPL(usb_gadget_get_string);
+
+/**
+ * usb_validate_langid - validate usb language identifiers
+ * @lang: usb language identifier
+ *
+ * Returns true for valid language identifier, otherwise false.
+ */
+bool usb_validate_langid(u16 langid)
+{
+ u16 primary_lang = langid & 0x3ff; /* bit [9:0] */
+ u16 sub_lang = langid >> 10; /* bit [15:10] */
+
+ switch (primary_lang) {
+ case 0:
+ case 0x62 ... 0xfe:
+ case 0x100 ... 0x3ff:
+ return false;
+ }
+ if (!sub_lang)
+ return false;
+
+ return true;
+}
+EXPORT_SYMBOL_GPL(usb_validate_langid);
diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h
index 124462d65eac..1a05227bdfae 100644
--- a/include/linux/usb/gadget.h
+++ b/include/linux/usb/gadget.h
@@ -773,6 +773,9 @@ struct usb_gadget_string_container {
/* put descriptor for string with that id into buf (buflen >= 256) */
int usb_gadget_get_string(const struct usb_gadget_strings *table, int id, u8 *buf);
+/* check if the given language identifier is valid */
+bool usb_validate_langid(u16 langid);
+
/*-------------------------------------------------------------------------*/
/* utility to simplify managing config descriptors */
--
2.17.1
next prev parent reply other threads:[~2020-03-15 19:17 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-15 19:16 [PATCH v2 0/6] usb: gadget: aspeed: allow to customize vhub device rentao.bupt
2020-03-15 19:16 ` [PATCH v2 1/6] usb: gadget: aspeed: support multiple language strings rentao.bupt
2020-03-15 19:16 ` rentao.bupt [this message]
2020-03-15 19:16 ` [PATCH v2 3/6] usb: gadget: aspeed: allow to set usb strings in device tree rentao.bupt
2020-03-15 19:16 ` [PATCH v2 4/6] usb: gadget: aspeed: allow to set device IDs " rentao.bupt
2020-03-15 19:16 ` [PATCH v2 5/6] usb: gadget: aspeed: fixup usb1 device descriptor at init time rentao.bupt
2020-03-15 19:16 ` [PATCH v2 6/6] dt-bindings: usb: document aspeed vhub device ID/string properties rentao.bupt
2020-03-30 19:23 ` Rob Herring
2020-03-31 0:13 ` Benjamin Herrenschmidt
2020-03-31 16:21 ` Rob Herring
2020-04-01 0:47 ` Tao Ren
2020-04-02 10:37 ` Benjamin Herrenschmidt
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=20200315191632.12536-3-rentao.bupt@gmail.com \
--to=rentao.bupt@gmail.com \
--cc=andrew@aj.id.au \
--cc=balbi@kernel.org \
--cc=benh@kernel.crashing.org \
--cc=chunfeng.yun@mediatek.com \
--cc=colin.king@canonical.com \
--cc=devicetree@vger.kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=joel@jms.id.au \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-aspeed@lists.ozlabs.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=openbmc@lists.ozlabs.org \
--cc=robh+dt@kernel.org \
--cc=swboyd@chromium.org \
--cc=taoren@fb.com \
/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;
as well as URLs for NNTP newsgroup(s).