Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
usbstring.c
Go to the documentation of this file.
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>
12 #include <linux/list.h>
13 #include <linux/string.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/nls.h>
17 
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/gadget.h>
20 
21 
39 int
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));
63  len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
64  (wchar_t *) &buf[2], 126);
65  if (len < 0)
66  return -EINVAL;
67  buf [0] = (len + 1) * 2;
68  buf [1] = USB_DT_STRING;
69  return buf [0];
70 }
71