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