11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2003 David Brownell 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 51da177e4SLinus Torvalds * it under the terms of the GNU Lesser General Public License as published 61da177e4SLinus Torvalds * by the Free Software Foundation; either version 2.1 of the License, or 71da177e4SLinus Torvalds * (at your option) any later version. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/errno.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/list.h> 131da177e4SLinus Torvalds #include <linux/string.h> 141da177e4SLinus Torvalds #include <linux/device.h> 151da177e4SLinus Torvalds #include <linux/init.h> 161da177e4SLinus Torvalds 175f848137SDavid Brownell #include <linux/usb/ch9.h> 18*9454a57aSDavid Brownell #include <linux/usb/gadget.h> 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <asm/unaligned.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds static int utf8_to_utf16le(const char *s, __le16 *cp, unsigned len) 241da177e4SLinus Torvalds { 251da177e4SLinus Torvalds int count = 0; 261da177e4SLinus Torvalds u8 c; 271da177e4SLinus Torvalds u16 uchar; 281da177e4SLinus Torvalds 291da177e4SLinus Torvalds /* this insists on correct encodings, though not minimal ones. 301da177e4SLinus Torvalds * BUT it currently rejects legit 4-byte UTF-8 code points, 311da177e4SLinus Torvalds * which need surrogate pairs. (Unicode 3.1 can use them.) 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds while (len != 0 && (c = (u8) *s++) != 0) { 341da177e4SLinus Torvalds if (unlikely(c & 0x80)) { 351da177e4SLinus Torvalds // 2-byte sequence: 361da177e4SLinus Torvalds // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx 371da177e4SLinus Torvalds if ((c & 0xe0) == 0xc0) { 381da177e4SLinus Torvalds uchar = (c & 0x1f) << 6; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds c = (u8) *s++; 411da177e4SLinus Torvalds if ((c & 0xc0) != 0xc0) 421da177e4SLinus Torvalds goto fail; 431da177e4SLinus Torvalds c &= 0x3f; 441da177e4SLinus Torvalds uchar |= c; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds // 3-byte sequence (most CJKV characters): 471da177e4SLinus Torvalds // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx 481da177e4SLinus Torvalds } else if ((c & 0xf0) == 0xe0) { 491da177e4SLinus Torvalds uchar = (c & 0x0f) << 12; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds c = (u8) *s++; 521da177e4SLinus Torvalds if ((c & 0xc0) != 0xc0) 531da177e4SLinus Torvalds goto fail; 541da177e4SLinus Torvalds c &= 0x3f; 551da177e4SLinus Torvalds uchar |= c << 6; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds c = (u8) *s++; 581da177e4SLinus Torvalds if ((c & 0xc0) != 0xc0) 591da177e4SLinus Torvalds goto fail; 601da177e4SLinus Torvalds c &= 0x3f; 611da177e4SLinus Torvalds uchar |= c; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* no bogus surrogates */ 641da177e4SLinus Torvalds if (0xd800 <= uchar && uchar <= 0xdfff) 651da177e4SLinus Torvalds goto fail; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds // 4-byte sequence (surrogate pairs, currently rare): 681da177e4SLinus Torvalds // 11101110wwwwzzzzyy + 110111yyyyxxxxxx 691da177e4SLinus Torvalds // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 701da177e4SLinus Torvalds // (uuuuu = wwww + 1) 711da177e4SLinus Torvalds // FIXME accept the surrogate code points (only) 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds } else 741da177e4SLinus Torvalds goto fail; 751da177e4SLinus Torvalds } else 761da177e4SLinus Torvalds uchar = c; 771da177e4SLinus Torvalds put_unaligned (cpu_to_le16 (uchar), cp++); 781da177e4SLinus Torvalds count++; 791da177e4SLinus Torvalds len--; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds return count; 821da177e4SLinus Torvalds fail: 831da177e4SLinus Torvalds return -1; 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /** 881da177e4SLinus Torvalds * usb_gadget_get_string - fill out a string descriptor 891da177e4SLinus Torvalds * @table: of c strings encoded using UTF-8 901da177e4SLinus Torvalds * @id: string id, from low byte of wValue in get string descriptor 911da177e4SLinus Torvalds * @buf: at least 256 bytes 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * Finds the UTF-8 string matching the ID, and converts it into a 941da177e4SLinus Torvalds * string descriptor in utf16-le. 951da177e4SLinus Torvalds * Returns length of descriptor (always even) or negative errno 961da177e4SLinus Torvalds * 971da177e4SLinus Torvalds * If your driver needs stings in multiple languages, you'll probably 981da177e4SLinus Torvalds * "switch (wIndex) { ... }" in your ep0 string descriptor logic, 991da177e4SLinus Torvalds * using this routine after choosing which set of UTF-8 strings to use. 1001da177e4SLinus Torvalds * Note that US-ASCII is a strict subset of UTF-8; any string bytes with 1011da177e4SLinus Torvalds * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1 1021da177e4SLinus Torvalds * characters (which are also widely used in C strings). 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds int 1051da177e4SLinus Torvalds usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds struct usb_string *s; 1081da177e4SLinus Torvalds int len; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* descriptor 0 has the language id */ 1111da177e4SLinus Torvalds if (id == 0) { 1121da177e4SLinus Torvalds buf [0] = 4; 1131da177e4SLinus Torvalds buf [1] = USB_DT_STRING; 1141da177e4SLinus Torvalds buf [2] = (u8) table->language; 1151da177e4SLinus Torvalds buf [3] = (u8) (table->language >> 8); 1161da177e4SLinus Torvalds return 4; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds for (s = table->strings; s && s->s; s++) 1191da177e4SLinus Torvalds if (s->id == id) 1201da177e4SLinus Torvalds break; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* unrecognized: stall. */ 1231da177e4SLinus Torvalds if (!s || !s->s) 1241da177e4SLinus Torvalds return -EINVAL; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* string descriptors have length, tag, then UTF16-LE text */ 1271da177e4SLinus Torvalds len = min ((size_t) 126, strlen (s->s)); 1281da177e4SLinus Torvalds memset (buf + 2, 0, 2 * len); /* zero all the bytes */ 1291da177e4SLinus Torvalds len = utf8_to_utf16le(s->s, (__le16 *)&buf[2], len); 1301da177e4SLinus Torvalds if (len < 0) 1311da177e4SLinus Torvalds return -EINVAL; 1321da177e4SLinus Torvalds buf [0] = (len + 1) * 2; 1331da177e4SLinus Torvalds buf [1] = USB_DT_STRING; 1341da177e4SLinus Torvalds return buf [0]; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 137