| 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 |  * | 
 | 4 |  * Copyright (C) 2003-2005 David Brownell | 
 | 5 |  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | 
 | 6 |  * | 
 | 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. | 
 | 11 |  * | 
 | 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. | 
 | 16 |  * | 
 | 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 | 
 | 20 |  */ | 
 | 21 |  | 
 | 22 |  | 
 | 23 | // #define DEBUG 1 | 
 | 24 | // #define VERBOSE | 
 | 25 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/module.h> | 
 | 27 | #include <linux/kernel.h> | 
 | 28 | #include <linux/delay.h> | 
 | 29 | #include <linux/ioport.h> | 
 | 30 | #include <linux/sched.h> | 
 | 31 | #include <linux/slab.h> | 
 | 32 | #include <linux/smp_lock.h> | 
 | 33 | #include <linux/errno.h> | 
 | 34 | #include <linux/init.h> | 
 | 35 | #include <linux/timer.h> | 
 | 36 | #include <linux/list.h> | 
 | 37 | #include <linux/interrupt.h> | 
 | 38 | #include <linux/utsname.h> | 
 | 39 | #include <linux/device.h> | 
 | 40 | #include <linux/moduleparam.h> | 
 | 41 | #include <linux/ctype.h> | 
 | 42 |  | 
 | 43 | #include <asm/byteorder.h> | 
 | 44 | #include <asm/io.h> | 
 | 45 | #include <asm/irq.h> | 
 | 46 | #include <asm/system.h> | 
 | 47 | #include <asm/uaccess.h> | 
 | 48 | #include <asm/unaligned.h> | 
 | 49 |  | 
 | 50 | #include <linux/usb_ch9.h> | 
| David Brownell | a8c28f2 | 2006-06-13 09:57:47 -0700 | [diff] [blame] | 51 | #include <linux/usb/cdc.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/usb_gadget.h> | 
 | 53 |  | 
 | 54 | #include <linux/random.h> | 
 | 55 | #include <linux/netdevice.h> | 
 | 56 | #include <linux/etherdevice.h> | 
 | 57 | #include <linux/ethtool.h> | 
 | 58 |  | 
 | 59 | #include "gadget_chips.h" | 
 | 60 |  | 
 | 61 | /*-------------------------------------------------------------------------*/ | 
 | 62 |  | 
 | 63 | /* | 
 | 64 |  * Ethernet gadget driver -- with CDC and non-CDC options | 
 | 65 |  * Builds on hardware support for a full duplex link. | 
 | 66 |  * | 
 | 67 |  * CDC Ethernet is the standard USB solution for sending Ethernet frames | 
 | 68 |  * using USB.  Real hardware tends to use the same framing protocol but look | 
 | 69 |  * different for control features.  This driver strongly prefers to use | 
 | 70 |  * this USB-IF standard as its open-systems interoperability solution; | 
 | 71 |  * most host side USB stacks (except from Microsoft) support it. | 
 | 72 |  * | 
 | 73 |  * There's some hardware that can't talk CDC.  We make that hardware | 
 | 74 |  * implement a "minimalist" vendor-agnostic CDC core:  same framing, but | 
 | 75 |  * link-level setup only requires activating the configuration. | 
 | 76 |  * Linux supports it, but other host operating systems may not. | 
 | 77 |  * (This is a subset of CDC Ethernet.) | 
 | 78 |  * | 
 | 79 |  * A third option is also in use.  Rather than CDC Ethernet, or something | 
 | 80 |  * simpler, Microsoft pushes their own approach: RNDIS.  The published | 
 | 81 |  * RNDIS specs are ambiguous and appear to be incomplete, and are also | 
 | 82 |  * needlessly complex. | 
 | 83 |  */ | 
 | 84 |  | 
 | 85 | #define DRIVER_DESC		"Ethernet Gadget" | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 86 | #define DRIVER_VERSION		"May Day 2005" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 |  | 
 | 88 | static const char shortname [] = "ether"; | 
 | 89 | static const char driver_desc [] = DRIVER_DESC; | 
 | 90 |  | 
 | 91 | #define RX_EXTRA	20		/* guard against rx overflows */ | 
 | 92 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #include "rndis.h" | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 94 |  | 
 | 95 | #ifndef	CONFIG_USB_ETH_RNDIS | 
 | 96 | #define rndis_uninit(x)		do{}while(0) | 
 | 97 | #define rndis_deregister(c)	do{}while(0) | 
 | 98 | #define rndis_exit()		do{}while(0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #endif | 
 | 100 |  | 
 | 101 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ | 
 | 102 | #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \ | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 103 | 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \ | 
 | 104 | 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \ | 
 | 105 | 			|USB_CDC_PACKET_TYPE_DIRECTED) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
 | 107 |  | 
 | 108 | /*-------------------------------------------------------------------------*/ | 
 | 109 |  | 
 | 110 | struct eth_dev { | 
 | 111 | 	spinlock_t		lock; | 
 | 112 | 	struct usb_gadget	*gadget; | 
 | 113 | 	struct usb_request	*req;		/* for control responses */ | 
 | 114 | 	struct usb_request	*stat_req;	/* for cdc & rndis status */ | 
 | 115 |  | 
 | 116 | 	u8			config; | 
 | 117 | 	struct usb_ep		*in_ep, *out_ep, *status_ep; | 
 | 118 | 	const struct usb_endpoint_descriptor | 
 | 119 | 				*in, *out, *status; | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 120 |  | 
 | 121 | 	spinlock_t		req_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 	struct list_head	tx_reqs, rx_reqs; | 
 | 123 |  | 
 | 124 | 	struct net_device	*net; | 
 | 125 | 	struct net_device_stats	stats; | 
 | 126 | 	atomic_t		tx_qlen; | 
 | 127 |  | 
 | 128 | 	struct work_struct	work; | 
 | 129 | 	unsigned		zlp:1; | 
 | 130 | 	unsigned		cdc:1; | 
 | 131 | 	unsigned		rndis:1; | 
 | 132 | 	unsigned		suspended:1; | 
 | 133 | 	u16			cdc_filter; | 
 | 134 | 	unsigned long		todo; | 
 | 135 | #define	WORK_RX_MEMORY		0 | 
 | 136 | 	int			rndis_config; | 
 | 137 | 	u8			host_mac [ETH_ALEN]; | 
 | 138 | }; | 
 | 139 |  | 
 | 140 | /* This version autoconfigures as much as possible at run-time. | 
 | 141 |  * | 
 | 142 |  * It also ASSUMES a self-powered device, without remote wakeup, | 
 | 143 |  * although remote wakeup support would make sense. | 
 | 144 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  | 
 | 146 | /*-------------------------------------------------------------------------*/ | 
 | 147 |  | 
 | 148 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!! | 
 | 149 |  * Instead:  allocate your own, using normal USB-IF procedures. | 
 | 150 |  */ | 
 | 151 |  | 
 | 152 | /* Thanks to NetChip Technologies for donating this product ID. | 
 | 153 |  * It's for devices with only CDC Ethernet configurations. | 
 | 154 |  */ | 
 | 155 | #define CDC_VENDOR_NUM	0x0525		/* NetChip */ | 
 | 156 | #define CDC_PRODUCT_NUM	0xa4a1		/* Linux-USB Ethernet Gadget */ | 
 | 157 |  | 
 | 158 | /* For hardware that can't talk CDC, we use the same vendor ID that | 
 | 159 |  * ARM Linux has used for ethernet-over-usb, both with sa1100 and | 
 | 160 |  * with pxa250.  We're protocol-compatible, if the host-side drivers | 
 | 161 |  * use the endpoint descriptors.  bcdDevice (version) is nonzero, so | 
 | 162 |  * drivers that need to hard-wire endpoint numbers have a hook. | 
 | 163 |  * | 
 | 164 |  * The protocol is a minimal subset of CDC Ether, which works on any bulk | 
 | 165 |  * hardware that's not deeply broken ... even on hardware that can't talk | 
 | 166 |  * RNDIS (like SA-1100, with no interrupt endpoint, or anything that | 
 | 167 |  * doesn't handle control-OUT). | 
 | 168 |  */ | 
 | 169 | #define	SIMPLE_VENDOR_NUM	0x049f | 
 | 170 | #define	SIMPLE_PRODUCT_NUM	0x505a | 
 | 171 |  | 
 | 172 | /* For hardware that can talk RNDIS and either of the above protocols, | 
 | 173 |  * use this ID ... the windows INF files will know it.  Unless it's | 
 | 174 |  * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose | 
 | 175 |  * the non-RNDIS configuration. | 
 | 176 |  */ | 
 | 177 | #define RNDIS_VENDOR_NUM	0x0525	/* NetChip */ | 
 | 178 | #define RNDIS_PRODUCT_NUM	0xa4a2	/* Ethernet/RNDIS Gadget */ | 
 | 179 |  | 
 | 180 |  | 
 | 181 | /* Some systems will want different product identifers published in the | 
 | 182 |  * device descriptor, either numbers or strings or both.  These string | 
 | 183 |  * parameters are in UTF-8 (superset of ASCII's 7 bit characters). | 
 | 184 |  */ | 
 | 185 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 186 | static ushort idVendor; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | module_param(idVendor, ushort, S_IRUGO); | 
 | 188 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | 
 | 189 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 190 | static ushort idProduct; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | module_param(idProduct, ushort, S_IRUGO); | 
 | 192 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | 
 | 193 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 194 | static ushort bcdDevice; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | module_param(bcdDevice, ushort, S_IRUGO); | 
 | 196 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | 
 | 197 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 198 | static char *iManufacturer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | module_param(iManufacturer, charp, S_IRUGO); | 
 | 200 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | 
 | 201 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 202 | static char *iProduct; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | module_param(iProduct, charp, S_IRUGO); | 
 | 204 | MODULE_PARM_DESC(iProduct, "USB Product string"); | 
 | 205 |  | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 206 | static char *iSerialNumber; | 
 | 207 | module_param(iSerialNumber, charp, S_IRUGO); | 
 | 208 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber"); | 
 | 209 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 211 | static char *dev_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | module_param(dev_addr, charp, S_IRUGO); | 
 | 213 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); | 
 | 214 |  | 
 | 215 | /* this address is invisible to ifconfig */ | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 216 | static char *host_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | module_param(host_addr, charp, S_IRUGO); | 
 | 218 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); | 
 | 219 |  | 
 | 220 |  | 
 | 221 | /*-------------------------------------------------------------------------*/ | 
 | 222 |  | 
 | 223 | /* Include CDC support if we could run on CDC-capable hardware. */ | 
 | 224 |  | 
 | 225 | #ifdef CONFIG_USB_GADGET_NET2280 | 
 | 226 | #define	DEV_CONFIG_CDC | 
 | 227 | #endif | 
 | 228 |  | 
 | 229 | #ifdef CONFIG_USB_GADGET_DUMMY_HCD | 
 | 230 | #define	DEV_CONFIG_CDC | 
 | 231 | #endif | 
 | 232 |  | 
 | 233 | #ifdef CONFIG_USB_GADGET_GOKU | 
 | 234 | #define	DEV_CONFIG_CDC | 
 | 235 | #endif | 
 | 236 |  | 
 | 237 | #ifdef CONFIG_USB_GADGET_LH7A40X | 
 | 238 | #define DEV_CONFIG_CDC | 
 | 239 | #endif | 
 | 240 |  | 
 | 241 | #ifdef CONFIG_USB_GADGET_MQ11XX | 
 | 242 | #define	DEV_CONFIG_CDC | 
 | 243 | #endif | 
 | 244 |  | 
 | 245 | #ifdef CONFIG_USB_GADGET_OMAP | 
 | 246 | #define	DEV_CONFIG_CDC | 
 | 247 | #endif | 
 | 248 |  | 
 | 249 | #ifdef CONFIG_USB_GADGET_N9604 | 
 | 250 | #define	DEV_CONFIG_CDC | 
 | 251 | #endif | 
 | 252 |  | 
 | 253 | #ifdef CONFIG_USB_GADGET_PXA27X | 
 | 254 | #define DEV_CONFIG_CDC | 
 | 255 | #endif | 
 | 256 |  | 
 | 257 | #ifdef CONFIG_USB_GADGET_AT91 | 
 | 258 | #define DEV_CONFIG_CDC | 
 | 259 | #endif | 
 | 260 |  | 
