xref: /OK3568_Linux_fs/u-boot/drivers/usb/gadget/usbstring.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2003 David Brownell
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	LGPL-2.1+
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
7*4882a593Smuzhiyun  *                      Remy Bohmer <linux@bohmer.net>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <common.h>
11*4882a593Smuzhiyun #include <linux/errno.h>
12*4882a593Smuzhiyun #include <linux/usb/ch9.h>
13*4882a593Smuzhiyun #include <linux/usb/gadget.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <asm/unaligned.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 
utf8_to_utf16le(const char * s,__le16 * cp,unsigned len)18*4882a593Smuzhiyun static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	int	count = 0;
21*4882a593Smuzhiyun 	u8	c;
22*4882a593Smuzhiyun 	u16	uchar;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	/*
25*4882a593Smuzhiyun 	 * this insists on correct encodings, though not minimal ones.
26*4882a593Smuzhiyun 	 * BUT it currently rejects legit 4-byte UTF-8 code points,
27*4882a593Smuzhiyun 	 * which need surrogate pairs.  (Unicode 3.1 can use them.)
28*4882a593Smuzhiyun 	 */
29*4882a593Smuzhiyun 	while (len != 0 && (c = (u8) *s++) != 0) {
30*4882a593Smuzhiyun 		if ((c & 0x80)) {
31*4882a593Smuzhiyun 			/*
32*4882a593Smuzhiyun 			 * 2-byte sequence:
33*4882a593Smuzhiyun 			 * 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
34*4882a593Smuzhiyun 			 */
35*4882a593Smuzhiyun 			if ((c & 0xe0) == 0xc0) {
36*4882a593Smuzhiyun 				uchar = (c & 0x1f) << 6;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 				c = (u8) *s++;
39*4882a593Smuzhiyun 				if ((c & 0xc0) != 0x80)
40*4882a593Smuzhiyun 					goto fail;
41*4882a593Smuzhiyun 				c &= 0x3f;
42*4882a593Smuzhiyun 				uchar |= c;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 			/*
45*4882a593Smuzhiyun 			 * 3-byte sequence (most CJKV characters):
46*4882a593Smuzhiyun 			 * zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
47*4882a593Smuzhiyun 			 */
48*4882a593Smuzhiyun 			} else if ((c & 0xf0) == 0xe0) {
49*4882a593Smuzhiyun 				uchar = (c & 0x0f) << 12;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 				c = (u8) *s++;
52*4882a593Smuzhiyun 				if ((c & 0xc0) != 0x80)
53*4882a593Smuzhiyun 					goto fail;
54*4882a593Smuzhiyun 				c &= 0x3f;
55*4882a593Smuzhiyun 				uchar |= c << 6;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 				c = (u8) *s++;
58*4882a593Smuzhiyun 				if ((c & 0xc0) != 0x80)
59*4882a593Smuzhiyun 					goto fail;
60*4882a593Smuzhiyun 				c &= 0x3f;
61*4882a593Smuzhiyun 				uchar |= c;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 				/* no bogus surrogates */
64*4882a593Smuzhiyun 				if (0xd800 <= uchar && uchar <= 0xdfff)
65*4882a593Smuzhiyun 					goto fail;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 			/*
68*4882a593Smuzhiyun 			 * 4-byte sequence (surrogate pairs, currently rare):
69*4882a593Smuzhiyun 			 * 11101110wwwwzzzzyy + 110111yyyyxxxxxx
70*4882a593Smuzhiyun 			 *     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
71*4882a593Smuzhiyun 			 * (uuuuu = wwww + 1)
72*4882a593Smuzhiyun 			 * FIXME accept the surrogate code points (only)
73*4882a593Smuzhiyun 			 */
74*4882a593Smuzhiyun 			} else
75*4882a593Smuzhiyun 				goto fail;
76*4882a593Smuzhiyun 		} else
77*4882a593Smuzhiyun 			uchar = c;
78*4882a593Smuzhiyun 		put_unaligned_le16(uchar, cp++);
79*4882a593Smuzhiyun 		count++;
80*4882a593Smuzhiyun 		len--;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 	return count;
83*4882a593Smuzhiyun fail:
84*4882a593Smuzhiyun 	return -1;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /**
89*4882a593Smuzhiyun  * usb_gadget_get_string - fill out a string descriptor
90*4882a593Smuzhiyun  * @table: of c strings encoded using UTF-8
91*4882a593Smuzhiyun  * @id: string id, from low byte of wValue in get string descriptor
92*4882a593Smuzhiyun  * @buf: at least 256 bytes
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Finds the UTF-8 string matching the ID, and converts it into a
95*4882a593Smuzhiyun  * string descriptor in utf16-le.
96*4882a593Smuzhiyun  * Returns length of descriptor (always even) or negative errno
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * If your driver needs stings in multiple languages, you'll probably
99*4882a593Smuzhiyun  * "switch (wIndex) { ... }"  in your ep0 string descriptor logic,
100*4882a593Smuzhiyun  * using this routine after choosing which set of UTF-8 strings to use.
101*4882a593Smuzhiyun  * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
102*4882a593Smuzhiyun  * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
103*4882a593Smuzhiyun  * characters (which are also widely used in C strings).
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun int
usb_gadget_get_string(struct usb_gadget_strings * table,int id,u8 * buf)106*4882a593Smuzhiyun usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct usb_string	*s;
109*4882a593Smuzhiyun 	int			len;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (!table)
112*4882a593Smuzhiyun 		return -EINVAL;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* descriptor 0 has the language id */
115*4882a593Smuzhiyun 	if (id == 0) {
116*4882a593Smuzhiyun 		buf[0] = 4;
117*4882a593Smuzhiyun 		buf[1] = USB_DT_STRING;
118*4882a593Smuzhiyun 		buf[2] = (u8) table->language;
119*4882a593Smuzhiyun 		buf[3] = (u8) (table->language >> 8);
120*4882a593Smuzhiyun 		return 4;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 	for (s = table->strings; s && s->s; s++)
123*4882a593Smuzhiyun 		if (s->id == id)
124*4882a593Smuzhiyun 			break;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* unrecognized: stall. */
127*4882a593Smuzhiyun 	if (!s || !s->s)
128*4882a593Smuzhiyun 		return -EINVAL;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* string descriptors have length, tag, then UTF16-LE text */
131*4882a593Smuzhiyun 	len = min((size_t) 126, strlen(s->s));
132*4882a593Smuzhiyun 	memset(buf + 2, 0, 2 * len);	/* zero all the bytes */
133*4882a593Smuzhiyun 	len = utf8_to_utf16le(s->s, (__le16 *)&buf[2], len);
134*4882a593Smuzhiyun 	if (len < 0)
135*4882a593Smuzhiyun 		return -EINVAL;
136*4882a593Smuzhiyun 	buf[0] = (len + 1) * 2;
137*4882a593Smuzhiyun 	buf[1] = USB_DT_STRING;
138*4882a593Smuzhiyun 	return buf[0];
139*4882a593Smuzhiyun }
140