2 * hid.c -- HID Composite driver
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"
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,
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,
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 = "",
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[] = {
107 /****************************** Configurations ******************************/
109 static int do_config(struct usb_configuration *c)
111 struct hidg_func_node *e, *n;
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);
123 status = usb_add_function(c, e->f);
125 usb_put_function(e->f);
132 list_for_each_entry(n, &hidg_func_list, node) {
135 usb_remove_function(c, n->f);
136 usb_put_function(n->f);
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)
164 list_for_each_entry(n, &hidg_func_list, node) {
165 n->fi = usb_get_function_instance("hid");
167 status = PTR_ERR(n->fi);
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);
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);
194 usb_composite_overwrite_options(cdev, &coverwrite);
195 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
200 list_for_each_entry(m, &hidg_func_list, node) {
203 usb_put_function_instance(m->fi);
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);
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;
225 dev_err(&pdev->dev, "Platform data missing\n");
229 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
234 list_add_tail(&entry->node, &hidg_func_list);
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) {
252 /****************************** Some noise ******************************/
255 static struct usb_composite_driver hidg_driver = {
258 .strings = dev_strings,
259 .max_speed = USB_SPEED_HIGH,
261 .unbind = hid_unbind,
264 static struct platform_driver hidg_plat_driver = {
265 .remove = hidg_plat_driver_remove,
272 MODULE_DESCRIPTION(DRIVER_DESC);
273 MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
274 MODULE_LICENSE("GPL");
276 static int __init hidg_init(void)
280 status = platform_driver_probe(&hidg_plat_driver,
281 hidg_plat_driver_probe);
285 status = usb_composite_probe(&hidg_driver);
287 platform_driver_unregister(&hidg_plat_driver);
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);