blob: a837f3af2047b53bfee128d0ca074d544946133c [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/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David Brownell097db1d2008-06-19 18:18:27 -070047#include "g_zero.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "gadget_chips.h"
49
50
51/*-------------------------------------------------------------------------*/
52
David Brownell7e75bc02008-08-18 17:41:31 -070053/*
54 * Kbuild is not very cooperative with respect to linking separately
55 * compiled library objects into one module. So for now we won't use
56 * separate compilation ... ensuring init/exit sections work to shrink
57 * the runtime footprint, and giving us at least some parts of what
58 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
59 */
60#include "composite.c"
David Brownell7e75bc02008-08-18 17:41:31 -070061
62#include "f_sourcesink.c"
63#include "f_loopback.c"
64
65/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020066USB_GADGET_COMPOSITE_OPTIONS();
David Brownell7e75bc02008-08-18 17:41:31 -070067
David Brownell097db1d2008-06-19 18:18:27 -070068#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Brownell7472f382008-04-18 18:47:54 -070070static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070072unsigned buflen = 4096; /* only used for bulk endpoints */
David Brownell097db1d2008-06-19 18:18:27 -070073module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Normally the "loopback" configuration is second (index 1) so
77 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070078 * work better with hosts where config changes are problematic or
79 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103081static bool loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070082module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*-------------------------------------------------------------------------*/
85
86/* Thanks to NetChip Technologies for donating this product ID.
87 *
88 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
89 * Instead: allocate your own, using normal USB-IF procedures.
90 */
91#ifndef CONFIG_USB_ZERO_HNPTEST
92#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
93#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
David Brownellab943a22009-03-19 14:16:09 -070094#define DEFAULT_AUTORESUME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#else
96#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
97#define DRIVER_PRODUCT_NUM 0xbadd
David Brownellab943a22009-03-19 14:16:09 -070098#define DEFAULT_AUTORESUME 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif
100
David Brownellab943a22009-03-19 14:16:09 -0700101/* If the optional "autoresume" mode is enabled, it provides good
102 * functional coverage for the "USBCV" test harness from USB-IF.
103 * It's always set if OTG mode is enabled.
104 */
105unsigned autoresume = DEFAULT_AUTORESUME;
106module_param(autoresume, uint, S_IRUGO);
107MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*-------------------------------------------------------------------------*/
110
David Brownell7472f382008-04-18 18:47:54 -0700111static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .bLength = sizeof device_desc,
113 .bDescriptorType = USB_DT_DEVICE,
114
Harvey Harrison551509d2009-02-11 14:11:36 -0800115 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
117
Harvey Harrison551509d2009-02-11 14:11:36 -0800118 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
119 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .bNumConfigurations = 2,
121};
122
David Brownell097db1d2008-06-19 18:18:27 -0700123#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700124static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .bLength = sizeof otg_descriptor,
126 .bDescriptorType = USB_DT_OTG,
127
David Brownell097db1d2008-06-19 18:18:27 -0700128 /* REVISIT SRP-only hardware is possible, although
129 * it would not be called "OTG" ...
130 */
131 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
David Brownell097db1d2008-06-19 18:18:27 -0700134const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 NULL,
137};
David Brownell097db1d2008-06-19 18:18:27 -0700138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
David Brownell097db1d2008-06-19 18:18:27 -0700140/* string IDs are assigned dynamically */
David Brownellca2bdf42007-08-02 12:20:05 -0700141/* default serial number takes at least two packets */
142static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
David Brownell097db1d2008-06-19 18:18:27 -0700144static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +0200145 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200146 [USB_GADGET_PRODUCT_IDX].s = longname,
147 [USB_GADGET_SERIAL_IDX].s = serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 { } /* end of list */
149};
150
David Brownell097db1d2008-06-19 18:18:27 -0700151static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700153 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
David Brownell097db1d2008-06-19 18:18:27 -0700156static struct usb_gadget_strings *dev_strings[] = {
157 &stringtab_dev,
158 NULL,
159};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*-------------------------------------------------------------------------*/
162
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700163struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct usb_request *req;
166
David Brownell7472f382008-04-18 18:47:54 -0700167 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (req) {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700169 if (len)
170 req->length = len;
171 else
172 req->length = buflen;
173 req->buf = kmalloc(req->length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700175 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 req = NULL;
177 }
178 }
179 return req;
180}
181
David Brownell097db1d2008-06-19 18:18:27 -0700182void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
David Brownell9d8bab52007-07-01 11:04:54 -0700184 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700185 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
David Brownell097db1d2008-06-19 18:18:27 -0700188static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
David Brownell097db1d2008-06-19 18:18:27 -0700190 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Brownell097db1d2008-06-19 18:18:27 -0700192 if (ep->driver_data) {
193 value = usb_ep_disable(ep);
194 if (value < 0)
195 DBG(cdev, "disable %s --> %d\n",
196 ep->name, value);
197 ep->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
David Brownell097db1d2008-06-19 18:18:27 -0700201void disable_endpoints(struct usb_composite_dev *cdev,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700202 struct usb_ep *in, struct usb_ep *out,
203 struct usb_ep *iso_in, struct usb_ep *iso_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David Brownell097db1d2008-06-19 18:18:27 -0700205 disable_ep(cdev, in);
206 disable_ep(cdev, out);
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700207 if (iso_in)
208 disable_ep(cdev, iso_in);
209 if (iso_out)
210 disable_ep(cdev, iso_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/*-------------------------------------------------------------------------*/
214
David Brownellab943a22009-03-19 14:16:09 -0700215static struct timer_list autoresume_timer;
216
217static void zero_autoresume(unsigned long _c)
218{
219 struct usb_composite_dev *cdev = (void *)_c;
220 struct usb_gadget *g = cdev->gadget;
221
222 /* unconfigured devices can't issue wakeups */
223 if (!cdev->config)
224 return;
225
226 /* Normally the host would be woken up for something
227 * more significant than just a timer firing; likely
228 * because of some direct user request.
229 */
230 if (g->speed != USB_SPEED_UNKNOWN) {
231 int status = usb_gadget_wakeup(g);
232 INFO(cdev, "%s --> %d\n", __func__, status);
233 }
234}
235
236static void zero_suspend(struct usb_composite_dev *cdev)
237{
238 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
239 return;
240
241 if (autoresume) {
242 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
243 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
244 } else
245 DBG(cdev, "%s\n", __func__);
246}
247
248static void zero_resume(struct usb_composite_dev *cdev)
249{
250 DBG(cdev, "%s\n", __func__);
251 del_timer(&autoresume_timer);
252}
253
254/*-------------------------------------------------------------------------*/
255
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200256static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
David Brownell91e79c92005-07-13 15:18:30 -0700258 int gcnum;
David Brownell097db1d2008-06-19 18:18:27 -0700259 struct usb_gadget *gadget = cdev->gadget;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200260 int status;
David Brownell91e79c92005-07-13 15:18:30 -0700261
David Brownell097db1d2008-06-19 18:18:27 -0700262 /* Allocate string descriptor numbers ... note that string
263 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700264 */
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200265 status = usb_string_ids_tab(cdev, strings_dev);
266 if (status < 0)
267 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200269 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
270 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
271 device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
David Brownell097db1d2008-06-19 18:18:27 -0700272
David Brownellab943a22009-03-19 14:16:09 -0700273 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
274
David Brownell097db1d2008-06-19 18:18:27 -0700275 /* Register primary, then secondary configuration. Note that
276 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
David Brownell097db1d2008-06-19 18:18:27 -0700278 if (loopdefault) {
David Brownellab943a22009-03-19 14:16:09 -0700279 loopback_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100280 sourcesink_add(cdev, autoresume != 0);
David Brownell097db1d2008-06-19 18:18:27 -0700281 } else {
David Brownellab943a22009-03-19 14:16:09 -0700282 sourcesink_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100283 loopback_add(cdev, autoresume != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
David Brownell7472f382008-04-18 18:47:54 -0700286 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -0700287 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700288 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -0700289 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* gadget zero is so simple (for now, no altsettings) that
291 * it SHOULD NOT have problems with bulk-capable hardware.
David Brownell097db1d2008-06-19 18:18:27 -0700292 * so just warn about unrcognized controllers -- don't panic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
294 * things like configuration and altsetting numbering
295 * can need hardware-specific attention though.
296 */
David Brownell00274922007-11-19 12:58:36 -0800297 pr_warning("%s: controller '%s' not recognized\n",
David Brownell097db1d2008-06-19 18:18:27 -0700298 longname, gadget->name);
Harvey Harrison551509d2009-02-11 14:11:36 -0800299 device_desc.bcdDevice = cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200301 usb_composite_overwrite_options(cdev, &coverwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David Brownell097db1d2008-06-19 18:18:27 -0700303 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
David Brownellab943a22009-03-19 14:16:09 -0700308static int zero_unbind(struct usb_composite_dev *cdev)
309{
310 del_timer_sync(&autoresume_timer);
311 return 0;
312}
313
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200314static __refdata struct usb_composite_driver zero_driver = {
David Brownell097db1d2008-06-19 18:18:27 -0700315 .name = "zero",
316 .dev = &device_desc,
317 .strings = dev_strings,
Amit Blay57c97c02011-07-03 17:29:31 +0300318 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200319 .bind = zero_bind,
David Brownellab943a22009-03-19 14:16:09 -0700320 .unbind = zero_unbind,
321 .suspend = zero_suspend,
322 .resume = zero_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
David Brownellca2bdf42007-08-02 12:20:05 -0700325MODULE_AUTHOR("David Brownell");
326MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David Brownell7472f382008-04-18 18:47:54 -0700328static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200330 return usb_composite_probe(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
David Brownell7472f382008-04-18 18:47:54 -0700332module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
David Brownell7472f382008-04-18 18:47:54 -0700334static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
David Brownell097db1d2008-06-19 18:18:27 -0700336 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
David Brownell7472f382008-04-18 18:47:54 -0700338module_exit(cleanup);