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