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