blob: 807280d069f96e815ea11c1eddddb29cbe6692fd [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 *
David Brownellca2bdf42007-08-02 12:20:05 -070012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
David Brownellca2bdf42007-08-02 12:20:05 -070017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22
23/*
24 * Gadget Zero only needs two bulk endpoints, and is an example of how you
25 * can write a hardware-agnostic gadget driver running inside a USB device.
David Brownell7472f382008-04-18 18:47:54 -070026 * Some hardware details are visible, but don't affect most of the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Use it with the Linux host/master side "usbtest" driver to get a basic
29 * functional test of your device-side usb stack, or with "usb-skeleton".
30 *
31 * It supports two similar configurations. One sinks whatever the usb host
32 * writes, and in return sources zeroes. The other loops whatever the host
David Brownell097db1d2008-06-19 18:18:27 -070033 * writes back, so the host can read it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * Many drivers will only have one configuration, letting them be much
36 * simpler if they also don't support high speed operation (like this
37 * driver does).
David Brownellca2bdf42007-08-02 12:20:05 -070038 *
39 * Why is *this* driver using two configurations, rather than setting up
40 * two interfaces with different functions? To help verify that multiple
41 * configuration infrastucture is working correctly; also, so that it can
42 * work with low capability USB controllers without four bulk endpoints.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
David Brownell097db1d2008-06-19 18:18:27 -070045/*
46 * driver assumes self-powered hardware, and
47 * has no way for users to trigger remote wakeup.
48 */
49
David Brownellca2bdf42007-08-02 12:20:05 -070050/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/utsname.h>
55#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Brownell097db1d2008-06-19 18:18:27 -070057#include "g_zero.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "gadget_chips.h"
59
60
61/*-------------------------------------------------------------------------*/
62
David Brownell7e75bc02008-08-18 17:41:31 -070063/*
64 * Kbuild is not very cooperative with respect to linking separately
65 * compiled library objects into one module. So for now we won't use
66 * separate compilation ... ensuring init/exit sections work to shrink
67 * the runtime footprint, and giving us at least some parts of what
68 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
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
David Brownell097db1d2008-06-19 18:18:27 -070080#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Brownell7472f382008-04-18 18:47:54 -070082static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David Brownell097db1d2008-06-19 18:18:27 -070084unsigned buflen = 4096;
85module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Normally the "loopback" configuration is second (index 1) so
89 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070090 * work better with hosts where config changes are problematic or
91 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93static int loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070094module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*-------------------------------------------------------------------------*/
97
98/* Thanks to NetChip Technologies for donating this product ID.
99 *
100 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
101 * Instead: allocate your own, using normal USB-IF procedures.
102 */
103#ifndef CONFIG_USB_ZERO_HNPTEST
104#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
105#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
David Brownellab943a22009-03-19 14:16:09 -0700106#define DEFAULT_AUTORESUME 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#else
108#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
109#define DRIVER_PRODUCT_NUM 0xbadd
David Brownellab943a22009-03-19 14:16:09 -0700110#define DEFAULT_AUTORESUME 5
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112
David Brownellab943a22009-03-19 14:16:09 -0700113/* If the optional "autoresume" mode is enabled, it provides good
114 * functional coverage for the "USBCV" test harness from USB-IF.
115 * It's always set if OTG mode is enabled.
116 */
117unsigned autoresume = DEFAULT_AUTORESUME;
118module_param(autoresume, uint, S_IRUGO);
119MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*-------------------------------------------------------------------------*/
122
David Brownell7472f382008-04-18 18:47:54 -0700123static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .bLength = sizeof device_desc,
125 .bDescriptorType = USB_DT_DEVICE,
126
Harvey Harrison551509d2009-02-11 14:11:36 -0800127 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
129
Harvey Harrison551509d2009-02-11 14:11:36 -0800130 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
131 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .bNumConfigurations = 2,
133};
134
David Brownell097db1d2008-06-19 18:18:27 -0700135#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700136static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .bLength = sizeof otg_descriptor,
138 .bDescriptorType = USB_DT_OTG,
139
David Brownell097db1d2008-06-19 18:18:27 -0700140 /* REVISIT SRP-only hardware is possible, although
141 * it would not be called "OTG" ...
142 */
143 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
David Brownell097db1d2008-06-19 18:18:27 -0700146const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 NULL,
149};
David Brownell097db1d2008-06-19 18:18:27 -0700150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David Brownell097db1d2008-06-19 18:18:27 -0700152/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
David Brownell097db1d2008-06-19 18:18:27 -0700154#define STRING_MANUFACTURER_IDX 0
155#define STRING_PRODUCT_IDX 1
156#define STRING_SERIAL_IDX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David Brownellca2bdf42007-08-02 12:20:05 -0700158static char manufacturer[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Brownellca2bdf42007-08-02 12:20:05 -0700160/* default serial number takes at least two packets */
161static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David Brownell097db1d2008-06-19 18:18:27 -0700163static struct usb_string strings_dev[] = {
164 [STRING_MANUFACTURER_IDX].s = manufacturer,
165 [STRING_PRODUCT_IDX].s = longname,
166 [STRING_SERIAL_IDX].s = serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 { } /* end of list */
168};
169
David Brownell097db1d2008-06-19 18:18:27 -0700170static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700172 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
David Brownell097db1d2008-06-19 18:18:27 -0700175static struct usb_gadget_strings *dev_strings[] = {
176 &stringtab_dev,
177 NULL,
178};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/*-------------------------------------------------------------------------*/
181
David Brownell097db1d2008-06-19 18:18:27 -0700182struct usb_request *alloc_ep_req(struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct usb_request *req;
185
David Brownell7472f382008-04-18 18:47:54 -0700186 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (req) {
David Brownell097db1d2008-06-19 18:18:27 -0700188 req->length = buflen;
189 req->buf = kmalloc(buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700191 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 req = NULL;
193 }
194 }
195 return req;
196}
197
David Brownell097db1d2008-06-19 18:18:27 -0700198void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Brownell9d8bab52007-07-01 11:04:54 -0700200 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700201 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
David Brownell097db1d2008-06-19 18:18:27 -0700204static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Brownell097db1d2008-06-19 18:18:27 -0700206 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Brownell097db1d2008-06-19 18:18:27 -0700208 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215}
216
David Brownell097db1d2008-06-19 18:18:27 -0700217void disable_endpoints(struct usb_composite_dev *cdev,
218 struct usb_ep *in, struct usb_ep *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
David Brownell097db1d2008-06-19 18:18:27 -0700220 disable_ep(cdev, in);
221 disable_ep(cdev, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*-------------------------------------------------------------------------*/
225
David Brownellab943a22009-03-19 14:16:09 -0700226static 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 /* unconfigured devices can't issue wakeups */
234 if (!cdev->config)
235 return;
236
237 /* Normally the host would be woken up for something
238 * more significant than just a timer firing; likely
239 * because of some direct user request.
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
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200267static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
David Brownell91e79c92005-07-13 15:18:30 -0700269 int gcnum;
David Brownell097db1d2008-06-19 18:18:27 -0700270 struct usb_gadget *gadget = cdev->gadget;
271 int id;
David Brownell91e79c92005-07-13 15:18:30 -0700272
David Brownell097db1d2008-06-19 18:18:27 -0700273 /* Allocate string descriptor numbers ... note that string
274 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700275 */
David Brownell097db1d2008-06-19 18:18:27 -0700276 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
David Brownell097db1d2008-06-19 18:18:27 -0700282 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
David Brownellab943a22009-03-19 14:16:09 -0700294 setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);
295
David Brownell097db1d2008-06-19 18:18:27 -0700296 /* Register primary, then secondary configuration. Note that
297 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
David Brownell097db1d2008-06-19 18:18:27 -0700299 if (loopdefault) {
David Brownellab943a22009-03-19 14:16:09 -0700300 loopback_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100301 sourcesink_add(cdev, autoresume != 0);
David Brownell097db1d2008-06-19 18:18:27 -0700302 } else {
David Brownellab943a22009-03-19 14:16:09 -0700303 sourcesink_add(cdev, autoresume != 0);
Christoph Egger90f79762010-02-05 13:24:12 +0100304 loopback_add(cdev, autoresume != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Brownell7472f382008-04-18 18:47:54 -0700307 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -0700308 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700309 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -0700310 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* gadget zero is so simple (for now, no altsettings) that
312 * it SHOULD NOT have problems with bulk-capable hardware.
David Brownell097db1d2008-06-19 18:18:27 -0700313 * so just warn about unrcognized controllers -- don't panic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
315 * things like configuration and altsetting numbering
316 * can need hardware-specific attention though.
317 */
David Brownell00274922007-11-19 12:58:36 -0800318 pr_warning("%s: controller '%s' not recognized\n",
David Brownell097db1d2008-06-19 18:18:27 -0700319 longname, gadget->name);
Harvey Harrison551509d2009-02-11 14:11:36 -0800320 device_desc.bcdDevice = cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323
David Brownell097db1d2008-06-19 18:18:27 -0700324 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Brownell7472f382008-04-18 18:47:54 -0700326 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700327 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 gadget->name);
329
330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
David Brownellab943a22009-03-19 14:16:09 -0700333static int zero_unbind(struct usb_composite_dev *cdev)
334{
335 del_timer_sync(&autoresume_timer);
336 return 0;
337}
338
David Brownell097db1d2008-06-19 18:18:27 -0700339static struct usb_composite_driver zero_driver = {
340 .name = "zero",
341 .dev = &device_desc,
342 .strings = dev_strings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .bind = zero_bind,
David Brownellab943a22009-03-19 14:16:09 -0700344 .unbind = zero_unbind,
345 .suspend = zero_suspend,
346 .resume = zero_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
David Brownellca2bdf42007-08-02 12:20:05 -0700349MODULE_AUTHOR("David Brownell");
350MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David Brownell7472f382008-04-18 18:47:54 -0700352static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
David Brownell097db1d2008-06-19 18:18:27 -0700354 return usb_composite_register(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
David Brownell7472f382008-04-18 18:47:54 -0700356module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Brownell7472f382008-04-18 18:47:54 -0700358static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Brownell097db1d2008-06-19 18:18:27 -0700360 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
David Brownell7472f382008-04-18 18:47:54 -0700362module_exit(cleanup);