usb: gadget: move usb_gadget_controller_number() into a .c file and libcomposite
[deliverable/linux.git] / drivers / usb / gadget / hid.c
CommitLineData
71adf118
FC
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.
71adf118
FC
12 */
13
14
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/list.h>
18
dc995fc2 19#include "gadget_chips.h"
71adf118
FC
20#define DRIVER_DESC "HID Gadget"
21#define DRIVER_VERSION "2010/03/16"
22
23/*-------------------------------------------------------------------------*/
24
25#define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
26#define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
27
28/*-------------------------------------------------------------------------*/
29
30/*
31 * kbuild is not very cooperative with respect to linking separately
32 * compiled library objects into one module. So for now we won't use
33 * separate compilation ... ensuring init/exit sections work to shrink
34 * the runtime footprint, and giving us at least some parts of what
35 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
36 */
37
38#include "composite.c"
71adf118
FC
39
40#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/*-------------------------------------------------------------------------*/
51
52static struct usb_device_descriptor device_desc = {
53 .bLength = sizeof device_desc,
54 .bDescriptorType = USB_DT_DEVICE,
55
56 .bcdUSB = cpu_to_le16(0x0200),
57
58 /* .bDeviceClass = USB_CLASS_COMM, */
59 /* .bDeviceSubClass = 0, */
60 /* .bDeviceProtocol = 0, */
33d2832a
OF
61 .bDeviceClass = USB_CLASS_PER_INTERFACE,
62 .bDeviceSubClass = 0,
63 .bDeviceProtocol = 0,
71adf118
FC
64 /* .bMaxPacketSize0 = f(hardware) */
65
66 /* Vendor and product id can be overridden by module parameters. */
67 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
68 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
69 /* .bcdDevice = f(hardware) */
70 /* .iManufacturer = DYNAMIC */
71 /* .iProduct = DYNAMIC */
72 /* NO SERIAL NUMBER */
73 .bNumConfigurations = 1,
74};
75
76static struct usb_otg_descriptor otg_descriptor = {
77 .bLength = sizeof otg_descriptor,
78 .bDescriptorType = USB_DT_OTG,
79
80 /* REVISIT SRP-only hardware is possible, although
81 * it would not be called "OTG" ...
82 */
83 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
84};
85
86static const struct usb_descriptor_header *otg_desc[] = {
87 (struct usb_descriptor_header *) &otg_descriptor,
88 NULL,
89};
90
91
92/* string IDs are assigned dynamically */
93
94#define STRING_MANUFACTURER_IDX 0
95#define STRING_PRODUCT_IDX 1
96
97static char manufacturer[50];
98
99static struct usb_string strings_dev[] = {
100 [STRING_MANUFACTURER_IDX].s = manufacturer,
101 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
102 { } /* end of list */
103};
104
105static struct usb_gadget_strings stringtab_dev = {
106 .language = 0x0409, /* en-us */
107 .strings = strings_dev,
108};
109
110static struct usb_gadget_strings *dev_strings[] = {
111 &stringtab_dev,
112 NULL,
113};
114
115
116
117/****************************** Configurations ******************************/
118
e12995ec 119static int __init do_config(struct usb_configuration *c)
71adf118
FC
120{
121 struct hidg_func_node *e;
122 int func = 0, status = 0;
123
124 if (gadget_is_otg(c->cdev->gadget)) {
125 c->descriptors = otg_desc;
126 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
127 }
128
129 list_for_each_entry(e, &hidg_func_list, node) {
130 status = hidg_bind_config(c, e->func, func++);
131 if (status)
132 break;
133 }
134
135 return status;
136}
137
138static struct usb_configuration config_driver = {
139 .label = "HID Gadget",
71adf118
FC
140 .bConfigurationValue = 1,
141 /* .iConfiguration = DYNAMIC */
142 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
143};
144
145/****************************** Gadget Bind ******************************/
146
e12995ec 147static int __init hid_bind(struct usb_composite_dev *cdev)
71adf118
FC
148{
149 struct usb_gadget *gadget = cdev->gadget;
150 struct list_head *tmp;
151 int status, gcnum, funcs = 0;
152
153 list_for_each(tmp, &hidg_func_list)
154 funcs++;
155
156 if (!funcs)
157 return -ENODEV;
158
159 /* set up HID */
160 status = ghid_setup(cdev->gadget, funcs);
161 if (status < 0)
162 return status;
163
164 gcnum = usb_gadget_controller_number(gadget);
165 if (gcnum >= 0)
166 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
167 else
168 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
169
170
171 /* Allocate string descriptor numbers ... note that string
172 * contents can be overridden by the composite_dev glue.
173 */
174
175 /* device descriptor strings: manufacturer, product */
176 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
177 init_utsname()->sysname, init_utsname()->release,
178 gadget->name);
71adf118 179
e1f15ccb 180 status = usb_string_ids_tab(cdev, strings_dev);
71adf118
FC
181 if (status < 0)
182 return status;
e1f15ccb
SAS
183 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
184 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
71adf118
FC
185
186 /* register our configuration */
c9bfff9c 187 status = usb_add_config(cdev, &config_driver, do_config);
71adf118
FC
188 if (status < 0)
189 return status;
190
191 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
192
193 return 0;
194}
195
196static int __exit hid_unbind(struct usb_composite_dev *cdev)
197{
198 ghid_cleanup();
199 return 0;
200}
201
202static int __init hidg_plat_driver_probe(struct platform_device *pdev)
203{
204 struct hidg_func_descriptor *func = pdev->dev.platform_data;
205 struct hidg_func_node *entry;
206
207 if (!func) {
208 dev_err(&pdev->dev, "Platform data missing\n");
209 return -ENODEV;
210 }
211
212 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
213 if (!entry)
214 return -ENOMEM;
215
216 entry->func = func;
217 list_add_tail(&entry->node, &hidg_func_list);
218
219 return 0;
220}
221
222static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
223{
224 struct hidg_func_node *e, *n;
225
226 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
227 list_del(&e->node);
228 kfree(e);
229 }
230
231 return 0;
232}
233
234
235/****************************** Some noise ******************************/
236
237
c2ec75c2 238static __refdata struct usb_composite_driver hidg_driver = {
71adf118
FC
239 .name = "g_hid",
240 .dev = &device_desc,
241 .strings = dev_strings,
35a0e0bf 242 .max_speed = USB_SPEED_HIGH,
03e42bd5 243 .bind = hid_bind,
71adf118
FC
244 .unbind = __exit_p(hid_unbind),
245};
246
247static struct platform_driver hidg_plat_driver = {
248 .remove = __devexit_p(hidg_plat_driver_remove),
249 .driver = {
250 .owner = THIS_MODULE,
251 .name = "hidg",
252 },
253};
254
255
256MODULE_DESCRIPTION(DRIVER_DESC);
257MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
258MODULE_LICENSE("GPL");
259
260static int __init hidg_init(void)
261{
da01c7a4
PK
262 int status;
263
264 status = platform_driver_probe(&hidg_plat_driver,
265 hidg_plat_driver_probe);
266 if (status < 0)
267 return status;
268
03e42bd5 269 status = usb_composite_probe(&hidg_driver);
da01c7a4
PK
270 if (status < 0)
271 platform_driver_unregister(&hidg_plat_driver);
272
273 return status;
71adf118
FC
274}
275module_init(hidg_init);
276
277static void __exit hidg_cleanup(void)
278{
279 platform_driver_unregister(&hidg_plat_driver);
280 usb_composite_unregister(&hidg_driver);
281}
282module_exit(hidg_cleanup);
This page took 0.37101 seconds and 5 git commands to generate.