4  * Authors:     Benedikt Spranger, Pengutronix
 
   5  *              Robert Schwebel, Pengutronix
 
   7  *              This program is free software; you can redistribute it and/or
 
   8  *              modify it under the terms of the GNU General Public License
 
   9  *              version 2, as published by the Free Software Foundation.
 
  11  *              This software was originally developed in conformance with
 
  12  *              Microsoft's Remote NDIS Specification License Agreement.
 
  14  * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
 
  15  *              Fixed message length bug in init_response
 
  17  * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
 
  18  *              Fixed rndis_rm_hdr length bug.
 
  20  * Copyright (C) 2004 by David Brownell
 
  21  *              updates to merge with Linux 2.6, better match RNDIS spec
 
  24 #include <linux/module.h>
 
  25 #include <linux/moduleparam.h>
 
  26 #include <linux/kernel.h>
 
  27 #include <linux/errno.h>
 
  28 #include <linux/init.h>
 
  29 #include <linux/list.h>
 
  30 #include <linux/proc_fs.h>
 
  31 #include <linux/seq_file.h>
 
  32 #include <linux/netdevice.h>
 
  35 #include <asm/byteorder.h>
 
  36 #include <asm/system.h>
 
  37 #include <asm/unaligned.h>
 
  45 /* The driver for your USB chip needs to support ep0 OUT to work with
 
  46  * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
 
  48  * Windows hosts need an INF file like Documentation/usb/linux.inf
 
  49  * and will be happier if you provide the host_addr module parameter.
 
  53 static int rndis_debug = 0;
 
  54 module_param (rndis_debug, int, 0);
 
  55 MODULE_PARM_DESC (rndis_debug, "enable debugging");
 
  60 #define DBG(str,args...) do { \
 
  62                 pr_debug(str , ## args); \
 
  65 #define RNDIS_MAX_CONFIGS       1
 
  68 static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
 
  71 static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
 
  73 /* Function Prototypes */
 
  74 static rndis_resp_t *rndis_add_response (int configNr, u32 length);
 
  78 static const u32 oid_supported_list [] =
 
  80         /* the general stuff */
 
  81         OID_GEN_SUPPORTED_LIST,
 
  82         OID_GEN_HARDWARE_STATUS,
 
  83         OID_GEN_MEDIA_SUPPORTED,
 
  85         OID_GEN_MAXIMUM_FRAME_SIZE,
 
  87         OID_GEN_TRANSMIT_BLOCK_SIZE,
 
  88         OID_GEN_RECEIVE_BLOCK_SIZE,
 
  90         OID_GEN_VENDOR_DESCRIPTION,
 
  91         OID_GEN_VENDOR_DRIVER_VERSION,
 
  92         OID_GEN_CURRENT_PACKET_FILTER,
 
  93         OID_GEN_MAXIMUM_TOTAL_SIZE,
 
  94         OID_GEN_MEDIA_CONNECT_STATUS,
 
  95         OID_GEN_PHYSICAL_MEDIUM,
 
  97         /* the statistical stuff */
 
 102         OID_GEN_RCV_NO_BUFFER,
 
 103 #ifdef  RNDIS_OPTIONAL_STATS
 
 104         OID_GEN_DIRECTED_BYTES_XMIT,
 
 105         OID_GEN_DIRECTED_FRAMES_XMIT,
 
 106         OID_GEN_MULTICAST_BYTES_XMIT,
 
 107         OID_GEN_MULTICAST_FRAMES_XMIT,
 
 108         OID_GEN_BROADCAST_BYTES_XMIT,
 
 109         OID_GEN_BROADCAST_FRAMES_XMIT,
 
 110         OID_GEN_DIRECTED_BYTES_RCV,
 
 111         OID_GEN_DIRECTED_FRAMES_RCV,
 
 112         OID_GEN_MULTICAST_BYTES_RCV,
 
 113         OID_GEN_MULTICAST_FRAMES_RCV,
 
 114         OID_GEN_BROADCAST_BYTES_RCV,
 
 115         OID_GEN_BROADCAST_FRAMES_RCV,
 
 116         OID_GEN_RCV_CRC_ERROR,
 
 117         OID_GEN_TRANSMIT_QUEUE_LENGTH,
 
 118 #endif  /* RNDIS_OPTIONAL_STATS */
 
 120         /* mandatory 802.3 */
 
 121         /* the general stuff */
 
 122         OID_802_3_PERMANENT_ADDRESS,
 
 123         OID_802_3_CURRENT_ADDRESS,
 
 124         OID_802_3_MULTICAST_LIST,
 
 125         OID_802_3_MAC_OPTIONS,
 
 126         OID_802_3_MAXIMUM_LIST_SIZE,
 
 128         /* the statistical stuff */
 
 129         OID_802_3_RCV_ERROR_ALIGNMENT,
 
 130         OID_802_3_XMIT_ONE_COLLISION,
 
 131         OID_802_3_XMIT_MORE_COLLISIONS,
 
 132 #ifdef  RNDIS_OPTIONAL_STATS
 
 133         OID_802_3_XMIT_DEFERRED,
 
 134         OID_802_3_XMIT_MAX_COLLISIONS,
 
 135         OID_802_3_RCV_OVERRUN,
 
 136         OID_802_3_XMIT_UNDERRUN,
 
 137         OID_802_3_XMIT_HEARTBEAT_FAILURE,
 
 138         OID_802_3_XMIT_TIMES_CRS_LOST,
 
 139         OID_802_3_XMIT_LATE_COLLISIONS,
 
 140 #endif  /* RNDIS_OPTIONAL_STATS */
 
 143         /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
 
 144          * don't say what they mean ... and the NDIS specs are often
 
 145          * confusing and/or ambiguous in this context.  (That is, more
 
 146          * so than their specs for the other OIDs.)
 
 148          * FIXME someone who knows what these should do, please
 
 152         /* power management */
 
 153         OID_PNP_CAPABILITIES,
 
 159         OID_PNP_ENABLE_WAKE_UP,
 
 160         OID_PNP_ADD_WAKE_UP_PATTERN,
 
 161         OID_PNP_REMOVE_WAKE_UP_PATTERN,
 
 162 #endif  /* RNDIS_WAKEUP */
 
 163 #endif  /* RNDIS_PM */
 
 169 gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
 
 172         int                     retval = -ENOTSUPP;
 
 173         u32                     length = 4;     /* usually */
 
 176         rndis_query_cmplt_type  *resp;
 
 177         struct net_device       *net;
 
 178         struct net_device_stats *stats;
 
 180         if (!r) return -ENOMEM;
 
 181         resp = (rndis_query_cmplt_type *) r->buf;
 
 183         if (!resp) return -ENOMEM;
 
 185         if (buf_len && rndis_debug > 1) {
 
 186                 DBG("query OID %08x value, len %d:\n", OID, buf_len);
 
 187                 for (i = 0; i < buf_len; i += 16) {
 
 188                         DBG("%03d: %08x %08x %08x %08x\n", i,
 
 189                                 get_unaligned_le32(&buf[i]),
 
 190                                 get_unaligned_le32(&buf[i + 4]),
 
 191                                 get_unaligned_le32(&buf[i + 8]),
 
 192                                 get_unaligned_le32(&buf[i + 12]));
 
 196         /* response goes here, right after the header */
 
 197         outbuf = (__le32 *) &resp[1];
 
 198         resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
 
 200         net = rndis_per_dev_params[configNr].dev;
 
 202                 stats = net->get_stats(net);
 
 208         /* general oids (table 4-1) */
 
 211         case OID_GEN_SUPPORTED_LIST:
 
 212                 DBG("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
 
 213                 length = sizeof (oid_supported_list);
 
 214                 count  = length / sizeof (u32);
 
 215                 for (i = 0; i < count; i++)
 
 216                         outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
 
 221         case OID_GEN_HARDWARE_STATUS:
 
 222                 DBG("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
 
 224                  * Hardware must be ready to receive high level protocols.
 
 226                  * reddite ergo quae sunt Caesaris Caesari
 
 227                  * et quae sunt Dei Deo!
 
 229                 *outbuf = __constant_cpu_to_le32 (0);
 
 234         case OID_GEN_MEDIA_SUPPORTED:
 
 235                 DBG("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
 
 236                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
 
 241         case OID_GEN_MEDIA_IN_USE:
 
 242                 DBG("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
 
 243                 /* one medium, one transport... (maybe you do it better) */
 
 244                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
 
 249         case OID_GEN_MAXIMUM_FRAME_SIZE:
 
 250                 DBG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
 
 251                 if (rndis_per_dev_params [configNr].dev) {
 
 252                         *outbuf = cpu_to_le32 (
 
 253                                 rndis_per_dev_params [configNr].dev->mtu);
 
 259         case OID_GEN_LINK_SPEED:
 
 261                         DBG("%s: OID_GEN_LINK_SPEED\n", __func__);
 
 262                 if (rndis_per_dev_params [configNr].media_state
 
 263                                 == NDIS_MEDIA_STATE_DISCONNECTED)
 
 264                         *outbuf = __constant_cpu_to_le32 (0);
 
 266                         *outbuf = cpu_to_le32 (
 
 267                                 rndis_per_dev_params [configNr].speed);
 
 272         case OID_GEN_TRANSMIT_BLOCK_SIZE:
 
 273                 DBG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
 
 274                 if (rndis_per_dev_params [configNr].dev) {
 
 275                         *outbuf = cpu_to_le32 (
 
 276                                 rndis_per_dev_params [configNr].dev->mtu);
 
 282         case OID_GEN_RECEIVE_BLOCK_SIZE:
 
 283                 DBG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
 
 284                 if (rndis_per_dev_params [configNr].dev) {
 
 285                         *outbuf = cpu_to_le32 (
 
 286                                 rndis_per_dev_params [configNr].dev->mtu);
 
 292         case OID_GEN_VENDOR_ID:
 
 293                 DBG("%s: OID_GEN_VENDOR_ID\n", __func__);
 
 294                 *outbuf = cpu_to_le32 (
 
 295                         rndis_per_dev_params [configNr].vendorID);
 
 300         case OID_GEN_VENDOR_DESCRIPTION:
 
 301                 DBG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
 
 302                 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
 
 304                         rndis_per_dev_params [configNr].vendorDescr, length);
 
 308         case OID_GEN_VENDOR_DRIVER_VERSION:
 
 309                 DBG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
 
 311                 *outbuf = rndis_driver_version;
 
 316         case OID_GEN_CURRENT_PACKET_FILTER:
 
 317                 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
 
 318                 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
 
 323         case OID_GEN_MAXIMUM_TOTAL_SIZE:
 
 324                 DBG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
 
 325                 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
 
 330         case OID_GEN_MEDIA_CONNECT_STATUS:
 
 332                         DBG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
 
 333                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
 
 338         case OID_GEN_PHYSICAL_MEDIUM:
 
 339                 DBG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
 
 340                 *outbuf = __constant_cpu_to_le32 (0);
 
 344         /* The RNDIS specification is incomplete/wrong.   Some versions
 
 345          * of MS-Windows expect OIDs that aren't specified there.  Other
 
 346          * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
 
 348         case OID_GEN_MAC_OPTIONS:               /* from WinME */
 
 349                 DBG("%s: OID_GEN_MAC_OPTIONS\n", __func__);
 
 350                 *outbuf = __constant_cpu_to_le32(
 
 351                           NDIS_MAC_OPTION_RECEIVE_SERIALIZED
 
 352                         | NDIS_MAC_OPTION_FULL_DUPLEX);
 
 356         /* statistics OIDs (table 4-2) */
 
 359         case OID_GEN_XMIT_OK:
 
 361                         DBG("%s: OID_GEN_XMIT_OK\n", __func__);
 
 363                         *outbuf = cpu_to_le32(stats->tx_packets
 
 364                                 - stats->tx_errors - stats->tx_dropped);
 
 372                         DBG("%s: OID_GEN_RCV_OK\n", __func__);
 
 374                         *outbuf = cpu_to_le32(stats->rx_packets
 
 375                                 - stats->rx_errors - stats->rx_dropped);
 
 381         case OID_GEN_XMIT_ERROR:
 
 383                         DBG("%s: OID_GEN_XMIT_ERROR\n", __func__);
 
 385                         *outbuf = cpu_to_le32(stats->tx_errors);
 
 391         case OID_GEN_RCV_ERROR:
 
 393                         DBG("%s: OID_GEN_RCV_ERROR\n", __func__);
 
 395                         *outbuf = cpu_to_le32(stats->rx_errors);
 
 401         case OID_GEN_RCV_NO_BUFFER:
 
 402                 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
 
 404                         *outbuf = cpu_to_le32(stats->rx_dropped);
 
 409         /* ieee802.3 OIDs (table 4-3) */
 
 412         case OID_802_3_PERMANENT_ADDRESS:
 
 413                 DBG("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
 
 414                 if (rndis_per_dev_params [configNr].dev) {
 
 417                                 rndis_per_dev_params [configNr].host_mac,
 
 424         case OID_802_3_CURRENT_ADDRESS:
 
 425                 DBG("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
 
 426                 if (rndis_per_dev_params [configNr].dev) {
 
 429                                 rndis_per_dev_params [configNr].host_mac,
 
 436         case OID_802_3_MULTICAST_LIST:
 
 437                 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__);
 
 438                 /* Multicast base address only */
 
 439                 *outbuf = __constant_cpu_to_le32 (0xE0000000);
 
 444         case OID_802_3_MAXIMUM_LIST_SIZE:
 
 445                 DBG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
 
 446                 /* Multicast base address only */
 
 447                 *outbuf = __constant_cpu_to_le32 (1);
 
 451         case OID_802_3_MAC_OPTIONS:
 
 452                 DBG("%s: OID_802_3_MAC_OPTIONS\n", __func__);
 
 455         /* ieee802.3 statistics OIDs (table 4-4) */
 
 458         case OID_802_3_RCV_ERROR_ALIGNMENT:
 
 459                 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
 
 461                         *outbuf = cpu_to_le32(stats->rx_frame_errors);
 
 467         case OID_802_3_XMIT_ONE_COLLISION:
 
 468                 DBG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
 
 469                 *outbuf = __constant_cpu_to_le32 (0);
 
 474         case OID_802_3_XMIT_MORE_COLLISIONS:
 
 475                 DBG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
 
 476                 *outbuf = __constant_cpu_to_le32 (0);
 
 481                 pr_warning("%s: query unknown OID 0x%08X\n",
 
 487         resp->InformationBufferLength = cpu_to_le32 (length);
 
 488         r->length = length + sizeof *resp;
 
 489         resp->MessageLength = cpu_to_le32 (r->length);
 
 493 static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
 
 496         rndis_set_cmplt_type            *resp;
 
 497         int                             i, retval = -ENOTSUPP;
 
 498         struct rndis_params             *params;
 
 502         resp = (rndis_set_cmplt_type *) r->buf;
 
 506         if (buf_len && rndis_debug > 1) {
 
 507                 DBG("set OID %08x value, len %d:\n", OID, buf_len);
 
 508                 for (i = 0; i < buf_len; i += 16) {
 
 509                         DBG("%03d: %08x %08x %08x %08x\n", i,
 
 510                                 get_unaligned_le32(&buf[i]),
 
 511                                 get_unaligned_le32(&buf[i + 4]),
 
 512                                 get_unaligned_le32(&buf[i + 8]),
 
 513                                 get_unaligned_le32(&buf[i + 12]));
 
 517         params = &rndis_per_dev_params [configNr];
 
 519         case OID_GEN_CURRENT_PACKET_FILTER:
 
 521                 /* these NDIS_PACKET_TYPE_* bitflags are shared with
 
 522                  * cdc_filter; it's not RNDIS-specific
 
 523                  * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
 
 524                  *      PROMISCUOUS, DIRECTED,
 
 525                  *      MULTICAST, ALL_MULTICAST, BROADCAST
 
 527                 *params->filter = (u16)get_unaligned_le32(buf);
 
 528                 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
 
 529                         __func__, *params->filter);
 
 531                 /* this call has a significant side effect:  it's
 
 532                  * what makes the packet flow start and stop, like
 
 533                  * activating the CDC Ethernet altsetting.
 
 536                 if (*params->filter) {
 
 537                         params->state = RNDIS_DATA_INITIALIZED;
 
 538                         netif_carrier_on(params->dev);
 
 539                         if (netif_running(params->dev))
 
 540                                 netif_wake_queue (params->dev);
 
 542                         params->state = RNDIS_INITIALIZED;
 
 543                         netif_carrier_off (params->dev);
 
 544                         netif_stop_queue (params->dev);
 
 548         case OID_802_3_MULTICAST_LIST:
 
 549                 /* I think we can ignore this */
 
 550                 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__);
 
 555                 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
 
 556                          __func__, OID, buf_len);
 
 566 static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
 
 568         rndis_init_cmplt_type   *resp;
 
 570         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 575         r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
 
 578         resp = (rndis_init_cmplt_type *) r->buf;
 
 580         resp->MessageType = __constant_cpu_to_le32 (
 
 581                         REMOTE_NDIS_INITIALIZE_CMPLT);
 
 582         resp->MessageLength = __constant_cpu_to_le32 (52);
 
 583         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
 
 584         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
 
 585         resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
 
 586         resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
 
 587         resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
 
 588         resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
 
 589         resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
 
 590         resp->MaxTransferSize = cpu_to_le32 (
 
 592                 + sizeof (struct ethhdr)
 
 593                 + sizeof (struct rndis_packet_msg_type)
 
 595         resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
 
 596         resp->AFListOffset = __constant_cpu_to_le32 (0);
 
 597         resp->AFListSize = __constant_cpu_to_le32 (0);
 
 599         params->resp_avail(params->v);
 
 603 static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
 
 605         rndis_query_cmplt_type *resp;
 
 607         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 609         // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID));
 
 614          * we need more memory:
 
 615          * gen_ndis_query_resp expects enough space for
 
 616          * rndis_query_cmplt_type followed by data.
 
 617          * oid_supported_list is the largest data reply
 
 619         r = rndis_add_response (configNr,
 
 620                 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
 
 623         resp = (rndis_query_cmplt_type *) r->buf;
 
 625         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
 
 626         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
 
 628         if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
 
 629                         le32_to_cpu(buf->InformationBufferOffset)
 
 631                         le32_to_cpu(buf->InformationBufferLength),
 
 633                 /* OID not supported */
 
 634                 resp->Status = __constant_cpu_to_le32 (
 
 635                                 RNDIS_STATUS_NOT_SUPPORTED);
 
 636                 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
 
 637                 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
 
 638                 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
 
 640                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
 
 642         params->resp_avail(params->v);
 
 646 static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
 
 648         u32                     BufLength, BufOffset;
 
 649         rndis_set_cmplt_type    *resp;
 
 651         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 653         r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
 
 656         resp = (rndis_set_cmplt_type *) r->buf;
 
 658         BufLength = le32_to_cpu (buf->InformationBufferLength);
 
 659         BufOffset = le32_to_cpu (buf->InformationBufferOffset);
 
 662         DBG("%s: Length: %d\n", __func__, BufLength);
 
 663         DBG("%s: Offset: %d\n", __func__, BufOffset);
 
 664         DBG("%s: InfoBuffer: ", __func__);
 
 666         for (i = 0; i < BufLength; i++) {
 
 667                 DBG("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
 
 673         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
 
 674         resp->MessageLength = __constant_cpu_to_le32 (16);
 
 675         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
 
 676         if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
 
 677                         ((u8 *) buf) + 8 + BufOffset, BufLength, r))
 
 678                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
 
 680                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
 
 682         params->resp_avail(params->v);
 
 686 static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
 
 688         rndis_reset_cmplt_type  *resp;
 
 690         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 692         r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
 
 695         resp = (rndis_reset_cmplt_type *) r->buf;
 
 697         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
 
 698         resp->MessageLength = __constant_cpu_to_le32 (16);
 
 699         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
 
 700         /* resent information */
 
 701         resp->AddressingReset = __constant_cpu_to_le32 (1);
 
 703         params->resp_avail(params->v);
 
 707 static int rndis_keepalive_response (int configNr,
 
 708                                 rndis_keepalive_msg_type *buf)
 
 710         rndis_keepalive_cmplt_type      *resp;
 
 712         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 714         /* host "should" check only in RNDIS_DATA_INITIALIZED state */
 
 716         r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
 
 719         resp = (rndis_keepalive_cmplt_type *) r->buf;
 
 721         resp->MessageType = __constant_cpu_to_le32 (
 
 722                         REMOTE_NDIS_KEEPALIVE_CMPLT);
 
 723         resp->MessageLength = __constant_cpu_to_le32 (16);
 
 724         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
 
 725         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
 
 727         params->resp_avail(params->v);
 
 733  * Device to Host Comunication
 
 735 static int rndis_indicate_status_msg (int configNr, u32 status)
 
 737         rndis_indicate_status_msg_type  *resp;
 
 739         struct rndis_params     *params = rndis_per_dev_params + configNr;
 
 741         if (params->state == RNDIS_UNINITIALIZED)
 
 744         r = rndis_add_response (configNr,
 
 745                                 sizeof (rndis_indicate_status_msg_type));
 
 748         resp = (rndis_indicate_status_msg_type *) r->buf;
 
 750         resp->MessageType = __constant_cpu_to_le32 (
 
 751                         REMOTE_NDIS_INDICATE_STATUS_MSG);
 
 752         resp->MessageLength = __constant_cpu_to_le32 (20);
 
 753         resp->Status = cpu_to_le32 (status);
 
 754         resp->StatusBufferLength = __constant_cpu_to_le32 (0);
 
 755         resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
 
 757         params->resp_avail(params->v);
 
 761 int rndis_signal_connect (int configNr)
 
 763         rndis_per_dev_params [configNr].media_state
 
 764                         = NDIS_MEDIA_STATE_CONNECTED;
 
 765         return rndis_indicate_status_msg (configNr,
 
 766                                           RNDIS_STATUS_MEDIA_CONNECT);
 
 769 int rndis_signal_disconnect (int configNr)
 
 771         rndis_per_dev_params [configNr].media_state
 
 772                         = NDIS_MEDIA_STATE_DISCONNECTED;
 
 773         return rndis_indicate_status_msg (configNr,
 
 774                                           RNDIS_STATUS_MEDIA_DISCONNECT);
 
 777 void rndis_uninit (int configNr)
 
 782         if (configNr >= RNDIS_MAX_CONFIGS)
 
 784         rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
 
 786         /* drain the response queue */
 
 787         while ((buf = rndis_get_next_response(configNr, &length)))
 
 788                 rndis_free_response(configNr, buf);
 
 791 void rndis_set_host_mac (int configNr, const u8 *addr)
 
 793         rndis_per_dev_params [configNr].host_mac = addr;
 
 799 int rndis_msg_parser (u8 configNr, u8 *buf)
 
 801         u32 MsgType, MsgLength;
 
 803         struct rndis_params             *params;
 
 808         tmp = (__le32 *) buf;
 
 809         MsgType   = get_unaligned_le32(tmp++);
 
 810         MsgLength = get_unaligned_le32(tmp++);
 
 812         if (configNr >= RNDIS_MAX_CONFIGS)
 
 814         params = &rndis_per_dev_params [configNr];
 
 816         /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
 
 817          * rx/tx statistics and link status, in addition to KEEPALIVE traffic
 
 818          * and normal HC level polling to see if there's any IN traffic.
 
 821         /* For USB: responses may take up to 10 seconds */
 
 823         case REMOTE_NDIS_INITIALIZE_MSG:
 
 824                 DBG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
 
 826                 params->state = RNDIS_INITIALIZED;
 
 827                 return  rndis_init_response (configNr,
 
 828                                         (rndis_init_msg_type *) buf);
 
 830         case REMOTE_NDIS_HALT_MSG:
 
 831                 DBG("%s: REMOTE_NDIS_HALT_MSG\n",
 
 833                 params->state = RNDIS_UNINITIALIZED;
 
 835                         netif_carrier_off (params->dev);
 
 836                         netif_stop_queue (params->dev);
 
 840         case REMOTE_NDIS_QUERY_MSG:
 
 841                 return rndis_query_response (configNr,
 
 842                                         (rndis_query_msg_type *) buf);
 
 844         case REMOTE_NDIS_SET_MSG:
 
 845                 return rndis_set_response (configNr,
 
 846                                         (rndis_set_msg_type *) buf);
 
 848         case REMOTE_NDIS_RESET_MSG:
 
 849                 DBG("%s: REMOTE_NDIS_RESET_MSG\n",
 
 851                 return rndis_reset_response (configNr,
 
 852                                         (rndis_reset_msg_type *) buf);
 
 854         case REMOTE_NDIS_KEEPALIVE_MSG:
 
 855                 /* For USB: host does this every 5 seconds */
 
 857                         DBG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
 
 859                 return rndis_keepalive_response (configNr,
 
 860                                                  (rndis_keepalive_msg_type *)
 
 864                 /* At least Windows XP emits some undefined RNDIS messages.
 
 865                  * In one case those messages seemed to relate to the host
 
 868                 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
 
 869                         __func__ , MsgType, MsgLength);
 
 872                         for (i = 0; i < MsgLength; i += 16) {
 
 874                                         " %02x %02x %02x %02x"
 
 875                                         " %02x %02x %02x %02x"
 
 876                                         " %02x %02x %02x %02x"
 
 877                                         " %02x %02x %02x %02x"
 
 885                                                 buf[i+10], buf[i+11],
 
 886                                         buf[i+12], buf [i+13],
 
 887                                                 buf[i+14], buf[i+15]);
 
 896 int rndis_register(void (*resp_avail)(void *v), void *v)
 
 903         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
 
 904                 if (!rndis_per_dev_params [i].used) {
 
 905                         rndis_per_dev_params [i].used = 1;
 
 906                         rndis_per_dev_params [i].resp_avail = resp_avail;
 
 907                         rndis_per_dev_params [i].v = v;
 
 908                         DBG("%s: configNr = %d\n", __func__, i);
 
 917 void rndis_deregister (int configNr)
 
 919         DBG("%s: \n", __func__ );
 
 921         if (configNr >= RNDIS_MAX_CONFIGS) return;
 
 922         rndis_per_dev_params [configNr].used = 0;
 
 927 int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
 
 929         DBG("%s:\n", __func__ );
 
 932         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
 
 934         rndis_per_dev_params [configNr].dev = dev;
 
 935         rndis_per_dev_params [configNr].filter = cdc_filter;
 
 940 int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
 
 942         DBG("%s:\n", __func__ );
 
 943         if (!vendorDescr) return -1;
 
 944         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
 
 946         rndis_per_dev_params [configNr].vendorID = vendorID;
 
 947         rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
 
 952 int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
 
 954         DBG("%s: %u %u\n", __func__, medium, speed);
 
 955         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
 
 957         rndis_per_dev_params [configNr].medium = medium;
 
 958         rndis_per_dev_params [configNr].speed = speed;
 
 963 void rndis_add_hdr (struct sk_buff *skb)
 
 965         struct rndis_packet_msg_type    *header;
 
 969         header = (void *) skb_push (skb, sizeof *header);
 
 970         memset (header, 0, sizeof *header);
 
 971         header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
 
 972         header->MessageLength = cpu_to_le32(skb->len);
 
 973         header->DataOffset = __constant_cpu_to_le32 (36);
 
 974         header->DataLength = cpu_to_le32(skb->len - sizeof *header);
 
 977 void rndis_free_response (int configNr, u8 *buf)
 
 980         struct list_head        *act, *tmp;
 
 982         list_for_each_safe (act, tmp,
 
 983                         &(rndis_per_dev_params [configNr].resp_queue))
 
 985                 r = list_entry (act, rndis_resp_t, list);
 
 986                 if (r && r->buf == buf) {
 
 993 u8 *rndis_get_next_response (int configNr, u32 *length)
 
 996         struct list_head        *act, *tmp;
 
 998         if (!length) return NULL;
 
1000         list_for_each_safe (act, tmp,
 
1001                         &(rndis_per_dev_params [configNr].resp_queue))
 
1003                 r = list_entry (act, rndis_resp_t, list);
 
1006                         *length = r->length;
 
1014 static rndis_resp_t *rndis_add_response (int configNr, u32 length)
 
1018         /* NOTE:  this gets copied into ether.c USB_BUFSIZ bytes ... */
 
1019         r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
 
1020         if (!r) return NULL;
 
1022         r->buf = (u8 *) (r + 1);
 
1026         list_add_tail (&r->list,
 
1027                 &(rndis_per_dev_params [configNr].resp_queue));
 
1031 int rndis_rm_hdr(struct sk_buff *skb)
 
1033         /* tmp points to a struct rndis_packet_msg_type */
 
1034         __le32          *tmp = (void *) skb->data;
 
1036         /* MessageType, MessageLength */
 
1037         if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
 
1038                         != get_unaligned(tmp++))
 
1042         /* DataOffset, DataLength */
 
1043         if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8))
 
1045         skb_trim(skb, get_unaligned_le32(tmp++));
 
1050 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
 
1052 static int rndis_proc_show(struct seq_file *m, void *v)
 
1054         rndis_params *param = m->private;
 
1063                          "vendor ID : 0x%08X\n"
 
1065                          param->confignr, (param->used) ? "y" : "n",
 
1067                          switch (param->state) {
 
1068                          case RNDIS_UNINITIALIZED:
 
1069                                 s = "RNDIS_UNINITIALIZED"; break;
 
1070                          case RNDIS_INITIALIZED:
 
1071                                 s = "RNDIS_INITIALIZED"; break;
 
1072                          case RNDIS_DATA_INITIALIZED:
 
1073                                 s = "RNDIS_DATA_INITIALIZED"; break;
 
1076                          (param->media_state) ? 0 : param->speed*100,
 
1077                          (param->media_state) ? "disconnected" : "connected",
 
1078                          param->vendorID, param->vendorDescr);
 
1082 static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
 
1083                 size_t count, loff_t *ppos)
 
1085         rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
 
1087         int i, fl_speed = 0;
 
1089         for (i = 0; i < count; i++) {
 
1091                 if (get_user(c, buffer))
 
1105                         speed = speed*10 + c - '0';
 
1109                         rndis_signal_connect (p->confignr);
 
1113                         rndis_signal_disconnect(p->confignr);
 
1116                         if (fl_speed) p->speed = speed;
 
1117                         else DBG("%c is not valid\n", c);
 
1127 static int rndis_proc_open(struct inode *inode, struct file *file)
 
1129         return single_open(file, rndis_proc_show, PDE(inode)->data);
 
1132 static const struct file_operations rndis_proc_fops = {
 
1133         .owner          = THIS_MODULE,
 
1134         .open           = rndis_proc_open,
 
1136         .llseek         = seq_lseek,
 
1137         .release        = single_release,
 
1138         .write          = rndis_proc_write,
 
1141 #define NAME_TEMPLATE   "driver/rndis-%03d"
 
1143 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
 
1145 #endif  /* CONFIG_USB_GADGET_DEBUG_FILES */
 
1148 int __init rndis_init (void)
 
1152         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
 
1153 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
 
1156                 sprintf (name, NAME_TEMPLATE, i);
 
1157                 if (!(rndis_connect_state [i]
 
1158                                 = proc_create_data(name, 0660, NULL,
 
1160                                         (void *)(rndis_per_dev_params + i))))
 
1162                         DBG("%s :remove entries", __func__);
 
1164                                 sprintf (name, NAME_TEMPLATE, --i);
 
1165                                 remove_proc_entry (name, NULL);
 
1171                 rndis_per_dev_params [i].confignr = i;
 
1172                 rndis_per_dev_params [i].used = 0;
 
1173                 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
 
1174                 rndis_per_dev_params [i].media_state
 
1175                                 = NDIS_MEDIA_STATE_DISCONNECTED;
 
1176                 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
 
1182 void rndis_exit (void)
 
1184 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
 
1188         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
 
1189                 sprintf (name, NAME_TEMPLATE, i);
 
1190                 remove_proc_entry (name, NULL);