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