GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / usb / gadget / legacy / 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 #include <linux/module.h>
19 #include <linux/usb/composite.h>
20 #include <linux/usb/g_hid.h>
21
22 #define DRIVER_DESC             "HID Gadget"
23 #define DRIVER_VERSION          "2010/03/16"
24
25 #include "u_hid.h"
26
27 /*-------------------------------------------------------------------------*/
28
29 #define HIDG_VENDOR_NUM         0x0525  /* XXX NetChip */
30 #define HIDG_PRODUCT_NUM        0xa4ac  /* Linux-USB HID gadget */
31
32 /*-------------------------------------------------------------------------*/
33
34 struct hidg_func_node {
35         struct usb_function_instance *fi;
36         struct usb_function *f;
37         struct list_head node;
38         struct hidg_func_descriptor *func;
39 };
40
41 static LIST_HEAD(hidg_func_list);
42
43 /*-------------------------------------------------------------------------*/
44 USB_GADGET_COMPOSITE_OPTIONS();
45
46 static struct usb_device_descriptor device_desc = {
47         .bLength =              sizeof device_desc,
48         .bDescriptorType =      USB_DT_DEVICE,
49
50         /* .bcdUSB = DYNAMIC */
51
52         /* .bDeviceClass =              USB_CLASS_COMM, */
53         /* .bDeviceSubClass =   0, */
54         /* .bDeviceProtocol =   0, */
55         .bDeviceClass =         USB_CLASS_PER_INTERFACE,
56         .bDeviceSubClass =      0,
57         .bDeviceProtocol =      0,
58         /* .bMaxPacketSize0 = f(hardware) */
59
60         /* Vendor and product id can be overridden by module parameters.  */
61         .idVendor =             cpu_to_le16(HIDG_VENDOR_NUM),
62         .idProduct =            cpu_to_le16(HIDG_PRODUCT_NUM),
63         /* .bcdDevice = f(hardware) */
64         /* .iManufacturer = DYNAMIC */
65         /* .iProduct = DYNAMIC */
66         /* NO SERIAL NUMBER */
67         .bNumConfigurations =   1,
68 };
69
70 static const struct usb_descriptor_header *otg_desc[2];
71
72 /* string IDs are assigned dynamically */
73 static struct usb_string strings_dev[] = {
74         [USB_GADGET_MANUFACTURER_IDX].s = "",
75         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
76         [USB_GADGET_SERIAL_IDX].s = "",
77         {  } /* end of list */
78 };
79
80 static struct usb_gadget_strings stringtab_dev = {
81         .language       = 0x0409,       /* en-us */
82         .strings        = strings_dev,
83 };
84
85 static struct usb_gadget_strings *dev_strings[] = {
86         &stringtab_dev,
87         NULL,
88 };
89
90
91
92 /****************************** Configurations ******************************/
93
94 static int do_config(struct usb_configuration *c)
95 {
96         struct hidg_func_node *e, *n;
97         int status = 0;
98
99         if (gadget_is_otg(c->cdev->gadget)) {
100                 c->descriptors = otg_desc;
101                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
102         }
103
104         list_for_each_entry(e, &hidg_func_list, node) {
105                 e->f = usb_get_function(e->fi);
106                 if (IS_ERR(e->f)) {
107                         status = PTR_ERR(e->f);
108                         goto put;
109                 }
110                 status = usb_add_function(c, e->f);
111                 if (status < 0) {
112                         usb_put_function(e->f);
113                         goto put;
114                 }
115         }
116
117         return 0;
118 put:
119         list_for_each_entry(n, &hidg_func_list, node) {
120                 if (n == e)
121                         break;
122                 usb_remove_function(c, n->f);
123                 usb_put_function(n->f);
124         }
125         return status;
126 }
127
128 static struct usb_configuration config_driver = {
129         .label                  = "HID Gadget",
130         .bConfigurationValue    = 1,
131         /* .iConfiguration = DYNAMIC */
132         .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
133 };
134
135 /****************************** Gadget Bind ******************************/
136
137 static int hid_bind(struct usb_composite_dev *cdev)
138 {
139         struct usb_gadget *gadget = cdev->gadget;
140         struct list_head *tmp;
141         struct hidg_func_node *n, *m;
142         struct f_hid_opts *hid_opts;
143         int status, funcs = 0;
144
145         list_for_each(tmp, &hidg_func_list)
146                 funcs++;
147
148         if (!funcs)
149                 return -ENODEV;
150
151         list_for_each_entry(n, &hidg_func_list, node) {
152                 n->fi = usb_get_function_instance("hid");
153                 if (IS_ERR(n->fi)) {
154                         status = PTR_ERR(n->fi);
155                         goto put;
156                 }
157                 hid_opts = container_of(n->fi, struct f_hid_opts, func_inst);
158                 hid_opts->subclass = n->func->subclass;
159                 hid_opts->protocol = n->func->protocol;
160                 hid_opts->report_length = n->func->report_length;
161                 hid_opts->report_desc_length = n->func->report_desc_length;
162                 hid_opts->report_desc = n->func->report_desc;
163         }
164
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                 goto put;
173         device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
174         device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
175
176         if (gadget_is_otg(gadget) && !otg_desc[0]) {
177                 struct usb_descriptor_header *usb_desc;
178
179                 usb_desc = usb_otg_descriptor_alloc(gadget);
180                 if (!usb_desc) {
181                         status = -ENOMEM;
182                         goto put;
183                 }
184                 usb_otg_descriptor_init(gadget, usb_desc);
185                 otg_desc[0] = usb_desc;
186                 otg_desc[1] = NULL;
187         }
188
189         /* register our configuration */
190         status = usb_add_config(cdev, &config_driver, do_config);
191         if (status < 0)
192                 goto free_otg_desc;
193
194         usb_composite_overwrite_options(cdev, &coverwrite);
195         dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
196
197         return 0;
198
199 free_otg_desc:
200         kfree(otg_desc[0]);
201         otg_desc[0] = NULL;
202 put:
203         list_for_each_entry(m, &hidg_func_list, node) {
204                 if (m == n)
205                         break;
206                 usb_put_function_instance(m->fi);
207         }
208         return status;
209 }
210
211 static int hid_unbind(struct usb_composite_dev *cdev)
212 {
213         struct hidg_func_node *n;
214
215         list_for_each_entry(n, &hidg_func_list, node) {
216                 usb_put_function(n->f);
217                 usb_put_function_instance(n->fi);
218         }
219
220         kfree(otg_desc[0]);
221         otg_desc[0] = NULL;
222
223         return 0;
224 }
225
226 static int hidg_plat_driver_probe(struct platform_device *pdev)
227 {
228         struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev);
229         struct hidg_func_node *entry;
230
231         if (!func) {
232                 dev_err(&pdev->dev, "Platform data missing\n");
233                 return -ENODEV;
234         }
235
236         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
237         if (!entry)
238                 return -ENOMEM;
239
240         entry->func = func;
241         list_add_tail(&entry->node, &hidg_func_list);
242
243         return 0;
244 }
245
246 static int hidg_plat_driver_remove(struct platform_device *pdev)
247 {
248         struct hidg_func_node *e, *n;
249
250         list_for_each_entry_safe(e, n, &hidg_func_list, node) {
251                 list_del(&e->node);
252                 kfree(e);
253         }
254
255         return 0;
256 }
257
258
259 /****************************** Some noise ******************************/
260
261
262 static struct usb_composite_driver hidg_driver = {
263         .name           = "g_hid",
264         .dev            = &device_desc,
265         .strings        = dev_strings,
266         .max_speed      = USB_SPEED_HIGH,
267         .bind           = hid_bind,
268         .unbind         = hid_unbind,
269 };
270
271 static struct platform_driver hidg_plat_driver = {
272         .remove         = hidg_plat_driver_remove,
273         .driver         = {
274                 .name   = "hidg",
275         },
276 };
277
278
279 MODULE_DESCRIPTION(DRIVER_DESC);
280 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
281 MODULE_LICENSE("GPL");
282
283 static int __init hidg_init(void)
284 {
285         int status;
286
287         status = platform_driver_probe(&hidg_plat_driver,
288                                 hidg_plat_driver_probe);
289         if (status < 0)
290                 return status;
291
292         status = usb_composite_probe(&hidg_driver);
293         if (status < 0)
294                 platform_driver_unregister(&hidg_plat_driver);
295
296         return status;
297 }
298 module_init(hidg_init);
299
300 static void __exit hidg_cleanup(void)
301 {
302         usb_composite_unregister(&hidg_driver);
303         platform_driver_unregister(&hidg_plat_driver);
304 }
305 module_exit(hidg_cleanup);