zero.c 9.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * zero.c -- Gadget Zero, for USB development
 *
4 5
 * Copyright (C) 2003-2008 David Brownell
 * Copyright (C) 2008 by Nokia Corporation
L
Linus Torvalds 已提交
6
 *
D
David Brownell 已提交
7 8 9 10
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
L
Linus Torvalds 已提交
11 12 13 14 15 16
 */


/*
 * Gadget Zero only needs two bulk endpoints, and is an example of how you
 * can write a hardware-agnostic gadget driver running inside a USB device.
17
 * Some hardware details are visible, but don't affect most of the driver.
L
Linus Torvalds 已提交
18 19 20 21 22 23
 *
 * Use it with the Linux host/master side "usbtest" driver to get a basic
 * functional test of your device-side usb stack, or with "usb-skeleton".
 *
 * It supports two similar configurations.  One sinks whatever the usb host
 * writes, and in return sources zeroes.  The other loops whatever the host
24
 * writes back, so the host can read it.
L
Linus Torvalds 已提交
25 26 27 28
 *
 * Many drivers will only have one configuration, letting them be much
 * simpler if they also don't support high speed operation (like this
 * driver does).
D
David Brownell 已提交
29 30 31 32 33
 *
 * Why is *this* driver using two configurations, rather than setting up
 * two interfaces with different functions?  To help verify that multiple
 * configuration infrastucture is working correctly; also, so that it can
 * work with low capability USB controllers without four bulk endpoints.
L
Linus Torvalds 已提交
34 35
 */

36 37 38 39 40
/*
 * driver assumes self-powered hardware, and
 * has no way for users to trigger remote wakeup.
 */

D
David Brownell 已提交
41
/* #define VERBOSE_DEBUG */
L
Linus Torvalds 已提交
42 43

#include <linux/kernel.h>
44
#include <linux/slab.h>
L
Linus Torvalds 已提交
45 46 47
#include <linux/utsname.h>
#include <linux/device.h>

48
#include "g_zero.h"
L
Linus Torvalds 已提交
49 50 51
#include "gadget_chips.h"


52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*-------------------------------------------------------------------------*/

/*
 * Kbuild is not very cooperative with respect to linking separately
 * compiled library objects into one module.  So for now we won't use
 * separate compilation ... ensuring init/exit sections work to shrink
 * the runtime footprint, and giving us at least some parts of what
 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
 */
#include "composite.c"

#include "f_sourcesink.c"
#include "f_loopback.c"

L
Linus Torvalds 已提交
66
/*-------------------------------------------------------------------------*/
67
USB_GADGET_COMPOSITE_OPTIONS();
L
Linus Torvalds 已提交
68

69
#define DRIVER_VERSION		"Cinco de Mayo 2008"
L
Linus Torvalds 已提交
70

71
static const char longname[] = "Gadget Zero";
L
Linus Torvalds 已提交
72

73
unsigned buflen = 4096;		/* only used for bulk endpoints */
74
module_param(buflen, uint, 0);
L
Linus Torvalds 已提交
75 76 77 78

/*
 * Normally the "loopback" configuration is second (index 1) so
 * it's not the default.  Here's where to change that order, to
79 80
 * work better with hosts where config changes are problematic or
 * controllers (like original superh) that only support one config.
L
Linus Torvalds 已提交
81
 */
82
static bool loopdefault = 0;
83
module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94

/*-------------------------------------------------------------------------*/

/* Thanks to NetChip Technologies for donating this product ID.
 *
 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 * Instead:  allocate your own, using normal USB-IF procedures.
 */
#ifndef	CONFIG_USB_ZERO_HNPTEST
#define DRIVER_VENDOR_NUM	0x0525		/* NetChip */
#define DRIVER_PRODUCT_NUM	0xa4a0		/* Linux-USB "Gadget Zero" */
95
#define DEFAULT_AUTORESUME	0
L
Linus Torvalds 已提交
96 97 98
#else
#define DRIVER_VENDOR_NUM	0x1a0a		/* OTG test device IDs */
#define DRIVER_PRODUCT_NUM	0xbadd
99
#define DEFAULT_AUTORESUME	5
L
Linus Torvalds 已提交
100 101
#endif

102 103 104 105 106 107 108 109
/* If the optional "autoresume" mode is enabled, it provides good
 * functional coverage for the "USBCV" test harness from USB-IF.
 * It's always set if OTG mode is enabled.
 */
unsigned autoresume = DEFAULT_AUTORESUME;
module_param(autoresume, uint, S_IRUGO);
MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");

