usb: gadget: Provide a default implementation of default manufacturer string
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / gadget / hid.c
1 /*
2 * hid.c -- HID Composite driver
3 *
4 * Based on multi.c
5 *
6 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/list.h>
18
19 #include "gadget_chips.h"
20 #define DRIVER_DESC "HID Gadget"
21 #define DRIVER_VERSION "2010/03/16"
22
23 /*-------------------------------------------------------------------------*/
24
25 #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
26 #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
27
28 /*-------------------------------------------------------------------------*/
29
30 /*
31 * kbuild is not very cooperative with respect to linking separately
32 * compiled library objects into one module. So for now we won't use
33 * separate compilation ... ensuring init/exit sections work to shrink
34 * the runtime footprint, and giving us at least some parts of what
35 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
36 */
37
38 #include "composite.c"
39
40 #include "f_hid.c"
41
42
43 struct hidg_func_node {
44 struct list_head node;
45 struct hidg_func_descriptor *func;
46 };
47
48 static LIST_HEAD(hidg_func_list);
49
50 /*-------------------------------------------------------------------------*/
51 USB_GADGET_COMPOSITE_OPTIONS();
52
53 static struct usb_device_descriptor device_desc = {
54 .bLength = sizeof device_desc,
55 .bDescriptorType = USB_DT_DEVICE,
56
57 .bcdUSB = cpu_to_le16(0x0200),
58
59 /* .bDeviceClass = USB_CLASS_COMM, */
60 /* .bDeviceSubClass = 0, */
61 /* .bDeviceProtocol = 0, */
62 .bDeviceClass = USB_CLASS_PER_INTERFACE,
63 .bDeviceSubClass = 0,
64 .bDeviceProtocol = 0,
65 /* .bMaxPacketSize0 = f(hardware) */
66
67 /* Vendor and product id can be overridden by module parameters. */
68 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
69 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
70 /* .bcdDevice = f(hardware) */
71 /* .iManufacturer = DYNAMIC */
72 /* .iProduct = DYNAMIC */
73 /* NO SERIAL NUMBER */
74 .bNumConfigurations = 1,
75 };
76
77 static struct usb_otg_descriptor otg_descriptor = {
78 .bLength = sizeof otg_descriptor,
79 .bDescriptorType = USB_DT_OTG,
80
81 /* REVISIT SRP-only hardware is possible, although
82 * it would not be called "OTG" ...
83 */
84 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
85 };
86
87 static const struct usb_descriptor_header *otg_desc[] = {
88 (struct usb_descriptor_header *) &otg_descriptor,
89 NULL,
90 };
91
92
93 /* string IDs are assigned dynamically */
94 static struct usb_string strings_dev[] = {
95 [USB_GADGET_MANUFACTURER_IDX].s = "",
96 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
97 [USB_GADGET_SERIAL_IDX].s = "",
98 { } /* end of list */
99 };
100
101 static struct usb_gadget_strings stringtab_dev = {
102 .language = 0x0409, /* en-us */
103 .strings = strings_dev,
104 };
105
106 static struct usb_gadget_strings *dev_strings[] = {
107 &stringtab_dev,
108 NULL,
109 };
110
111
112
113 /****************************** Configurations ******************************/
114
115 static int __init do_config(struct usb_configuration *c)
116 {
117 struct hidg_func_node *e;
118 int func = 0, status = 0;
119
120 if (gadget_is_otg(c->cdev->gadget)) {
121 c->descriptors = otg_desc;
122 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
123 }
124
125 list_for_each_entry(e, &hidg_func_list, node) {
126 status = hidg_bind_config(c, e->func, func++);
127 if (status)
128 break;
129 }
130
131 return status;
132 }
133
134 static struct usb_configuration config_driver = {
135 .label = "HID Gadget",
136 .bConfigurationValue = 1,
137 /* .iConfiguration = DYNAMIC */
138 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
139 };
140
141 /****************************** Gadget Bind ******************************/
142
143 static int __init hid_bind(struct usb_composite_dev *cdev)
144 {
145 struct usb_gadget *gadget = cdev->gadget;
146 struct list_head *tmp;
147 int status, gcnum, funcs = 0;
148
149 list_for_each(tmp, &hidg_func_list)
150 funcs++;
151
152 if (!funcs)
153 return -ENODEV;
154
155 /* set up HID */
156 status = ghid_setup(cdev->gadget, funcs);
157 if (status < 0)
158 return status;
159
160 gcnum = usb_gadget_controller_number(gadget);
161 if (gcnum >= 0)
162 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
163 else
164 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
165
166 /* Allocate string descriptor numbers ... note that string
167 * contents can be overridden by the composite_dev glue.
168 */
169
170 status = usb_string_ids_tab(cdev, strings_dev);
171 if (status < 0)
172 return status;
173 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
174 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
175
176 /* register our configuration */
177 status = usb_add_config(cdev, &config_driver, do_config);
178 if (status < 0)
179 return status;
180
181 usb_composite_overwrite_options(cdev, &coverwrite);
182 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
183
184 return 0;
185 }
186
187 static int __exit hid_unbind(struct usb_composite_dev *cdev)
188 {
189 ghid_cleanup();
190 return 0;
191 }
192
193 static int __init hidg_plat_driver_probe(struct platform_device *pdev)
194 {
195 struct hidg_func_descriptor *func = pdev->dev.platform_data;
196 struct hidg_func_node *entry;
197
198 if (!func) {
199 dev_err(&pdev->dev, "Platform data missing\n");
200 return -ENODEV;
201 }
202
203 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
204 if (!entry)
205 return -ENOMEM;
206
207 entry->func = func;
208 list_add_tail(&entry->node, &hidg_func_list);
209
210 return 0;
211 }
212
213 static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
214 {
215 struct hidg_func_node *e, *n;
216
217 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
218 list_del(&e->node);
219 kfree(e);
220 }
221
222 return 0;
223 }
224
225
226 /****************************** Some noise ******************************/
227
228
229 static __refdata struct usb_composite_driver hidg_driver = {
230 .name = "g_hid",
231 .dev = &device_desc,
232 .strings = dev_strings,
233 .max_speed = USB_SPEED_HIGH,
234 .bind = hid_bind,
235 .unbind = __exit_p(hid_unbind),
236 };
237
238 static struct platform_driver hidg_plat_driver = {
239 .remove = __devexit_p(hidg_plat_driver_remove),
240 .driver = {
241 .owner = THIS_MODULE,
242 .name = "hidg",
243 },
244 };
245
246
247 MODULE_DESCRIPTION(DRIVER_DESC);
248 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
249 MODULE_LICENSE("GPL");
250
251 static int __init hidg_init(void)
252 {
253 int status;
254
255 status = platform_driver_probe(&hidg_plat_driver,
256 hidg_plat_driver_probe);
257 if (status < 0)
258 return status;
259
260 status = usb_composite_probe(&hidg_driver);
261 if (status < 0)
262 platform_driver_unregister(&hidg_plat_driver);
263
264 return status;
265 }
266 module_init(hidg_init);
267
268 static void __exit hidg_cleanup(void)
269 {
270 platform_driver_unregister(&hidg_plat_driver);
271 usb_composite_unregister(&hidg_driver);
272 }
273 module_exit(hidg_cleanup);