Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1+ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2003 David Brownell |
| 4 | * |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 5 | * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 6 | * Remy Bohmer <linux@bohmer.net> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 11 | #include <linux/usb/ch9.h> |
| 12 | #include <linux/usb/gadget.h> |
Li Jun | e787da4 | 2021-01-25 21:43:47 +0800 | [diff] [blame] | 13 | #include <linux/utf.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * usb_gadget_get_string - fill out a string descriptor |
| 17 | * @table: of c strings encoded using UTF-8 |
| 18 | * @id: string id, from low byte of wValue in get string descriptor |
| 19 | * @buf: at least 256 bytes |
| 20 | * |
| 21 | * Finds the UTF-8 string matching the ID, and converts it into a |
| 22 | * string descriptor in utf16-le. |
| 23 | * Returns length of descriptor (always even) or negative errno |
| 24 | * |
| 25 | * If your driver needs stings in multiple languages, you'll probably |
| 26 | * "switch (wIndex) { ... }" in your ep0 string descriptor logic, |
| 27 | * using this routine after choosing which set of UTF-8 strings to use. |
| 28 | * Note that US-ASCII is a strict subset of UTF-8; any string bytes with |
| 29 | * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1 |
| 30 | * characters (which are also widely used in C strings). |
| 31 | */ |
| 32 | int |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 33 | usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 34 | { |
| 35 | struct usb_string *s; |
| 36 | int len; |
| 37 | |
Rob Herring | ef60fda | 2014-04-18 08:54:28 -0500 | [diff] [blame] | 38 | if (!table) |
| 39 | return -EINVAL; |
| 40 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 41 | /* descriptor 0 has the language id */ |
| 42 | if (id == 0) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 43 | buf[0] = 4; |
| 44 | buf[1] = USB_DT_STRING; |
| 45 | buf[2] = (u8) table->language; |
| 46 | buf[3] = (u8) (table->language >> 8); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 47 | return 4; |
| 48 | } |
| 49 | for (s = table->strings; s && s->s; s++) |
| 50 | if (s->id == id) |
| 51 | break; |
| 52 | |
| 53 | /* unrecognized: stall. */ |
| 54 | if (!s || !s->s) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | /* string descriptors have length, tag, then UTF16-LE text */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 58 | len = min((size_t) 126, strlen(s->s)); |
| 59 | memset(buf + 2, 0, 2 * len); /* zero all the bytes */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 60 | len = utf8_to_utf16le(s->s, (__le16 *)&buf[2], len); |
| 61 | if (len < 0) |
| 62 | return -EINVAL; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 63 | buf[0] = (len + 1) * 2; |
| 64 | buf[1] = USB_DT_STRING; |
| 65 | return buf[0]; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 66 | } |