blob: 26cb4ea2aaf7b294855a554869f9c72175304e12 [file] [log] [blame]
Fabien Chouteau71adf112010-04-08 09:31:15 +02001/*
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.
Fabien Chouteau71adf112010-04-08 09:31:15 +020012 */
13
14
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/list.h>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020018#include <linux/module.h>
19#include <linux/usb/composite.h>
Fabien Chouteau71adf112010-04-08 09:31:15 +020020
Sebastian Andrzej Siewiordc995fc2012-09-06 20:11:12 +020021#include "gadget_chips.h"
Fabien Chouteau71adf112010-04-08 09:31:15 +020022#define DRIVER_DESC "HID Gadget"
23#define DRIVER_VERSION "2010/03/16"
24
25/*-------------------------------------------------------------------------*/
26
27#define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
28#define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
29
30/*-------------------------------------------------------------------------*/
31
32/*
33 * kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
Andrzej Pietrasiewiczcb382532014-11-06 11:11:59 +010039#define USBF_HID_INCLUDED
Fabien Chouteau71adf112010-04-08 09:31:15 +020040#include "f_hid.c"
41
42
43struct hidg_func_node {
44 struct list_head node;
45 struct hidg_func_descriptor *func;
46};
47
48static LIST_HEAD(hidg_func_list);
49
50/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020051USB_GADGET_COMPOSITE_OPTIONS();
Fabien Chouteau71adf112010-04-08 09:31:15 +020052
53static 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, */
Orjan Friberg33d28322012-03-07 17:16:14 +010062 .bDeviceClass = USB_CLASS_PER_INTERFACE,
63 .bDeviceSubClass = 0,
64 .bDeviceProtocol = 0,
Fabien Chouteau71adf112010-04-08 09:31:15 +020065 /* .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
77static 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
87static 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 */
Fabien Chouteau71adf112010-04-08 09:31:15 +020094static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020095 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020096 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
97 [USB_GADGET_SERIAL_IDX].s = "",
Fabien Chouteau71adf112010-04-08 09:31:15 +020098 { } /* end of list */
99};
100
101static struct usb_gadget_strings stringtab_dev = {
102 .language = 0x0409, /* en-us */
103 .strings = strings_dev,
104};
105
106static struct usb_gadget_strings *dev_strings[] = {
107 &stringtab_dev,
108 NULL,
109};
110
111
112
113/****************************** Configurations ******************************/
114
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200115static int __init do_config(struct usb_configuration *c)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200116{
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
134static struct usb_configuration config_driver = {
135 .label = "HID Gadget",
Fabien Chouteau71adf112010-04-08 09:31:15 +0200136 .bConfigurationValue = 1,
137 /* .iConfiguration = DYNAMIC */
138 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
139};
140
141/****************************** Gadget Bind ******************************/
142
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200143static int __init hid_bind(struct usb_composite_dev *cdev)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200144{
145 struct usb_gadget *gadget = cdev->gadget;
146 struct list_head *tmp;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +0200147 int status, funcs = 0;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200148
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
Fabien Chouteau71adf112010-04-08 09:31:15 +0200160 /* Allocate string descriptor numbers ... note that string
161 * contents can be overridden by the composite_dev glue.
162 */
163
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200164 status = usb_string_ids_tab(cdev, strings_dev);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200165 if (status < 0)
166 return status;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200167 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
168 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200169
170 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200171 status = usb_add_config(cdev, &config_driver, do_config);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200172 if (status < 0)
173 return status;
174
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200175 usb_composite_overwrite_options(cdev, &coverwrite);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200176 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
177
178 return 0;
179}
180
181static int __exit hid_unbind(struct usb_composite_dev *cdev)
182{
183 ghid_cleanup();
184 return 0;
185}
186
187static int __init hidg_plat_driver_probe(struct platform_device *pdev)
188{
Jingoo Hane01ee9f2013-07-30 17:00:51 +0900189 struct hidg_func_descriptor *func = dev_get_platdata(&pdev->dev);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200190 struct hidg_func_node *entry;
191
192 if (!func) {
193 dev_err(&pdev->dev, "Platform data missing\n");
194 return -ENODEV;
195 }
196
197 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
198 if (!entry)
199 return -ENOMEM;
200
201 entry->func = func;
202 list_add_tail(&entry->node, &hidg_func_list);
203
204 return 0;
205}
206
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500207static int hidg_plat_driver_remove(struct platform_device *pdev)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200208{
209 struct hidg_func_node *e, *n;
210
211 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
212 list_del(&e->node);
213 kfree(e);
214 }
215
216 return 0;
217}
218
219
220/****************************** Some noise ******************************/
221
222
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200223static __refdata struct usb_composite_driver hidg_driver = {
Fabien Chouteau71adf112010-04-08 09:31:15 +0200224 .name = "g_hid",
225 .dev = &device_desc,
226 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300227 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200228 .bind = hid_bind,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200229 .unbind = __exit_p(hid_unbind),
230};
231
232static struct platform_driver hidg_plat_driver = {
Bill Pemberton76904172012-11-19 13:21:08 -0500233 .remove = hidg_plat_driver_remove,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200234 .driver = {
235 .owner = THIS_MODULE,
236 .name = "hidg",
237 },
238};
239
240
241MODULE_DESCRIPTION(DRIVER_DESC);
242MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
243MODULE_LICENSE("GPL");
244
245static int __init hidg_init(void)
246{
Peter Korsgaardda01c7a2010-04-26 10:05:06 +0200247 int status;
248
249 status = platform_driver_probe(&hidg_plat_driver,
250 hidg_plat_driver_probe);
251 if (status < 0)
252 return status;
253
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200254 status = usb_composite_probe(&hidg_driver);
Peter Korsgaardda01c7a2010-04-26 10:05:06 +0200255 if (status < 0)
256 platform_driver_unregister(&hidg_plat_driver);
257
258 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200259}
260module_init(hidg_init);
261
262static void __exit hidg_cleanup(void)
263{
Fabien Chouteau71adf112010-04-08 09:31:15 +0200264 usb_composite_unregister(&hidg_driver);
Andrzej Pietrasiewicz00896f62014-11-06 11:11:58 +0100265 platform_driver_unregister(&hidg_plat_driver);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200266}
267module_exit(hidg_cleanup);