blob: 20614dce8db9a81be569bc7de9a482b6267f0b78 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/utsname.h>
54#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Brownell097db1d2008-06-19 18:18:27 -070056#include "g_zero.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "gadget_chips.h"
58
59
60/*-------------------------------------------------------------------------*/
61
David Brownell7e75bc02008-08-18 17:41:31 -070062/*
63 * Kbuild is not very cooperative with respect to linking separately
64 * compiled library objects into one module. So for now we won't use
65 * separate compilation ... ensuring init/exit sections work to shrink
66 * the runtime footprint, and giving us at least some parts of what
67 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
68 */
69#include "composite.c"
70#include "usbstring.c"
71#include "config.c"
72#include "epautoconf.c"
73
74#include "f_sourcesink.c"
75#include "f_loopback.c"
76
77/*-------------------------------------------------------------------------*/
78
David Brownell097db1d2008-06-19 18:18:27 -070079#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
David Brownell7472f382008-04-18 18:47:54 -070081static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
David Brownell097db1d2008-06-19 18:18:27 -070083unsigned buflen = 4096;
84module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Normally the "loopback" configuration is second (index 1) so
88 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070089 * work better with hosts where config changes are problematic or
90 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92static int loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070093module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*-------------------------------------------------------------------------*/
96
97/* Thanks to NetChip Technologies for donating this product ID.
98 *
99 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
100 * Instead: allocate your own, using normal USB-IF procedures.
101 */
102#ifndef CONFIG_USB_ZERO_HNPTEST
103#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
104#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
105#else
106#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
107#define DRIVER_PRODUCT_NUM 0xbadd
108#endif
109
110/*-------------------------------------------------------------------------*/
111
David Brownell7472f382008-04-18 18:47:54 -0700112static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .bLength = sizeof device_desc,
114 .bDescriptorType = USB_DT_DEVICE,
115
Harvey Harrison551509d2009-02-11 14:11:36 -0800116 .bcdUSB = cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
118
Harvey Harrison551509d2009-02-11 14:11:36 -0800119 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
120 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .bNumConfigurations = 2,
122};
123
David Brownell097db1d2008-06-19 18:18:27 -0700124#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700125static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .bLength = sizeof otg_descriptor,
127 .bDescriptorType = USB_DT_OTG,
128
David Brownell097db1d2008-06-19 18:18:27 -0700129 /* REVISIT SRP-only hardware is possible, although
130 * it would not be called "OTG" ...
131 */
132 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
David Brownell097db1d2008-06-19 18:18:27 -0700135const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 NULL,
138};
David Brownell097db1d2008-06-19 18:18:27 -0700139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
David Brownell097db1d2008-06-19 18:18:27 -0700141/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David Brownell097db1d2008-06-19 18:18:27 -0700143#define STRING_MANUFACTURER_IDX 0
144#define STRING_PRODUCT_IDX 1
145#define STRING_SERIAL_IDX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David Brownellca2bdf42007-08-02 12:20:05 -0700147static char manufacturer[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Brownellca2bdf42007-08-02 12:20:05 -0700149/* default serial number takes at least two packets */
150static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David Brownell097db1d2008-06-19 18:18:27 -0700152static struct usb_string strings_dev[] = {
153 [STRING_MANUFACTURER_IDX].s = manufacturer,
154 [STRING_PRODUCT_IDX].s = longname,
155 [STRING_SERIAL_IDX].s = serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 { } /* end of list */
157};
158
David Brownell097db1d2008-06-19 18:18:27 -0700159static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700161 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
David Brownell097db1d2008-06-19 18:18:27 -0700164static struct usb_gadget_strings *dev_strings[] = {
165 &stringtab_dev,
166 NULL,
167};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/*-------------------------------------------------------------------------*/
170
David Brownell097db1d2008-06-19 18:18:27 -0700171struct usb_request *alloc_ep_req(struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct usb_request *req;
174
David Brownell7472f382008-04-18 18:47:54 -0700175 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (req) {
David Brownell097db1d2008-06-19 18:18:27 -0700177 req->length = buflen;
178 req->buf = kmalloc(buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700180 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 req = NULL;
182 }
183 }
184 return req;
185}
186
David Brownell097db1d2008-06-19 18:18:27 -0700187void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
David Brownell9d8bab52007-07-01 11:04:54 -0700189 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700190 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
David Brownell097db1d2008-06-19 18:18:27 -0700193static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
David Brownell097db1d2008-06-19 18:18:27 -0700195 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
David Brownell097db1d2008-06-19 18:18:27 -0700197 if (ep->driver_data) {
198 value = usb_ep_disable(ep);
199 if (value < 0)
200 DBG(cdev, "disable %s --> %d\n",
201 ep->name, value);
202 ep->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204}
205
David Brownell097db1d2008-06-19 18:18:27 -0700206void disable_endpoints(struct usb_composite_dev *cdev,
207 struct usb_ep *in, struct usb_ep *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David Brownell097db1d2008-06-19 18:18:27 -0700209 disable_ep(cdev, in);
210 disable_ep(cdev, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213/*-------------------------------------------------------------------------*/
214
David Brownell097db1d2008-06-19 18:18:27 -0700215static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
David Brownell91e79c92005-07-13 15:18:30 -0700217 int gcnum;
David Brownell097db1d2008-06-19 18:18:27 -0700218 struct usb_gadget *gadget = cdev->gadget;
219 int id;
David Brownell91e79c92005-07-13 15:18:30 -0700220
David Brownell097db1d2008-06-19 18:18:27 -0700221 /* Allocate string descriptor numbers ... note that string
222 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700223 */
David Brownell097db1d2008-06-19 18:18:27 -0700224 id = usb_string_id(cdev);
225 if (id < 0)
226 return id;
227 strings_dev[STRING_MANUFACTURER_IDX].id = id;
228 device_desc.iManufacturer = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
David Brownell097db1d2008-06-19 18:18:27 -0700230 id = usb_string_id(cdev);
231 if (id < 0)
232 return id;
233 strings_dev[STRING_PRODUCT_IDX].id = id;
234 device_desc.iProduct = id;
235
236 id = usb_string_id(cdev);
237 if (id < 0)
238 return id;
239 strings_dev[STRING_SERIAL_IDX].id = id;
240 device_desc.iSerialNumber = id;
241
242 /* Register primary, then secondary configuration. Note that
243 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
David Brownell097db1d2008-06-19 18:18:27 -0700245 if (loopdefault) {
246 loopback_add(cdev);
247 if (!gadget_is_sh(gadget))
248 sourcesink_add(cdev);
249 } else {
250 sourcesink_add(cdev);
251 if (!gadget_is_sh(gadget))
252 loopback_add(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Brownell7472f382008-04-18 18:47:54 -0700255 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -0700256 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700257 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -0700258 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* gadget zero is so simple (for now, no altsettings) that
260 * it SHOULD NOT have problems with bulk-capable hardware.
David Brownell097db1d2008-06-19 18:18:27 -0700261 * so just warn about unrcognized controllers -- don't panic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * things like configuration and altsetting numbering
264 * can need hardware-specific attention though.
265 */
David Brownell00274922007-11-19 12:58:36 -0800266 pr_warning("%s: controller '%s' not recognized\n",
David Brownell097db1d2008-06-19 18:18:27 -0700267 longname, gadget->name);
Harvey Harrison551509d2009-02-11 14:11:36 -0800268 device_desc.bcdDevice = cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271
David Brownell097db1d2008-06-19 18:18:27 -0700272 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Brownell7472f382008-04-18 18:47:54 -0700274 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700275 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 gadget->name);
277
278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
David Brownell097db1d2008-06-19 18:18:27 -0700281static struct usb_composite_driver zero_driver = {
282 .name = "zero",
283 .dev = &device_desc,
284 .strings = dev_strings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .bind = zero_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
David Brownellca2bdf42007-08-02 12:20:05 -0700288MODULE_AUTHOR("David Brownell");
289MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Brownell7472f382008-04-18 18:47:54 -0700291static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
David Brownell097db1d2008-06-19 18:18:27 -0700293 return usb_composite_register(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
David Brownell7472f382008-04-18 18:47:54 -0700295module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
David Brownell7472f382008-04-18 18:47:54 -0700297static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
David Brownell097db1d2008-06-19 18:18:27 -0700299 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
David Brownell7472f382008-04-18 18:47:54 -0700301module_exit(cleanup);