blob: 12ad516ada77110a236600647feeaf3bbdbf954d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * zero.c -- Gadget Zero, for USB development
3 *
David Brownell097db1d2008-06-19 18:18:27 -07004 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
David Brownellca2bdf42007-08-02 12:20:05 -07007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13
14/*
15 * Gadget Zero only needs two bulk endpoints, and is an example of how you
16 * can write a hardware-agnostic gadget driver running inside a USB device.
David Brownell7472f382008-04-18 18:47:54 -070017 * Some hardware details are visible, but don't affect most of the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * Use it with the Linux host/master side "usbtest" driver to get a basic
20 * functional test of your device-side usb stack, or with "usb-skeleton".
21 *
22 * It supports two similar configurations. One sinks whatever the usb host
23 * writes, and in return sources zeroes. The other loops whatever the host
David Brownell097db1d2008-06-19 18:18:27 -070024 * writes back, so the host can read it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
26 * Many drivers will only have one configuration, letting them be much
27 * simpler if they also don't support high speed operation (like this
28 * driver does).
David Brownellca2bdf42007-08-02 12:20:05 -070029 *
30 * Why is *this* driver using two configurations, rather than setting up
31 * two interfaces with different functions? To help verify that multiple
32 * configuration infrastucture is working correctly; also, so that it can
33 * work with low capability USB controllers without four bulk endpoints.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
David Brownell097db1d2008-06-19 18:18:27 -070036/*
37 * driver assumes self-powered hardware, and
38 * has no way for users to trigger remote wakeup.
39 */
40
David Brownellca2bdf42007-08-02 12:20:05 -070041/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/utsname.h>
46#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Brownell097db1d2008-06-19 18:18:27 -070048#include "g_zero.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "gadget_chips.h"
50
51
52/*-------------------------------------------------------------------------*/
53
David Brownell7e75bc02008-08-18 17:41:31 -070054/*
55 * Kbuild is not very cooperative with respect to linking separately
56 * compiled library objects into one module. So for now we won't use
57 * separate compilation ... ensuring init/exit sections work to shrink
58 * the runtime footprint, and giving us at least some parts of what
59 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
60 */
61#include "composite.c"
62#include "usbstring.c"
63#include "config.c"
64#include "epautoconf.c"
65
66#include "f_sourcesink.c"
67#include "f_loopback.c"
68
69/*-------------------------------------------------------------------------*/
70
David Brownell097db1d2008-06-19 18:18:27 -070071#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Brownell7472f382008-04-18 18:47:54 -070073static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070075unsigned buflen = 4096; /* only used for bulk endpoints */
David Brownell097db1d2008-06-19 18:18:27 -070076module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Normally the "loopback" configuration is second (index 1) so
80 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070081 * work better with hosts where config changes are problematic or
82 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103084static bool loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070085module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*-------------------------------------------------------------------------*/
88
89/* Thanks to NetChip Technologies for donating this product ID.
90 *
91 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
92 * Instead: allocate your own, using normal USB-IF procedures.
93 */
94#ifndef CONFIG_USB_ZERO_HNPTEST
95#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
96#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
David Brownellab943a22009-03-19 14:16:09 -070097#define DEFAULT_AUTORESUME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#else
99#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
100#define DRIVER_PRODUCT_NUM 0xbadd
David Brownellab943a22009-03-19 14:16:09 -0700101#define DEFAULT_AUTORESUME 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
103
David Brownellab943a22009-03-19 14:16:09 -0700104/* If the optional "autoresume" mode is enabled, it provides good
105 * functional coverage for the "USBCV" test harness from USB-IF.
106 * It's always set if OTG mode is enabled.
107 */
108unsigned autoresume = DEFAULT_AUTORESUME;
109module_param(autoresume, uint, S_IRUGO);
110MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*-------------------------------------------------------------------------*/
113
David Brownell7472f382008-04-18 18:47:54 -0700114static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .bLength = sizeof device_desc,
116 .bDescriptorType = USB_DT_DEVICE,
117
Harvey Harrison551509d2009-02-11 14:11:36 -0800118 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
120
Harvey Harrison551509d2009-02-11 14:11:36 -0800121 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
122 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .bNumConfigurations = 2,
124};
125
David Brownell097db1d2008-06-19 18:18:27 -0700126#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700127static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .bLength = sizeof otg_descriptor,
129 .bDescriptorType = USB_DT_OTG,
130
David Brownell097db1d2008-06-19 18:18:27 -0700131 /* REVISIT SRP-only hardware is possible, although
132 * it would not be called "OTG" ...
133 */
134 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
David Brownell097db1d2008-06-19 18:18:27 -0700137const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 NULL,
140};
David Brownell097db1d2008-06-19 18:18:27 -0700141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David Brownell097db1d2008-06-19 18:18:27 -0700143/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Brownell097db1d2008-06-19 18:18:27 -0700145#define STRING_MANUFACTURER_IDX 0
146#define STRING_PRODUCT_IDX 1
147#define STRING_SERIAL_IDX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Brownellca2bdf42007-08-02 12:20:05 -0700149static char manufacturer[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
David Brownellca2bdf42007-08-02 12:20:05 -0700151/* default serial number takes at least two packets */
152static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
David Brownell097db1d2008-06-19 18:18:27 -0700154static struct usb_string strings_dev[] = {
155 [STRING_MANUFACTURER_IDX].s = manufacturer,
156 [STRING_PRODUCT_IDX].s = longname,
157 [STRING_SERIAL_IDX].s = serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 { } /* end of list */
159};
160
David Brownell097db1d2008-06-19 18:18:27 -0700161static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700163 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
David Brownell097db1d2008-06-19 18:18:27 -0700166static struct usb_gadget_strings *dev_strings[] = {
167 &stringtab_dev,
168 NULL,
169};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*-------------------------------------------------------------------------*/
172
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700173struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct usb_request *req;
176
David Brownell7472f382008-04-18 18:47:54 -0700177 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (req) {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700179 if (len)
180 req->length = len;
181 else
182 req->length = buflen;
183 req->buf = kmalloc(req->length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700185 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 req = NULL;
187 }
188 }
189 return req;
190}
191
David Brownell097db1d2008-06-19 18:18:27 -0700192void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
David Brownell9d8bab52007-07-01 11:04:54 -0700194 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700195 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
David Brownell097db1d2008-06-19 18:18:27 -0700198static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Brownell097db1d2008-06-19 18:18:27 -0700200 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
David Brownell097db1d2008-06-19 18:18:27 -0700202 if (ep->driver_data) {
203 value = usb_ep_disable(ep);
204 if (value < 0)
205 DBG(cdev, "disable %s --> %d\n",
206 ep->name, value);
207 ep->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209}
210
David Brownell097db1d2008-06-19 18:18:27 -0700211void disable_endpoints(struct usb_composite_dev *cdev,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700212 struct usb_ep *in, struct usb_ep *out,
213 struct usb_ep *iso_in, struct usb_ep *iso_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Brownell097db1d2008-06-19 18:18:27 -0700215 disable_ep(cdev, in);
216 disable_ep(cdev, out);
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700217 if (iso_in)
218 disable_ep(cdev, iso_in);
219 if (iso_out)
220 disable_ep(cdev, iso_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*-------------------------------------------------------------------------*/
224
David Brownellab943a22009-03-19 14:16:09 -0700225static struct timer_list autoresume_timer;
226
227static void zero_autoresume(unsigned long _c)
228{
229 struct usb_composite_dev *cdev = (void *)_c;
230 struct usb_gadget *g = cdev->gadget;
231
232 /* unconfigured devices can't issue wakeups */
233 if (!cdev->config)
234 return;
235
236 /* Normally the host would be woken up for something
237 * more significant than just a timer firing; likely
238 * because of some direct user request.
239 */
240 if (g->speed != USB_SPEED_UNKNOWN) {
241 int status = usb_gadget_wakeup(g);
242 INFO(cdev, "%s --> %d\n", __func__, status);
243 }
244}
245
246static void zero_suspend(struct usb_composite_dev *cdev)
247{
248 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
249 return;
250
251 if (autoresume) {
252 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
253 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
254 } else
255 DBG(cdev, "%s\n", __func__);
256}
257
258static void zero_resume(struct usb_composite_dev *cdev)
259{
260 DBG(cdev, "%s\n", __func__);
261 del_timer(&autoresume_timer);
262}
263
264/*-------------------------------------------------------------------------*/
265
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200266static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
David Brownell91e79c92005-07-13 15:18:30 -0700268 int gcnum;
David Brownell097db1d2008-06-19 18:18:27 -0700269 struct usb_gadget *gadget = cdev->gadget;
270 int id;
David Brownell91e79c92005-07-13 15:18:30 -0700271
David Brownell097db1d2008-06-19 18:18:27 -0700272 /* Allocate string descriptor numbers ... note that string
273 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700274 */
David Brownell097db1d2008-06-19 18:18:27 -0700275 id = usb_string_id(cdev);
276 if (id < 0)
277 return id;
278 strings_dev[STRING_MANUFACTURER_IDX].id = id;
279 device_desc.iManufacturer = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Brownell097db1d2008-06-19 18:18:27 -0700281 id = usb_string_id(cdev);
282 if (id < 0)
283 return id;
284 strings_dev[STRING_PRODUCT_IDX].id = id;
285 device_desc.iProduct = id;
286
287 id = usb_string_id(cdev);
288 if (id < 0)
289 return id;
290 strings_dev[STRING_SERIAL_IDX].id = id;
291 device_desc.iSerialNumber = id;
292
David Brownellab943a22009-03-19 14:16:09 -0700293 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
294
David Brownell097db1d2008-06-19 18:18:27 -0700295 /* Register primary, then secondary configuration. Note that
296 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
David Brownell097db1d2008-06-19 18:18:27 -0700298 if (loopdefault) {
David Brownellab943a22009-03-19 14:16:09 -0700299 loopback_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100300 sourcesink_add(cdev, autoresume != 0);
David Brownell097db1d2008-06-19 18:18:27 -0700301 } else {
David Brownellab943a22009-03-19 14:16:09 -0700302 sourcesink_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100303 loopback_add(cdev, autoresume != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Brownell7472f382008-04-18 18:47:54 -0700306 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -0700307 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700308 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -0700309 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* gadget zero is so simple (for now, no altsettings) that
311 * it SHOULD NOT have problems with bulk-capable hardware.
David Brownell097db1d2008-06-19 18:18:27 -0700312 * so just warn about unrcognized controllers -- don't panic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * things like configuration and altsetting numbering
315 * can need hardware-specific attention though.
316 */
David Brownell00274922007-11-19 12:58:36 -0800317 pr_warning("%s: controller '%s' not recognized\n",
David Brownell097db1d2008-06-19 18:18:27 -0700318 longname, gadget->name);
Harvey Harrison551509d2009-02-11 14:11:36 -0800319 device_desc.bcdDevice = cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
David Brownell097db1d2008-06-19 18:18:27 -0700322 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David Brownell7472f382008-04-18 18:47:54 -0700324 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700325 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 gadget->name);
327
328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
David Brownellab943a22009-03-19 14:16:09 -0700331static int zero_unbind(struct usb_composite_dev *cdev)
332{
333 del_timer_sync(&autoresume_timer);
334 return 0;
335}
336
David Brownell097db1d2008-06-19 18:18:27 -0700337static struct usb_composite_driver zero_driver = {
338 .name = "zero",
339 .dev = &device_desc,
340 .strings = dev_strings,
Amit Blay57c97c02011-07-03 17:29:31 +0300341 .max_speed = USB_SPEED_SUPER,
David Brownellab943a22009-03-19 14:16:09 -0700342 .unbind = zero_unbind,
343 .suspend = zero_suspend,
344 .resume = zero_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
David Brownellca2bdf42007-08-02 12:20:05 -0700347MODULE_AUTHOR("David Brownell");
348MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Brownell7472f382008-04-18 18:47:54 -0700350static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200352 return usb_composite_probe(&zero_driver, zero_bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
David Brownell7472f382008-04-18 18:47:54 -0700354module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Brownell7472f382008-04-18 18:47:54 -0700356static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David Brownell097db1d2008-06-19 18:18:27 -0700358 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
David Brownell7472f382008-04-18 18:47:54 -0700360module_exit(cleanup);