Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2003 David Brownell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | a84d9e5 | 2012-09-06 20:11:09 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/list.h> |
| 10 | #include <linux/string.h> |
| 11 | #include <linux/device.h> |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 12 | #include <linux/nls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 14 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 15 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 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 |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 22 | * @buf: at least 256 bytes, must be 16-bit aligned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
| 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 |
| 27 | * |
| 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). |
| 34 | */ |
| 35 | int |
Benjamin Herrenschmidt | 655016d | 2018-05-04 06:59:42 +1000 | [diff] [blame] | 36 | usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
| 38 | struct usb_string *s; |
| 39 | int len; |
| 40 | |
| 41 | /* descriptor 0 has the language id */ |
| 42 | if (id == 0) { |
| 43 | buf [0] = 4; |
| 44 | buf [1] = USB_DT_STRING; |
| 45 | buf [2] = (u8) table->language; |
| 46 | buf [3] = (u8) (table->language >> 8); |
| 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 */ |
| 58 | len = min ((size_t) 126, strlen (s->s)); |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 59 | len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN, |
| 60 | (wchar_t *) &buf[2], 126); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (len < 0) |
| 62 | return -EINVAL; |
| 63 | buf [0] = (len + 1) * 2; |
| 64 | buf [1] = USB_DT_STRING; |
| 65 | return buf [0]; |
| 66 | } |
Sebastian Andrzej Siewior | a84d9e5 | 2012-09-06 20:11:09 +0200 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(usb_gadget_get_string); |
Tao Ren | 17309a6 | 2020-03-15 12:16:28 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * usb_validate_langid - validate usb language identifiers |
Randy Dunlap | 4a0f5a7 | 2020-06-28 20:08:03 -0700 | [diff] [blame] | 71 | * @langid: usb language identifier |
Tao Ren | 17309a6 | 2020-03-15 12:16:28 -0700 | [diff] [blame] | 72 | * |
| 73 | * Returns true for valid language identifier, otherwise false. |
| 74 | */ |
| 75 | bool usb_validate_langid(u16 langid) |
| 76 | { |
| 77 | u16 primary_lang = langid & 0x3ff; /* bit [9:0] */ |
| 78 | u16 sub_lang = langid >> 10; /* bit [15:10] */ |
| 79 | |
| 80 | switch (primary_lang) { |
| 81 | case 0: |
| 82 | case 0x62 ... 0xfe: |
| 83 | case 0x100 ... 0x3ff: |
| 84 | return false; |
| 85 | } |
| 86 | if (!sub_lang) |
| 87 | return false; |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(usb_validate_langid); |