1 // SPDX-License-Identifier: LGPL-2.1+
3 * Copyright (C) 2003 David Brownell
6 #include <linux/errno.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/list.h>
10 #include <linux/string.h>
11 #include <linux/device.h>
12 #include <linux/nls.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/gadget.h>
19 * usb_gadget_get_string - fill out a string descriptor
20 * @table: of c strings encoded using UTF-8
21 * @id: string id, from low byte of wValue in get string descriptor
22 * @buf: at least 256 bytes, must be 16-bit aligned
24 * Finds the UTF-8 string matching the ID, and converts it into a
25 * string descriptor in utf16-le.
26 * Returns length of descriptor (always even) or negative errno
28 * If your driver needs stings in multiple languages, you'll probably
29 * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
30 * using this routine after choosing which set of UTF-8 strings to use.
31 * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
32 * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
33 * characters (which are also widely used in C strings).
36 usb_gadget_get_string (struct usb_gadget_strings
*table
, int id
, u8
*buf
)
41 /* descriptor 0 has the language id */
44 buf
[1] = USB_DT_STRING
;
45 buf
[2] = (u8
) table
->language
;
46 buf
[3] = (u8
) (table
->language
>> 8);
49 for (s
= table
->strings
; s
&& s
->s
; s
++)
53 /* unrecognized: stall. */
57 /* string descriptors have length, tag, then UTF16-LE text */
58 len
= min ((size_t) 126, strlen (s
->s
));
59 len
= utf8s_to_utf16s(s
->s
, len
, UTF16_LITTLE_ENDIAN
,
60 (wchar_t *) &buf
[2], 126);
63 buf
[0] = (len
+ 1) * 2;
64 buf
[1] = USB_DT_STRING
;
67 EXPORT_SYMBOL_GPL(usb_gadget_get_string
);