| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options | 
 | 3 |  * | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 4 |  * Copyright (C) 2003-2005,2008 David Brownell | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 |  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 6 |  * Copyright (C) 2008 Nokia Corporation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * | 
 | 8 |  * This program is free software; you can redistribute it and/or modify | 
 | 9 |  * it under the terms of the GNU General Public License as published by | 
 | 10 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 11 |  * (at your option) any later version. | 
 | 12 |  * | 
 | 13 |  * This program is distributed in the hope that it will be useful, | 
 | 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 16 |  * GNU General Public License for more details. | 
 | 17 |  * | 
 | 18 |  * You should have received a copy of the GNU General Public License | 
 | 19 |  * along with this program; if not, write to the Free Software | 
 | 20 |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
 | 21 |  */ | 
 | 22 |  | 
| David Brownell | 0cf4f2d | 2007-08-02 00:00:38 -0700 | [diff] [blame] | 23 | /* #define VERBOSE_DEBUG */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/utsname.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 28 | #include "u_ether.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
 | 31 | /* | 
 | 32 |  * Ethernet gadget driver -- with CDC and non-CDC options | 
 | 33 |  * Builds on hardware support for a full duplex link. | 
 | 34 |  * | 
 | 35 |  * CDC Ethernet is the standard USB solution for sending Ethernet frames | 
 | 36 |  * using USB.  Real hardware tends to use the same framing protocol but look | 
 | 37 |  * different for control features.  This driver strongly prefers to use | 
 | 38 |  * this USB-IF standard as its open-systems interoperability solution; | 
 | 39 |  * most host side USB stacks (except from Microsoft) support it. | 
 | 40 |  * | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 41 |  * This is sometimes called "CDC ECM" (Ethernet Control Model) to support | 
 | 42 |  * TLA-soup.  "CDC ACM" (Abstract Control Model) is for modems, and a new | 
 | 43 |  * "CDC EEM" (Ethernet Emulation Model) is starting to spread. | 
 | 44 |  * | 
 | 45 |  * There's some hardware that can't talk CDC ECM.  We make that hardware | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but | 
| David Brownell | 11d5489 | 2006-12-11 15:59:04 -0800 | [diff] [blame] | 47 |  * link-level setup only requires activating the configuration.  Only the | 
 | 48 |  * endpoint descriptors, and product/vendor IDs, are relevant; no control | 
 | 49 |  * operations are available.  Linux supports it, but other host operating | 
 | 50 |  * systems may not.  (This is a subset of CDC Ethernet.) | 
 | 51 |  * | 
 | 52 |  * It turns out that if you add a few descriptors to that "CDC Subset", | 
 | 53 |  * (Windows) host side drivers from MCCI can treat it as one submode of | 
 | 54 |  * a proprietary scheme called "SAFE" ... without needing to know about | 
 | 55 |  * specific product/vendor IDs.  So we do that, making it easier to use | 
 | 56 |  * those MS-Windows drivers.  Those added descriptors make it resemble a | 
 | 57 |  * CDC MDLM device, but they don't change device behavior at all.  (See | 
 | 58 |  * MCCI Engineering report 950198 "SAFE Networking Functions".) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 |  * | 
 | 60 |  * A third option is also in use.  Rather than CDC Ethernet, or something | 
 | 61 |  * simpler, Microsoft pushes their own approach: RNDIS.  The published | 
 | 62 |  * RNDIS specs are ambiguous and appear to be incomplete, and are also | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 63 |  * needlessly complex.  They borrow more from CDC ACM than CDC ECM. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  */ | 
 | 65 |  | 
 | 66 | #define DRIVER_DESC		"Ethernet Gadget" | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 67 | #define DRIVER_VERSION		"Memorial Day 2008" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_USB_ETH_RNDIS | 
 | 70 | #define PREFIX			"RNDIS/" | 
 | 71 | #else | 
 | 72 | #define PREFIX			"" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #endif | 
 | 74 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 75 | /* | 
 | 76 |  * This driver aims for interoperability by using CDC ECM unless | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |  * | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 78 |  *		can_support_ecm() | 
 | 79 |  * | 
 | 80 |  * returns false, in which case it supports the CDC Subset.  By default, | 
 | 81 |  * that returns true; most hardware has no problems with CDC ECM, that's | 
 | 82 |  * a good default.  Previous versions of this driver had no default; this | 
 | 83 |  * version changes that, removing overhead for new controller support. | 
 | 84 |  * | 
 | 85 |  *	IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE! | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 88 | static inline bool has_rndis(void) | 
 | 89 | { | 
 | 90 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 91 | 	return true; | 
 | 92 | #else | 
 | 93 | 	return false; | 
 | 94 | #endif | 
 | 95 | } | 
 | 96 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /*-------------------------------------------------------------------------*/ | 
 | 98 |  | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 99 | /* | 
 | 100 |  * Kbuild is not very cooperative with respect to linking separately | 
 | 101 |  * compiled library objects into one module.  So for now we won't use | 
 | 102 |  * separate compilation ... ensuring init/exit sections work to shrink | 
 | 103 |  * the runtime footprint, and giving us at least some parts of what | 
 | 104 |  * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | 
 | 105 |  */ | 
 | 106 | #include "composite.c" | 
 | 107 | #include "usbstring.c" | 
 | 108 | #include "config.c" | 
 | 109 | #include "epautoconf.c" | 
 | 110 |  | 
 | 111 | #include "f_ecm.c" | 
 | 112 | #include "f_subset.c" | 
 | 113 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 114 | #include "f_rndis.c" | 
 | 115 | #include "rndis.c" | 
 | 116 | #endif | 
 | 117 | #include "u_ether.c" | 
 | 118 |  | 
 | 119 | /*-------------------------------------------------------------------------*/ | 
 | 120 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!! | 
 | 122 |  * Instead:  allocate your own, using normal USB-IF procedures. | 
 | 123 |  */ | 
 | 124 |  | 
 | 125 | /* Thanks to NetChip Technologies for donating this product ID. | 
 | 126 |  * It's for devices with only CDC Ethernet configurations. | 
 | 127 |  */ | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 128 | #define CDC_VENDOR_NUM		0x0525	/* NetChip */ | 
 | 129 | #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 |  | 
 | 131 | /* For hardware that can't talk CDC, we use the same vendor ID that | 
 | 132 |  * ARM Linux has used for ethernet-over-usb, both with sa1100 and | 
 | 133 |  * with pxa250.  We're protocol-compatible, if the host-side drivers | 
 | 134 |  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so | 
 | 135 |  * drivers that need to hard-wire endpoint numbers have a hook. | 
 | 136 |  * | 
 | 137 |  * The protocol is a minimal subset of CDC Ether, which works on any bulk | 
 | 138 |  * hardware that's not deeply broken ... even on hardware that can't talk | 
 | 139 |  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that | 
 | 140 |  * doesn't handle control-OUT). | 
 | 141 |  */ | 
 | 142 | #define	SIMPLE_VENDOR_NUM	0x049f | 
 | 143 | #define	SIMPLE_PRODUCT_NUM	0x505a | 
 | 144 |  | 
 | 145 | /* For hardware that can talk RNDIS and either of the above protocols, | 
 | 146 |  * use this ID ... the windows INF files will know it.  Unless it's | 
 | 147 |  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose | 
 | 148 |  * the non-RNDIS configuration. | 
 | 149 |  */ | 
 | 150 | #define RNDIS_VENDOR_NUM	0x0525	/* NetChip */ | 
 | 151 | #define RNDIS_PRODUCT_NUM	0xa4a2	/* Ethernet/RNDIS Gadget */ | 
 | 152 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /*-------------------------------------------------------------------------*/ | 
 | 154 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 155 | static struct usb_device_descriptor device_desc = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | 	.bLength =		sizeof device_desc, | 
 | 157 | 	.bDescriptorType =	USB_DT_DEVICE, | 
 | 158 |  | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 159 | 	.bcdUSB =		cpu_to_le16 (0x0200), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
 | 161 | 	.bDeviceClass =		USB_CLASS_COMM, | 
 | 162 | 	.bDeviceSubClass =	0, | 
 | 163 | 	.bDeviceProtocol =	0, | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 164 | 	/* .bMaxPacketSize0 = f(hardware) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 166 | 	/* Vendor and product id defaults change according to what configs | 
 | 167 | 	 * we support.  (As does bNumConfigurations.)  These values can | 
 | 168 | 	 * also be overridden by module parameters. | 
 | 169 | 	 */ | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 170 | 	.idVendor =		cpu_to_le16 (CDC_VENDOR_NUM), | 
 | 171 | 	.idProduct =		cpu_to_le16 (CDC_PRODUCT_NUM), | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 172 | 	/* .bcdDevice = f(hardware) */ | 
 | 173 | 	/* .iManufacturer = DYNAMIC */ | 
 | 174 | 	/* .iProduct = DYNAMIC */ | 
 | 175 | 	/* NO SERIAL NUMBER */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 	.bNumConfigurations =	1, | 
 | 177 | }; | 
 | 178 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 179 | static struct usb_otg_descriptor otg_descriptor = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | 	.bLength =		sizeof otg_descriptor, | 
 | 181 | 	.bDescriptorType =	USB_DT_OTG, | 
 | 182 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 183 | 	/* REVISIT SRP-only hardware is possible, although | 
 | 184 | 	 * it would not be called "OTG" ... | 
 | 185 | 	 */ | 
 | 186 | 	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | }; | 
 | 188 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 189 | static const struct usb_descriptor_header *otg_desc[] = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 	NULL, | 
 | 192 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 195 | /* string IDs are assigned dynamically */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 197 | #define STRING_MANUFACTURER_IDX		0 | 
 | 198 | #define STRING_PRODUCT_IDX		1 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 200 | static char manufacturer[50]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 202 | static struct usb_string strings_dev[] = { | 
 | 203 | 	[STRING_MANUFACTURER_IDX].s = manufacturer, | 
 | 204 | 	[STRING_PRODUCT_IDX].s = PREFIX DRIVER_DESC, | 
 | 205 | 	{  } /* end of list */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | }; | 
 | 207 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 208 | static struct usb_gadget_strings stringtab_dev = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 	.language	= 0x0409,	/* en-us */ | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 210 | 	.strings	= strings_dev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | }; | 
 | 212 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 213 | static struct usb_gadget_strings *dev_strings[] = { | 
 | 214 | 	&stringtab_dev, | 
 | 215 | 	NULL, | 
 | 216 | }; | 
 | 217 |  | 
 | 218 | static u8 hostaddr[ETH_ALEN]; | 
 | 219 |  | 
 | 220 | /*-------------------------------------------------------------------------*/ | 
 | 221 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 223 |  * We may not have an RNDIS configuration, but if we do it needs to be | 
 | 224 |  * the first one present.  That's to make Microsoft's drivers happy, | 
 | 225 |  * and to follow DOCSIS 1.0 (cable modem standard). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 |  */ | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 227 | static int __init rndis_do_config(struct usb_configuration *c) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 229 | 	/* FIXME alloc iConfiguration string, set it in c->strings */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 231 | 	if (gadget_is_otg(c->cdev->gadget)) { | 
 | 232 | 		c->descriptors = otg_desc; | 
 | 233 | 		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | 	} | 
 | 235 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 236 | 	return rndis_bind_config(c, hostaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } | 
 | 238 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 239 | static struct usb_configuration rndis_config_driver = { | 
 | 240 | 	.label			= "RNDIS", | 
 | 241 | 	.bind			= rndis_do_config, | 
 | 242 | 	.bConfigurationValue	= 2, | 
 | 243 | 	/* .iConfiguration = DYNAMIC */ | 
 | 244 | 	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER, | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 245 | }; | 
 | 246 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /*-------------------------------------------------------------------------*/ | 
 | 248 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 249 | /* | 
 | 250 |  * We _always_ have an ECM or CDC Subset configuration. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 |  */ | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 252 | static int __init eth_do_config(struct usb_configuration *c) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 254 | 	/* FIXME alloc iConfiguration string, set it in c->strings */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 256 | 	if (gadget_is_otg(c->cdev->gadget)) { | 
 | 257 | 		c->descriptors = otg_desc; | 
 | 258 | 		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | 	} | 
 | 260 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 261 | 	if (can_support_ecm(c->cdev->gadget)) | 
 | 262 | 		return ecm_bind_config(c, hostaddr); | 
 | 263 | 	else | 
 | 264 | 		return geth_bind_config(c, hostaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } | 
 | 266 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 267 | static struct usb_configuration eth_config_driver = { | 
 | 268 | 	/* .label = f(hardware) */ | 
 | 269 | 	.bind			= eth_do_config, | 
 | 270 | 	.bConfigurationValue	= 1, | 
 | 271 | 	/* .iConfiguration = DYNAMIC */ | 
 | 272 | 	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER, | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 273 | }; | 
 | 274 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /*-------------------------------------------------------------------------*/ | 
 | 276 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 277 | static int __init eth_bind(struct usb_composite_dev *cdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 279 | 	int			gcnum; | 
 | 280 | 	struct usb_gadget	*gadget = cdev->gadget; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | 	int			status; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 282 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 283 | 	/* set up network link layer */ | 
 | 284 | 	status = gether_setup(cdev->gadget, hostaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 	if (status < 0) | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 286 | 		return status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 288 | 	/* set up main config label and device descriptor */ | 
 | 289 | 	if (can_support_ecm(cdev->gadget)) { | 
 | 290 | 		/* ECM */ | 
 | 291 | 		eth_config_driver.label = "CDC Ethernet (ECM)"; | 
 | 292 | 	} else { | 
 | 293 | 		/* CDC Subset */ | 
 | 294 | 		eth_config_driver.label = "CDC Subset/SAFE"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 |  | 
| David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 296 | 		device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM); | 
 | 297 | 		device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM); | 
 | 298 | 		if (!has_rndis()) | 
 | 299 | 			device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | 	} | 
 | 301 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 302 | 	if (has_rndis()) { | 
 | 303 | 		/* RNDIS plus ECM-or-Subset */ | 
| David Brownell | 4e19f22 | 2009-06-19 03:09:04 -0700 | [diff] [blame] | 304 | 		device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM); | 
 | 305 | 		device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM); | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 306 | 		device_desc.bNumConfigurations = 2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 	} | 
 | 308 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 309 | 	gcnum = usb_gadget_controller_number(gadget); | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 310 | 	if (gcnum >= 0) | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 311 | 		device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 312 | 	else { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 313 | 		/* We assume that can_support_ecm() tells the truth; | 
 | 314 | 		 * but if the controller isn't recognized at all then | 
 | 315 | 		 * that assumption is a bit more likely to be wrong. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | 		 */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 317 | 		dev_warn(&gadget->dev, | 
 | 318 | 				"controller '%s' not recognized; trying %s\n", | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 319 | 				gadget->name, | 
 | 320 | 				eth_config_driver.label); | 
 | 321 | 		device_desc.bcdDevice = | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 322 | 			cpu_to_le16(0x0300 | 0x0099); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | 	} | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 324 |  | 
 | 325 |  | 
 | 326 | 	/* Allocate string descriptor numbers ... note that string | 
 | 327 | 	 * contents can be overridden by the composite_dev glue. | 
 | 328 | 	 */ | 
 | 329 |  | 
 | 330 | 	/* device descriptor strings: manufacturer, product */ | 
 | 331 | 	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | 
| Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 332 | 		init_utsname()->sysname, init_utsname()->release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | 		gadget->name); | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 334 | 	status = usb_string_id(cdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | 	if (status < 0) | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 336 | 		goto fail; | 
 | 337 | 	strings_dev[STRING_MANUFACTURER_IDX].id = status; | 
 | 338 | 	device_desc.iManufacturer = status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 340 | 	status = usb_string_id(cdev); | 
 | 341 | 	if (status < 0) | 
 | 342 | 		goto fail; | 
 | 343 | 	strings_dev[STRING_PRODUCT_IDX].id = status; | 
 | 344 | 	device_desc.iProduct = status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 346 | 	/* register our configuration(s); RNDIS first, if it's used */ | 
 | 347 | 	if (has_rndis()) { | 
 | 348 | 		status = usb_add_config(cdev, &rndis_config_driver); | 
 | 349 | 		if (status < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 			goto fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 353 | 	status = usb_add_config(cdev, ð_config_driver); | 
 | 354 | 	if (status < 0) | 
 | 355 | 		goto fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 |  | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 357 | 	dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", | 
 | 358 | 			DRIVER_DESC); | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 359 |  | 
 | 360 | 	return 0; | 
 | 361 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | fail: | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 363 | 	gether_cleanup(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | 	return status; | 
 | 365 | } | 
 | 366 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 367 | static int __exit eth_unbind(struct usb_composite_dev *cdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 369 | 	gether_cleanup(); | 
 | 370 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 373 | static struct usb_composite_driver eth_driver = { | 
 | 374 | 	.name		= "g_ether", | 
 | 375 | 	.dev		= &device_desc, | 
 | 376 | 	.strings	= dev_strings, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | 	.bind		= eth_bind, | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 378 | 	.unbind		= __exit_p(eth_unbind), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | }; | 
 | 380 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 381 | MODULE_DESCRIPTION(PREFIX DRIVER_DESC); | 
 | 382 | MODULE_AUTHOR("David Brownell, Benedikt Spanger"); | 
 | 383 | MODULE_LICENSE("GPL"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 385 | static int __init init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 387 | 	return usb_composite_register(ð_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 389 | module_init(init); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 391 | static void __exit cleanup(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 393 | 	usb_composite_unregister(ð_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } | 
| David Brownell | 0391c82 | 2008-06-19 18:20:11 -0700 | [diff] [blame] | 395 | module_exit(cleanup); |