xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/usbstring.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: LGPL-2.1+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2003 David Brownell
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/errno.h>
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/list.h>
10*4882a593Smuzhiyun #include <linux/string.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/nls.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/usb/ch9.h>
15*4882a593Smuzhiyun #include <linux/usb/gadget.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun  * usb_gadget_get_string - fill out a string descriptor
20*4882a593Smuzhiyun  * @table: of c strings encoded using UTF-8
21*4882a593Smuzhiyun  * @id: string id, from low byte of wValue in get string descriptor
22*4882a593Smuzhiyun  * @buf: at least 256 bytes, must be 16-bit aligned
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * Finds the UTF-8 string matching the ID, and converts it into a
25*4882a593Smuzhiyun  * string descriptor in utf16-le.
26*4882a593Smuzhiyun  * Returns length of descriptor (always even) or negative errno
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * If your driver needs stings in multiple languages, you'll probably
29*4882a593Smuzhiyun  * "switch (wIndex) { ... }"  in your ep0 string descriptor logic,
30*4882a593Smuzhiyun  * using this routine after choosing which set of UTF-8 strings to use.
31*4882a593Smuzhiyun  * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
32*4882a593Smuzhiyun  * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
33*4882a593Smuzhiyun  * characters (which are also widely used in C strings).
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun int
usb_gadget_get_string(const struct usb_gadget_strings * table,int id,u8 * buf)36*4882a593Smuzhiyun usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	struct usb_string	*s;
39*4882a593Smuzhiyun 	int			len;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	/* descriptor 0 has the language id */
42*4882a593Smuzhiyun 	if (id == 0) {
43*4882a593Smuzhiyun 		buf [0] = 4;
44*4882a593Smuzhiyun 		buf [1] = USB_DT_STRING;
45*4882a593Smuzhiyun 		buf [2] = (u8) table->language;
46*4882a593Smuzhiyun 		buf [3] = (u8) (table->language >> 8);
47*4882a593Smuzhiyun 		return 4;
48*4882a593Smuzhiyun 	}
49*4882a593Smuzhiyun 	for (s = table->strings; s && s->s; s++)
50*4882a593Smuzhiyun 		if (s->id == id)
51*4882a593Smuzhiyun 			break;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* unrecognized: stall. */
54*4882a593Smuzhiyun 	if (!s || !s->s)
55*4882a593Smuzhiyun 		return -EINVAL;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* string descriptors have length, tag, then UTF16-LE text */
58*4882a593Smuzhiyun 	len = min((size_t)USB_MAX_STRING_LEN, strlen(s->s));
59*4882a593Smuzhiyun 	len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
60*4882a593Smuzhiyun 			(wchar_t *) &buf[2], USB_MAX_STRING_LEN);
61*4882a593Smuzhiyun 	if (len < 0)
62*4882a593Smuzhiyun 		return -EINVAL;
63*4882a593Smuzhiyun 	buf [0] = (len + 1) * 2;
64*4882a593Smuzhiyun 	buf [1] = USB_DT_STRING;
65*4882a593Smuzhiyun 	return buf [0];
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_gadget_get_string);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /**
70*4882a593Smuzhiyun  * usb_validate_langid - validate usb language identifiers
71*4882a593Smuzhiyun  * @langid: usb language identifier
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * Returns true for valid language identifier, otherwise false.
74*4882a593Smuzhiyun  */
usb_validate_langid(u16 langid)75*4882a593Smuzhiyun bool usb_validate_langid(u16 langid)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	u16 primary_lang = langid & 0x3ff;	/* bit [9:0] */
78*4882a593Smuzhiyun 	u16 sub_lang = langid >> 10;		/* bit [15:10] */
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	switch (primary_lang) {
81*4882a593Smuzhiyun 	case 0:
82*4882a593Smuzhiyun 	case 0x62 ... 0xfe:
83*4882a593Smuzhiyun 	case 0x100 ... 0x3ff:
84*4882a593Smuzhiyun 		return false;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 	if (!sub_lang)
87*4882a593Smuzhiyun 		return false;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return true;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_validate_langid);
92