Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Siewior | a84d9e5 | 2012-09-06 20:11:09 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/init.h> |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 17 | #include <linux/nls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 19 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 20 | #include <linux/usb/gadget.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * usb_gadget_get_string - fill out a string descriptor |
| 25 | * @table: of c strings encoded using UTF-8 |
| 26 | * @id: string id, from low byte of wValue in get string descriptor |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 27 | * @buf: at least 256 bytes, must be 16-bit aligned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * |
| 29 | * Finds the UTF-8 string matching the ID, and converts it into a |
| 30 | * string descriptor in utf16-le. |
| 31 | * Returns length of descriptor (always even) or negative errno |
| 32 | * |
| 33 | * If your driver needs stings in multiple languages, you'll probably |
| 34 | * "switch (wIndex) { ... }" in your ep0 string descriptor logic, |
| 35 | * using this routine after choosing which set of UTF-8 strings to use. |
| 36 | * Note that US-ASCII is a strict subset of UTF-8; any string bytes with |
| 37 | * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1 |
| 38 | * characters (which are also widely used in C strings). |
| 39 | */ |
| 40 | int |
| 41 | usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf) |
| 42 | { |
| 43 | struct usb_string *s; |
| 44 | int len; |
| 45 | |
| 46 | /* descriptor 0 has the language id */ |
| 47 | if (id == 0) { |
| 48 | buf [0] = 4; |
| 49 | buf [1] = USB_DT_STRING; |
| 50 | buf [2] = (u8) table->language; |
| 51 | buf [3] = (u8) (table->language >> 8); |
| 52 | return 4; |
| 53 | } |
| 54 | for (s = table->strings; s && s->s; s++) |
| 55 | if (s->id == id) |
| 56 | break; |
| 57 | |
| 58 | /* unrecognized: stall. */ |
| 59 | if (!s || !s->s) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | /* string descriptors have length, tag, then UTF16-LE text */ |
| 63 | len = min ((size_t) 126, strlen (s->s)); |
Alan Stern | 86dc243 | 2011-11-17 16:42:24 -0500 | [diff] [blame] | 64 | len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN, |
| 65 | (wchar_t *) &buf[2], 126); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (len < 0) |
| 67 | return -EINVAL; |
| 68 | buf [0] = (len + 1) * 2; |
| 69 | buf [1] = USB_DT_STRING; |
| 70 | return buf [0]; |
| 71 | } |
Sebastian Andrzej Siewior | a84d9e5 | 2012-09-06 20:11:09 +0200 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(usb_gadget_get_string); |