usbstring.c 2.4 KB

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