blob: 0514e7b5201a7483a04813dde1ccd63a886f5928 [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
James Sullinsaed76a12014-03-22 17:47:58 -050014#define DEBUG 1
15#define VERBOSE_DEBUG 1
Fabien Chouteau71adf112010-04-08 09:31:15 +020016
17#include <linux/kernel.h>
18#include <linux/platform_device.h>
19#include <linux/list.h>
20
James Sullinsaed76a12014-03-22 17:47:58 -050021#include <linux/usb/g_hid.h>
22
Fabien Chouteau71adf112010-04-08 09:31:15 +020023#define DRIVER_DESC "HID Gadget"
24#define DRIVER_VERSION "2010/03/16"
25
26/*-------------------------------------------------------------------------*/
27
28#define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
29#define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
30
31/*-------------------------------------------------------------------------*/
32
33/*
34 * kbuild is not very cooperative with respect to linking separately
35 * compiled library objects into one module. So for now we won't use
36 * separate compilation ... ensuring init/exit sections work to shrink
37 * the runtime footprint, and giving us at least some parts of what
38 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
39 */
40
41#include "composite.c"
42#include "usbstring.c"
43#include "config.c"
44#include "epautoconf.c"
45
46#include "f_hid.c"
47
48
49struct hidg_func_node {
50 struct list_head node;
51 struct hidg_func_descriptor *func;
52};
53
54static LIST_HEAD(hidg_func_list);
55
James Sullinsaed76a12014-03-22 17:47:58 -050056
57/* hid descriptor for a keyboard */
58static struct hidg_func_descriptor my_hid_data = {
59 .subclass = 0, /* No subclass */
60 .protocol = 1, /* Keyboard */
61 .report_length = 8,
62 .report_desc_length = 63,
63 .report_desc = {
64 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
65 0x09, 0x06, /* USAGE (Keyboard) */
66 0xa1, 0x01, /* COLLECTION (Application) */
67 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
68 0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
69 0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
70 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
71 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
72 0x75, 0x01, /* REPORT_SIZE (1) */
73 0x95, 0x08, /* REPORT_COUNT (8) */
74 0x81, 0x02, /* INPUT (Data,Var,Abs) */
75 0x95, 0x01, /* REPORT_COUNT (1) */
76 0x75, 0x08, /* REPORT_SIZE (8) */
77 0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
78 0x95, 0x05, /* REPORT_COUNT (5) */
79 0x75, 0x01, /* REPORT_SIZE (1) */
80 0x05, 0x08, /* USAGE_PAGE (LEDs) */
81 0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
82 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
83 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
84 0x95, 0x01, /* REPORT_COUNT (1) */
85 0x75, 0x03, /* REPORT_SIZE (3) */
86 0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
87 0x95, 0x06, /* REPORT_COUNT (6) */
88 0x75, 0x08, /* REPORT_SIZE (8) */
89 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
90 0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
91 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
92 0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
93 0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
94 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
95 0xc0 /* END_COLLECTION */
96 }
97};
98
99static struct platform_device my_hid = {
100 .name = "hidg",
101 .id = 0,
102 .num_resources = 0,
103 .resource = 0,
104 .dev.platform_data = &my_hid_data,
105};
106
Fabien Chouteau71adf112010-04-08 09:31:15 +0200107/*-------------------------------------------------------------------------*/
108
109static struct usb_device_descriptor device_desc = {
110 .bLength = sizeof device_desc,
111 .bDescriptorType = USB_DT_DEVICE,
112
113 .bcdUSB = cpu_to_le16(0x0200),
114
115 /* .bDeviceClass = USB_CLASS_COMM, */
116 /* .bDeviceSubClass = 0, */
117 /* .bDeviceProtocol = 0, */
Orjan Friberg33d28322012-03-07 17:16:14 +0100118 .bDeviceClass = USB_CLASS_PER_INTERFACE,
119 .bDeviceSubClass = 0,
120 .bDeviceProtocol = 0,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200121 /* .bMaxPacketSize0 = f(hardware) */
122
123 /* Vendor and product id can be overridden by module parameters. */
124 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
125 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
126 /* .bcdDevice = f(hardware) */
127 /* .iManufacturer = DYNAMIC */
128 /* .iProduct = DYNAMIC */
129 /* NO SERIAL NUMBER */
130 .bNumConfigurations = 1,
131};
132
133static struct usb_otg_descriptor otg_descriptor = {
134 .bLength = sizeof otg_descriptor,
135 .bDescriptorType = USB_DT_OTG,
136
137 /* REVISIT SRP-only hardware is possible, although
138 * it would not be called "OTG" ...
139 */
140 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
141};
142
143static const struct usb_descriptor_header *otg_desc[] = {
144 (struct usb_descriptor_header *) &otg_descriptor,
145 NULL,
146};
147
148
149/* string IDs are assigned dynamically */
150
151#define STRING_MANUFACTURER_IDX 0
152#define STRING_PRODUCT_IDX 1
153
154static char manufacturer[50];
155
156static struct usb_string strings_dev[] = {
157 [STRING_MANUFACTURER_IDX].s = manufacturer,
158 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
159 { } /* end of list */
160};
161
162static struct usb_gadget_strings stringtab_dev = {
163 .language = 0x0409, /* en-us */
164 .strings = strings_dev,
165};
166
167static struct usb_gadget_strings *dev_strings[] = {
168 &stringtab_dev,
169 NULL,
170};
171
172
173
174/****************************** Configurations ******************************/
175
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200176static int __init do_config(struct usb_configuration *c)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200177{
178 struct hidg_func_node *e;
179 int func = 0, status = 0;
180
181 if (gadget_is_otg(c->cdev->gadget)) {
182 c->descriptors = otg_desc;
183 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
184 }
185
186 list_for_each_entry(e, &hidg_func_list, node) {
187 status = hidg_bind_config(c, e->func, func++);
188 if (status)
189 break;
190 }
191
192 return status;
193}
194
195static struct usb_configuration config_driver = {
196 .label = "HID Gadget",
Fabien Chouteau71adf112010-04-08 09:31:15 +0200197 .bConfigurationValue = 1,
198 /* .iConfiguration = DYNAMIC */
199 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
200};
201
202/****************************** Gadget Bind ******************************/
203
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200204static int __init hid_bind(struct usb_composite_dev *cdev)
Fabien Chouteau71adf112010-04-08 09:31:15 +0200205{
206 struct usb_gadget *gadget = cdev->gadget;
207 struct list_head *tmp;
208 int status, gcnum, funcs = 0;
209
210 list_for_each(tmp, &hidg_func_list)
211 funcs++;
212
213 if (!funcs)
214 return -ENODEV;
215
216 /* set up HID */
217 status = ghid_setup(cdev->gadget, funcs);
218 if (status < 0)
219 return status;
220
221 gcnum = usb_gadget_controller_number(gadget);
222 if (gcnum >= 0)
223 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
224 else
225 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
226
227
228 /* Allocate string descriptor numbers ... note that string
229 * contents can be overridden by the composite_dev glue.
230 */
231
232 /* device descriptor strings: manufacturer, product */
233 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
234 init_utsname()->sysname, init_utsname()->release,
235 gadget->name);
236 status = usb_string_id(cdev);
237 if (status < 0)
238 return status;
239 strings_dev[STRING_MANUFACTURER_IDX].id = status;
240 device_desc.iManufacturer = status;
241
242 status = usb_string_id(cdev);
243 if (status < 0)
244 return status;
245 strings_dev[STRING_PRODUCT_IDX].id = status;
246 device_desc.iProduct = status;
247
248 /* register our configuration */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200249 status = usb_add_config(cdev, &config_driver, do_config);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200250 if (status < 0)
251 return status;
252
253 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
254
255 return 0;
256}
257
258static int __exit hid_unbind(struct usb_composite_dev *cdev)
259{
260 ghid_cleanup();
261 return 0;
262}
263
264static int __init hidg_plat_driver_probe(struct platform_device *pdev)
265{
266 struct hidg_func_descriptor *func = pdev->dev.platform_data;
267 struct hidg_func_node *entry;
268
269 if (!func) {
270 dev_err(&pdev->dev, "Platform data missing\n");
271 return -ENODEV;
272 }
273
274 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
275 if (!entry)
276 return -ENOMEM;
277
278 entry->func = func;
279 list_add_tail(&entry->node, &hidg_func_list);
280
281 return 0;
282}
283
284static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
285{
286 struct hidg_func_node *e, *n;
287
288 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
289 list_del(&e->node);
290 kfree(e);
291 }
292
293 return 0;
294}
295
296
297/****************************** Some noise ******************************/
298
299
300static struct usb_composite_driver hidg_driver = {
301 .name = "g_hid",
302 .dev = &device_desc,
303 .strings = dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300304 .max_speed = USB_SPEED_HIGH,
Fabien Chouteau71adf112010-04-08 09:31:15 +0200305 .unbind = __exit_p(hid_unbind),
306};
307
308static struct platform_driver hidg_plat_driver = {
309 .remove = __devexit_p(hidg_plat_driver_remove),
310 .driver = {
311 .owner = THIS_MODULE,
312 .name = "hidg",
313 },
314};
315
316
317MODULE_DESCRIPTION(DRIVER_DESC);
318MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
319MODULE_LICENSE("GPL");
320
321static int __init hidg_init(void)
322{
Peter Korsgaardda01c7a2010-04-26 10:05:06 +0200323 int status;
324
James Sullinsaed76a12014-03-22 17:47:58 -0500325 status = platform_device_register(&my_hid);
326 if (status < 0) {
327 printk("____ reg failed\n");
328 platform_device_unregister(&my_hid);
329 return status;
330 }
331
Peter Korsgaardda01c7a2010-04-26 10:05:06 +0200332 status = platform_driver_probe(&hidg_plat_driver,
333 hidg_plat_driver_probe);
334 if (status < 0)
335 return status;
336
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200337 status = usb_composite_probe(&hidg_driver, hid_bind);
Peter Korsgaardda01c7a2010-04-26 10:05:06 +0200338 if (status < 0)
339 platform_driver_unregister(&hidg_plat_driver);
340
341 return status;
Fabien Chouteau71adf112010-04-08 09:31:15 +0200342}
343module_init(hidg_init);
344
345static void __exit hidg_cleanup(void)
346{
347 platform_driver_unregister(&hidg_plat_driver);
James Sullinsaed76a12014-03-22 17:47:58 -0500348 platform_device_unregister(&my_hid);
Fabien Chouteau71adf112010-04-08 09:31:15 +0200349 usb_composite_unregister(&hidg_driver);
350}
351module_exit(hidg_cleanup);