usbstring.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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) 126, strlen (s->s));
  52. len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
  53. (wchar_t *) &buf[2], 126);
  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);