blob: 8ba0bee4e6c01767ed1b6cc09be6f06247c4e946 [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 */
David Brownell7e75bc02008-08-18 17:41:31 -070060#include "f_sourcesink.c"
61#include "f_loopback.c"
62
63/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020064USB_GADGET_COMPOSITE_OPTIONS();
David Brownell7e75bc02008-08-18 17:41:31 -070065
David Brownell097db1d2008-06-19 18:18:27 -070066#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David Brownell7472f382008-04-18 18:47:54 -070068static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070070unsigned buflen = 4096; /* only used for bulk endpoints */
David Brownell097db1d2008-06-19 18:18:27 -070071module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Normally the "loopback" configuration is second (index 1) so
75 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070076 * work better with hosts where config changes are problematic or
77 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103079static bool loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070080module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*-------------------------------------------------------------------------*/
83
84/* Thanks to NetChip Technologies for donating this product ID.
85 *
86 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
87 * Instead: allocate your own, using normal USB-IF procedures.
88 */
89#ifndef CONFIG_USB_ZERO_HNPTEST
90#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
91#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
David Brownellab943a22009-03-19 14:16:09 -070092#define DEFAULT_AUTORESUME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#else
94#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
95#define DRIVER_PRODUCT_NUM 0xbadd
David Brownellab943a22009-03-19 14:16:09 -070096#define DEFAULT_AUTORESUME 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif
98
David Brownellab943a22009-03-19 14:16:09 -070099/* If the optional "autoresume" mode is enabled, it provides good
100 * functional coverage for the "USBCV" test harness from USB-IF.
101 * It's always set if OTG mode is enabled.
102 */
103unsigned autoresume = DEFAULT_AUTORESUME;
104module_param(autoresume, uint, S_IRUGO);
105MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*-------------------------------------------------------------------------*/
108
David Brownell7472f382008-04-18 18:47:54 -0700109static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .bLength = sizeof device_desc,
111 .bDescriptorType = USB_DT_DEVICE,
112
Harvey Harrison551509d2009-02-11 14:11:36 -0800113 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
115
Harvey Harrison551509d2009-02-11 14:11:36 -0800116 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
117 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .bNumConfigurations = 2,
119};
120
David Brownell097db1d2008-06-19 18:18:27 -0700121#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700122static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .bLength = sizeof otg_descriptor,
124 .bDescriptorType = USB_DT_OTG,
125
David Brownell097db1d2008-06-19 18:18:27 -0700126 /* REVISIT SRP-only hardware is possible, although
127 * it would not be called "OTG" ...
128 */
129 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
David Brownell097db1d2008-06-19 18:18:27 -0700132const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 NULL,
135};
David Brownell097db1d2008-06-19 18:18:27 -0700136#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David Brownell097db1d2008-06-19 18:18:27 -0700138/* string IDs are assigned dynamically */
David Brownellca2bdf42007-08-02 12:20:05 -0700139/* default serial number takes at least two packets */
140static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100142#define USB_GZERO_SS_DESC (USB_GADGET_FIRST_AVAIL_IDX + 0)
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100143#define USB_GZERO_LB_DESC (USB_GADGET_FIRST_AVAIL_IDX + 1)
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100144
David Brownell097db1d2008-06-19 18:18:27 -0700145static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +0200146 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200147 [USB_GADGET_PRODUCT_IDX].s = longname,
148 [USB_GADGET_SERIAL_IDX].s = serial,
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100149 [USB_GZERO_SS_DESC].s = "source and sink data",
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100150 [USB_GZERO_LB_DESC].s = "loop input to output",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 { } /* end of list */
152};
153
David Brownell097db1d2008-06-19 18:18:27 -0700154static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700156 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
David Brownell097db1d2008-06-19 18:18:27 -0700159static struct usb_gadget_strings *dev_strings[] = {
160 &stringtab_dev,
161 NULL,
162};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*-------------------------------------------------------------------------*/
165
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700166struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct usb_request *req;
169
David Brownell7472f382008-04-18 18:47:54 -0700170 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (req) {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700172 if (len)
173 req->length = len;
174 else
175 req->length = buflen;
176 req->buf = kmalloc(req->length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700178 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 req = NULL;
180 }
181 }
182 return req;
183}
184
David Brownell097db1d2008-06-19 18:18:27 -0700185void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
David Brownell9d8bab52007-07-01 11:04:54 -0700187 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700188 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
David Brownell097db1d2008-06-19 18:18:27 -0700191static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
David Brownell097db1d2008-06-19 18:18:27 -0700193 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
David Brownell097db1d2008-06-19 18:18:27 -0700195 if (ep->driver_data) {
196 value = usb_ep_disable(ep);
197 if (value < 0)
198 DBG(cdev, "disable %s --> %d\n",
199 ep->name, value);
200 ep->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
203
David Brownell097db1d2008-06-19 18:18:27 -0700204void disable_endpoints(struct usb_composite_dev *cdev,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700205 struct usb_ep *in, struct usb_ep *out,
206 struct usb_ep *iso_in, struct usb_ep *iso_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Brownell097db1d2008-06-19 18:18:27 -0700208 disable_ep(cdev, in);
209 disable_ep(cdev, out);
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700210 if (iso_in)
211 disable_ep(cdev, iso_in);
212 if (iso_out)
213 disable_ep(cdev, iso_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*-------------------------------------------------------------------------*/
217
David Brownellab943a22009-03-19 14:16:09 -0700218static struct timer_list autoresume_timer;
219
220static void zero_autoresume(unsigned long _c)
221{
222 struct usb_composite_dev *cdev = (void *)_c;
223 struct usb_gadget *g = cdev->gadget;
224
225 /* unconfigured devices can't issue wakeups */
226 if (!cdev->config)
227 return;
228
229 /* Normally the host would be woken up for something
230 * more significant than just a timer firing; likely
231 * because of some direct user request.
232 */
233 if (g->speed != USB_SPEED_UNKNOWN) {
234 int status = usb_gadget_wakeup(g);
235 INFO(cdev, "%s --> %d\n", __func__, status);
236 }
237}
238
239static void zero_suspend(struct usb_composite_dev *cdev)
240{
241 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
242 return;
243
244 if (autoresume) {
245 mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
246 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
247 } else
248 DBG(cdev, "%s\n", __func__);
249}
250
251static void zero_resume(struct usb_composite_dev *cdev)
252{
253 DBG(cdev, "%s\n", __func__);
254 del_timer(&autoresume_timer);
255}
256
257/*-------------------------------------------------------------------------*/
258
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100259static struct usb_configuration loopback_driver = {
260 .label = "loopback",
261 .strings = loopback_strings,
262 .bConfigurationValue = 2,
263 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
264 /* .iConfiguration = DYNAMIC */
265};
266
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100267static struct usb_configuration sourcesink_driver = {
268 .label = "source/sink",
269 .strings = sourcesink_strings,
270 .setup = ss_config_setup,
271 .bConfigurationValue = 3,
272 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
273 /* .iConfiguration = DYNAMIC */
274};
275
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200276static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200278 int status;
David Brownell91e79c92005-07-13 15:18:30 -0700279
David Brownell097db1d2008-06-19 18:18:27 -0700280 /* Allocate string descriptor numbers ... note that string
281 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700282 */
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200283 status = usb_string_ids_tab(cdev, strings_dev);
284 if (status < 0)
285 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200287 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
288 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
289 device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
David Brownell097db1d2008-06-19 18:18:27 -0700290
David Brownellab943a22009-03-19 14:16:09 -0700291 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
292
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100293 sourcesink_driver.iConfiguration = strings_dev[USB_GZERO_SS_DESC].id;
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100294 loopback_driver.iConfiguration = strings_dev[USB_GZERO_LB_DESC].id;
295
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100296 /* support autoresume for remote wakeup testing */
297 sourcesink_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100298 loopback_driver.bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100299 sourcesink_driver.descriptors = NULL;
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100300 loopback_driver.descriptors = NULL;
301 if (autoresume) {
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100302 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100303 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
304 }
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100305
306 /* support OTG systems */
307 if (gadget_is_otg(cdev->gadget)) {
308 sourcesink_driver.descriptors = otg_desc;
309 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100310 loopback_driver.descriptors = otg_desc;
311 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100312 }
313
David Brownell097db1d2008-06-19 18:18:27 -0700314 /* Register primary, then secondary configuration. Note that
315 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
David Brownell097db1d2008-06-19 18:18:27 -0700317 if (loopdefault) {
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100318 usb_add_config(cdev, &loopback_driver, loopback_bind_config);
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100319 usb_add_config(cdev, &sourcesink_driver,
320 sourcesink_bind_config);
David Brownell097db1d2008-06-19 18:18:27 -0700321 } else {
Sebastian Andrzej Siewioreeae5402012-12-23 21:09:58 +0100322 usb_add_config(cdev, &sourcesink_driver,
323 sourcesink_bind_config);
Sebastian Andrzej Siewior78f46f02012-12-23 21:09:59 +0100324 usb_add_config(cdev, &loopback_driver, loopback_bind_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200327 usb_composite_overwrite_options(cdev, &coverwrite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Brownell097db1d2008-06-19 18:18:27 -0700329 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
David Brownellab943a22009-03-19 14:16:09 -0700334static int zero_unbind(struct usb_composite_dev *cdev)
335{
336 del_timer_sync(&autoresume_timer);
337 return 0;
338}
339
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200340static __refdata struct usb_composite_driver zero_driver = {
David Brownell097db1d2008-06-19 18:18:27 -0700341 .name = "zero",
342 .dev = &device_desc,
343 .strings = dev_strings,
Amit Blay57c97c02011-07-03 17:29:31 +0300344 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200345 .bind = zero_bind,
David Brownellab943a22009-03-19 14:16:09 -0700346 .unbind = zero_unbind,
347 .suspend = zero_suspend,
348 .resume = zero_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
David Brownellca2bdf42007-08-02 12:20:05 -0700351MODULE_AUTHOR("David Brownell");
352MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Brownell7472f382008-04-18 18:47:54 -0700354static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200356 return usb_composite_probe(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
David Brownell7472f382008-04-18 18:47:54 -0700358module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Brownell7472f382008-04-18 18:47:54 -0700360static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
David Brownell097db1d2008-06-19 18:18:27 -0700362 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
David Brownell7472f382008-04-18 18:47:54 -0700364module_exit(cleanup);