| David Brownell | 1c05ad4 | 2006-01-25 08:45:59 -0800 | [diff] [blame] | 261 | #ifdef CONFIG_USB_GADGET_MUSBHSFC | 
 | 262 | #define DEV_CONFIG_CDC | 
 | 263 | #endif | 
 | 264 |  | 
 | 265 | #ifdef CONFIG_USB_GADGET_MUSBHDRC | 
 | 266 | #define DEV_CONFIG_CDC | 
 | 267 | #endif | 
 | 268 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 |  | 
 | 270 | /* For CDC-incapable hardware, choose the simple cdc subset. | 
 | 271 |  * Anything that talks bulk (without notable bugs) can do this. | 
 | 272 |  */ | 
 | 273 | #ifdef CONFIG_USB_GADGET_PXA2XX | 
 | 274 | #define	DEV_CONFIG_SUBSET | 
 | 275 | #endif | 
 | 276 |  | 
 | 277 | #ifdef CONFIG_USB_GADGET_SH | 
 | 278 | #define	DEV_CONFIG_SUBSET | 
 | 279 | #endif | 
 | 280 |  | 
 | 281 | #ifdef CONFIG_USB_GADGET_SA1100 | 
 | 282 | /* use non-CDC for backwards compatibility */ | 
 | 283 | #define	DEV_CONFIG_SUBSET | 
 | 284 | #endif | 
 | 285 |  | 
 | 286 | #ifdef CONFIG_USB_GADGET_S3C2410 | 
 | 287 | #define DEV_CONFIG_CDC | 
 | 288 | #endif | 
 | 289 |  | 
 | 290 | /*-------------------------------------------------------------------------*/ | 
 | 291 |  | 
 | 292 | /* "main" config is either CDC, or its simple subset */ | 
 | 293 | static inline int is_cdc(struct eth_dev *dev) | 
 | 294 | { | 
 | 295 | #if	!defined(DEV_CONFIG_SUBSET) | 
 | 296 | 	return 1;		/* only cdc possible */ | 
 | 297 | #elif	!defined (DEV_CONFIG_CDC) | 
 | 298 | 	return 0;		/* only subset possible */ | 
 | 299 | #else | 
 | 300 | 	return dev->cdc;	/* depends on what hardware we found */ | 
 | 301 | #endif | 
 | 302 | } | 
 | 303 |  | 
 | 304 | /* "secondary" RNDIS config may sometimes be activated */ | 
 | 305 | static inline int rndis_active(struct eth_dev *dev) | 
 | 306 | { | 
 | 307 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 308 | 	return dev->rndis; | 
 | 309 | #else | 
 | 310 | 	return 0; | 
 | 311 | #endif | 
 | 312 | } | 
 | 313 |  | 
 | 314 | #define	subset_active(dev)	(!is_cdc(dev) && !rndis_active(dev)) | 
 | 315 | #define	cdc_active(dev)		( is_cdc(dev) && !rndis_active(dev)) | 
 | 316 |  | 
 | 317 |  | 
 | 318 |  | 
 | 319 | #define DEFAULT_QLEN	2	/* double buffering by default */ | 
 | 320 |  | 
 | 321 | /* peak bulk transfer bits-per-second */ | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 322 | #define	HS_BPS		(13 * 512 * 8 * 1000 * 8) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | #define	FS_BPS		(19 *  64 * 1 * 1000 * 8) | 
 | 324 |  | 
 | 325 | #ifdef CONFIG_USB_GADGET_DUALSPEED | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 326 | #define	DEVSPEED	USB_SPEED_HIGH | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 |  | 
 | 328 | static unsigned qmult = 5; | 
 | 329 | module_param (qmult, uint, S_IRUGO|S_IWUSR); | 
 | 330 |  | 
 | 331 |  | 
 | 332 | /* for dual-speed hardware, use deeper queues at highspeed */ | 
 | 333 | #define qlen(gadget) \ | 
 | 334 | 	(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) | 
 | 335 |  | 
 | 336 | /* also defer IRQs on highspeed TX */ | 
 | 337 | #define TX_DELAY	qmult | 
 | 338 |  | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 339 | static inline int BITRATE(struct usb_gadget *g) | 
 | 340 | { | 
 | 341 | 	return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; | 
 | 342 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 |  | 
 | 344 | #else	/* full speed (low speed doesn't do bulk) */ | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 345 | #define	DEVSPEED	USB_SPEED_FULL | 
 | 346 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | #define qlen(gadget) DEFAULT_QLEN | 
 | 348 |  | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 349 | static inline int BITRATE(struct usb_gadget *g) | 
 | 350 | { | 
 | 351 | 	return FS_BPS; | 
 | 352 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | #endif | 
 | 354 |  | 
 | 355 |  | 
 | 356 | /*-------------------------------------------------------------------------*/ | 
 | 357 |  | 
 | 358 | #define xprintk(d,level,fmt,args...) \ | 
 | 359 | 	printk(level "%s: " fmt , (d)->net->name , ## args) | 
 | 360 |  | 
 | 361 | #ifdef DEBUG | 
 | 362 | #undef DEBUG | 
 | 363 | #define DEBUG(dev,fmt,args...) \ | 
 | 364 | 	xprintk(dev , KERN_DEBUG , fmt , ## args) | 
 | 365 | #else | 
 | 366 | #define DEBUG(dev,fmt,args...) \ | 
 | 367 | 	do { } while (0) | 
 | 368 | #endif /* DEBUG */ | 
 | 369 |  | 
 | 370 | #ifdef VERBOSE | 
 | 371 | #define VDEBUG	DEBUG | 
 | 372 | #else | 
 | 373 | #define VDEBUG(dev,fmt,args...) \ | 
 | 374 | 	do { } while (0) | 
 | 375 | #endif /* DEBUG */ | 
 | 376 |  | 
 | 377 | #define ERROR(dev,fmt,args...) \ | 
 | 378 | 	xprintk(dev , KERN_ERR , fmt , ## args) | 
 | 379 | #define WARN(dev,fmt,args...) \ | 
 | 380 | 	xprintk(dev , KERN_WARNING , fmt , ## args) | 
 | 381 | #define INFO(dev,fmt,args...) \ | 
 | 382 | 	xprintk(dev , KERN_INFO , fmt , ## args) | 
 | 383 |  | 
 | 384 | /*-------------------------------------------------------------------------*/ | 
 | 385 |  | 
 | 386 | /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly | 
 | 387 |  * ep0 implementation:  descriptors, config management, setup(). | 
 | 388 |  * also optional class-specific notification interrupt transfer. | 
 | 389 |  */ | 
 | 390 |  | 
 | 391 | /* | 
 | 392 |  * DESCRIPTORS ... most are static, but strings and (full) configuration | 
 | 393 |  * descriptors are built on demand.  For now we do either full CDC, or | 
 | 394 |  * our simple subset, with RNDIS as an optional second configuration. | 
 | 395 |  * | 
 | 396 |  * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet.  But | 
 | 397 |  * the class descriptors match a modem (they're ignored; it's really just | 
 | 398 |  * Ethernet functionality), they don't need the NOP altsetting, and the | 
 | 399 |  * status transfer endpoint isn't optional. | 
 | 400 |  */ | 
 | 401 |  | 
 | 402 | #define STRING_MANUFACTURER		1 | 
 | 403 | #define STRING_PRODUCT			2 | 
 | 404 | #define STRING_ETHADDR			3 | 
 | 405 | #define STRING_DATA			4 | 
 | 406 | #define STRING_CONTROL			5 | 
 | 407 | #define STRING_RNDIS_CONTROL		6 | 
 | 408 | #define STRING_CDC			7 | 
 | 409 | #define STRING_SUBSET			8 | 
 | 410 | #define STRING_RNDIS			9 | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 411 | #define STRING_SERIALNUMBER		10 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 |  | 
| David Brownell | 340600a | 2005-04-28 13:45:25 -0700 | [diff] [blame] | 413 | /* holds our biggest descriptor (or RNDIS response) */ | 
 | 414 | #define USB_BUFSIZ	256 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 |  | 
 | 416 | /* | 
 | 417 |  * This device advertises one configuration, eth_config, unless RNDIS | 
 | 418 |  * is enabled (rndis_config) on hardware supporting at least two configs. | 
 | 419 |  * | 
 | 420 |  * NOTE:  Controllers like superh_udc should probably be able to use | 
 | 421 |  * an RNDIS-only configuration. | 
 | 422 |  * | 
 | 423 |  * FIXME define some higher-powered configurations to make it easier | 
 | 424 |  * to recharge batteries ... | 
 | 425 |  */ | 
 | 426 |  | 
 | 427 | #define DEV_CONFIG_VALUE	1	/* cdc or subset */ | 
 | 428 | #define DEV_RNDIS_CONFIG_VALUE	2	/* rndis; optional */ | 
 | 429 |  | 
 | 430 | static struct usb_device_descriptor | 
 | 431 | device_desc = { | 
 | 432 | 	.bLength =		sizeof device_desc, | 
 | 433 | 	.bDescriptorType =	USB_DT_DEVICE, | 
 | 434 |  | 
 | 435 | 	.bcdUSB =		__constant_cpu_to_le16 (0x0200), | 
 | 436 |  | 
 | 437 | 	.bDeviceClass =		USB_CLASS_COMM, | 
 | 438 | 	.bDeviceSubClass =	0, | 
 | 439 | 	.bDeviceProtocol =	0, | 
 | 440 |  | 
 | 441 | 	.idVendor =		__constant_cpu_to_le16 (CDC_VENDOR_NUM), | 
 | 442 | 	.idProduct =		__constant_cpu_to_le16 (CDC_PRODUCT_NUM), | 
 | 443 | 	.iManufacturer =	STRING_MANUFACTURER, | 
 | 444 | 	.iProduct =		STRING_PRODUCT, | 
 | 445 | 	.bNumConfigurations =	1, | 
 | 446 | }; | 
 | 447 |  | 
 | 448 | static struct usb_otg_descriptor | 
 | 449 | otg_descriptor = { | 
 | 450 | 	.bLength =		sizeof otg_descriptor, | 
 | 451 | 	.bDescriptorType =	USB_DT_OTG, | 
 | 452 |  | 
 | 453 | 	.bmAttributes =		USB_OTG_SRP, | 
 | 454 | }; | 
 | 455 |  | 
 | 456 | static struct usb_config_descriptor | 
 | 457 | eth_config = { | 
 | 458 | 	.bLength =		sizeof eth_config, | 
 | 459 | 	.bDescriptorType =	USB_DT_CONFIG, | 
 | 460 |  | 
 | 461 | 	/* compute wTotalLength on the fly */ | 
 | 462 | 	.bNumInterfaces =	2, | 
 | 463 | 	.bConfigurationValue =	DEV_CONFIG_VALUE, | 
 | 464 | 	.iConfiguration =	STRING_CDC, | 
 | 465 | 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | 
 | 466 | 	.bMaxPower =		50, | 
 | 467 | }; | 
 | 468 |  | 
 | 469 | #ifdef	CONFIG_USB_ETH_RNDIS | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 470 | static struct usb_config_descriptor | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | rndis_config = { | 
 | 472 | 	.bLength =              sizeof rndis_config, | 
 | 473 | 	.bDescriptorType =      USB_DT_CONFIG, | 
 | 474 |  | 
 | 475 | 	/* compute wTotalLength on the fly */ | 
 | 476 | 	.bNumInterfaces =       2, | 
 | 477 | 	.bConfigurationValue =  DEV_RNDIS_CONFIG_VALUE, | 
 | 478 | 	.iConfiguration =       STRING_RNDIS, | 
 | 479 | 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, | 
 | 480 | 	.bMaxPower =            50, | 
 | 481 | }; | 
 | 482 | #endif | 
 | 483 |  | 
 | 484 | /* | 
 | 485 |  * Compared to the simple CDC subset, the full CDC Ethernet model adds | 
 | 486 |  * three class descriptors, two interface descriptors, optional status | 
 | 487 |  * endpoint.  Both have a "data" interface and two bulk endpoints. | 
 | 488 |  * There are also differences in how control requests are handled. | 
 | 489 |  * | 
 | 490 |  * RNDIS shares a lot with CDC-Ethernet, since it's a variant of | 
 | 491 |  * the CDC-ACM (modem) spec. | 
 | 492 |  */ | 
 | 493 |  | 
 | 494 | #ifdef	DEV_CONFIG_CDC | 
 | 495 | static struct usb_interface_descriptor | 
 | 496 | control_intf = { | 
 | 497 | 	.bLength =		sizeof control_intf, | 
 | 498 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 499 |  | 
 | 500 | 	.bInterfaceNumber =	0, | 
 | 501 | 	/* status endpoint is optional; this may be patched later */ | 
 | 502 | 	.bNumEndpoints =	1, | 
 | 503 | 	.bInterfaceClass =	USB_CLASS_COMM, | 
 | 504 | 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET, | 
 | 505 | 	.bInterfaceProtocol =	USB_CDC_PROTO_NONE, | 
 | 506 | 	.iInterface =		STRING_CONTROL, | 
 | 507 | }; | 
 | 508 | #endif | 
 | 509 |  | 
 | 510 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 511 | static const struct usb_interface_descriptor | 
 | 512 | rndis_control_intf = { | 
 | 513 | 	.bLength =              sizeof rndis_control_intf, | 
 | 514 | 	.bDescriptorType =      USB_DT_INTERFACE, | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 515 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | 	.bInterfaceNumber =     0, | 
 | 517 | 	.bNumEndpoints =        1, | 
 | 518 | 	.bInterfaceClass =      USB_CLASS_COMM, | 
 | 519 | 	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM, | 
 | 520 | 	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR, | 
 | 521 | 	.iInterface =           STRING_RNDIS_CONTROL, | 
 | 522 | }; | 
 | 523 | #endif | 
 | 524 |  | 
 | 525 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
 | 526 |  | 
 | 527 | static const struct usb_cdc_header_desc header_desc = { | 
 | 528 | 	.bLength =		sizeof header_desc, | 
 | 529 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
 | 530 | 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE, | 
 | 531 |  | 
 | 532 | 	.bcdCDC =		__constant_cpu_to_le16 (0x0110), | 
 | 533 | }; | 
 | 534 |  | 
 | 535 | static const struct usb_cdc_union_desc union_desc = { | 
 | 536 | 	.bLength =		sizeof union_desc, | 
 | 537 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
 | 538 | 	.bDescriptorSubType =	USB_CDC_UNION_TYPE, | 
 | 539 |  | 
 | 540 | 	.bMasterInterface0 =	0,	/* index of control interface */ | 
 | 541 | 	.bSlaveInterface0 =	1,	/* index of DATA interface */ | 
 | 542 | }; | 
 | 543 |  | 
 | 544 | #endif	/* CDC || RNDIS */ | 
 | 545 |  | 
 | 546 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 547 |  | 
 | 548 | static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 549 | 	.bLength =		sizeof call_mgmt_descriptor, | 
 | 550 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
 | 551 | 	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 553 | 	.bmCapabilities =	0x00, | 
 | 554 | 	.bDataInterface =	0x01, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | }; | 
 | 556 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 557 | static const struct usb_cdc_acm_descriptor acm_descriptor = { | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 558 | 	.bLength =		sizeof acm_descriptor, | 
 | 559 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
 | 560 | 	.bDescriptorSubType =	USB_CDC_ACM_TYPE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 562 | 	.bmCapabilities =	0x00, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | }; | 
 | 564 |  | 
 | 565 | #endif | 
 | 566 |  | 
 | 567 | #ifdef	DEV_CONFIG_CDC | 
 | 568 |  | 
 | 569 | static const struct usb_cdc_ether_desc ether_desc = { | 
 | 570 | 	.bLength =		sizeof ether_desc, | 
 | 571 | 	.bDescriptorType =	USB_DT_CS_INTERFACE, | 
 | 572 | 	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE, | 
 | 573 |  | 
 | 574 | 	/* this descriptor actually adds value, surprise! */ | 
 | 575 | 	.iMACAddress =		STRING_ETHADDR, | 
 | 576 | 	.bmEthernetStatistics =	__constant_cpu_to_le32 (0), /* no statistics */ | 
 | 577 | 	.wMaxSegmentSize =	__constant_cpu_to_le16 (ETH_FRAME_LEN), | 
 | 578 | 	.wNumberMCFilters =	__constant_cpu_to_le16 (0), | 
 | 579 | 	.bNumberPowerFilters =	0, | 
 | 580 | }; | 
 | 581 |  | 
 | 582 | #endif | 
 | 583 |  | 
 | 584 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
 | 585 |  | 
 | 586 | /* include the status endpoint if we can, even where it's optional. | 
 | 587 |  * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one | 
| Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 588 |  * packet, to simplify cancellation; and a big transfer interval, to | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 |  * waste less bandwidth. | 
 | 590 |  * | 
 | 591 |  * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even | 
 | 592 |  * if they ignore the connect/disconnect notifications that real aether | 
 | 593 |  * can provide.  more advanced cdc configurations might want to support | 
 | 594 |  * encapsulated commands (vendor-specific, using control-OUT). | 
 | 595 |  * | 
 | 596 |  * RNDIS requires the status endpoint, since it uses that encapsulation | 
 | 597 |  * mechanism for its funky RPC scheme. | 
 | 598 |  */ | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 599 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */ | 
 | 601 | #define STATUS_BYTECOUNT		16	/* 8 byte header + data */ | 
 | 602 |  | 
 | 603 | static struct usb_endpoint_descriptor | 
 | 604 | fs_status_desc = { | 
 | 605 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 606 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 607 |  | 
 | 608 | 	.bEndpointAddress =	USB_DIR_IN, | 
 | 609 | 	.bmAttributes =		USB_ENDPOINT_XFER_INT, | 
 | 610 | 	.wMaxPacketSize =	__constant_cpu_to_le16 (STATUS_BYTECOUNT), | 
 | 611 | 	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC, | 
 | 612 | }; | 
 | 613 | #endif | 
 | 614 |  | 
 | 615 | #ifdef	DEV_CONFIG_CDC | 
 | 616 |  | 
 | 617 | /* the default data interface has no endpoints ... */ | 
 | 618 |  | 
 | 619 | static const struct usb_interface_descriptor | 
 | 620 | data_nop_intf = { | 
 | 621 | 	.bLength =		sizeof data_nop_intf, | 
 | 622 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 623 |  | 
 | 624 | 	.bInterfaceNumber =	1, | 
 | 625 | 	.bAlternateSetting =	0, | 
 | 626 | 	.bNumEndpoints =	0, | 
 | 627 | 	.bInterfaceClass =	USB_CLASS_CDC_DATA, | 
 | 628 | 	.bInterfaceSubClass =	0, | 
 | 629 | 	.bInterfaceProtocol =	0, | 
 | 630 | }; | 
 | 631 |  | 
 | 632 | /* ... but the "real" data interface has two bulk endpoints */ | 
 | 633 |  | 
 | 634 | static const struct usb_interface_descriptor | 
 | 635 | data_intf = { | 
 | 636 | 	.bLength =		sizeof data_intf, | 
 | 637 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 638 |  | 
 | 639 | 	.bInterfaceNumber =	1, | 
 | 640 | 	.bAlternateSetting =	1, | 
 | 641 | 	.bNumEndpoints =	2, | 
 | 642 | 	.bInterfaceClass =	USB_CLASS_CDC_DATA, | 
 | 643 | 	.bInterfaceSubClass =	0, | 
 | 644 | 	.bInterfaceProtocol =	0, | 
 | 645 | 	.iInterface =		STRING_DATA, | 
 | 646 | }; | 
 | 647 |  | 
 | 648 | #endif | 
 | 649 |  | 
 | 650 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 651 |  | 
 | 652 | /* RNDIS doesn't activate by changing to the "real" altsetting */ | 
 | 653 |  | 
 | 654 | static const struct usb_interface_descriptor | 
 | 655 | rndis_data_intf = { | 
 | 656 | 	.bLength =		sizeof rndis_data_intf, | 
 | 657 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 658 |  | 
 | 659 | 	.bInterfaceNumber =	1, | 
 | 660 | 	.bAlternateSetting =	0, | 
 | 661 | 	.bNumEndpoints =	2, | 
 | 662 | 	.bInterfaceClass =	USB_CLASS_CDC_DATA, | 
 | 663 | 	.bInterfaceSubClass =	0, | 
 | 664 | 	.bInterfaceProtocol =	0, | 
 | 665 | 	.iInterface =		STRING_DATA, | 
 | 666 | }; | 
 | 667 |  | 
 | 668 | #endif | 
 | 669 |  | 
 | 670 | #ifdef DEV_CONFIG_SUBSET | 
 | 671 |  | 
 | 672 | /* | 
 | 673 |  * "Simple" CDC-subset option is a simple vendor-neutral model that most | 
 | 674 |  * full speed controllers can handle:  one interface, two bulk endpoints. | 
 | 675 |  */ | 
 | 676 |  | 
 | 677 | static const struct usb_interface_descriptor | 
 | 678 | subset_data_intf = { | 
 | 679 | 	.bLength =		sizeof subset_data_intf, | 
 | 680 | 	.bDescriptorType =	USB_DT_INTERFACE, | 
 | 681 |  | 
 | 682 | 	.bInterfaceNumber =	0, | 
 | 683 | 	.bAlternateSetting =	0, | 
 | 684 | 	.bNumEndpoints =	2, | 
 | 685 | 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC, | 
 | 686 | 	.bInterfaceSubClass =	0, | 
 | 687 | 	.bInterfaceProtocol =	0, | 
 | 688 | 	.iInterface =		STRING_DATA, | 
 | 689 | }; | 
 | 690 |  | 
 | 691 | #endif	/* SUBSET */ | 
 | 692 |  | 
 | 693 |  | 
 | 694 | static struct usb_endpoint_descriptor | 
 | 695 | fs_source_desc = { | 
 | 696 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 697 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 698 |  | 
 | 699 | 	.bEndpointAddress =	USB_DIR_IN, | 
 | 700 | 	.bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
 | 701 | }; | 
 | 702 |  | 
 | 703 | static struct usb_endpoint_descriptor | 
 | 704 | fs_sink_desc = { | 
 | 705 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 706 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 707 |  | 
 | 708 | 	.bEndpointAddress =	USB_DIR_OUT, | 
 | 709 | 	.bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
 | 710 | }; | 
 | 711 |  | 
 | 712 | static const struct usb_descriptor_header *fs_eth_function [11] = { | 
 | 713 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
 | 714 | #ifdef DEV_CONFIG_CDC | 
 | 715 | 	/* "cdc" mode descriptors */ | 
 | 716 | 	(struct usb_descriptor_header *) &control_intf, | 
 | 717 | 	(struct usb_descriptor_header *) &header_desc, | 
 | 718 | 	(struct usb_descriptor_header *) &union_desc, | 
 | 719 | 	(struct usb_descriptor_header *) ðer_desc, | 
 | 720 | 	/* NOTE: status endpoint may need to be removed */ | 
 | 721 | 	(struct usb_descriptor_header *) &fs_status_desc, | 
 | 722 | 	/* data interface, with altsetting */ | 
 | 723 | 	(struct usb_descriptor_header *) &data_nop_intf, | 
 | 724 | 	(struct usb_descriptor_header *) &data_intf, | 
 | 725 | 	(struct usb_descriptor_header *) &fs_source_desc, | 
 | 726 | 	(struct usb_descriptor_header *) &fs_sink_desc, | 
 | 727 | 	NULL, | 
 | 728 | #endif /* DEV_CONFIG_CDC */ | 
 | 729 | }; | 
 | 730 |  | 
 | 731 | static inline void __init fs_subset_descriptors(void) | 
 | 732 | { | 
 | 733 | #ifdef DEV_CONFIG_SUBSET | 
 | 734 | 	fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | 
 | 735 | 	fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc; | 
 | 736 | 	fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc; | 
 | 737 | 	fs_eth_function[4] = NULL; | 
 | 738 | #else | 
 | 739 | 	fs_eth_function[1] = NULL; | 
 | 740 | #endif | 
 | 741 | } | 
 | 742 |  | 
 | 743 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 744 | static const struct usb_descriptor_header *fs_rndis_function [] = { | 
 | 745 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
 | 746 | 	/* control interface matches ACM, not Ethernet */ | 
 | 747 | 	(struct usb_descriptor_header *) &rndis_control_intf, | 
 | 748 | 	(struct usb_descriptor_header *) &header_desc, | 
 | 749 | 	(struct usb_descriptor_header *) &call_mgmt_descriptor, | 
 | 750 | 	(struct usb_descriptor_header *) &acm_descriptor, | 
 | 751 | 	(struct usb_descriptor_header *) &union_desc, | 
 | 752 | 	(struct usb_descriptor_header *) &fs_status_desc, | 
 | 753 | 	/* data interface has no altsetting */ | 
 | 754 | 	(struct usb_descriptor_header *) &rndis_data_intf, | 
 | 755 | 	(struct usb_descriptor_header *) &fs_source_desc, | 
 | 756 | 	(struct usb_descriptor_header *) &fs_sink_desc, | 
 | 757 | 	NULL, | 
 | 758 | }; | 
 | 759 | #endif | 
 | 760 |  | 
 | 761 | #ifdef	CONFIG_USB_GADGET_DUALSPEED | 
 | 762 |  | 
 | 763 | /* | 
 | 764 |  * usb 2.0 devices need to expose both high speed and full speed | 
 | 765 |  * descriptors, unless they only run at full speed. | 
 | 766 |  */ | 
 | 767 |  | 
 | 768 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
 | 769 | static struct usb_endpoint_descriptor | 
 | 770 | hs_status_desc = { | 
 | 771 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 772 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 773 |  | 
 | 774 | 	.bmAttributes =		USB_ENDPOINT_XFER_INT, | 
 | 775 | 	.wMaxPacketSize =	__constant_cpu_to_le16 (STATUS_BYTECOUNT), | 
 | 776 | 	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4, | 
 | 777 | }; | 
 | 778 | #endif /* DEV_CONFIG_CDC */ | 
 | 779 |  | 
 | 780 | static struct usb_endpoint_descriptor | 
 | 781 | hs_source_desc = { | 
 | 782 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 783 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 784 |  | 
 | 785 | 	.bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
 | 786 | 	.wMaxPacketSize =	__constant_cpu_to_le16 (512), | 
 | 787 | }; | 
 | 788 |  | 
 | 789 | static struct usb_endpoint_descriptor | 
 | 790 | hs_sink_desc = { | 
 | 791 | 	.bLength =		USB_DT_ENDPOINT_SIZE, | 
 | 792 | 	.bDescriptorType =	USB_DT_ENDPOINT, | 
 | 793 |  | 
 | 794 | 	.bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
 | 795 | 	.wMaxPacketSize =	__constant_cpu_to_le16 (512), | 
 | 796 | }; | 
 | 797 |  | 
 | 798 | static struct usb_qualifier_descriptor | 
 | 799 | dev_qualifier = { | 
 | 800 | 	.bLength =		sizeof dev_qualifier, | 
 | 801 | 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER, | 
 | 802 |  | 
 | 803 | 	.bcdUSB =		__constant_cpu_to_le16 (0x0200), | 
 | 804 | 	.bDeviceClass =		USB_CLASS_COMM, | 
 | 805 |  | 
 | 806 | 	.bNumConfigurations =	1, | 
 | 807 | }; | 
 | 808 |  | 
 | 809 | static const struct usb_descriptor_header *hs_eth_function [11] = { | 
 | 810 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
 | 811 | #ifdef DEV_CONFIG_CDC | 
 | 812 | 	/* "cdc" mode descriptors */ | 
 | 813 | 	(struct usb_descriptor_header *) &control_intf, | 
 | 814 | 	(struct usb_descriptor_header *) &header_desc, | 
 | 815 | 	(struct usb_descriptor_header *) &union_desc, | 
 | 816 | 	(struct usb_descriptor_header *) ðer_desc, | 
 | 817 | 	/* NOTE: status endpoint may need to be removed */ | 
 | 818 | 	(struct usb_descriptor_header *) &hs_status_desc, | 
 | 819 | 	/* data interface, with altsetting */ | 
 | 820 | 	(struct usb_descriptor_header *) &data_nop_intf, | 
 | 821 | 	(struct usb_descriptor_header *) &data_intf, | 
 | 822 | 	(struct usb_descriptor_header *) &hs_source_desc, | 
 | 823 | 	(struct usb_descriptor_header *) &hs_sink_desc, | 
 | 824 | 	NULL, | 
 | 825 | #endif /* DEV_CONFIG_CDC */ | 
 | 826 | }; | 
 | 827 |  | 
 | 828 | static inline void __init hs_subset_descriptors(void) | 
 | 829 | { | 
 | 830 | #ifdef DEV_CONFIG_SUBSET | 
 | 831 | 	hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; | 
 | 832 | 	hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc; | 
 | 833 | 	hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc; | 
 | 834 | 	hs_eth_function[4] = NULL; | 
 | 835 | #else | 
 | 836 | 	hs_eth_function[1] = NULL; | 
 | 837 | #endif | 
 | 838 | } | 
 | 839 |  | 
 | 840 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 841 | static const struct usb_descriptor_header *hs_rndis_function [] = { | 
 | 842 | 	(struct usb_descriptor_header *) &otg_descriptor, | 
 | 843 | 	/* control interface matches ACM, not Ethernet */ | 
 | 844 | 	(struct usb_descriptor_header *) &rndis_control_intf, | 
 | 845 | 	(struct usb_descriptor_header *) &header_desc, | 
 | 846 | 	(struct usb_descriptor_header *) &call_mgmt_descriptor, | 
 | 847 | 	(struct usb_descriptor_header *) &acm_descriptor, | 
 | 848 | 	(struct usb_descriptor_header *) &union_desc, | 
 | 849 | 	(struct usb_descriptor_header *) &hs_status_desc, | 
 | 850 | 	/* data interface has no altsetting */ | 
 | 851 | 	(struct usb_descriptor_header *) &rndis_data_intf, | 
 | 852 | 	(struct usb_descriptor_header *) &hs_source_desc, | 
 | 853 | 	(struct usb_descriptor_header *) &hs_sink_desc, | 
 | 854 | 	NULL, | 
 | 855 | }; | 
 | 856 | #endif | 
 | 857 |  | 
 | 858 |  | 
 | 859 | /* maxpacket and other transfer characteristics vary by speed. */ | 
 | 860 | #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs)) | 
 | 861 |  | 
 | 862 | #else | 
 | 863 |  | 
 | 864 | /* if there's no high speed support, maxpacket doesn't change. */ | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 865 | #define ep_desc(g,hs,fs) (((void)(g)), (fs)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 |  | 
 | 867 | static inline void __init hs_subset_descriptors(void) | 
 | 868 | { | 
 | 869 | } | 
 | 870 |  | 
 | 871 | #endif	/* !CONFIG_USB_GADGET_DUALSPEED */ | 
 | 872 |  | 
 | 873 | /*-------------------------------------------------------------------------*/ | 
 | 874 |  | 
 | 875 | /* descriptors that are built on-demand */ | 
 | 876 |  | 
 | 877 | static char				manufacturer [50]; | 
 | 878 | static char				product_desc [40] = DRIVER_DESC; | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 879 | static char				serial_number [20]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 |  | 
 | 881 | #ifdef	DEV_CONFIG_CDC | 
 | 882 | /* address that the host will use ... usually assigned at random */ | 
 | 883 | static char				ethaddr [2 * ETH_ALEN + 1]; | 
 | 884 | #endif | 
 | 885 |  | 
 | 886 | /* static strings, in UTF-8 */ | 
 | 887 | static struct usb_string		strings [] = { | 
 | 888 | 	{ STRING_MANUFACTURER,	manufacturer, }, | 
 | 889 | 	{ STRING_PRODUCT,	product_desc, }, | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 890 | 	{ STRING_SERIALNUMBER,	serial_number, }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | 	{ STRING_DATA,		"Ethernet Data", }, | 
 | 892 | #ifdef	DEV_CONFIG_CDC | 
 | 893 | 	{ STRING_CDC,		"CDC Ethernet", }, | 
 | 894 | 	{ STRING_ETHADDR,	ethaddr, }, | 
 | 895 | 	{ STRING_CONTROL,	"CDC Communications Control", }, | 
 | 896 | #endif | 
 | 897 | #ifdef	DEV_CONFIG_SUBSET | 
 | 898 | 	{ STRING_SUBSET,	"CDC Ethernet Subset", }, | 
 | 899 | #endif | 
 | 900 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 901 | 	{ STRING_RNDIS,		"RNDIS", }, | 
 | 902 | 	{ STRING_RNDIS_CONTROL,	"RNDIS Communications Control", }, | 
 | 903 | #endif | 
 | 904 | 	{  }		/* end of list */ | 
 | 905 | }; | 
 | 906 |  | 
 | 907 | static struct usb_gadget_strings	stringtab = { | 
 | 908 | 	.language	= 0x0409,	/* en-us */ | 
 | 909 | 	.strings	= strings, | 
 | 910 | }; | 
 | 911 |  | 
 | 912 | /* | 
 | 913 |  * one config, two interfaces:  control, data. | 
 | 914 |  * complications: class descriptors, and an altsetting. | 
 | 915 |  */ | 
 | 916 | static int | 
 | 917 | config_buf (enum usb_device_speed speed, | 
 | 918 | 	u8 *buf, u8 type, | 
 | 919 | 	unsigned index, int is_otg) | 
 | 920 | { | 
 | 921 | 	int					len; | 
 | 922 | 	const struct usb_config_descriptor	*config; | 
 | 923 | 	const struct usb_descriptor_header	**function; | 
 | 924 | #ifdef CONFIG_USB_GADGET_DUALSPEED | 
 | 925 | 	int				hs = (speed == USB_SPEED_HIGH); | 
 | 926 |  | 
 | 927 | 	if (type == USB_DT_OTHER_SPEED_CONFIG) | 
 | 928 | 		hs = !hs; | 
 | 929 | #define which_fn(t)	(hs ? hs_ ## t ## _function : fs_ ## t ## _function) | 
 | 930 | #else | 
 | 931 | #define	which_fn(t)	(fs_ ## t ## _function) | 
 | 932 | #endif | 
 | 933 |  | 
 | 934 | 	if (index >= device_desc.bNumConfigurations) | 
 | 935 | 		return -EINVAL; | 
 | 936 |  | 
 | 937 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 938 | 	/* list the RNDIS config first, to make Microsoft's drivers | 
 | 939 | 	 * happy. DOCSIS 1.0 needs this too. | 
 | 940 | 	 */ | 
 | 941 | 	if (device_desc.bNumConfigurations == 2 && index == 0) { | 
 | 942 | 		config = &rndis_config; | 
 | 943 | 		function = which_fn (rndis); | 
 | 944 | 	} else | 
 | 945 | #endif | 
 | 946 | 	{ | 
 | 947 | 		config = ð_config; | 
 | 948 | 		function = which_fn (eth); | 
 | 949 | 	} | 
 | 950 |  | 
 | 951 | 	/* for now, don't advertise srp-only devices */ | 
 | 952 | 	if (!is_otg) | 
 | 953 | 		function++; | 
 | 954 |  | 
 | 955 | 	len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function); | 
 | 956 | 	if (len < 0) | 
 | 957 | 		return len; | 
 | 958 | 	((struct usb_config_descriptor *) buf)->bDescriptorType = type; | 
 | 959 | 	return len; | 
 | 960 | } | 
 | 961 |  | 
 | 962 | /*-------------------------------------------------------------------------*/ | 
 | 963 |  | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 964 | static void eth_start (struct eth_dev *dev, gfp_t gfp_flags); | 
 | 965 | static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 967 | static int | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 968 | set_ether_config (struct eth_dev *dev, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 970 | 	int					result = 0; | 
 | 971 | 	struct usb_gadget			*gadget = dev->gadget; | 
 | 972 |  | 
| Ian Campbell | e828264 | 2005-06-29 10:20:29 +0100 | [diff] [blame] | 973 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 974 | 	/* status endpoint used for RNDIS and (optionally) CDC */ | 
 | 975 | 	if (!subset_active(dev) && dev->status_ep) { | 
 | 976 | 		dev->status = ep_desc (gadget, &hs_status_desc, | 
 | 977 | 						&fs_status_desc); | 
 | 978 | 		dev->status_ep->driver_data = dev; | 
 | 979 |  | 
 | 980 | 		result = usb_ep_enable (dev->status_ep, dev->status); | 
 | 981 | 		if (result != 0) { | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 982 | 			DEBUG (dev, "enable %s --> %d\n", | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 983 | 				dev->status_ep->name, result); | 
 | 984 | 			goto done; | 
 | 985 | 		} | 
 | 986 | 	} | 
| Ian Campbell | e828264 | 2005-06-29 10:20:29 +0100 | [diff] [blame] | 987 | #endif | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 988 |  | 
 | 989 | 	dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc); | 
 | 990 | 	dev->in_ep->driver_data = dev; | 
 | 991 |  | 
 | 992 | 	dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc); | 
 | 993 | 	dev->out_ep->driver_data = dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 |  | 
 | 995 | 	/* With CDC,  the host isn't allowed to use these two data | 
 | 996 | 	 * endpoints in the default altsetting for the interface. | 
 | 997 | 	 * so we don't activate them yet.  Reset from SET_INTERFACE. | 
 | 998 | 	 * | 
 | 999 | 	 * Strictly speaking RNDIS should work the same: activation is | 
 | 1000 | 	 * a side effect of setting a packet filter.  Deactivation is | 
 | 1001 | 	 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. | 
 | 1002 | 	 */ | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1003 | 	if (!cdc_active(dev)) { | 
 | 1004 | 		result = usb_ep_enable (dev->in_ep, dev->in); | 
 | 1005 | 		if (result != 0) { | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1006 | 			DEBUG(dev, "enable %s --> %d\n", | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1007 | 				dev->in_ep->name, result); | 
 | 1008 | 			goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | 		} | 
 | 1010 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1011 | 		result = usb_ep_enable (dev->out_ep, dev->out); | 
 | 1012 | 		if (result != 0) { | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1013 | 			DEBUG (dev, "enable %s --> %d\n", | 
| Matt Reimer | 4186c29 | 2006-06-07 11:46:13 -0700 | [diff] [blame] | 1014 | 				dev->out_ep->name, result); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1015 | 			goto done; | 
 | 1016 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1019 | done: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | 	if (result == 0) | 
 | 1021 | 		result = alloc_requests (dev, qlen (gadget), gfp_flags); | 
 | 1022 |  | 
 | 1023 | 	/* on error, disable any endpoints  */ | 
 | 1024 | 	if (result < 0) { | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1025 | 		if (!subset_active(dev)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | 			(void) usb_ep_disable (dev->status_ep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | 		dev->status = NULL; | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1028 | 		(void) usb_ep_disable (dev->in_ep); | 
 | 1029 | 		(void) usb_ep_disable (dev->out_ep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | 		dev->in = NULL; | 
 | 1031 | 		dev->out = NULL; | 
 | 1032 | 	} else | 
 | 1033 |  | 
 | 1034 | 	/* activate non-CDC configs right away | 
 | 1035 | 	 * this isn't strictly according to the RNDIS spec | 
 | 1036 | 	 */ | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1037 | 	if (!cdc_active (dev)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | 		netif_carrier_on (dev->net); | 
 | 1039 | 		if (netif_running (dev->net)) { | 
 | 1040 | 			spin_unlock (&dev->lock); | 
 | 1041 | 			eth_start (dev, GFP_ATOMIC); | 
 | 1042 | 			spin_lock (&dev->lock); | 
 | 1043 | 		} | 
 | 1044 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 |  | 
 | 1046 | 	if (result == 0) | 
 | 1047 | 		DEBUG (dev, "qlen %d\n", qlen (gadget)); | 
 | 1048 |  | 
 | 1049 | 	/* caller is responsible for cleanup on error */ | 
 | 1050 | 	return result; | 
 | 1051 | } | 
 | 1052 |  | 
 | 1053 | static void eth_reset_config (struct eth_dev *dev) | 
 | 1054 | { | 
 | 1055 | 	struct usb_request	*req; | 
 | 1056 |  | 
 | 1057 | 	if (dev->config == 0) | 
 | 1058 | 		return; | 
 | 1059 |  | 
 | 1060 | 	DEBUG (dev, "%s\n", __FUNCTION__); | 
 | 1061 |  | 
 | 1062 | 	netif_stop_queue (dev->net); | 
 | 1063 | 	netif_carrier_off (dev->net); | 
| David Brownell | 340600a | 2005-04-28 13:45:25 -0700 | [diff] [blame] | 1064 | 	rndis_uninit(dev->rndis_config); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 |  | 
 | 1066 | 	/* disable endpoints, forcing (synchronous) completion of | 
 | 1067 | 	 * pending i/o.  then free the requests. | 
 | 1068 | 	 */ | 
 | 1069 | 	if (dev->in) { | 
 | 1070 | 		usb_ep_disable (dev->in_ep); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1071 | 		spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | 		while (likely (!list_empty (&dev->tx_reqs))) { | 
 | 1073 | 			req = container_of (dev->tx_reqs.next, | 
 | 1074 | 						struct usb_request, list); | 
 | 1075 | 			list_del (&req->list); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1076 |  | 
 | 1077 | 			spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | 			usb_ep_free_request (dev->in_ep, req); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1079 | 			spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | 		} | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1081 | 		spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | 	} | 
 | 1083 | 	if (dev->out) { | 
 | 1084 | 		usb_ep_disable (dev->out_ep); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1085 | 		spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | 		while (likely (!list_empty (&dev->rx_reqs))) { | 
 | 1087 | 			req = container_of (dev->rx_reqs.next, | 
 | 1088 | 						struct usb_request, list); | 
 | 1089 | 			list_del (&req->list); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1090 |  | 
 | 1091 | 			spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | 			usb_ep_free_request (dev->out_ep, req); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1093 | 			spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | 		} | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1095 | 		spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | 	} | 
 | 1097 |  | 
 | 1098 | 	if (dev->status) { | 
 | 1099 | 		usb_ep_disable (dev->status_ep); | 
 | 1100 | 	} | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1101 | 	dev->rndis = 0; | 
 | 1102 | 	dev->cdc_filter = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | 	dev->config = 0; | 
 | 1104 | } | 
 | 1105 |  | 
 | 1106 | /* change our operational config.  must agree with the code | 
 | 1107 |  * that returns config descriptors, and altsetting code. | 
 | 1108 |  */ | 
 | 1109 | static int | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1110 | eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | { | 
 | 1112 | 	int			result = 0; | 
 | 1113 | 	struct usb_gadget	*gadget = dev->gadget; | 
 | 1114 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | 	if (gadget_is_sa1100 (gadget) | 
 | 1116 | 			&& dev->config | 
 | 1117 | 			&& atomic_read (&dev->tx_qlen) != 0) { | 
 | 1118 | 		/* tx fifo is full, but we can't clear it...*/ | 
 | 1119 | 		INFO (dev, "can't change configurations\n"); | 
 | 1120 | 		return -ESPIPE; | 
 | 1121 | 	} | 
 | 1122 | 	eth_reset_config (dev); | 
 | 1123 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | 	switch (number) { | 
 | 1125 | 	case DEV_CONFIG_VALUE: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | 		result = set_ether_config (dev, gfp_flags); | 
 | 1127 | 		break; | 
 | 1128 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 1129 | 	case DEV_RNDIS_CONFIG_VALUE: | 
 | 1130 | 		dev->rndis = 1; | 
 | 1131 | 		result = set_ether_config (dev, gfp_flags); | 
 | 1132 | 		break; | 
 | 1133 | #endif | 
 | 1134 | 	default: | 
 | 1135 | 		result = -EINVAL; | 
 | 1136 | 		/* FALL THROUGH */ | 
 | 1137 | 	case 0: | 
 | 1138 | 		break; | 
 | 1139 | 	} | 
 | 1140 |  | 
 | 1141 | 	if (result) { | 
 | 1142 | 		if (number) | 
 | 1143 | 			eth_reset_config (dev); | 
 | 1144 | 		usb_gadget_vbus_draw(dev->gadget, | 
 | 1145 | 				dev->gadget->is_otg ? 8 : 100); | 
 | 1146 | 	} else { | 
 | 1147 | 		char *speed; | 
 | 1148 | 		unsigned power; | 
 | 1149 |  | 
 | 1150 | 		power = 2 * eth_config.bMaxPower; | 
 | 1151 | 		usb_gadget_vbus_draw(dev->gadget, power); | 
 | 1152 |  | 
 | 1153 | 		switch (gadget->speed) { | 
 | 1154 | 		case USB_SPEED_FULL:	speed = "full"; break; | 
 | 1155 | #ifdef CONFIG_USB_GADGET_DUALSPEED | 
 | 1156 | 		case USB_SPEED_HIGH:	speed = "high"; break; | 
 | 1157 | #endif | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1158 | 		default:		speed = "?"; break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | 		} | 
 | 1160 |  | 
 | 1161 | 		dev->config = number; | 
 | 1162 | 		INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n", | 
 | 1163 | 				speed, number, power, driver_desc, | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1164 | 				rndis_active(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | 					? "RNDIS" | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1166 | 					: (cdc_active(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | 						? "CDC Ethernet" | 
 | 1168 | 						: "CDC Ethernet Subset")); | 
 | 1169 | 	} | 
 | 1170 | 	return result; | 
 | 1171 | } | 
 | 1172 |  | 
 | 1173 | /*-------------------------------------------------------------------------*/ | 
 | 1174 |  | 
 | 1175 | #ifdef	DEV_CONFIG_CDC | 
 | 1176 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1177 | /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM) | 
 | 1178 |  * only to notify the host about link status changes (which we support) or | 
 | 1179 |  * report completion of some encapsulated command (as used in RNDIS).  Since | 
 | 1180 |  * we want this CDC Ethernet code to be vendor-neutral, we don't use that | 
 | 1181 |  * command mechanism; and only one status request is ever queued. | 
 | 1182 |  */ | 
 | 1183 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | static void eth_status_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1185 | { | 
 | 1186 | 	struct usb_cdc_notification	*event = req->buf; | 
 | 1187 | 	int				value = req->status; | 
 | 1188 | 	struct eth_dev			*dev = ep->driver_data; | 
 | 1189 |  | 
 | 1190 | 	/* issue the second notification if host reads the first */ | 
 | 1191 | 	if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION | 
 | 1192 | 			&& value == 0) { | 
 | 1193 | 		__le32	*data = req->buf + sizeof *event; | 
 | 1194 |  | 
 | 1195 | 		event->bmRequestType = 0xA1; | 
 | 1196 | 		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; | 
 | 1197 | 		event->wValue = __constant_cpu_to_le16 (0); | 
 | 1198 | 		event->wIndex = __constant_cpu_to_le16 (1); | 
 | 1199 | 		event->wLength = __constant_cpu_to_le16 (8); | 
 | 1200 |  | 
 | 1201 | 		/* SPEED_CHANGE data is up/down speeds in bits/sec */ | 
 | 1202 | 		data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget)); | 
 | 1203 |  | 
 | 1204 | 		req->length = STATUS_BYTECOUNT; | 
 | 1205 | 		value = usb_ep_queue (ep, req, GFP_ATOMIC); | 
 | 1206 | 		DEBUG (dev, "send SPEED_CHANGE --> %d\n", value); | 
 | 1207 | 		if (value == 0) | 
 | 1208 | 			return; | 
 | 1209 | 	} else if (value != -ECONNRESET) | 
 | 1210 | 		DEBUG (dev, "event %02x --> %d\n", | 
 | 1211 | 			event->bNotificationType, value); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1212 | 	req->context = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } | 
 | 1214 |  | 
 | 1215 | static void issue_start_status (struct eth_dev *dev) | 
 | 1216 | { | 
 | 1217 | 	struct usb_request		*req = dev->stat_req; | 
 | 1218 | 	struct usb_cdc_notification	*event; | 
 | 1219 | 	int				value; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1220 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | 	DEBUG (dev, "%s, flush old status first\n", __FUNCTION__); | 
 | 1222 |  | 
 | 1223 | 	/* flush old status | 
 | 1224 | 	 * | 
 | 1225 | 	 * FIXME ugly idiom, maybe we'd be better with just | 
 | 1226 | 	 * a "cancel the whole queue" primitive since any | 
 | 1227 | 	 * unlink-one primitive has way too many error modes. | 
 | 1228 | 	 * here, we "know" toggle is already clear... | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1229 | 	 * | 
 | 1230 | 	 * FIXME iff req->context != null just dequeue it | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | 	 */ | 
 | 1232 | 	usb_ep_disable (dev->status_ep); | 
 | 1233 | 	usb_ep_enable (dev->status_ep, dev->status); | 
 | 1234 |  | 
 | 1235 | 	/* 3.8.1 says to issue first NETWORK_CONNECTION, then | 
 | 1236 | 	 * a SPEED_CHANGE.  could be useful in some configs. | 
 | 1237 | 	 */ | 
 | 1238 | 	event = req->buf; | 
 | 1239 | 	event->bmRequestType = 0xA1; | 
 | 1240 | 	event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; | 
 | 1241 | 	event->wValue = __constant_cpu_to_le16 (1);	/* connected */ | 
 | 1242 | 	event->wIndex = __constant_cpu_to_le16 (1); | 
 | 1243 | 	event->wLength = 0; | 
 | 1244 |  | 
 | 1245 | 	req->length = sizeof *event; | 
 | 1246 | 	req->complete = eth_status_complete; | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1247 | 	req->context = dev; | 
 | 1248 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | 	value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC); | 
 | 1250 | 	if (value < 0) | 
 | 1251 | 		DEBUG (dev, "status buf queue --> %d\n", value); | 
 | 1252 | } | 
 | 1253 |  | 
 | 1254 | #endif | 
 | 1255 |  | 
 | 1256 | /*-------------------------------------------------------------------------*/ | 
 | 1257 |  | 
 | 1258 | static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1259 | { | 
 | 1260 | 	if (req->status || req->actual != req->length) | 
 | 1261 | 		DEBUG ((struct eth_dev *) ep->driver_data, | 
 | 1262 | 				"setup complete --> %d, %d/%d\n", | 
 | 1263 | 				req->status, req->actual, req->length); | 
 | 1264 | } | 
 | 1265 |  | 
 | 1266 | #ifdef CONFIG_USB_ETH_RNDIS | 
 | 1267 |  | 
 | 1268 | static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1269 | { | 
 | 1270 | 	if (req->status || req->actual != req->length) | 
 | 1271 | 		DEBUG ((struct eth_dev *) ep->driver_data, | 
 | 1272 | 			"rndis response complete --> %d, %d/%d\n", | 
 | 1273 | 			req->status, req->actual, req->length); | 
 | 1274 |  | 
 | 1275 | 	/* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ | 
 | 1276 | } | 
 | 1277 |  | 
 | 1278 | static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1279 | { | 
 | 1280 | 	struct eth_dev          *dev = ep->driver_data; | 
 | 1281 | 	int			status; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1282 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | 	/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ | 
 | 1284 | 	spin_lock(&dev->lock); | 
 | 1285 | 	status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf); | 
 | 1286 | 	if (status < 0) | 
 | 1287 | 		ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status); | 
 | 1288 | 	spin_unlock(&dev->lock); | 
 | 1289 | } | 
 | 1290 |  | 
 | 1291 | #endif	/* RNDIS */ | 
 | 1292 |  | 
 | 1293 | /* | 
 | 1294 |  * The setup() callback implements all the ep0 functionality that's not | 
 | 1295 |  * handled lower down.  CDC has a number of less-common features: | 
 | 1296 |  * | 
 | 1297 |  *  - two interfaces:  control, and ethernet data | 
 | 1298 |  *  - Ethernet data interface has two altsettings:  default, and active | 
 | 1299 |  *  - class-specific descriptors for the control interface | 
 | 1300 |  *  - class-specific control requests | 
 | 1301 |  */ | 
 | 1302 | static int | 
 | 1303 | eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | 
 | 1304 | { | 
 | 1305 | 	struct eth_dev		*dev = get_gadget_data (gadget); | 
 | 1306 | 	struct usb_request	*req = dev->req; | 
 | 1307 | 	int			value = -EOPNOTSUPP; | 
| David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 1308 | 	u16			wIndex = le16_to_cpu(ctrl->wIndex); | 
 | 1309 | 	u16			wValue = le16_to_cpu(ctrl->wValue); | 
 | 1310 | 	u16			wLength = le16_to_cpu(ctrl->wLength); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 |  | 
 | 1312 | 	/* descriptors just go into the pre-allocated ep0 buffer, | 
 | 1313 | 	 * while config change events may enable network traffic. | 
 | 1314 | 	 */ | 
 | 1315 | 	req->complete = eth_setup_complete; | 
 | 1316 | 	switch (ctrl->bRequest) { | 
 | 1317 |  | 
 | 1318 | 	case USB_REQ_GET_DESCRIPTOR: | 
 | 1319 | 		if (ctrl->bRequestType != USB_DIR_IN) | 
 | 1320 | 			break; | 
 | 1321 | 		switch (wValue >> 8) { | 
 | 1322 |  | 
 | 1323 | 		case USB_DT_DEVICE: | 
 | 1324 | 			value = min (wLength, (u16) sizeof device_desc); | 
 | 1325 | 			memcpy (req->buf, &device_desc, value); | 
 | 1326 | 			break; | 
 | 1327 | #ifdef CONFIG_USB_GADGET_DUALSPEED | 
 | 1328 | 		case USB_DT_DEVICE_QUALIFIER: | 
 | 1329 | 			if (!gadget->is_dualspeed) | 
 | 1330 | 				break; | 
 | 1331 | 			value = min (wLength, (u16) sizeof dev_qualifier); | 
 | 1332 | 			memcpy (req->buf, &dev_qualifier, value); | 
 | 1333 | 			break; | 
 | 1334 |  | 
 | 1335 | 		case USB_DT_OTHER_SPEED_CONFIG: | 
 | 1336 | 			if (!gadget->is_dualspeed) | 
 | 1337 | 				break; | 
 | 1338 | 			// FALLTHROUGH | 
 | 1339 | #endif /* CONFIG_USB_GADGET_DUALSPEED */ | 
 | 1340 | 		case USB_DT_CONFIG: | 
 | 1341 | 			value = config_buf (gadget->speed, req->buf, | 
 | 1342 | 					wValue >> 8, | 
 | 1343 | 					wValue & 0xff, | 
 | 1344 | 					gadget->is_otg); | 
 | 1345 | 			if (value >= 0) | 
 | 1346 | 				value = min (wLength, (u16) value); | 
 | 1347 | 			break; | 
 | 1348 |  | 
 | 1349 | 		case USB_DT_STRING: | 
 | 1350 | 			value = usb_gadget_get_string (&stringtab, | 
 | 1351 | 					wValue & 0xff, req->buf); | 
 | 1352 | 			if (value >= 0) | 
 | 1353 | 				value = min (wLength, (u16) value); | 
 | 1354 | 			break; | 
 | 1355 | 		} | 
 | 1356 | 		break; | 
 | 1357 |  | 
 | 1358 | 	case USB_REQ_SET_CONFIGURATION: | 
 | 1359 | 		if (ctrl->bRequestType != 0) | 
 | 1360 | 			break; | 
 | 1361 | 		if (gadget->a_hnp_support) | 
 | 1362 | 			DEBUG (dev, "HNP available\n"); | 
 | 1363 | 		else if (gadget->a_alt_hnp_support) | 
 | 1364 | 			DEBUG (dev, "HNP needs a different root port\n"); | 
 | 1365 | 		spin_lock (&dev->lock); | 
 | 1366 | 		value = eth_set_config (dev, wValue, GFP_ATOMIC); | 
 | 1367 | 		spin_unlock (&dev->lock); | 
 | 1368 | 		break; | 
 | 1369 | 	case USB_REQ_GET_CONFIGURATION: | 
 | 1370 | 		if (ctrl->bRequestType != USB_DIR_IN) | 
 | 1371 | 			break; | 
 | 1372 | 		*(u8 *)req->buf = dev->config; | 
 | 1373 | 		value = min (wLength, (u16) 1); | 
 | 1374 | 		break; | 
 | 1375 |  | 
 | 1376 | 	case USB_REQ_SET_INTERFACE: | 
 | 1377 | 		if (ctrl->bRequestType != USB_RECIP_INTERFACE | 
 | 1378 | 				|| !dev->config | 
 | 1379 | 				|| wIndex > 1) | 
 | 1380 | 			break; | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1381 | 		if (!cdc_active(dev) && wIndex != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | 			break; | 
 | 1383 | 		spin_lock (&dev->lock); | 
 | 1384 |  | 
 | 1385 | 		/* PXA hardware partially handles SET_INTERFACE; | 
 | 1386 | 		 * we need to kluge around that interference. | 
 | 1387 | 		 */ | 
 | 1388 | 		if (gadget_is_pxa (gadget)) { | 
 | 1389 | 			value = eth_set_config (dev, DEV_CONFIG_VALUE, | 
 | 1390 | 						GFP_ATOMIC); | 
 | 1391 | 			goto done_set_intf; | 
 | 1392 | 		} | 
 | 1393 |  | 
 | 1394 | #ifdef DEV_CONFIG_CDC | 
 | 1395 | 		switch (wIndex) { | 
 | 1396 | 		case 0:		/* control/master intf */ | 
 | 1397 | 			if (wValue != 0) | 
 | 1398 | 				break; | 
 | 1399 | 			if (dev->status) { | 
 | 1400 | 				usb_ep_disable (dev->status_ep); | 
 | 1401 | 				usb_ep_enable (dev->status_ep, dev->status); | 
 | 1402 | 			} | 
 | 1403 | 			value = 0; | 
 | 1404 | 			break; | 
 | 1405 | 		case 1:		/* data intf */ | 
 | 1406 | 			if (wValue > 1) | 
 | 1407 | 				break; | 
 | 1408 | 			usb_ep_disable (dev->in_ep); | 
 | 1409 | 			usb_ep_disable (dev->out_ep); | 
 | 1410 |  | 
 | 1411 | 			/* CDC requires the data transfers not be done from | 
 | 1412 | 			 * the default interface setting ... also, setting | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1413 | 			 * the non-default interface resets filters etc. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | 			 */ | 
 | 1415 | 			if (wValue == 1) { | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1416 | 				if (!cdc_active (dev)) | 
 | 1417 | 					break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | 				usb_ep_enable (dev->in_ep, dev->in); | 
 | 1419 | 				usb_ep_enable (dev->out_ep, dev->out); | 
 | 1420 | 				dev->cdc_filter = DEFAULT_FILTER; | 
 | 1421 | 				netif_carrier_on (dev->net); | 
 | 1422 | 				if (dev->status) | 
 | 1423 | 					issue_start_status (dev); | 
 | 1424 | 				if (netif_running (dev->net)) { | 
 | 1425 | 					spin_unlock (&dev->lock); | 
 | 1426 | 					eth_start (dev, GFP_ATOMIC); | 
 | 1427 | 					spin_lock (&dev->lock); | 
 | 1428 | 				} | 
 | 1429 | 			} else { | 
 | 1430 | 				netif_stop_queue (dev->net); | 
 | 1431 | 				netif_carrier_off (dev->net); | 
 | 1432 | 			} | 
 | 1433 | 			value = 0; | 
 | 1434 | 			break; | 
 | 1435 | 		} | 
 | 1436 | #else | 
 | 1437 | 		/* FIXME this is wrong, as is the assumption that | 
 | 1438 | 		 * all non-PXA hardware talks real CDC ... | 
 | 1439 | 		 */ | 
 | 1440 | 		dev_warn (&gadget->dev, "set_interface ignored!\n"); | 
 | 1441 | #endif /* DEV_CONFIG_CDC */ | 
 | 1442 |  | 
 | 1443 | done_set_intf: | 
 | 1444 | 		spin_unlock (&dev->lock); | 
 | 1445 | 		break; | 
 | 1446 | 	case USB_REQ_GET_INTERFACE: | 
 | 1447 | 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) | 
 | 1448 | 				|| !dev->config | 
 | 1449 | 				|| wIndex > 1) | 
 | 1450 | 			break; | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1451 | 		if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | 			break; | 
 | 1453 |  | 
 | 1454 | 		/* for CDC, iff carrier is on, data interface is active. */ | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1455 | 		if (rndis_active(dev) || wIndex != 1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | 			*(u8 *)req->buf = 0; | 
 | 1457 | 		else | 
 | 1458 | 			*(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; | 
 | 1459 | 		value = min (wLength, (u16) 1); | 
 | 1460 | 		break; | 
 | 1461 |  | 
 | 1462 | #ifdef DEV_CONFIG_CDC | 
 | 1463 | 	case USB_CDC_SET_ETHERNET_PACKET_FILTER: | 
 | 1464 | 		/* see 6.2.30: no data, wIndex = interface, | 
 | 1465 | 		 * wValue = packet filter bitmap | 
 | 1466 | 		 */ | 
 | 1467 | 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1468 | 				|| !cdc_active(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | 				|| wLength != 0 | 
 | 1470 | 				|| wIndex > 1) | 
 | 1471 | 			break; | 
 | 1472 | 		DEBUG (dev, "packet filter %02x\n", wValue); | 
 | 1473 | 		dev->cdc_filter = wValue; | 
 | 1474 | 		value = 0; | 
 | 1475 | 		break; | 
 | 1476 |  | 
 | 1477 | 	/* and potentially: | 
 | 1478 | 	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: | 
 | 1479 | 	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: | 
 | 1480 | 	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: | 
 | 1481 | 	 * case USB_CDC_GET_ETHERNET_STATISTIC: | 
 | 1482 | 	 */ | 
 | 1483 |  | 
 | 1484 | #endif /* DEV_CONFIG_CDC */ | 
 | 1485 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1486 | #ifdef CONFIG_USB_ETH_RNDIS | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | 	/* RNDIS uses the CDC command encapsulation mechanism to implement | 
 | 1488 | 	 * an RPC scheme, with much getting/setting of attributes by OID. | 
 | 1489 | 	 */ | 
 | 1490 | 	case USB_CDC_SEND_ENCAPSULATED_COMMAND: | 
 | 1491 | 		if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1492 | 				|| !rndis_active(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | 				|| wLength > USB_BUFSIZ | 
 | 1494 | 				|| wValue | 
 | 1495 | 				|| rndis_control_intf.bInterfaceNumber | 
 | 1496 | 					!= wIndex) | 
 | 1497 | 			break; | 
 | 1498 | 		/* read the request, then process it */ | 
 | 1499 | 		value = wLength; | 
 | 1500 | 		req->complete = rndis_command_complete; | 
 | 1501 | 		/* later, rndis_control_ack () sends a notification */ | 
 | 1502 | 		break; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1503 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | 	case USB_CDC_GET_ENCAPSULATED_RESPONSE: | 
 | 1505 | 		if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) | 
 | 1506 | 					== ctrl->bRequestType | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1507 | 				&& rndis_active(dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | 				// && wLength >= 0x0400 | 
 | 1509 | 				&& !wValue | 
 | 1510 | 				&& rndis_control_intf.bInterfaceNumber | 
 | 1511 | 					== wIndex) { | 
 | 1512 | 			u8 *buf; | 
 | 1513 |  | 
 | 1514 | 			/* return the result */ | 
 | 1515 | 			buf = rndis_get_next_response (dev->rndis_config, | 
 | 1516 | 						       &value); | 
 | 1517 | 			if (buf) { | 
 | 1518 | 				memcpy (req->buf, buf, value); | 
 | 1519 | 				req->complete = rndis_response_complete; | 
 | 1520 | 				rndis_free_response(dev->rndis_config, buf); | 
 | 1521 | 			} | 
 | 1522 | 			/* else stalls ... spec says to avoid that */ | 
 | 1523 | 		} | 
 | 1524 | 		break; | 
 | 1525 | #endif	/* RNDIS */ | 
 | 1526 |  | 
 | 1527 | 	default: | 
 | 1528 | 		VDEBUG (dev, | 
 | 1529 | 			"unknown control req%02x.%02x v%04x i%04x l%d\n", | 
 | 1530 | 			ctrl->bRequestType, ctrl->bRequest, | 
 | 1531 | 			wValue, wIndex, wLength); | 
 | 1532 | 	} | 
 | 1533 |  | 
 | 1534 | 	/* respond with data transfer before status phase? */ | 
 | 1535 | 	if (value >= 0) { | 
 | 1536 | 		req->length = value; | 
 | 1537 | 		req->zero = value < wLength | 
 | 1538 | 				&& (value % gadget->ep0->maxpacket) == 0; | 
 | 1539 | 		value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC); | 
 | 1540 | 		if (value < 0) { | 
 | 1541 | 			DEBUG (dev, "ep_queue --> %d\n", value); | 
 | 1542 | 			req->status = 0; | 
 | 1543 | 			eth_setup_complete (gadget->ep0, req); | 
 | 1544 | 		} | 
 | 1545 | 	} | 
 | 1546 |  | 
 | 1547 | 	/* host either stalls (value < 0) or reports success */ | 
 | 1548 | 	return value; | 
 | 1549 | } | 
 | 1550 |  | 
 | 1551 | static void | 
 | 1552 | eth_disconnect (struct usb_gadget *gadget) | 
 | 1553 | { | 
 | 1554 | 	struct eth_dev		*dev = get_gadget_data (gadget); | 
 | 1555 | 	unsigned long		flags; | 
 | 1556 |  | 
 | 1557 | 	spin_lock_irqsave (&dev->lock, flags); | 
 | 1558 | 	netif_stop_queue (dev->net); | 
 | 1559 | 	netif_carrier_off (dev->net); | 
 | 1560 | 	eth_reset_config (dev); | 
 | 1561 | 	spin_unlock_irqrestore (&dev->lock, flags); | 
 | 1562 |  | 
 | 1563 | 	/* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ | 
 | 1564 |  | 
 | 1565 | 	/* next we may get setup() calls to enumerate new connections; | 
 | 1566 | 	 * or an unbind() during shutdown (including removing module). | 
 | 1567 | 	 */ | 
 | 1568 | } | 
 | 1569 |  | 
 | 1570 | /*-------------------------------------------------------------------------*/ | 
 | 1571 |  | 
 | 1572 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ | 
 | 1573 |  | 
 | 1574 | static int eth_change_mtu (struct net_device *net, int new_mtu) | 
 | 1575 | { | 
 | 1576 | 	struct eth_dev	*dev = netdev_priv(net); | 
 | 1577 |  | 
| David Brownell | 7802ac5 | 2006-01-22 10:33:27 -0800 | [diff] [blame] | 1578 | 	if (dev->rndis) | 
 | 1579 | 		return -EBUSY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 |  | 
 | 1581 | 	if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) | 
 | 1582 | 		return -ERANGE; | 
 | 1583 | 	/* no zero-length packet read wanted after mtu-sized packets */ | 
 | 1584 | 	if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0) | 
 | 1585 | 		return -EDOM; | 
 | 1586 | 	net->mtu = new_mtu; | 
 | 1587 | 	return 0; | 
 | 1588 | } | 
 | 1589 |  | 
 | 1590 | static struct net_device_stats *eth_get_stats (struct net_device *net) | 
 | 1591 | { | 
 | 1592 | 	return &((struct eth_dev *)netdev_priv(net))->stats; | 
 | 1593 | } | 
 | 1594 |  | 
 | 1595 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) | 
 | 1596 | { | 
 | 1597 | 	struct eth_dev	*dev = netdev_priv(net); | 
 | 1598 | 	strlcpy(p->driver, shortname, sizeof p->driver); | 
 | 1599 | 	strlcpy(p->version, DRIVER_VERSION, sizeof p->version); | 
 | 1600 | 	strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version); | 
 | 1601 | 	strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info); | 
 | 1602 | } | 
 | 1603 |  | 
 | 1604 | static u32 eth_get_link(struct net_device *net) | 
 | 1605 | { | 
 | 1606 | 	struct eth_dev	*dev = netdev_priv(net); | 
 | 1607 | 	return dev->gadget->speed != USB_SPEED_UNKNOWN; | 
 | 1608 | } | 
 | 1609 |  | 
 | 1610 | static struct ethtool_ops ops = { | 
 | 1611 | 	.get_drvinfo = eth_get_drvinfo, | 
 | 1612 | 	.get_link = eth_get_link | 
 | 1613 | }; | 
 | 1614 |  | 
 | 1615 | static void defer_kevent (struct eth_dev *dev, int flag) | 
 | 1616 | { | 
 | 1617 | 	if (test_and_set_bit (flag, &dev->todo)) | 
 | 1618 | 		return; | 
 | 1619 | 	if (!schedule_work (&dev->work)) | 
 | 1620 | 		ERROR (dev, "kevent %d may have been dropped\n", flag); | 
 | 1621 | 	else | 
 | 1622 | 		DEBUG (dev, "kevent %d scheduled\n", flag); | 
 | 1623 | } | 
 | 1624 |  | 
 | 1625 | static void rx_complete (struct usb_ep *ep, struct usb_request *req); | 
 | 1626 |  | 
 | 1627 | static int | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1628 | rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | { | 
 | 1630 | 	struct sk_buff		*skb; | 
 | 1631 | 	int			retval = -ENOMEM; | 
 | 1632 | 	size_t			size; | 
 | 1633 |  | 
 | 1634 | 	/* Padding up to RX_EXTRA handles minor disagreements with host. | 
 | 1635 | 	 * Normally we use the USB "terminate on short read" convention; | 
 | 1636 | 	 * so allow up to (N*maxpacket), since that memory is normally | 
 | 1637 | 	 * already allocated.  Some hardware doesn't deal well with short | 
 | 1638 | 	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a | 
 | 1639 | 	 * byte off the end (to force hardware errors on overflow). | 
 | 1640 | 	 * | 
 | 1641 | 	 * RNDIS uses internal framing, and explicitly allows senders to | 
 | 1642 | 	 * pad to end-of-packet.  That's potentially nice for speed, | 
 | 1643 | 	 * but means receivers can't recover synch on their own. | 
 | 1644 | 	 */ | 
 | 1645 | 	size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA); | 
 | 1646 | 	size += dev->out_ep->maxpacket - 1; | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1647 | 	if (rndis_active(dev)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | 		size += sizeof (struct rndis_packet_msg_type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | 	size -= size % dev->out_ep->maxpacket; | 
 | 1650 |  | 
 | 1651 | 	if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) { | 
 | 1652 | 		DEBUG (dev, "no rx skb\n"); | 
 | 1653 | 		goto enomem; | 
 | 1654 | 	} | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1655 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | 	/* Some platforms perform better when IP packets are aligned, | 
 | 1657 | 	 * but on at least one, checksumming fails otherwise.  Note: | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1658 | 	 * RNDIS headers involve variable numbers of LE32 values. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | 	 */ | 
 | 1660 | 	skb_reserve(skb, NET_IP_ALIGN); | 
 | 1661 |  | 
 | 1662 | 	req->buf = skb->data; | 
 | 1663 | 	req->length = size; | 
 | 1664 | 	req->complete = rx_complete; | 
 | 1665 | 	req->context = skb; | 
 | 1666 |  | 
 | 1667 | 	retval = usb_ep_queue (dev->out_ep, req, gfp_flags); | 
 | 1668 | 	if (retval == -ENOMEM) | 
 | 1669 | enomem: | 
 | 1670 | 		defer_kevent (dev, WORK_RX_MEMORY); | 
 | 1671 | 	if (retval) { | 
 | 1672 | 		DEBUG (dev, "rx submit --> %d\n", retval); | 
 | 1673 | 		dev_kfree_skb_any (skb); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1674 | 		spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | 		list_add (&req->list, &dev->rx_reqs); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1676 | 		spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | 	} | 
 | 1678 | 	return retval; | 
 | 1679 | } | 
 | 1680 |  | 
 | 1681 | static void rx_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1682 | { | 
 | 1683 | 	struct sk_buff	*skb = req->context; | 
 | 1684 | 	struct eth_dev	*dev = ep->driver_data; | 
 | 1685 | 	int		status = req->status; | 
 | 1686 |  | 
 | 1687 | 	switch (status) { | 
 | 1688 |  | 
 | 1689 | 	/* normal completion */ | 
 | 1690 | 	case 0: | 
 | 1691 | 		skb_put (skb, req->actual); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | 		/* we know MaxPacketsPerTransfer == 1 here */ | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1693 | 		if (rndis_active(dev)) | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1694 | 			status = rndis_rm_hdr (skb); | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1695 | 		if (status < 0 | 
 | 1696 | 				|| ETH_HLEN > skb->len | 
 | 1697 | 				|| skb->len > ETH_FRAME_LEN) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | 			dev->stats.rx_errors++; | 
 | 1699 | 			dev->stats.rx_length_errors++; | 
 | 1700 | 			DEBUG (dev, "rx length %d\n", skb->len); | 
 | 1701 | 			break; | 
 | 1702 | 		} | 
 | 1703 |  | 
 | 1704 | 		skb->dev = dev->net; | 
 | 1705 | 		skb->protocol = eth_type_trans (skb, dev->net); | 
 | 1706 | 		dev->stats.rx_packets++; | 
 | 1707 | 		dev->stats.rx_bytes += skb->len; | 
 | 1708 |  | 
 | 1709 | 		/* no buffer copies needed, unless hardware can't | 
 | 1710 | 		 * use skb buffers. | 
 | 1711 | 		 */ | 
 | 1712 | 		status = netif_rx (skb); | 
 | 1713 | 		skb = NULL; | 
 | 1714 | 		break; | 
 | 1715 |  | 
 | 1716 | 	/* software-driven interface shutdown */ | 
 | 1717 | 	case -ECONNRESET:		// unlink | 
 | 1718 | 	case -ESHUTDOWN:		// disconnect etc | 
 | 1719 | 		VDEBUG (dev, "rx shutdown, code %d\n", status); | 
 | 1720 | 		goto quiesce; | 
 | 1721 |  | 
 | 1722 | 	/* for hardware automagic (such as pxa) */ | 
 | 1723 | 	case -ECONNABORTED:		// endpoint reset | 
 | 1724 | 		DEBUG (dev, "rx %s reset\n", ep->name); | 
 | 1725 | 		defer_kevent (dev, WORK_RX_MEMORY); | 
 | 1726 | quiesce: | 
 | 1727 | 		dev_kfree_skb_any (skb); | 
 | 1728 | 		goto clean; | 
 | 1729 |  | 
 | 1730 | 	/* data overrun */ | 
 | 1731 | 	case -EOVERFLOW: | 
 | 1732 | 		dev->stats.rx_over_errors++; | 
 | 1733 | 		// FALLTHROUGH | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1734 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | 	default: | 
 | 1736 | 		dev->stats.rx_errors++; | 
 | 1737 | 		DEBUG (dev, "rx status %d\n", status); | 
 | 1738 | 		break; | 
 | 1739 | 	} | 
 | 1740 |  | 
 | 1741 | 	if (skb) | 
 | 1742 | 		dev_kfree_skb_any (skb); | 
 | 1743 | 	if (!netif_running (dev->net)) { | 
 | 1744 | clean: | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1745 | 		spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | 		list_add (&req->list, &dev->rx_reqs); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1747 | 		spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | 		req = NULL; | 
 | 1749 | 	} | 
 | 1750 | 	if (req) | 
 | 1751 | 		rx_submit (dev, req, GFP_ATOMIC); | 
 | 1752 | } | 
 | 1753 |  | 
 | 1754 | static int prealloc (struct list_head *list, struct usb_ep *ep, | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1755 | 			unsigned n, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | { | 
 | 1757 | 	unsigned		i; | 
 | 1758 | 	struct usb_request	*req; | 
 | 1759 |  | 
 | 1760 | 	if (!n) | 
 | 1761 | 		return -ENOMEM; | 
 | 1762 |  | 
 | 1763 | 	/* queue/recycle up to N requests */ | 
 | 1764 | 	i = n; | 
 | 1765 | 	list_for_each_entry (req, list, list) { | 
 | 1766 | 		if (i-- == 0) | 
 | 1767 | 			goto extra; | 
 | 1768 | 	} | 
 | 1769 | 	while (i--) { | 
 | 1770 | 		req = usb_ep_alloc_request (ep, gfp_flags); | 
 | 1771 | 		if (!req) | 
 | 1772 | 			return list_empty (list) ? -ENOMEM : 0; | 
 | 1773 | 		list_add (&req->list, list); | 
 | 1774 | 	} | 
 | 1775 | 	return 0; | 
 | 1776 |  | 
 | 1777 | extra: | 
 | 1778 | 	/* free extras */ | 
 | 1779 | 	for (;;) { | 
 | 1780 | 		struct list_head	*next; | 
 | 1781 |  | 
 | 1782 | 		next = req->list.next; | 
 | 1783 | 		list_del (&req->list); | 
 | 1784 | 		usb_ep_free_request (ep, req); | 
 | 1785 |  | 
 | 1786 | 		if (next == list) | 
 | 1787 | 			break; | 
 | 1788 |  | 
 | 1789 | 		req = container_of (next, struct usb_request, list); | 
 | 1790 | 	} | 
 | 1791 | 	return 0; | 
 | 1792 | } | 
 | 1793 |  | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1794 | static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | { | 
 | 1796 | 	int status; | 
 | 1797 |  | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1798 | 	spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | 	status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags); | 
 | 1800 | 	if (status < 0) | 
 | 1801 | 		goto fail; | 
 | 1802 | 	status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags); | 
 | 1803 | 	if (status < 0) | 
 | 1804 | 		goto fail; | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1805 | 	goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | fail: | 
 | 1807 | 	DEBUG (dev, "can't alloc requests\n"); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1808 | done: | 
 | 1809 | 	spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | 	return status; | 
 | 1811 | } | 
 | 1812 |  | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1813 | static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | { | 
 | 1815 | 	struct usb_request	*req; | 
 | 1816 | 	unsigned long		flags; | 
 | 1817 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | 	/* fill unused rxq slots with some skb */ | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1819 | 	spin_lock_irqsave(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | 	while (!list_empty (&dev->rx_reqs)) { | 
 | 1821 | 		req = container_of (dev->rx_reqs.next, | 
 | 1822 | 				struct usb_request, list); | 
 | 1823 | 		list_del_init (&req->list); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1824 | 		spin_unlock_irqrestore(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 |  | 
 | 1826 | 		if (rx_submit (dev, req, gfp_flags) < 0) { | 
 | 1827 | 			defer_kevent (dev, WORK_RX_MEMORY); | 
 | 1828 | 			return; | 
 | 1829 | 		} | 
 | 1830 |  | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1831 | 		spin_lock_irqsave(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | 	} | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1833 | 	spin_unlock_irqrestore(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | } | 
 | 1835 |  | 
 | 1836 | static void eth_work (void *_dev) | 
 | 1837 | { | 
 | 1838 | 	struct eth_dev		*dev = _dev; | 
 | 1839 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1840 | 	if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | 		if (netif_running (dev->net)) | 
 | 1842 | 			rx_fill (dev, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | 	} | 
 | 1844 |  | 
 | 1845 | 	if (dev->todo) | 
 | 1846 | 		DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo); | 
 | 1847 | } | 
 | 1848 |  | 
 | 1849 | static void tx_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 1850 | { | 
 | 1851 | 	struct sk_buff	*skb = req->context; | 
 | 1852 | 	struct eth_dev	*dev = ep->driver_data; | 
 | 1853 |  | 
 | 1854 | 	switch (req->status) { | 
 | 1855 | 	default: | 
 | 1856 | 		dev->stats.tx_errors++; | 
 | 1857 | 		VDEBUG (dev, "tx err %d\n", req->status); | 
 | 1858 | 		/* FALLTHROUGH */ | 
 | 1859 | 	case -ECONNRESET:		// unlink | 
 | 1860 | 	case -ESHUTDOWN:		// disconnect etc | 
 | 1861 | 		break; | 
 | 1862 | 	case 0: | 
 | 1863 | 		dev->stats.tx_bytes += skb->len; | 
 | 1864 | 	} | 
 | 1865 | 	dev->stats.tx_packets++; | 
 | 1866 |  | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1867 | 	spin_lock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | 	list_add (&req->list, &dev->tx_reqs); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1869 | 	spin_unlock(&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | 	dev_kfree_skb_any (skb); | 
 | 1871 |  | 
 | 1872 | 	atomic_dec (&dev->tx_qlen); | 
 | 1873 | 	if (netif_carrier_ok (dev->net)) | 
 | 1874 | 		netif_wake_queue (dev->net); | 
 | 1875 | } | 
 | 1876 |  | 
 | 1877 | static inline int eth_is_promisc (struct eth_dev *dev) | 
 | 1878 | { | 
 | 1879 | 	/* no filters for the CDC subset; always promisc */ | 
 | 1880 | 	if (subset_active (dev)) | 
 | 1881 | 		return 1; | 
 | 1882 | 	return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; | 
 | 1883 | } | 
 | 1884 |  | 
 | 1885 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) | 
 | 1886 | { | 
 | 1887 | 	struct eth_dev		*dev = netdev_priv(net); | 
 | 1888 | 	int			length = skb->len; | 
 | 1889 | 	int			retval; | 
 | 1890 | 	struct usb_request	*req = NULL; | 
 | 1891 | 	unsigned long		flags; | 
 | 1892 |  | 
 | 1893 | 	/* apply outgoing CDC or RNDIS filters */ | 
 | 1894 | 	if (!eth_is_promisc (dev)) { | 
 | 1895 | 		u8		*dest = skb->data; | 
 | 1896 |  | 
 | 1897 | 		if (dest [0] & 0x01) { | 
 | 1898 | 			u16	type; | 
 | 1899 |  | 
 | 1900 | 			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host | 
 | 1901 | 			 * SET_ETHERNET_MULTICAST_FILTERS requests | 
 | 1902 | 			 */ | 
 | 1903 | 			if (memcmp (dest, net->broadcast, ETH_ALEN) == 0) | 
 | 1904 | 				type = USB_CDC_PACKET_TYPE_BROADCAST; | 
 | 1905 | 			else | 
 | 1906 | 				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; | 
 | 1907 | 			if (!(dev->cdc_filter & type)) { | 
 | 1908 | 				dev_kfree_skb_any (skb); | 
 | 1909 | 				return 0; | 
 | 1910 | 			} | 
 | 1911 | 		} | 
 | 1912 | 		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */ | 
 | 1913 | 	} | 
 | 1914 |  | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1915 | 	spin_lock_irqsave(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | 	req = container_of (dev->tx_reqs.next, struct usb_request, list); | 
 | 1917 | 	list_del (&req->list); | 
 | 1918 | 	if (list_empty (&dev->tx_reqs)) | 
 | 1919 | 		netif_stop_queue (net); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1920 | 	spin_unlock_irqrestore(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 |  | 
 | 1922 | 	/* no buffer copies needed, unless the network stack did it | 
 | 1923 | 	 * or the hardware can't use skb buffers. | 
 | 1924 | 	 * or there's not enough space for any RNDIS headers we need | 
 | 1925 | 	 */ | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 1926 | 	if (rndis_active(dev)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | 		struct sk_buff	*skb_rndis; | 
 | 1928 |  | 
 | 1929 | 		skb_rndis = skb_realloc_headroom (skb, | 
 | 1930 | 				sizeof (struct rndis_packet_msg_type)); | 
 | 1931 | 		if (!skb_rndis) | 
 | 1932 | 			goto drop; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 1933 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | 		dev_kfree_skb_any (skb); | 
 | 1935 | 		skb = skb_rndis; | 
 | 1936 | 		rndis_add_hdr (skb); | 
 | 1937 | 		length = skb->len; | 
 | 1938 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | 	req->buf = skb->data; | 
 | 1940 | 	req->context = skb; | 
 | 1941 | 	req->complete = tx_complete; | 
 | 1942 |  | 
 | 1943 | 	/* use zlp framing on tx for strict CDC-Ether conformance, | 
 | 1944 | 	 * though any robust network rx path ignores extra padding. | 
 | 1945 | 	 * and some hardware doesn't like to write zlps. | 
 | 1946 | 	 */ | 
 | 1947 | 	req->zero = 1; | 
 | 1948 | 	if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) | 
 | 1949 | 		length++; | 
 | 1950 |  | 
 | 1951 | 	req->length = length; | 
 | 1952 |  | 
 | 1953 | #ifdef	CONFIG_USB_GADGET_DUALSPEED | 
 | 1954 | 	/* throttle highspeed IRQ rate back slightly */ | 
 | 1955 | 	req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) | 
 | 1956 | 		? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0) | 
 | 1957 | 		: 0; | 
 | 1958 | #endif | 
 | 1959 |  | 
 | 1960 | 	retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); | 
 | 1961 | 	switch (retval) { | 
 | 1962 | 	default: | 
 | 1963 | 		DEBUG (dev, "tx queue err %d\n", retval); | 
 | 1964 | 		break; | 
 | 1965 | 	case 0: | 
 | 1966 | 		net->trans_start = jiffies; | 
 | 1967 | 		atomic_inc (&dev->tx_qlen); | 
 | 1968 | 	} | 
 | 1969 |  | 
 | 1970 | 	if (retval) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | drop: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | 		dev->stats.tx_dropped++; | 
 | 1973 | 		dev_kfree_skb_any (skb); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1974 | 		spin_lock_irqsave(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | 		if (list_empty (&dev->tx_reqs)) | 
 | 1976 | 			netif_start_queue (net); | 
 | 1977 | 		list_add (&req->list, &dev->tx_reqs); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 1978 | 		spin_unlock_irqrestore(&dev->req_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | 	} | 
 | 1980 | 	return 0; | 
 | 1981 | } | 
 | 1982 |  | 
 | 1983 | /*-------------------------------------------------------------------------*/ | 
 | 1984 |  | 
 | 1985 | #ifdef CONFIG_USB_ETH_RNDIS | 
 | 1986 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1987 | /* The interrupt endpoint is used in RNDIS to notify the host when messages | 
 | 1988 |  * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT | 
 | 1989 |  * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even | 
 | 1990 |  * REMOTE_NDIS_KEEPALIVE_MSG. | 
 | 1991 |  * | 
 | 1992 |  * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and | 
 | 1993 |  * normally just one notification will be queued. | 
 | 1994 |  */ | 
 | 1995 |  | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1996 | static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 1997 | static void eth_req_free (struct usb_ep *ep, struct usb_request *req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 |  | 
 | 1999 | static void | 
 | 2000 | rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req) | 
 | 2001 | { | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2002 | 	struct eth_dev          *dev = ep->driver_data; | 
 | 2003 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | 	if (req->status || req->actual != req->length) | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2005 | 		DEBUG (dev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | 			"rndis control ack complete --> %d, %d/%d\n", | 
 | 2007 | 			req->status, req->actual, req->length); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2008 | 	req->context = NULL; | 
 | 2009 |  | 
 | 2010 | 	if (req != dev->stat_req) | 
 | 2011 | 		eth_req_free(ep, req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } | 
 | 2013 |  | 
 | 2014 | static int rndis_control_ack (struct net_device *net) | 
 | 2015 | { | 
 | 2016 | 	struct eth_dev          *dev = netdev_priv(net); | 
 | 2017 | 	u32                     length; | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 2018 | 	struct usb_request      *resp = dev->stat_req; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2019 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | 	/* in case RNDIS calls this after disconnect */ | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 2021 | 	if (!dev->status) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | 		DEBUG (dev, "status ENODEV\n"); | 
 | 2023 | 		return -ENODEV; | 
 | 2024 | 	} | 
 | 2025 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2026 | 	/* in case queue length > 1 */ | 
 | 2027 | 	if (resp->context) { | 
 | 2028 | 		resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC); | 
 | 2029 | 		if (!resp) | 
 | 2030 | 			return -ENOMEM; | 
 | 2031 | 	} | 
 | 2032 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | 	/* Send RNDIS RESPONSE_AVAILABLE notification; | 
 | 2034 | 	 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too | 
 | 2035 | 	 */ | 
 | 2036 | 	resp->length = 8; | 
 | 2037 | 	resp->complete = rndis_control_ack_complete; | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2038 | 	resp->context = dev; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2039 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | 	*((__le32 *) resp->buf) = __constant_cpu_to_le32 (1); | 
 | 2041 | 	*((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0); | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2042 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | 	length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC); | 
 | 2044 | 	if (length < 0) { | 
 | 2045 | 		resp->status = 0; | 
 | 2046 | 		rndis_control_ack_complete (dev->status_ep, resp); | 
 | 2047 | 	} | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2048 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | 	return 0; | 
 | 2050 | } | 
 | 2051 |  | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 2052 | #else | 
 | 2053 |  | 
 | 2054 | #define	rndis_control_ack	NULL | 
 | 2055 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | #endif	/* RNDIS */ | 
 | 2057 |  | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 2058 | static void eth_start (struct eth_dev *dev, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | { | 
 | 2060 | 	DEBUG (dev, "%s\n", __FUNCTION__); | 
 | 2061 |  | 
 | 2062 | 	/* fill the rx queue */ | 
 | 2063 | 	rx_fill (dev, gfp_flags); | 
 | 2064 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2065 | 	/* and open the tx floodgates */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | 	atomic_set (&dev->tx_qlen, 0); | 
 | 2067 | 	netif_wake_queue (dev->net); | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 2068 | 	if (rndis_active(dev)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | 		rndis_set_param_medium (dev->rndis_config, | 
 | 2070 | 					NDIS_MEDIUM_802_3, | 
| David Brownell | 6cdee10 | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 2071 | 					BITRATE(dev->gadget)/100); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2072 | 		(void) rndis_signal_connect (dev->rndis_config); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | } | 
 | 2075 |  | 
 | 2076 | static int eth_open (struct net_device *net) | 
 | 2077 | { | 
 | 2078 | 	struct eth_dev		*dev = netdev_priv(net); | 
 | 2079 |  | 
 | 2080 | 	DEBUG (dev, "%s\n", __FUNCTION__); | 
 | 2081 | 	if (netif_carrier_ok (dev->net)) | 
 | 2082 | 		eth_start (dev, GFP_KERNEL); | 
 | 2083 | 	return 0; | 
 | 2084 | } | 
 | 2085 |  | 
 | 2086 | static int eth_stop (struct net_device *net) | 
 | 2087 | { | 
 | 2088 | 	struct eth_dev		*dev = netdev_priv(net); | 
 | 2089 |  | 
 | 2090 | 	VDEBUG (dev, "%s\n", __FUNCTION__); | 
 | 2091 | 	netif_stop_queue (net); | 
 | 2092 |  | 
 | 2093 | 	DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2094 | 		dev->stats.rx_packets, dev->stats.tx_packets, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | 		dev->stats.rx_errors, dev->stats.tx_errors | 
 | 2096 | 		); | 
 | 2097 |  | 
 | 2098 | 	/* ensure there are no more active requests */ | 
 | 2099 | 	if (dev->config) { | 
 | 2100 | 		usb_ep_disable (dev->in_ep); | 
 | 2101 | 		usb_ep_disable (dev->out_ep); | 
 | 2102 | 		if (netif_carrier_ok (dev->net)) { | 
 | 2103 | 			DEBUG (dev, "host still using in/out endpoints\n"); | 
 | 2104 | 			// FIXME idiom may leave toggle wrong here | 
 | 2105 | 			usb_ep_enable (dev->in_ep, dev->in); | 
 | 2106 | 			usb_ep_enable (dev->out_ep, dev->out); | 
 | 2107 | 		} | 
 | 2108 | 		if (dev->status_ep) { | 
 | 2109 | 			usb_ep_disable (dev->status_ep); | 
 | 2110 | 			usb_ep_enable (dev->status_ep, dev->status); | 
 | 2111 | 		} | 
 | 2112 | 	} | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2113 |  | 
| David Brownell | 45e45ab | 2005-05-16 08:26:38 -0700 | [diff] [blame] | 2114 | 	if (rndis_active(dev)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | 		rndis_set_param_medium (dev->rndis_config, | 
 | 2116 | 					NDIS_MEDIUM_802_3, 0); | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2117 | 		(void) rndis_signal_disconnect (dev->rndis_config); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 |  | 
 | 2120 | 	return 0; | 
 | 2121 | } | 
 | 2122 |  | 
 | 2123 | /*-------------------------------------------------------------------------*/ | 
 | 2124 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2125 | static struct usb_request * | 
| Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 2126 | eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | { | 
 | 2128 | 	struct usb_request	*req; | 
 | 2129 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2130 | 	req = usb_ep_alloc_request (ep, gfp_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | 	if (!req) | 
 | 2132 | 		return NULL; | 
 | 2133 |  | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2134 | 	req->buf = kmalloc (size, gfp_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | 	if (!req->buf) { | 
 | 2136 | 		usb_ep_free_request (ep, req); | 
 | 2137 | 		req = NULL; | 
 | 2138 | 	} | 
 | 2139 | 	return req; | 
 | 2140 | } | 
 | 2141 |  | 
 | 2142 | static void | 
 | 2143 | eth_req_free (struct usb_ep *ep, struct usb_request *req) | 
 | 2144 | { | 
 | 2145 | 	kfree (req->buf); | 
 | 2146 | 	usb_ep_free_request (ep, req); | 
 | 2147 | } | 
 | 2148 |  | 
 | 2149 |  | 
| David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 2150 | static void /* __init_or_exit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | eth_unbind (struct usb_gadget *gadget) | 
 | 2152 | { | 
 | 2153 | 	struct eth_dev		*dev = get_gadget_data (gadget); | 
 | 2154 |  | 
 | 2155 | 	DEBUG (dev, "unbind\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | 	rndis_deregister (dev->rndis_config); | 
 | 2157 | 	rndis_exit (); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 |  | 
 | 2159 | 	/* we've already been disconnected ... no i/o is active */ | 
 | 2160 | 	if (dev->req) { | 
 | 2161 | 		eth_req_free (gadget->ep0, dev->req); | 
 | 2162 | 		dev->req = NULL; | 
 | 2163 | 	} | 
 | 2164 | 	if (dev->stat_req) { | 
 | 2165 | 		eth_req_free (dev->status_ep, dev->stat_req); | 
 | 2166 | 		dev->stat_req = NULL; | 
 | 2167 | 	} | 
 | 2168 |  | 
 | 2169 | 	unregister_netdev (dev->net); | 
 | 2170 | 	free_netdev(dev->net); | 
 | 2171 |  | 
 | 2172 | 	/* assuming we used keventd, it must quiesce too */ | 
 | 2173 | 	flush_scheduled_work (); | 
 | 2174 | 	set_gadget_data (gadget, NULL); | 
 | 2175 | } | 
 | 2176 |  | 
| David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 2177 | static u8 __devinit nibble (unsigned char c) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | { | 
 | 2179 | 	if (likely (isdigit (c))) | 
 | 2180 | 		return c - '0'; | 
 | 2181 | 	c = toupper (c); | 
 | 2182 | 	if (likely (isxdigit (c))) | 
 | 2183 | 		return 10 + c - 'A'; | 
 | 2184 | 	return 0; | 
 | 2185 | } | 
 | 2186 |  | 
| David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 2187 | static int __devinit get_ether_addr(const char *str, u8 *dev_addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | { | 
 | 2189 | 	if (str) { | 
 | 2190 | 		unsigned	i; | 
 | 2191 |  | 
 | 2192 | 		for (i = 0; i < 6; i++) { | 
 | 2193 | 			unsigned char num; | 
 | 2194 |  | 
 | 2195 | 			if((*str == '.') || (*str == ':')) | 
 | 2196 | 				str++; | 
 | 2197 | 			num = nibble(*str++) << 4; | 
 | 2198 | 			num |= (nibble(*str++)); | 
 | 2199 | 			dev_addr [i] = num; | 
 | 2200 | 		} | 
 | 2201 | 		if (is_valid_ether_addr (dev_addr)) | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 2202 | 			return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | 	} | 
 | 2204 | 	random_ether_addr(dev_addr); | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 2205 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } | 
 | 2207 |  | 
| David Brownell | a353678 | 2006-07-06 15:48:53 -0700 | [diff] [blame] | 2208 | static int __devinit | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | eth_bind (struct usb_gadget *gadget) | 
 | 2210 | { | 
 | 2211 | 	struct eth_dev		*dev; | 
 | 2212 | 	struct net_device	*net; | 
 | 2213 | 	u8			cdc = 1, zlp = 1, rndis = 1; | 
 | 2214 | 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL; | 
 | 2215 | 	int			status = -ENOMEM; | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 2216 | 	int			gcnum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 |  | 
 | 2218 | 	/* these flags are only ever cleared; compiler take note */ | 
 | 2219 | #ifndef	DEV_CONFIG_CDC | 
 | 2220 | 	cdc = 0; | 
 | 2221 | #endif | 
 | 2222 | #ifndef	CONFIG_USB_ETH_RNDIS | 
 | 2223 | 	rndis = 0; | 
 | 2224 | #endif | 
 | 2225 |  | 
 | 2226 | 	/* Because most host side USB stacks handle CDC Ethernet, that | 
 | 2227 | 	 * standard protocol is _strongly_ preferred for interop purposes. | 
 | 2228 | 	 * (By everyone except Microsoft.) | 
 | 2229 | 	 */ | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 2230 | 	if (gadget_is_pxa (gadget)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | 		/* pxa doesn't support altsettings */ | 
 | 2232 | 		cdc = 0; | 
 | 2233 | 	} else if (gadget_is_sh(gadget)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | 		/* sh doesn't support multiple interfaces or configs */ | 
 | 2235 | 		cdc = 0; | 
 | 2236 | 		rndis = 0; | 
 | 2237 | 	} else if (gadget_is_sa1100 (gadget)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | 		/* hardware can't write zlps */ | 
 | 2239 | 		zlp = 0; | 
 | 2240 | 		/* sa1100 CAN do CDC, without status endpoint ... we use | 
 | 2241 | 		 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". | 
 | 2242 | 		 */ | 
 | 2243 | 		cdc = 0; | 
| David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 2244 | 	} | 
 | 2245 |  | 
 | 2246 | 	gcnum = usb_gadget_controller_number (gadget); | 
 | 2247 | 	if (gcnum >= 0) | 
 | 2248 | 		device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum); | 
 | 2249 | 	else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | 		/* can't assume CDC works.  don't want to default to | 
 | 2251 | 		 * anything less functional on CDC-capable hardware, | 
 | 2252 | 		 * so we fail in this case. | 
 | 2253 | 		 */ | 
 | 2254 | 		dev_err (&gadget->dev, | 
 | 2255 | 			"controller '%s' not recognized\n", | 
 | 2256 | 			gadget->name); | 
 | 2257 | 		return -ENODEV; | 
 | 2258 | 	} | 
 | 2259 | 	snprintf (manufacturer, sizeof manufacturer, "%s %s/%s", | 
 | 2260 | 		system_utsname.sysname, system_utsname.release, | 
 | 2261 | 		gadget->name); | 
 | 2262 |  | 
 | 2263 | 	/* If there's an RNDIS configuration, that's what Windows wants to | 
 | 2264 | 	 * be using ... so use these product IDs here and in the "linux.inf" | 
 | 2265 | 	 * needed to install MSFT drivers.  Current Linux kernels will use | 
 | 2266 | 	 * the second configuration if it's CDC Ethernet, and need some help | 
 | 2267 | 	 * to choose the right configuration otherwise. | 
 | 2268 | 	 */ | 
 | 2269 | 	if (rndis) { | 
 | 2270 | 		device_desc.idVendor = | 
 | 2271 | 			__constant_cpu_to_le16(RNDIS_VENDOR_NUM); | 
 | 2272 | 		device_desc.idProduct = | 
 | 2273 | 			__constant_cpu_to_le16(RNDIS_PRODUCT_NUM); | 
 | 2274 | 		snprintf (product_desc, sizeof product_desc, | 
 | 2275 | 			"RNDIS/%s", driver_desc); | 
 | 2276 |  | 
 | 2277 | 	/* CDC subset ... recognized by Linux since 2.4.10, but Windows | 
 | 2278 | 	 * drivers aren't widely available. | 
 | 2279 | 	 */ | 
 | 2280 | 	} else if (!cdc) { | 
 | 2281 | 		device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; | 
 | 2282 | 		device_desc.idVendor = | 
 | 2283 | 			__constant_cpu_to_le16(SIMPLE_VENDOR_NUM); | 
 | 2284 | 		device_desc.idProduct = | 
 | 2285 | 			__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); | 
 | 2286 | 	} | 
 | 2287 |  | 
 | 2288 | 	/* support optional vendor/distro customization */ | 
 | 2289 | 	if (idVendor) { | 
 | 2290 | 		if (!idProduct) { | 
 | 2291 | 			dev_err (&gadget->dev, "idVendor needs idProduct!\n"); | 
 | 2292 | 			return -ENODEV; | 
 | 2293 | 		} | 
 | 2294 | 		device_desc.idVendor = cpu_to_le16(idVendor); | 
 | 2295 | 		device_desc.idProduct = cpu_to_le16(idProduct); | 
 | 2296 | 		if (bcdDevice) | 
 | 2297 | 			device_desc.bcdDevice = cpu_to_le16(bcdDevice); | 
 | 2298 | 	} | 
 | 2299 | 	if (iManufacturer) | 
 | 2300 | 		strlcpy (manufacturer, iManufacturer, sizeof manufacturer); | 
 | 2301 | 	if (iProduct) | 
 | 2302 | 		strlcpy (product_desc, iProduct, sizeof product_desc); | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 2303 | 	if (iSerialNumber) { | 
 | 2304 | 		device_desc.iSerialNumber = STRING_SERIALNUMBER, | 
 | 2305 | 		strlcpy(serial_number, iSerialNumber, sizeof serial_number); | 
 | 2306 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 |  | 
 | 2308 | 	/* all we really need is bulk IN/OUT */ | 
 | 2309 | 	usb_ep_autoconfig_reset (gadget); | 
 | 2310 | 	in_ep = usb_ep_autoconfig (gadget, &fs_source_desc); | 
 | 2311 | 	if (!in_ep) { | 
 | 2312 | autoconf_fail: | 
 | 2313 | 		dev_err (&gadget->dev, | 
 | 2314 | 			"can't autoconfigure on %s\n", | 
 | 2315 | 			gadget->name); | 
 | 2316 | 		return -ENODEV; | 
 | 2317 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | 	in_ep->driver_data = in_ep;	/* claim */ | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2319 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | 	out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc); | 
 | 2321 | 	if (!out_ep) | 
 | 2322 | 		goto autoconf_fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | 	out_ep->driver_data = out_ep;	/* claim */ | 
 | 2324 |  | 
 | 2325 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
 | 2326 | 	/* CDC Ethernet control interface doesn't require a status endpoint. | 
 | 2327 | 	 * Since some hosts expect one, try to allocate one anyway. | 
 | 2328 | 	 */ | 
 | 2329 | 	if (cdc || rndis) { | 
 | 2330 | 		status_ep = usb_ep_autoconfig (gadget, &fs_status_desc); | 
 | 2331 | 		if (status_ep) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | 			status_ep->driver_data = status_ep;	/* claim */ | 
 | 2333 | 		} else if (rndis) { | 
 | 2334 | 			dev_err (&gadget->dev, | 
 | 2335 | 				"can't run RNDIS on %s\n", | 
 | 2336 | 				gadget->name); | 
 | 2337 | 			return -ENODEV; | 
 | 2338 | #ifdef DEV_CONFIG_CDC | 
 | 2339 | 		/* pxa25x only does CDC subset; often used with RNDIS */ | 
 | 2340 | 		} else if (cdc) { | 
 | 2341 | 			control_intf.bNumEndpoints = 0; | 
 | 2342 | 			/* FIXME remove endpoint from descriptor list */ | 
 | 2343 | #endif | 
 | 2344 | 		} | 
 | 2345 | 	} | 
 | 2346 | #endif | 
 | 2347 |  | 
 | 2348 | 	/* one config:  cdc, else minimal subset */ | 
 | 2349 | 	if (!cdc) { | 
 | 2350 | 		eth_config.bNumInterfaces = 1; | 
 | 2351 | 		eth_config.iConfiguration = STRING_SUBSET; | 
 | 2352 | 		fs_subset_descriptors(); | 
 | 2353 | 		hs_subset_descriptors(); | 
 | 2354 | 	} | 
 | 2355 |  | 
| David Brownell | e1394b4 | 2006-04-02 10:20:43 -0800 | [diff] [blame] | 2356 | 	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | 
 | 2357 | 	usb_gadget_set_selfpowered (gadget); | 
 | 2358 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | 	/* For now RNDIS is always a second config */ | 
 | 2360 | 	if (rndis) | 
 | 2361 | 		device_desc.bNumConfigurations = 2; | 
 | 2362 |  | 
 | 2363 | #ifdef	CONFIG_USB_GADGET_DUALSPEED | 
 | 2364 | 	if (rndis) | 
 | 2365 | 		dev_qualifier.bNumConfigurations = 2; | 
 | 2366 | 	else if (!cdc) | 
 | 2367 | 		dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; | 
 | 2368 |  | 
 | 2369 | 	/* assumes ep0 uses the same value for both speeds ... */ | 
 | 2370 | 	dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; | 
 | 2371 |  | 
 | 2372 | 	/* and that all endpoints are dual-speed */ | 
 | 2373 | 	hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; | 
 | 2374 | 	hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; | 
 | 2375 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2376 | 	if (status_ep) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | 		hs_status_desc.bEndpointAddress = | 
 | 2378 | 				fs_status_desc.bEndpointAddress; | 
 | 2379 | #endif | 
 | 2380 | #endif	/* DUALSPEED */ | 
 | 2381 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | 	if (gadget->is_otg) { | 
 | 2383 | 		otg_descriptor.bmAttributes |= USB_OTG_HNP, | 
 | 2384 | 		eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | 
 | 2385 | 		eth_config.bMaxPower = 4; | 
 | 2386 | #ifdef	CONFIG_USB_ETH_RNDIS | 
 | 2387 | 		rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | 
 | 2388 | 		rndis_config.bMaxPower = 4; | 
 | 2389 | #endif | 
 | 2390 | 	} | 
 | 2391 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2392 | 	net = alloc_etherdev (sizeof *dev); | 
 | 2393 | 	if (!net) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | 		return status; | 
 | 2395 | 	dev = netdev_priv(net); | 
 | 2396 | 	spin_lock_init (&dev->lock); | 
| David Brownell | 789851c | 2006-08-21 15:26:38 -0700 | [diff] [blame] | 2397 | 	spin_lock_init (&dev->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | 	INIT_WORK (&dev->work, eth_work, dev); | 
 | 2399 | 	INIT_LIST_HEAD (&dev->tx_reqs); | 
 | 2400 | 	INIT_LIST_HEAD (&dev->rx_reqs); | 
 | 2401 |  | 
 | 2402 | 	/* network device setup */ | 
 | 2403 | 	dev->net = net; | 
 | 2404 | 	SET_MODULE_OWNER (net); | 
 | 2405 | 	strcpy (net->name, "usb%d"); | 
 | 2406 | 	dev->cdc = cdc; | 
 | 2407 | 	dev->zlp = zlp; | 
 | 2408 |  | 
 | 2409 | 	dev->in_ep = in_ep; | 
 | 2410 | 	dev->out_ep = out_ep; | 
 | 2411 | 	dev->status_ep = status_ep; | 
 | 2412 |  | 
 | 2413 | 	/* Module params for these addresses should come from ID proms. | 
 | 2414 | 	 * The host side address is used with CDC and RNDIS, and commonly | 
 | 2415 | 	 * ends up in a persistent config database. | 
 | 2416 | 	 */ | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 2417 | 	if (get_ether_addr(dev_addr, net->dev_addr)) | 
 | 2418 | 		dev_warn(&gadget->dev, | 
 | 2419 | 			"using random %s ethernet address\n", "self"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | 	if (cdc || rndis) { | 
| Aras Vaichas | 1afc64a | 2006-02-18 12:31:23 -0800 | [diff] [blame] | 2421 | 		if (get_ether_addr(host_addr, dev->host_mac)) | 
 | 2422 | 			dev_warn(&gadget->dev, | 
 | 2423 | 				"using random %s ethernet address\n", "host"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | #ifdef	DEV_CONFIG_CDC | 
 | 2425 | 		snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X", | 
 | 2426 | 			dev->host_mac [0], dev->host_mac [1], | 
 | 2427 | 			dev->host_mac [2], dev->host_mac [3], | 
 | 2428 | 			dev->host_mac [4], dev->host_mac [5]); | 
 | 2429 | #endif | 
 | 2430 | 	} | 
 | 2431 |  | 
 | 2432 | 	if (rndis) { | 
 | 2433 | 		status = rndis_init(); | 
 | 2434 | 		if (status < 0) { | 
 | 2435 | 			dev_err (&gadget->dev, "can't init RNDIS, %d\n", | 
 | 2436 | 				status); | 
 | 2437 | 			goto fail; | 
 | 2438 | 		} | 
 | 2439 | 	} | 
 | 2440 |  | 
 | 2441 | 	net->change_mtu = eth_change_mtu; | 
 | 2442 | 	net->get_stats = eth_get_stats; | 
 | 2443 | 	net->hard_start_xmit = eth_start_xmit; | 
 | 2444 | 	net->open = eth_open; | 
 | 2445 | 	net->stop = eth_stop; | 
 | 2446 | 	// watchdog_timeo, tx_timeout ... | 
 | 2447 | 	// set_multicast_list | 
 | 2448 | 	SET_ETHTOOL_OPS(net, &ops); | 
 | 2449 |  | 
 | 2450 | 	/* preallocate control message data and buffer */ | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2451 | 	dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | 	if (!dev->req) | 
 | 2453 | 		goto fail; | 
 | 2454 | 	dev->req->complete = eth_setup_complete; | 
 | 2455 |  | 
 | 2456 | 	/* ... and maybe likewise for status transfer */ | 
| Ian Campbell | 05f3340 | 2005-06-29 10:15:32 +0100 | [diff] [blame] | 2457 | #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | 	if (dev->status_ep) { | 
 | 2459 | 		dev->stat_req = eth_req_alloc (dev->status_ep, | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2460 | 					STATUS_BYTECOUNT, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | 		if (!dev->stat_req) { | 
 | 2462 | 			eth_req_free (gadget->ep0, dev->req); | 
 | 2463 | 			goto fail; | 
 | 2464 | 		} | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2465 | 		dev->stat_req->context = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | 	} | 
| David Brownell | 822e14a | 2005-06-13 06:55:03 -0700 | [diff] [blame] | 2467 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 |  | 
 | 2469 | 	/* finish hookup to lower layer ... */ | 
 | 2470 | 	dev->gadget = gadget; | 
 | 2471 | 	set_gadget_data (gadget, dev); | 
 | 2472 | 	gadget->ep0->driver_data = dev; | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2473 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | 	/* two kinds of host-initiated state changes: | 
 | 2475 | 	 *  - iff DATA transfer is active, carrier is "on" | 
 | 2476 | 	 *  - tx queueing enabled if open *and* carrier is "on" | 
 | 2477 | 	 */ | 
 | 2478 | 	netif_stop_queue (dev->net); | 
 | 2479 | 	netif_carrier_off (dev->net); | 
 | 2480 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2481 | 	SET_NETDEV_DEV (dev->net, &gadget->dev); | 
 | 2482 | 	status = register_netdev (dev->net); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | 	if (status < 0) | 
 | 2484 | 		goto fail1; | 
 | 2485 |  | 
 | 2486 | 	INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc); | 
 | 2487 | 	INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name, | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2488 | 		out_ep->name, in_ep->name, | 
 | 2489 | 		status_ep ? " STATUS " : "", | 
 | 2490 | 		status_ep ? status_ep->name : "" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | 		); | 
 | 2492 | 	INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | 
 | 2493 | 		net->dev_addr [0], net->dev_addr [1], | 
 | 2494 | 		net->dev_addr [2], net->dev_addr [3], | 
 | 2495 | 		net->dev_addr [4], net->dev_addr [5]); | 
 | 2496 |  | 
 | 2497 | 	if (cdc || rndis) | 
 | 2498 | 		INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", | 
 | 2499 | 			dev->host_mac [0], dev->host_mac [1], | 
 | 2500 | 			dev->host_mac [2], dev->host_mac [3], | 
 | 2501 | 			dev->host_mac [4], dev->host_mac [5]); | 
 | 2502 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | 	if (rndis) { | 
 | 2504 | 		u32	vendorID = 0; | 
 | 2505 |  | 
 | 2506 | 		/* FIXME RNDIS vendor id == "vendor NIC code" == ? */ | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2507 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | 		dev->rndis_config = rndis_register (rndis_control_ack); | 
 | 2509 | 		if (dev->rndis_config < 0) { | 
 | 2510 | fail0: | 
 | 2511 | 			unregister_netdev (dev->net); | 
 | 2512 | 			status = -ENODEV; | 
 | 2513 | 			goto fail; | 
 | 2514 | 		} | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2515 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | 		/* these set up a lot of the OIDs that RNDIS needs */ | 
 | 2517 | 		rndis_set_host_mac (dev->rndis_config, dev->host_mac); | 
 | 2518 | 		if (rndis_set_param_dev (dev->rndis_config, dev->net, | 
| David Brownell | 340600a | 2005-04-28 13:45:25 -0700 | [diff] [blame] | 2519 | 					 &dev->stats, &dev->cdc_filter)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | 			goto fail0; | 
 | 2521 | 		if (rndis_set_param_vendor (dev->rndis_config, vendorID, | 
 | 2522 | 					    manufacturer)) | 
 | 2523 | 			goto fail0; | 
 | 2524 | 		if (rndis_set_param_medium (dev->rndis_config, | 
 | 2525 | 					    NDIS_MEDIUM_802_3, | 
 | 2526 | 					    0)) | 
 | 2527 | 			goto fail0; | 
 | 2528 | 		INFO (dev, "RNDIS ready\n"); | 
 | 2529 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 |  | 
 | 2531 | 	return status; | 
 | 2532 |  | 
 | 2533 | fail1: | 
 | 2534 | 	dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status); | 
 | 2535 | fail: | 
 | 2536 | 	eth_unbind (gadget); | 
 | 2537 | 	return status; | 
 | 2538 | } | 
 | 2539 |  | 
 | 2540 | /*-------------------------------------------------------------------------*/ | 
 | 2541 |  | 
 | 2542 | static void | 
 | 2543 | eth_suspend (struct usb_gadget *gadget) | 
 | 2544 | { | 
 | 2545 | 	struct eth_dev		*dev = get_gadget_data (gadget); | 
 | 2546 |  | 
 | 2547 | 	DEBUG (dev, "suspend\n"); | 
 | 2548 | 	dev->suspended = 1; | 
 | 2549 | } | 
 | 2550 |  | 
 | 2551 | static void | 
 | 2552 | eth_resume (struct usb_gadget *gadget) | 
 | 2553 | { | 
 | 2554 | 	struct eth_dev		*dev = get_gadget_data (gadget); | 
 | 2555 |  | 
 | 2556 | 	DEBUG (dev, "resume\n"); | 
 | 2557 | 	dev->suspended = 0; | 
 | 2558 | } | 
 | 2559 |  | 
 | 2560 | /*-------------------------------------------------------------------------*/ | 
 | 2561 |  | 
 | 2562 | static struct usb_gadget_driver eth_driver = { | 
| David Brownell | 907cba3 | 2005-04-28 13:48:09 -0700 | [diff] [blame] | 2563 | 	.speed		= DEVSPEED, | 
 | 2564 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | 	.function	= (char *) driver_desc, | 
 | 2566 | 	.bind		= eth_bind, | 
| David Brownell | 329af28 | 2006-02-18 12:31:05 -0800 | [diff] [blame] | 2567 | 	.unbind		= __exit_p(eth_unbind), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 |  | 
 | 2569 | 	.setup		= eth_setup, | 
 | 2570 | 	.disconnect	= eth_disconnect, | 
 | 2571 |  | 
 | 2572 | 	.suspend	= eth_suspend, | 
 | 2573 | 	.resume		= eth_resume, | 
 | 2574 |  | 
| David Brownell | 7e27f18 | 2006-06-13 09:54:40 -0700 | [diff] [blame] | 2575 | 	.driver	= { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | 		.name		= (char *) shortname, | 
| Ben Dooks | d0d5049 | 2005-10-10 10:52:33 +0100 | [diff] [blame] | 2577 | 		.owner		= THIS_MODULE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | 	}, | 
 | 2579 | }; | 
 | 2580 |  | 
 | 2581 | MODULE_DESCRIPTION (DRIVER_DESC); | 
 | 2582 | MODULE_AUTHOR ("David Brownell, Benedikt Spanger"); | 
 | 2583 | MODULE_LICENSE ("GPL"); | 
 | 2584 |  | 
 | 2585 |  | 
 | 2586 | static int __init init (void) | 
 | 2587 | { | 
 | 2588 | 	return usb_gadget_register_driver (ð_driver); | 
 | 2589 | } | 
 | 2590 | module_init (init); | 
 | 2591 |  | 
 | 2592 | static void __exit cleanup (void) | 
 | 2593 | { | 
 | 2594 | 	usb_gadget_unregister_driver (ð_driver); | 
 | 2595 | } | 
 | 2596 | module_exit (cleanup); | 
 | 2597 |  |