xref: /linux/drivers/usb/gadget/usbstring.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
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
usb_gadget_get_string(const struct usb_gadget_strings * table,int id,u8 * buf)36655016dcSBenjamin 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 */
5881c74628SMacpaul Lin 	len = min((size_t)USB_MAX_STRING_LEN, strlen(s->s));
5986dc243cSAlan Stern 	len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
6081c74628SMacpaul Lin 			(wchar_t *) &buf[2], USB_MAX_STRING_LEN);
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);
6817309a6aSTao Ren 
6917309a6aSTao Ren /**
7017309a6aSTao Ren  * usb_validate_langid - validate usb language identifiers
71*69f6a918SColton Lewis  * @langid: usb language identifier
7217309a6aSTao Ren  *
7317309a6aSTao Ren  * Returns true for valid language identifier, otherwise false.
7417309a6aSTao Ren  */
usb_validate_langid(u16 langid)7517309a6aSTao Ren bool usb_validate_langid(u16 langid)
7617309a6aSTao Ren {
7717309a6aSTao Ren 	u16 primary_lang = langid & 0x3ff;	/* bit [9:0] */
7817309a6aSTao Ren 	u16 sub_lang = langid >> 10;		/* bit [15:10] */
7917309a6aSTao Ren 
8017309a6aSTao Ren 	switch (primary_lang) {
8117309a6aSTao Ren 	case 0:
8217309a6aSTao Ren 	case 0x62 ... 0xfe:
8317309a6aSTao Ren 	case 0x100 ... 0x3ff:
8417309a6aSTao Ren 		return false;
8517309a6aSTao Ren 	}
8617309a6aSTao Ren 	if (!sub_lang)
8717309a6aSTao Ren 		return false;
8817309a6aSTao Ren 
8917309a6aSTao Ren 	return true;
9017309a6aSTao Ren }
9117309a6aSTao Ren EXPORT_SYMBOL_GPL(usb_validate_langid);
92