L
Linus Torvalds 已提交
110 111
/*-------------------------------------------------------------------------*/

112
static struct usb_device_descriptor device_desc = {
L
Linus Torvalds 已提交
113 114 115
	.bLength =		sizeof device_desc,
	.bDescriptorType =	USB_DT_DEVICE,

116
	.bcdUSB =		cpu_to_le16(0x0200),
L
Linus Torvalds 已提交
117 118
	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,

119 120
	.idVendor =		cpu_to_le16(DRIVER_VENDOR_NUM),
	.idProduct =		cpu_to_le16(DRIVER_PRODUCT_NUM),
L
Linus Torvalds 已提交
121 122 123
	.bNumConfigurations =	2,
};

124
#ifdef CONFIG_USB_OTG
125
static struct usb_otg_descriptor otg_descriptor = {
L
Linus Torvalds 已提交
126 127 128
	.bLength =		sizeof otg_descriptor,
	.bDescriptorType =	USB_DT_OTG,

129 130 131 132
	/* REVISIT SRP-only hardware is possible, although
	 * it would not be called "OTG" ...
	 */
	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP,
L
Linus Torvalds 已提交
133 134
};

135
const struct usb_descriptor_header *otg_desc[] = {
L
Linus Torvalds 已提交
136 137 138
	(struct usb_descriptor_header *) &otg_descriptor,
	NULL,
};
139
#endif
L
Linus Torvalds 已提交
140

141
/* string IDs are assigned dynamically */
D
David Brownell 已提交
142
static char manufacturer[50];
L
Linus Torvalds 已提交
143

D
David Brownell 已提交
144 145
/* default serial number takes at least two packets */
static char serial[] = "0123456789.0123456789.0123456789";
L
Linus Torvalds 已提交
146

147
static struct usb_string strings_dev[] = {
148 149 150
	[USB_GADGET_MANUFACTURER_IDX].s = manufacturer,
	[USB_GADGET_PRODUCT_IDX].s = longname,
	[USB_GADGET_SERIAL_IDX].s = serial,
L
Linus Torvalds 已提交
151 152 153
	{  }			/* end of list */
};

154
static struct usb_gadget_strings stringtab_dev = {
L
Linus Torvalds 已提交
155
	.language	= 0x0409,	/* en-us */
156
	.strings	= strings_dev,
L
Linus Torvalds 已提交
157 158
};

159 160 161 162
static struct usb_gadget_strings *dev_strings[] = {
	&stringtab_dev,
	NULL,
};
L
Linus Torvalds 已提交
163 164 165

/*-------------------------------------------------------------------------*/

166
struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
L
Linus Torvalds 已提交
167 168 169
{
	struct usb_request	*req;

170
	req = usb_ep_alloc_request(ep, GFP_ATOMIC);
L
Linus Torvalds 已提交
171
	if (req) {
172 173 174 175 176
		if (len)
			req->length = len;
		else
			req->length = buflen;
		req->buf = kmalloc(req->length, GFP_ATOMIC);
L
Linus Torvalds 已提交
177
		if (!req->buf) {
178
			usb_ep_free_request(ep, req);
L
Linus Torvalds 已提交
179 180 181 182 183 184
			req = NULL;
		}
	}
	return req;
}

185
void free_ep_req(struct usb_ep *ep, struct usb_request *req)
L
Linus Torvalds 已提交
186
{
187
	kfree(req->buf);
188
	usb_ep_free_request(ep, req);
L
Linus Torvalds 已提交
189 190
}

191
static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
L
Linus Torvalds 已提交
192
{
193 194 195 196 197 198 199 200
	int			value;

	if (ep->driver_data) {
		value = usb_ep_disable(ep);
		if (value < 0)
			DBG(cdev, "disable %s --> %d\n",
					ep->name, value);
		ep->driver_data = NULL;
L
Linus Torvalds 已提交
201 202 203
	}
}

204
void disable_endpoints(struct usb_composite_dev *cdev,
205 206
		struct usb_ep *in, struct usb_ep *out,
		struct usb_ep *iso_in, struct usb_ep *iso_out)
L
Linus Torvalds 已提交
207
{
208 209
	disable_ep(cdev, in);
	disable_ep(cdev, out);
210 211 212 213
	if (iso_in)
		disable_ep(cdev, iso_in);
	if (iso_out)
		disable_ep(cdev, iso_out);
L
Linus Torvalds 已提交
214 215 216 217
}

