1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include <linux/kernel.h>
53#include <linux/slab.h>
54#include <linux/utsname.h>
55#include <linux/device.h>
56
57#include "g_zero.h"
58#include "gadget_chips.h"
59
60
61
62
63
64
65
66
67
68
69
70#include "composite.c"
71#include "usbstring.c"
72#include "config.c"
73#include "epautoconf.c"
74
75#include "f_sourcesink.c"
76#include "f_loopback.c"
77
78
79
80#define DRIVER_VERSION "Cinco de Mayo 2008"
81
82static const char longname[] = "Gadget Zero";
83
84unsigned buflen = 4096;
85module_param(buflen, uint, 0);
86
87
88
89
90
91
92
93static int loopdefault = 0;
94module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
95
96
97
98
99
100
101
102
103#ifndef CONFIG_USB_ZERO_HNPTEST
104#define DRIVER_VENDOR_NUM 0x0525
105#define DRIVER_PRODUCT_NUM 0xa4a0
106#define DEFAULT_AUTORESUME 0
107#else
108#define DRIVER_VENDOR_NUM 0x1a0a
109#define DRIVER_PRODUCT_NUM 0xbadd
110#define DEFAULT_AUTORESUME 5
111#endif
112
113
114
115
116
117unsigned autoresume = DEFAULT_AUTORESUME;
118module_param(autoresume, uint, S_IRUGO);
119MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
120
121
122
123static struct usb_device_descriptor device_desc = {
124 .bLength = sizeof device_desc,
125 .bDescriptorType = USB_DT_DEVICE,
126
127 .bcdUSB = cpu_to_le16(0x0200),
128 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
129
130 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
131 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
132 .bNumConfigurations = 2,
133};
134
135#ifdef CONFIG_USB_OTG
136static struct usb_otg_descriptor otg_descriptor = {
137 .bLength = sizeof otg_descriptor,
138 .bDescriptorType = USB_DT_OTG,
139
140
141
142
143 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
144};
145
146const struct usb_descriptor_header *otg_desc[] = {
147 (struct usb_descriptor_header *) &otg_descriptor,
148 NULL,
149};
150#endif
151
152
153
154#define STRING_MANUFACTURER_IDX 0
155#define STRING_PRODUCT_IDX 1
156#define STRING_SERIAL_IDX 2
157
158static char manufacturer[50];
159
160
161static char serial[] = "0123456789.0123456789.0123456789";
162
163static struct usb_string strings_dev[] = {
164 [STRING_MANUFACTURER_IDX].s = manufacturer,
165 [STRING_PRODUCT_IDX].s = longname,
166 [STRING_SERIAL_IDX].s = serial,
167 { }
168};
169
170static struct usb_gadget_strings stringtab_dev = {
171 .language = 0x0409,
172 .strings = strings_dev,
173};
174
175static struct usb_gadget_strings *dev_strings[] = {
176 &stringtab_dev,
177 NULL,
178};
179
180
181
182struct usb_request *alloc_ep_req(struct usb_ep *ep)
183{
184 struct usb_request *req;
185
186 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
187 if (req) {
188 req->length = buflen;
189 req->buf = kmalloc(buflen, GFP_ATOMIC);
190 if (!req->buf) {
191 usb_ep_free_request(ep, req);
192 req = NULL;
193 }
194 }
195 return req;
196}
197
198void free_ep_req(struct usb_ep *ep, struct usb_request *req)
199{
200 kfree(req->buf);
201 usb_ep_free_request(ep, req);
202}
203
204static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
205{
206 int value;
207
208 if (ep->driver_data) {
209 value = usb_ep_disable(ep);
210 if (value < 0)
211 DBG(cdev, "disable %s --> %d\n",
212 ep->name, value);
213 ep->driver_data = NULL;
214 }
215}
216
217void disable_endpoints(struct usb_composite_dev *cdev,
218 struct usb_ep *in, struct usb_ep *out)
219{
220 disable_ep(cdev, in);
221 disable_ep(cdev, out);
222}
223
224
225
226static struct timer_list autoresume_timer;
227
228static void zero_autoresume(unsigned long _c)
229{
230 struct usb_composite_dev *cdev = (void *)_c;
231 struct usb_gadget *g = cdev->gadget;
232
233
234 if (!cdev->config)
235 return;
236
237
238
239
240
241 if (g->speed != USB_SPEED_UNKNOWN) {
242 int status = usb_gadget_wakeup(g);
243 INFO(cdev, "%s --> %d\n", __func__, status);
244 }
245}
246
247static void zero_suspend(struct usb_composite_dev *cdev)
248{
249 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
250 return;
251
252 if (autoresume) {
253 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
254 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
255 } else
256 DBG(cdev, "%s\n", __func__);
257}
258
259static void zero_resume(struct usb_composite_dev *cdev)
260{
261 DBG(cdev, "%s\n", __func__);
262 del_timer(&autoresume_timer);
263}
264
265
266
267static int __init zero_bind(struct usb_composite_dev *cdev)
268{
269 int gcnum;
270 struct usb_gadget *gadget = cdev->gadget;
271 int id;
272
273
274
275
276 id = usb_string_id(cdev);
277 if (id < 0)
278 return id;
279 strings_dev[STRING_MANUFACTURER_IDX].id = id;
280 device_desc.iManufacturer = id;
281
282 id = usb_string_id(cdev);
283 if (id < 0)
284 return id;
285 strings_dev[STRING_PRODUCT_IDX].id = id;
286 device_desc.iProduct = id;
287
288 id = usb_string_id(cdev);
289 if (id < 0)
290 return id;
291 strings_dev[STRING_SERIAL_IDX].id = id;
292 device_desc.iSerialNumber = id;
293
294 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
295
296
297
298
299 if (loopdefault) {
300 loopback_add(cdev, autoresume != 0);
301 sourcesink_add(cdev, autoresume != 0);
302 } else {
303 sourcesink_add(cdev, autoresume != 0);
304 loopback_add(cdev, autoresume != 0);
305 }
306
307 gcnum = usb_gadget_controller_number(gadget);
308 if (gcnum >= 0)
309 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
310 else {
311
312
313
314
315
316
317
318 pr_warning("%s: controller '%s' not recognized\n",
319 longname, gadget->name);
320 device_desc.bcdDevice = cpu_to_le16(0x9999);
321 }
322
323
324 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
325
326 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
327 init_utsname()->sysname, init_utsname()->release,
328 gadget->name);
329
330 return 0;
331}
332
333static int zero_unbind(struct usb_composite_dev *cdev)
334{
335 del_timer_sync(&autoresume_timer);
336 return 0;
337}
338
339static struct usb_composite_driver zero_driver = {
340 .name = "zero",
341 .dev = &device_desc,
342 .strings = dev_strings,
343 .unbind = zero_unbind,
344 .suspend = zero_suspend,
345 .resume = zero_resume,
346};
347
348MODULE_AUTHOR("David Brownell");
349MODULE_LICENSE("GPL");
350
351static int __init init(void)
352{
353 return usb_composite_probe(&zero_driver, zero_bind);
354}
355module_init(init);
356
357static void __exit cleanup(void)
358{
359 usb_composite_unregister(&zero_driver);
360}
361module_exit(cleanup);
362