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