Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / hid.c
blob2baa572686c6acbd5e1350ab167b65a367482302
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.
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/usb/composite.h>
20 #include <linux/usb/g_hid.h>
22 #include "gadget_chips.h"
23 #define DRIVER_DESC "HID Gadget"
24 #define DRIVER_VERSION "2010/03/16"
26 #include "u_hid.h"
28 /*-------------------------------------------------------------------------*/
30 #define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
31 #define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
33 /*-------------------------------------------------------------------------*/
35 struct hidg_func_node {
36 struct usb_function_instance *fi;
37 struct usb_function *f;
38 struct list_head node;
39 struct hidg_func_descriptor *func;
42 static LIST_HEAD(hidg_func_list);
44 /*-------------------------------------------------------------------------*/
45 USB_GADGET_COMPOSITE_OPTIONS();
47 static struct usb_device_descriptor device_desc = {
48 .bLength = sizeof device_desc,
49 .bDescriptorType = USB_DT_DEVICE,
51 .bcdUSB = cpu_to_le16(0x0200),
53 /* .bDeviceClass = USB_CLASS_COMM, */
54 /* .bDeviceSubClass = 0, */
55 /* .bDeviceProtocol = 0, */
56 .bDeviceClass = USB_CLASS_PER_INTERFACE,
57 .bDeviceSubClass = 0,
58 .bDeviceProtocol = 0,
59 /* .bMaxPacketSize0 = f(hardware) */
61 /* Vendor and product id can be overridden by module parameters. */
62 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
63 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
64 /* .bcdDevice = f(hardware) */
65 /* .iManufacturer = DYNAMIC */
66 /* .iProduct = DYNAMIC */
67 /* NO SERIAL NUMBER */
68 .bNumConfigurations = 1,
71 static struct usb_otg_descriptor otg_descriptor = {
72 .bLength = sizeof otg_descriptor,
73 .bDescriptorType = USB_DT_OTG,
75 /* REVISIT SRP-only hardware is possible, although
76 * it would not be called "OTG" ...
78 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
81 static const struct usb_descriptor_header *otg_desc[] = {
82 (struct usb_descriptor_header *) &otg_descriptor,
83 NULL,
87 /* string IDs are assigned dynamically */
88 static struct usb_string strings_dev[] = {
89 [USB_GADGET_MANUFACTURER_IDX].s = "",
90 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
91 [USB_GADGET_SERIAL_IDX].s = "",
92 { } /* end of list */
95 static struct usb_gadget_strings stringtab_dev = {
96 .language = 0x0409, /* en-us */
97 .strings = strings_dev,
100 static struct usb_gadget_strings *dev_strings[] = {
101 &stringtab_dev,
102 NULL,
107 /****************************** Configurations ******************************/
109 static int do_config(struct usb_configuration *c)
111 struct hidg_func_node *e, *n;
112 int status = 0;
114 if (gadget_is_otg(c->cdev->gadget)) {
115 c->descriptors = otg_desc;
116 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
119 list_for_each_entry(e, &hidg_func_list, node) {
120 e->f = usb_get_function(e->fi);
121 if (IS_ERR(e->f))
122 goto put;
123 status = usb_add_function(c, e->f);
124 if (status < 0) {
125 usb_put_function(e->f);
126 goto put;
130 return 0;
131 put:
132 list_for_each_entry(n, &hidg_func_list, node) {
133 if (n == e)
134 break;
135 usb_remove_function(c, n->f);
136 usb_put_function(n->f);
138 return status;
141 static struct usb_configuration config_driver = {
142 .label = "HID Gadget",
143 .bConfigurationValue = 1,
144 /* .iConfiguration = DYNAMIC */
145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
148 /****************************** Gadget Bind ******************************/
150 static int hid_bind(struct usb_composite_dev *cdev)
152 struct usb_gadget *gadget = cdev->gadget;
153 struct list_head *tmp;
154 struct hidg_func_node *n, *m;
155 struct f_hid_opts *hid_opts;
156 int status, funcs = 0;
158 list_for_each(tmp, &hidg_func_list)
159 funcs++;
161 if (!funcs)
162 return -ENODEV;
164 list_for_each_entry(n, &hidg_func_list, node) {
165 n->fi = usb_get_function_instance("hid");
166 if (IS_ERR(n->fi)) {
167 status = PTR_ERR(n->fi);
168 goto put;
170 hid_opts = container_of(n->fi, struct f_hid_opts, func_inst);
171 hid_opts->subclass = n->func->subclass;
172 hid_opts->protocol = n->func->protocol;
173 hid_opts->report_length = n->func->report_length;
174 hid_opts->report_desc_length = n->func->report_desc_length;
175 hid_opts->report_desc = n->func->report_desc;
179 /* Allocate string descriptor numbers ... note that string
180 * contents can be overridden by the composite_dev glue.
183 status = usb_string_ids_tab(cdev, strings_dev);
184 if (status < 0)
185 goto put;
186 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
187 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
189 /* register our configuration */
190 status = usb_add_config(cdev, &config_driver, do_config);
191 if (status < 0)
192 goto put;
194 usb_composite_overwrite_options(cdev, &coverwrite);
195 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
197 return 0;
199 put:
200 list_for_each_entry(m, &hidg_func_list, node) {
201 if (m == n)
202 break;
203 usb_put_function_instance(m->fi);
205 return status;
208 static int hid_unbind(struct usb_composite_dev *cdev)
210 struct hidg_func_node *n;
212 list_for_each_entry(n, &hidg_func_list, node) {
213 usb_put_function(n->f);
214 usb_put_function_instance(n->fi);
216 return 0;
219 static int hidg_plat_driver_probe(struct platform_device *pdev)
221 struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev);
222 struct hidg_func_node *entry;
224 if (!func) {
225 dev_err(&pdev->dev, "Platform data missing\n");
226 return -ENODEV;
229 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
230 if (!entry)
231 return -ENOMEM;
233 entry->func = func;
234 list_add_tail(&entry->node, &hidg_func_list);
236 return 0;
239 static int hidg_plat_driver_remove(struct platform_device *pdev)
241 struct hidg_func_node *e, *n;
243 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
244 list_del(&e->node);
245 kfree(e);
248 return 0;
252 /****************************** Some noise ******************************/
255 static struct usb_composite_driver hidg_driver = {
256 .name = "g_hid",
257 .dev = &device_desc,
258 .strings = dev_strings,
259 .max_speed = USB_SPEED_HIGH,
260 .bind = hid_bind,
261 .unbind = hid_unbind,
264 static struct platform_driver hidg_plat_driver = {
265 .remove = hidg_plat_driver_remove,
266 .driver = {
267 .name = "hidg",
272 MODULE_DESCRIPTION(DRIVER_DESC);
273 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
274 MODULE_LICENSE("GPL");
276 static int __init hidg_init(void)
278 int status;
280 status = platform_driver_probe(&hidg_plat_driver,
281 hidg_plat_driver_probe);
282 if (status < 0)
283 return status;
285 status = usb_composite_probe(&hidg_driver);
286 if (status < 0)
287 platform_driver_unregister(&hidg_plat_driver);
289 return status;
291 module_init(hidg_init);
293 static void __exit hidg_cleanup(void)
295 usb_composite_unregister(&hidg_driver);
296 platform_driver_unregister(&hidg_plat_driver);
298 module_exit(hidg_cleanup);