blob: aa0bd4f126a1e5f7892e1ce645b187d88e4ea7f1 [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 Brownell097db1d2008-06-19 18:18:27 -070062#define DRIVER_VERSION "Cinco de Mayo 2008"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Brownell7472f382008-04-18 18:47:54 -070064static const char longname[] = "Gadget Zero";
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David Brownell097db1d2008-06-19 18:18:27 -070066unsigned buflen = 4096;
67module_param(buflen, uint, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * Normally the "loopback" configuration is second (index 1) so
71 * it's not the default. Here's where to change that order, to
David Brownell097db1d2008-06-19 18:18:27 -070072 * work better with hosts where config changes are problematic or
73 * controllers (like original superh) that only support one config.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75static int loopdefault = 0;
David Brownell7472f382008-04-18 18:47:54 -070076module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*-------------------------------------------------------------------------*/
79
80/* Thanks to NetChip Technologies for donating this product ID.
81 *
82 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
83 * Instead: allocate your own, using normal USB-IF procedures.
84 */
85#ifndef CONFIG_USB_ZERO_HNPTEST
86#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
87#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
88#else
89#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
90#define DRIVER_PRODUCT_NUM 0xbadd
91#endif
92
93/*-------------------------------------------------------------------------*/
94
David Brownell7472f382008-04-18 18:47:54 -070095static struct usb_device_descriptor device_desc = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .bLength = sizeof device_desc,
97 .bDescriptorType = USB_DT_DEVICE,
98
David Brownell7472f382008-04-18 18:47:54 -070099 .bcdUSB = __constant_cpu_to_le16(0x0200),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
101
David Brownell7472f382008-04-18 18:47:54 -0700102 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_NUM),
103 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .bNumConfigurations = 2,
105};
106
David Brownell097db1d2008-06-19 18:18:27 -0700107#ifdef CONFIG_USB_OTG
David Brownell7472f382008-04-18 18:47:54 -0700108static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .bLength = sizeof otg_descriptor,
110 .bDescriptorType = USB_DT_OTG,
111
David Brownell097db1d2008-06-19 18:18:27 -0700112 /* REVISIT SRP-only hardware is possible, although
113 * it would not be called "OTG" ...
114 */
115 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
David Brownell097db1d2008-06-19 18:18:27 -0700118const struct usb_descriptor_header *otg_desc[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 (struct usb_descriptor_header *) &otg_descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 NULL,
121};
David Brownell097db1d2008-06-19 18:18:27 -0700122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
David Brownell097db1d2008-06-19 18:18:27 -0700124/* string IDs are assigned dynamically */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David Brownell097db1d2008-06-19 18:18:27 -0700126#define STRING_MANUFACTURER_IDX 0
127#define STRING_PRODUCT_IDX 1
128#define STRING_SERIAL_IDX 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David Brownellca2bdf42007-08-02 12:20:05 -0700130static char manufacturer[50];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Brownellca2bdf42007-08-02 12:20:05 -0700132/* default serial number takes at least two packets */
133static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David Brownell097db1d2008-06-19 18:18:27 -0700135static struct usb_string strings_dev[] = {
136 [STRING_MANUFACTURER_IDX].s = manufacturer,
137 [STRING_PRODUCT_IDX].s = longname,
138 [STRING_SERIAL_IDX].s = serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 { } /* end of list */
140};
141
David Brownell097db1d2008-06-19 18:18:27 -0700142static struct usb_gadget_strings stringtab_dev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .language = 0x0409, /* en-us */
David Brownell097db1d2008-06-19 18:18:27 -0700144 .strings = strings_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
David Brownell097db1d2008-06-19 18:18:27 -0700147static struct usb_gadget_strings *dev_strings[] = {
148 &stringtab_dev,
149 NULL,
150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*-------------------------------------------------------------------------*/
153
David Brownell097db1d2008-06-19 18:18:27 -0700154struct usb_request *alloc_ep_req(struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct usb_request *req;
157
David Brownell7472f382008-04-18 18:47:54 -0700158 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (req) {
David Brownell097db1d2008-06-19 18:18:27 -0700160 req->length = buflen;
161 req->buf = kmalloc(buflen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!req->buf) {
David Brownell7472f382008-04-18 18:47:54 -0700163 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 req = NULL;
165 }
166 }
167 return req;
168}
169
David Brownell097db1d2008-06-19 18:18:27 -0700170void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
David Brownell9d8bab52007-07-01 11:04:54 -0700172 kfree(req->buf);
David Brownell7472f382008-04-18 18:47:54 -0700173 usb_ep_free_request(ep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
David Brownell097db1d2008-06-19 18:18:27 -0700176static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
David Brownell097db1d2008-06-19 18:18:27 -0700178 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
David Brownell097db1d2008-06-19 18:18:27 -0700180 if (ep->driver_data) {
181 value = usb_ep_disable(ep);
182 if (value < 0)
183 DBG(cdev, "disable %s --> %d\n",
184 ep->name, value);
185 ep->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187}
188
David Brownell097db1d2008-06-19 18:18:27 -0700189void disable_endpoints(struct usb_composite_dev *cdev,
190 struct usb_ep *in, struct usb_ep *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Brownell097db1d2008-06-19 18:18:27 -0700192 disable_ep(cdev, in);
193 disable_ep(cdev, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*-------------------------------------------------------------------------*/
197
David Brownell097db1d2008-06-19 18:18:27 -0700198static int __init zero_bind(struct usb_composite_dev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Brownell91e79c92005-07-13 15:18:30 -0700200 int gcnum;
David Brownell097db1d2008-06-19 18:18:27 -0700201 struct usb_gadget *gadget = cdev->gadget;
202 int id;
David Brownell91e79c92005-07-13 15:18:30 -0700203
David Brownell097db1d2008-06-19 18:18:27 -0700204 /* Allocate string descriptor numbers ... note that string
205 * contents can be overridden by the composite_dev glue.
David Brownell91e79c92005-07-13 15:18:30 -0700206 */
David Brownell097db1d2008-06-19 18:18:27 -0700207 id = usb_string_id(cdev);
208 if (id < 0)
209 return id;
210 strings_dev[STRING_MANUFACTURER_IDX].id = id;
211 device_desc.iManufacturer = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David Brownell097db1d2008-06-19 18:18:27 -0700213 id = usb_string_id(cdev);
214 if (id < 0)
215 return id;
216 strings_dev[STRING_PRODUCT_IDX].id = id;
217 device_desc.iProduct = id;
218
219 id = usb_string_id(cdev);
220 if (id < 0)
221 return id;
222 strings_dev[STRING_SERIAL_IDX].id = id;
223 device_desc.iSerialNumber = id;
224
225 /* Register primary, then secondary configuration. Note that
226 * SH3 only allows one config...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
David Brownell097db1d2008-06-19 18:18:27 -0700228 if (loopdefault) {
229 loopback_add(cdev);
230 if (!gadget_is_sh(gadget))
231 sourcesink_add(cdev);
232 } else {
233 sourcesink_add(cdev);
234 if (!gadget_is_sh(gadget))
235 loopback_add(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
David Brownell7472f382008-04-18 18:47:54 -0700238 gcnum = usb_gadget_controller_number(gadget);
David Brownell91e79c92005-07-13 15:18:30 -0700239 if (gcnum >= 0)
David Brownell7472f382008-04-18 18:47:54 -0700240 device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
David Brownell91e79c92005-07-13 15:18:30 -0700241 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* gadget zero is so simple (for now, no altsettings) that
243 * it SHOULD NOT have problems with bulk-capable hardware.
David Brownell097db1d2008-06-19 18:18:27 -0700244 * so just warn about unrcognized controllers -- don't panic.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * things like configuration and altsetting numbering
247 * can need hardware-specific attention though.
248 */
David Brownell00274922007-11-19 12:58:36 -0800249 pr_warning("%s: controller '%s' not recognized\n",
David Brownell097db1d2008-06-19 18:18:27 -0700250 longname, gadget->name);
David Brownell7472f382008-04-18 18:47:54 -0700251 device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254
David Brownell097db1d2008-06-19 18:18:27 -0700255 INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David Brownell7472f382008-04-18 18:47:54 -0700257 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700258 init_utsname()->sysname, init_utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 gadget->name);
260
261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
David Brownell097db1d2008-06-19 18:18:27 -0700264static struct usb_composite_driver zero_driver = {
265 .name = "zero",
266 .dev = &device_desc,
267 .strings = dev_strings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .bind = zero_bind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
David Brownellca2bdf42007-08-02 12:20:05 -0700271MODULE_AUTHOR("David Brownell");
272MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Brownell7472f382008-04-18 18:47:54 -0700274static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
David Brownell097db1d2008-06-19 18:18:27 -0700276 return usb_composite_register(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
David Brownell7472f382008-04-18 18:47:54 -0700278module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
David Brownell7472f382008-04-18 18:47:54 -0700280static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
David Brownell097db1d2008-06-19 18:18:27 -0700282 usb_composite_unregister(&zero_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
David Brownell7472f382008-04-18 18:47:54 -0700284module_exit(cleanup);