blob: 73a4dfba0edbf8076c6183b817c1599d8dc58adb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 David Brownell
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <linux/errno.h>
11#include <linux/kernel.h>
Sebastian Andrzej Siewiora84d9e52012-09-06 20:11:09 +020012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/list.h>
14#include <linux/string.h>
15#include <linux/device.h>
Alan Stern86dc2432011-11-17 16:42:24 -050016#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David Brownell5f848132006-12-16 15:34:53 -080018#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070019#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/**
23 * usb_gadget_get_string - fill out a string descriptor
24 * @table: of c strings encoded using UTF-8
25 * @id: string id, from low byte of wValue in get string descriptor
Alan Stern86dc2432011-11-17 16:42:24 -050026 * @buf: at least 256 bytes, must be 16-bit aligned
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Finds the UTF-8 string matching the ID, and converts it into a
29 * string descriptor in utf16-le.
30 * Returns length of descriptor (always even) or negative errno
31 *
32 * If your driver needs stings in multiple languages, you'll probably
33 * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
34 * using this routine after choosing which set of UTF-8 strings to use.
35 * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
36 * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
37 * characters (which are also widely used in C strings).
38 */
39int
40usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
41{
42 struct usb_string *s;
43 int len;
44
45 /* descriptor 0 has the language id */
46 if (id == 0) {
47 buf [0] = 4;
48 buf [1] = USB_DT_STRING;
49 buf [2] = (u8) table->language;
50 buf [3] = (u8) (table->language >> 8);
51 return 4;
52 }
53 for (s = table->strings; s && s->s; s++)
54 if (s->id == id)
55 break;
56
57 /* unrecognized: stall. */
58 if (!s || !s->s)
59 return -EINVAL;
60
61 /* string descriptors have length, tag, then UTF16-LE text */
62 len = min ((size_t) 126, strlen (s->s));
Alan Stern86dc2432011-11-17 16:42:24 -050063 len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
64 (wchar_t *) &buf[2], 126);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (len < 0)
66 return -EINVAL;
67 buf [0] = (len + 1) * 2;
68 buf [1] = USB_DT_STRING;
69 return buf [0];
70}
Sebastian Andrzej Siewiora84d9e52012-09-06 20:11:09 +020071EXPORT_SYMBOL_GPL(usb_gadget_get_string);