xref: /linux/drivers/usb/gadget/usbstring.c (revision 655016dc2d30379a417404ae88df9bfb7ede38e6)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: LGPL-2.1+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2003 David Brownell
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/errno.h>
71da177e4SLinus Torvalds #include <linux/kernel.h>
8a84d9e53SSebastian Andrzej Siewior #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/list.h>
101da177e4SLinus Torvalds #include <linux/string.h>
111da177e4SLinus Torvalds #include <linux/device.h>
1286dc243cSAlan Stern #include <linux/nls.h>
131da177e4SLinus Torvalds 
145f848137SDavid Brownell #include <linux/usb/ch9.h>
159454a57aSDavid Brownell #include <linux/usb/gadget.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /**
191da177e4SLinus Torvalds  * usb_gadget_get_string - fill out a string descriptor
201da177e4SLinus Torvalds  * @table: of c strings encoded using UTF-8
211da177e4SLinus Torvalds  * @id: string id, from low byte of wValue in get string descriptor
2286dc243cSAlan Stern  * @buf: at least 256 bytes, must be 16-bit aligned
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Finds the UTF-8 string matching the ID, and converts it into a
251da177e4SLinus Torvalds  * string descriptor in utf16-le.
261da177e4SLinus Torvalds  * Returns length of descriptor (always even) or negative errno
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * If your driver needs stings in multiple languages, you'll probably
291da177e4SLinus Torvalds  * "switch (wIndex) { ... }"  in your ep0 string descriptor logic,
301da177e4SLinus Torvalds  * using this routine after choosing which set of UTF-8 strings to use.
311da177e4SLinus Torvalds  * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
321da177e4SLinus Torvalds  * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
331da177e4SLinus Torvalds  * characters (which are also widely used in C strings).
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds int
36*655016dcSBenjamin Herrenschmidt usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct usb_string	*s;
391da177e4SLinus Torvalds 	int			len;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	/* descriptor 0 has the language id */
421da177e4SLinus Torvalds 	if (id == 0) {
431da177e4SLinus Torvalds 		buf [0] = 4;
441da177e4SLinus Torvalds 		buf [1] = USB_DT_STRING;
451da177e4SLinus Torvalds 		buf [2] = (u8) table->language;
461da177e4SLinus Torvalds 		buf [3] = (u8) (table->language >> 8);
471da177e4SLinus Torvalds 		return 4;
481da177e4SLinus Torvalds 	}
491da177e4SLinus Torvalds 	for (s = table->strings; s && s->s; s++)
501da177e4SLinus Torvalds 		if (s->id == id)
511da177e4SLinus Torvalds 			break;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	/* unrecognized: stall. */
541da177e4SLinus Torvalds 	if (!s || !s->s)
551da177e4SLinus Torvalds 		return -EINVAL;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	/* string descriptors have length, tag, then UTF16-LE text */
581da177e4SLinus Torvalds 	len = min ((size_t) 126, strlen (s->s));
5986dc243cSAlan Stern 	len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
6086dc243cSAlan Stern 			(wchar_t *) &buf[2], 126);
611da177e4SLinus Torvalds 	if (len < 0)
621da177e4SLinus Torvalds 		return -EINVAL;
631da177e4SLinus Torvalds 	buf [0] = (len + 1) * 2;
641da177e4SLinus Torvalds 	buf [1] = USB_DT_STRING;
651da177e4SLinus Torvalds 	return buf [0];
661da177e4SLinus Torvalds }
67a84d9e53SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(usb_gadget_get_string);
68