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