| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * zero.c -- Gadget Zero, for USB development | 
 | 3 |  * | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 4 |  * Copyright (C) 2003-2008 David Brownell | 
 | 5 |  * Copyright (C) 2008 by Nokia Corporation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  * | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 7 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 |  * | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 12 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  * | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 17 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  */ | 
 | 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 Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 26 |  * Some hardware details are visible, but don't affect most of the driver. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  * | 
 | 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 Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 33 |  * writes back, so the host can read it. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  * | 
 | 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 Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 38 |  * | 
 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  */ | 
 | 44 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 45 | /* | 
 | 46 |  * driver assumes self-powered hardware, and | 
 | 47 |  * has no way for users to trigger remote wakeup. | 
 | 48 |  */ | 
 | 49 |  | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 50 | /* #define VERBOSE_DEBUG */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <linux/utsname.h> | 
 | 54 | #include <linux/device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 56 | #include "g_zero.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include "gadget_chips.h" | 
 | 58 |  | 
 | 59 |  | 
 | 60 | /*-------------------------------------------------------------------------*/ | 
 | 61 |  | 
| David Brownell | 7e75bc0 | 2008-08-18 17:41:31 -0700 | [diff] [blame] | 62 | /* | 
 | 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 Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 79 | #define DRIVER_VERSION		"Cinco de Mayo 2008" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 81 | static const char longname[] = "Gadget Zero"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 83 | unsigned buflen = 4096; | 
 | 84 | module_param(buflen, uint, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
 | 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 Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 89 |  * work better with hosts where config changes are problematic or | 
 | 90 |  * controllers (like original superh) that only support one config. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  */ | 
 | 92 | static int loopdefault = 0; | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 93 | module_param(loopdefault, bool, S_IRUGO|S_IWUSR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
 | 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" */ | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 105 | #define DEFAULT_AUTORESUME	0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #else | 
 | 107 | #define DRIVER_VENDOR_NUM	0x1a0a		/* OTG test device IDs */ | 
 | 108 | #define DRIVER_PRODUCT_NUM	0xbadd | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 109 | #define DEFAULT_AUTORESUME	5 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif | 
 | 111 |  | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 112 | /* If the optional "autoresume" mode is enabled, it provides good | 
 | 113 |  * functional coverage for the "USBCV" test harness from USB-IF. | 
 | 114 |  * It's always set if OTG mode is enabled. | 
 | 115 |  */ | 
 | 116 | unsigned autoresume = DEFAULT_AUTORESUME; | 
 | 117 | module_param(autoresume, uint, S_IRUGO); | 
 | 118 | MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup"); | 
 | 119 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /*-------------------------------------------------------------------------*/ | 
 | 121 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 122 | static struct usb_device_descriptor device_desc = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | 	.bLength =		sizeof device_desc, | 
 | 124 | 	.bDescriptorType =	USB_DT_DEVICE, | 
 | 125 |  | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 126 | 	.bcdUSB =		cpu_to_le16(0x0200), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 	.bDeviceClass =		USB_CLASS_VENDOR_SPEC, | 
 | 128 |  | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 129 | 	.idVendor =		cpu_to_le16(DRIVER_VENDOR_NUM), | 
 | 130 | 	.idProduct =		cpu_to_le16(DRIVER_PRODUCT_NUM), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 	.bNumConfigurations =	2, | 
 | 132 | }; | 
 | 133 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 134 | #ifdef CONFIG_USB_OTG | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 135 | static struct usb_otg_descriptor otg_descriptor = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 	.bLength =		sizeof otg_descriptor, | 
 | 137 | 	.bDescriptorType =	USB_DT_OTG, | 
 | 138 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 139 | 	/* REVISIT SRP-only hardware is possible, although | 
 | 140 | 	 * it would not be called "OTG" ... | 
 | 141 | 	 */ | 
 | 142 | 	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | }; | 
 | 144 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 145 | const struct usb_descriptor_header *otg_desc[] = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | 	NULL, | 
 | 148 | }; | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 149 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 151 | /* string IDs are assigned dynamically */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 153 | #define STRING_MANUFACTURER_IDX		0 | 
 | 154 | #define STRING_PRODUCT_IDX		1 | 
 | 155 | #define STRING_SERIAL_IDX		2 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 |  | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 157 | static char manufacturer[50]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 |  | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 159 | /* default serial number takes at least two packets */ | 
 | 160 | static char serial[] = "0123456789.0123456789.0123456789"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 162 | static struct usb_string strings_dev[] = { | 
 | 163 | 	[STRING_MANUFACTURER_IDX].s = manufacturer, | 
 | 164 | 	[STRING_PRODUCT_IDX].s = longname, | 
 | 165 | 	[STRING_SERIAL_IDX].s = serial, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | 	{  }			/* end of list */ | 
 | 167 | }; | 
 | 168 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 169 | static struct usb_gadget_strings stringtab_dev = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | 	.language	= 0x0409,	/* en-us */ | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 171 | 	.strings	= strings_dev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | }; | 
 | 173 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 174 | static struct usb_gadget_strings *dev_strings[] = { | 
 | 175 | 	&stringtab_dev, | 
 | 176 | 	NULL, | 
 | 177 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 |  | 
 | 179 | /*-------------------------------------------------------------------------*/ | 
 | 180 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 181 | struct usb_request *alloc_ep_req(struct usb_ep *ep) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { | 
 | 183 | 	struct usb_request	*req; | 
 | 184 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 185 | 	req = usb_ep_alloc_request(ep, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | 	if (req) { | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 187 | 		req->length = buflen; | 
 | 188 | 		req->buf = kmalloc(buflen, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | 		if (!req->buf) { | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 190 | 			usb_ep_free_request(ep, req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 			req = NULL; | 
 | 192 | 		} | 
 | 193 | 	} | 
 | 194 | 	return req; | 
 | 195 | } | 
 | 196 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 197 | void free_ep_req(struct usb_ep *ep, struct usb_request *req) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { | 
| David Brownell | 9d8bab5 | 2007-07-01 11:04:54 -0700 | [diff] [blame] | 199 | 	kfree(req->buf); | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 200 | 	usb_ep_free_request(ep, req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } | 
 | 202 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 203 | static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 205 | 	int			value; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 207 | 	if (ep->driver_data) { | 
 | 208 | 		value = usb_ep_disable(ep); | 
 | 209 | 		if (value < 0) | 
 | 210 | 			DBG(cdev, "disable %s --> %d\n", | 
 | 211 | 					ep->name, value); | 
 | 212 | 		ep->driver_data = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | 	} | 
 | 214 | } | 
 | 215 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 216 | void disable_endpoints(struct usb_composite_dev *cdev, | 
 | 217 | 		struct usb_ep *in, struct usb_ep *out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 219 | 	disable_ep(cdev, in); | 
 | 220 | 	disable_ep(cdev, out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } | 
 | 222 |  | 
 | 223 | /*-------------------------------------------------------------------------*/ | 
 | 224 |  | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 225 | static struct timer_list	autoresume_timer; | 
 | 226 |  | 
 | 227 | static void zero_autoresume(unsigned long _c) | 
 | 228 | { | 
 | 229 | 	struct usb_composite_dev	*cdev = (void *)_c; | 
 | 230 | 	struct usb_gadget		*g = cdev->gadget; | 
 | 231 |  | 
 | 232 | 	/* unconfigured devices can't issue wakeups */ | 
 | 233 | 	if (!cdev->config) | 
 | 234 | 		return; | 
 | 235 |  | 
 | 236 | 	/* Normally the host would be woken up for something | 
 | 237 | 	 * more significant than just a timer firing; likely | 
 | 238 | 	 * because of some direct user request. | 
 | 239 | 	 */ | 
 | 240 | 	if (g->speed != USB_SPEED_UNKNOWN) { | 
 | 241 | 		int status = usb_gadget_wakeup(g); | 
 | 242 | 		INFO(cdev, "%s --> %d\n", __func__, status); | 
 | 243 | 	} | 
 | 244 | } | 
 | 245 |  | 
 | 246 | static void zero_suspend(struct usb_composite_dev *cdev) | 
 | 247 | { | 
 | 248 | 	if (cdev->gadget->speed == USB_SPEED_UNKNOWN) | 
 | 249 | 		return; | 
 | 250 |  | 
 | 251 | 	if (autoresume) { | 
 | 252 | 		mod_timer(&autoresume_timer, jiffies + (HZ * autoresume)); | 
 | 253 | 		DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume); | 
 | 254 | 	} else | 
 | 255 | 		DBG(cdev, "%s\n", __func__); | 
 | 256 | } | 
 | 257 |  | 
 | 258 | static void zero_resume(struct usb_composite_dev *cdev) | 
 | 259 | { | 
 | 260 | 	DBG(cdev, "%s\n", __func__); | 
 | 261 | 	del_timer(&autoresume_timer); | 
 | 262 | } | 
 | 263 |  | 
 | 264 | /*-------------------------------------------------------------------------*/ | 
 | 265 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 266 | static int __init zero_bind(struct usb_composite_dev *cdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 268 | 	int			gcnum; | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 269 | 	struct usb_gadget	*gadget = cdev->gadget; | 
 | 270 | 	int			id; | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 271 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 272 | 	/* Allocate string descriptor numbers ... note that string | 
 | 273 | 	 * contents can be overridden by the composite_dev glue. | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 274 | 	 */ | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 275 | 	id = usb_string_id(cdev); | 
 | 276 | 	if (id < 0) | 
 | 277 | 		return id; | 
 | 278 | 	strings_dev[STRING_MANUFACTURER_IDX].id = id; | 
 | 279 | 	device_desc.iManufacturer = id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 281 | 	id = usb_string_id(cdev); | 
 | 282 | 	if (id < 0) | 
 | 283 | 		return id; | 
 | 284 | 	strings_dev[STRING_PRODUCT_IDX].id = id; | 
 | 285 | 	device_desc.iProduct = id; | 
 | 286 |  | 
 | 287 | 	id = usb_string_id(cdev); | 
 | 288 | 	if (id < 0) | 
 | 289 | 		return id; | 
 | 290 | 	strings_dev[STRING_SERIAL_IDX].id = id; | 
 | 291 | 	device_desc.iSerialNumber = id; | 
 | 292 |  | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 293 | 	setup_timer(&autoresume_timer, zero_autoresume, (unsigned long) cdev); | 
 | 294 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 295 | 	/* Register primary, then secondary configuration.  Note that | 
 | 296 | 	 * SH3 only allows one config... | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | 	 */ | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 298 | 	if (loopdefault) { | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 299 | 		loopback_add(cdev, autoresume != 0); | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 300 | 		if (!gadget_is_sh(gadget)) | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 301 | 			sourcesink_add(cdev, autoresume != 0); | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 302 | 	} else { | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 303 | 		sourcesink_add(cdev, autoresume != 0); | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 304 | 		if (!gadget_is_sh(gadget)) | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 305 | 			loopback_add(cdev, autoresume != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 308 | 	gcnum = usb_gadget_controller_number(gadget); | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 309 | 	if (gcnum >= 0) | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 310 | 		device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum); | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 311 | 	else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | 		/* gadget zero is so simple (for now, no altsettings) that | 
 | 313 | 		 * it SHOULD NOT have problems with bulk-capable hardware. | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 314 | 		 * so just warn about unrcognized controllers -- don't panic. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | 		 * | 
 | 316 | 		 * things like configuration and altsetting numbering | 
 | 317 | 		 * can need hardware-specific attention though. | 
 | 318 | 		 */ | 
| David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 319 | 		pr_warning("%s: controller '%s' not recognized\n", | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 320 | 			longname, gadget->name); | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 321 | 		device_desc.bcdDevice = cpu_to_le16(0x9999); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | 	} | 
 | 323 |  | 
 | 324 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 325 | 	INFO(cdev, "%s, version: " DRIVER_VERSION "\n", longname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 327 | 	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | 
| Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 328 | 		init_utsname()->sysname, init_utsname()->release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | 		gadget->name); | 
 | 330 |  | 
 | 331 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } | 
 | 333 |  | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 334 | static int zero_unbind(struct usb_composite_dev *cdev) | 
 | 335 | { | 
 | 336 | 	del_timer_sync(&autoresume_timer); | 
 | 337 | 	return 0; | 
 | 338 | } | 
 | 339 |  | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 340 | static struct usb_composite_driver zero_driver = { | 
 | 341 | 	.name		= "zero", | 
 | 342 | 	.dev		= &device_desc, | 
 | 343 | 	.strings	= dev_strings, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | 	.bind		= zero_bind, | 
| David Brownell | ab943a2 | 2009-03-19 14:16:09 -0700 | [diff] [blame] | 345 | 	.unbind		= zero_unbind, | 
 | 346 | 	.suspend	= zero_suspend, | 
 | 347 | 	.resume		= zero_resume, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | }; | 
 | 349 |  | 
| David Brownell | ca2bdf4 | 2007-08-02 12:20:05 -0700 | [diff] [blame] | 350 | MODULE_AUTHOR("David Brownell"); | 
 | 351 | MODULE_LICENSE("GPL"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 353 | static int __init init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 355 | 	return usb_composite_register(&zero_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 357 | module_init(init); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 |  | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 359 | static void __exit cleanup(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { | 
| David Brownell | 097db1d | 2008-06-19 18:18:27 -0700 | [diff] [blame] | 361 | 	usb_composite_unregister(&zero_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } | 
| David Brownell | 7472f38 | 2008-04-18 18:47:54 -0700 | [diff] [blame] | 363 | module_exit(cleanup); |