Add linux-next specific files for 20110421
[linux-2.6/next.git] / drivers / usb / gadget / hid.c
blob2523e54097bd3529dce275c9a8ef1f0cebff57c9
1 /*
2 * hid.c -- HID Composite driver
4 * Based on multi.c
6 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/platform_device.h>
26 #include <linux/list.h>
28 #define DRIVER_DESC "HID Gadget"
29 #define DRIVER_VERSION "2010/03/16"
31 /*-------------------------------------------------------------------------*/
33 #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
34 #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
36 /*-------------------------------------------------------------------------*/
39 * kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
46 #include "composite.c"
47 #include "usbstring.c"
48 #include "config.c"
49 #include "epautoconf.c"
51 #include "f_hid.c"
54 struct hidg_func_node {
55 struct list_head node;
56 struct hidg_func_descriptor *func;
59 static LIST_HEAD(hidg_func_list);
61 /*-------------------------------------------------------------------------*/
63 static struct usb_device_descriptor device_desc = {
64 .bLength = sizeof device_desc,
65 .bDescriptorType = USB_DT_DEVICE,
67 .bcdUSB = cpu_to_le16(0x0200),
69 /* .bDeviceClass = USB_CLASS_COMM, */
70 /* .bDeviceSubClass = 0, */
71 /* .bDeviceProtocol = 0, */
72 .bDeviceClass = 0xEF,
73 .bDeviceSubClass = 2,
74 .bDeviceProtocol = 1,
75 /* .bMaxPacketSize0 = f(hardware) */
77 /* Vendor and product id can be overridden by module parameters. */
78 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
79 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
80 /* .bcdDevice = f(hardware) */
81 /* .iManufacturer = DYNAMIC */
82 /* .iProduct = DYNAMIC */
83 /* NO SERIAL NUMBER */
84 .bNumConfigurations = 1,
87 static struct usb_otg_descriptor otg_descriptor = {
88 .bLength = sizeof otg_descriptor,
89 .bDescriptorType = USB_DT_OTG,
91 /* REVISIT SRP-only hardware is possible, although
92 * it would not be called "OTG" ...
94 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
97 static const struct usb_descriptor_header *otg_desc[] = {
98 (struct usb_descriptor_header *) &otg_descriptor,
99 NULL,
103 /* string IDs are assigned dynamically */
105 #define STRING_MANUFACTURER_IDX 0
106 #define STRING_PRODUCT_IDX 1
108 static char manufacturer[50];
110 static struct usb_string strings_dev[] = {
111 [STRING_MANUFACTURER_IDX].s = manufacturer,
112 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
113 { } /* end of list */
116 static struct usb_gadget_strings stringtab_dev = {
117 .language = 0x0409, /* en-us */
118 .strings = strings_dev,
121 static struct usb_gadget_strings *dev_strings[] = {
122 &stringtab_dev,
123 NULL,
128 /****************************** Configurations ******************************/
130 static int __init do_config(struct usb_configuration *c)
132 struct hidg_func_node *e;
133 int func = 0, status = 0;
135 if (gadget_is_otg(c->cdev->gadget)) {
136 c->descriptors = otg_desc;
137 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
140 list_for_each_entry(e, &hidg_func_list, node) {
141 status = hidg_bind_config(c, e->func, func++);
142 if (status)
143 break;
146 return status;
149 static struct usb_configuration config_driver = {
150 .label = "HID Gadget",
151 .bConfigurationValue = 1,
152 /* .iConfiguration = DYNAMIC */
153 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
156 /****************************** Gadget Bind ******************************/
158 static int __init hid_bind(struct usb_composite_dev *cdev)
160 struct usb_gadget *gadget = cdev->gadget;
161 struct list_head *tmp;
162 int status, gcnum, funcs = 0;
164 list_for_each(tmp, &hidg_func_list)
165 funcs++;
167 if (!funcs)
168 return -ENODEV;
170 /* set up HID */
171 status = ghid_setup(cdev->gadget, funcs);
172 if (status < 0)
173 return status;
175 gcnum = usb_gadget_controller_number(gadget);
176 if (gcnum >= 0)
177 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
178 else
179 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
182 /* Allocate string descriptor numbers ... note that string
183 * contents can be overridden by the composite_dev glue.
186 /* device descriptor strings: manufacturer, product */
187 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
188 init_utsname()->sysname, init_utsname()->release,
189 gadget->name);
190 status = usb_string_id(cdev);
191 if (status < 0)
192 return status;
193 strings_dev[STRING_MANUFACTURER_IDX].id = status;
194 device_desc.iManufacturer = status;
196 status = usb_string_id(cdev);
197 if (status < 0)
198 return status;
199 strings_dev[STRING_PRODUCT_IDX].id = status;
200 device_desc.iProduct = status;
202 /* register our configuration */
203 status = usb_add_config(cdev, &config_driver, do_config);
204 if (status < 0)
205 return status;
207 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
209 return 0;
212 static int __exit hid_unbind(struct usb_composite_dev *cdev)
214 ghid_cleanup();
215 return 0;
218 static int __init hidg_plat_driver_probe(struct platform_device *pdev)
220 struct hidg_func_descriptor *func = pdev->dev.platform_data;
221 struct hidg_func_node *entry;
223 if (!func) {
224 dev_err(&pdev->dev, "Platform data missing\n");
225 return -ENODEV;
228 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
229 if (!entry)
230 return -ENOMEM;
232 entry->func = func;
233 list_add_tail(&entry->node, &hidg_func_list);
235 return 0;
238 static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
240 struct hidg_func_node *e, *n;
242 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
243 list_del(&e->node);
244 kfree(e);
247 return 0;
251 /****************************** Some noise ******************************/
254 static struct usb_composite_driver hidg_driver = {
255 .name = "g_hid",
256 .dev = &device_desc,
257 .strings = dev_strings,
258 .unbind = __exit_p(hid_unbind),
261 static struct platform_driver hidg_plat_driver = {
262 .remove = __devexit_p(hidg_plat_driver_remove),
263 .driver = {
264 .owner = THIS_MODULE,
265 .name = "hidg",
270 MODULE_DESCRIPTION(DRIVER_DESC);
271 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
272 MODULE_LICENSE("GPL");
274 static int __init hidg_init(void)
276 int status;
278 status = platform_driver_probe(&hidg_plat_driver,
279 hidg_plat_driver_probe);
280 if (status < 0)
281 return status;
283 status = usb_composite_probe(&hidg_driver, hid_bind);
284 if (status < 0)
285 platform_driver_unregister(&hidg_plat_driver);
287 return status;
289 module_init(hidg_init);
291 static void __exit hidg_cleanup(void)
293 platform_driver_unregister(&hidg_plat_driver);
294 usb_composite_unregister(&hidg_driver);
296 module_exit(hidg_cleanup);