/*-------------------------------------------------------------------------*/

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static struct timer_list	autoresume_timer;

static void zero_autoresume(unsigned long _c)
{
	struct usb_composite_dev	*cdev = (void *)_c;
	struct usb_gadget		*g = cdev->gadget;

	/* unconfigured devices can't issue wakeups */
	if (!cdev->config)
		return;

	/* Normally the host would be woken up for something
	 * more significant than just a timer firing; likely
	 * because of some direct user request.
	 */
	if (g->speed != USB_SPEED_UNKNOWN) {
		int status = usb_gadget_wakeup(g);
		INFO(cdev, "%s --> %d\n", __func__, status);
	}
}

static void zero_suspend(struct usb_composite_dev *cdev)
{
	if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
		return;

	if (autoresume) {
		mod_timer(&autoresume_timer, jiffies + (HZ * autoresume));
		DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
	} else
		DBG(cdev, "%s\n", __func__);
}

static void zero_resume(struct usb_composite_dev *cdev)
{
	DBG(cdev, "%s\n", __func__);
	del_timer(&autoresume_timer);
}

/*-------------------------------------------------------------------------*/

259
static int __init zero_bind(struct usb_composite_dev *cdev)
L
Linus Torvalds 已提交
260
{
261
	int			gcnum;
262
	struct usb_gadget	*gadget = cdev->gadget;
263
	int			status;
264

265 266
	/* Allocate string descriptor numbers ... note that string
	 * contents can be overridden by the composite_dev glue.
267
	 */
268 269 270 271
	status = usb_string_ids_tab(cdev, strings_dev);
	if (status < 0)
		return status;

272 273 274
	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
	device_desc.iSerialNumber = strings_dev[USB_GADGET_SERIAL_IDX].id;
275

276 277
	setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev);

278 279
	/* Register primary, then secondary configuration.  Note that
	 * SH3 only allows one config...
L
Linus Torvalds 已提交
280
	 */
281
	if (loopdefault) {
282
		loopback_add(cdev, autoresume != 0);
283
		sourcesink_add(cdev, autoresume != 0);
284
	} else {
285
		sourcesink_add(cdev, autoresume != 0);
286
		loopback_add(cdev, autoresume != 0);
L
Linus Torvalds 已提交
287 288
	}

289
	gcnum = usb_gadget_controller_number(gadget);
290
	if (gcnum >= 0)
291
		device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
292
	else {
L
Linus Torvalds 已提交
293 294
		/* gadget zero is so simple (for now, no altsettings) that
		 * it SHOULD NOT have problems with bulk-capable hardware.
295
		 * so just warn about unrcognized controllers -- don't panic.
L
Linus Torvalds 已提交
296 297 298 299
		 *
		 * things like configuration and altsetting numbering
		 * can need hardware-specific attention though.
		 */
300
		pr_warning("%s: controller '%s' not recognized\n",
301
			longname, gadget->name);
302
		device_desc.bcdDevice = cpu_to_le16(0x9999);
L
Linus Torvalds 已提交
303
	}
304
	usb_composite_overwrite_options(cdev, &coverwrite);
L
Linus Torvalds 已提交
305

306
	INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname);
L
Linus Torvalds 已提交
307

308
	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
309
		init_utsname()->sysname, init_utsname()->release,
L
Linus Torvalds 已提交
310 311 312 313 314
		gadget->name);

	return 0;
}

315 316 317 318 319 320
static int zero_unbind(struct usb_composite_dev *cdev)
{
	del_timer_sync(&autoresume_timer);
	return 0;
}

321
static __refdata struct usb_composite_driver zero_driver = {
322 323 324
	.name		= "zero",
	.dev		= &device_desc,
	.strings	= dev_strings,
325
	.max_speed	= USB_SPEED_SUPER,
326
	.bind		= zero_bind,
327 328 329
	.unbind		= zero_unbind,
	.suspend	= zero_suspend,
	.resume		= zero_resume,
L
Linus Torvalds 已提交
330 331
};

D
David Brownell 已提交
332 333
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
334

335
static int __init init(void)
L
Linus Torvalds 已提交
336
{
337
	return usb_composite_probe(&zero_driver);
L
Linus Torvalds 已提交
338
}
339
module_init(init);
L
Linus Torvalds 已提交
340

341
static void __exit cleanup(void)
L
Linus Torvalds 已提交
342
{
343
	usb_composite_unregister(&zero_driver);
L
Linus Torvalds 已提交
344
}
345
module_exit(cleanup);