Skip to content

Commit

Permalink
usb: gadget: add "usb_validate_langid" function
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Felipe Balbi <balbi@kernel.org>
  • Loading branch information
Tao Ren authored and Felipe Balbi committed May 5, 2020
1 parent 5cc0710 commit 17309a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
14 changes: 1 addition & 13 deletions drivers/usb/gadget/configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,14 @@
int check_user_usb_string(const char *name,
struct usb_gadget_strings *stringtab_dev)
{
unsigned primary_lang;
unsigned sub_lang;
u16 num;
int ret;

ret = kstrtou16(name, 0, &num);
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;
Expand Down
24 changes: 24 additions & 0 deletions drivers/usb/gadget/usbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 3 additions & 0 deletions include/linux/usb/gadget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 17309a6

Please sign in to comment.