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