^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: LGPL-2.1+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2003 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/nls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/usb/ch9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * usb_gadget_get_string - fill out a string descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @table: of c strings encoded using UTF-8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @id: string id, from low byte of wValue in get string descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @buf: at least 256 bytes, must be 16-bit aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Finds the UTF-8 string matching the ID, and converts it into a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * string descriptor in utf16-le.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Returns length of descriptor (always even) or negative errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * If your driver needs stings in multiple languages, you'll probably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * using this routine after choosing which set of UTF-8 strings to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * characters (which are also widely used in C strings).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct usb_string *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* descriptor 0 has the language id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) buf [0] = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) buf [1] = USB_DT_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) buf [2] = (u8) table->language;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) buf [3] = (u8) (table->language >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) for (s = table->strings; s && s->s; s++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (s->id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* unrecognized: stall. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!s || !s->s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* string descriptors have length, tag, then UTF16-LE text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) len = min((size_t)USB_MAX_STRING_LEN, strlen(s->s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) (wchar_t *) &buf[2], USB_MAX_STRING_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) buf [0] = (len + 1) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) buf [1] = USB_DT_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return buf [0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) EXPORT_SYMBOL_GPL(usb_gadget_get_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * usb_validate_langid - validate usb language identifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * @langid: usb language identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Returns true for valid language identifier, otherwise false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) bool usb_validate_langid(u16 langid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) u16 primary_lang = langid & 0x3ff; /* bit [9:0] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u16 sub_lang = langid >> 10; /* bit [15:10] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) switch (primary_lang) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case 0x62 ... 0xfe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) case 0x100 ... 0x3ff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!sub_lang)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) EXPORT_SYMBOL_GPL(usb_validate_langid);