15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 200a2430fSAndrzej Pietrasiewicz /* 300a2430fSAndrzej Pietrasiewicz * RNDIS MSG parser 400a2430fSAndrzej Pietrasiewicz * 500a2430fSAndrzej Pietrasiewicz * Authors: Benedikt Spranger, Pengutronix 600a2430fSAndrzej Pietrasiewicz * Robert Schwebel, Pengutronix 700a2430fSAndrzej Pietrasiewicz * 800a2430fSAndrzej Pietrasiewicz * This software was originally developed in conformance with 900a2430fSAndrzej Pietrasiewicz * Microsoft's Remote NDIS Specification License Agreement. 1000a2430fSAndrzej Pietrasiewicz * 1100a2430fSAndrzej Pietrasiewicz * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de> 1200a2430fSAndrzej Pietrasiewicz * Fixed message length bug in init_response 1300a2430fSAndrzej Pietrasiewicz * 1400a2430fSAndrzej Pietrasiewicz * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de> 1500a2430fSAndrzej Pietrasiewicz * Fixed rndis_rm_hdr length bug. 1600a2430fSAndrzej Pietrasiewicz * 1700a2430fSAndrzej Pietrasiewicz * Copyright (C) 2004 by David Brownell 1800a2430fSAndrzej Pietrasiewicz * updates to merge with Linux 2.6, better match RNDIS spec 1900a2430fSAndrzej Pietrasiewicz */ 2000a2430fSAndrzej Pietrasiewicz 2100a2430fSAndrzej Pietrasiewicz #include <linux/module.h> 2200a2430fSAndrzej Pietrasiewicz #include <linux/moduleparam.h> 2300a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 2400a2430fSAndrzej Pietrasiewicz #include <linux/errno.h> 25d6d22922SAndrzej Pietrasiewicz #include <linux/idr.h> 2600a2430fSAndrzej Pietrasiewicz #include <linux/list.h> 2700a2430fSAndrzej Pietrasiewicz #include <linux/proc_fs.h> 2800a2430fSAndrzej Pietrasiewicz #include <linux/slab.h> 2900a2430fSAndrzej Pietrasiewicz #include <linux/seq_file.h> 3000a2430fSAndrzej Pietrasiewicz #include <linux/netdevice.h> 3100a2430fSAndrzej Pietrasiewicz 3200a2430fSAndrzej Pietrasiewicz #include <asm/io.h> 3300a2430fSAndrzej Pietrasiewicz #include <asm/byteorder.h> 3400a2430fSAndrzej Pietrasiewicz #include <asm/unaligned.h> 3500a2430fSAndrzej Pietrasiewicz 3600a2430fSAndrzej Pietrasiewicz #include "u_rndis.h" 3700a2430fSAndrzej Pietrasiewicz 3800a2430fSAndrzej Pietrasiewicz #undef VERBOSE_DEBUG 3900a2430fSAndrzej Pietrasiewicz 4000a2430fSAndrzej Pietrasiewicz #include "rndis.h" 4100a2430fSAndrzej Pietrasiewicz 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz /* The driver for your USB chip needs to support ep0 OUT to work with 4400a2430fSAndrzej Pietrasiewicz * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional). 4500a2430fSAndrzej Pietrasiewicz * 4600a2430fSAndrzej Pietrasiewicz * Windows hosts need an INF file like Documentation/usb/linux.inf 4700a2430fSAndrzej Pietrasiewicz * and will be happier if you provide the host_addr module parameter. 4800a2430fSAndrzej Pietrasiewicz */ 4900a2430fSAndrzej Pietrasiewicz 5000a2430fSAndrzej Pietrasiewicz #if 0 5100a2430fSAndrzej Pietrasiewicz static int rndis_debug = 0; 5200a2430fSAndrzej Pietrasiewicz module_param (rndis_debug, int, 0); 5300a2430fSAndrzej Pietrasiewicz MODULE_PARM_DESC (rndis_debug, "enable debugging"); 5400a2430fSAndrzej Pietrasiewicz #else 5500a2430fSAndrzej Pietrasiewicz #define rndis_debug 0 5600a2430fSAndrzej Pietrasiewicz #endif 5700a2430fSAndrzej Pietrasiewicz 58d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 5900a2430fSAndrzej Pietrasiewicz 60d6d22922SAndrzej Pietrasiewicz #define NAME_TEMPLATE "driver/rndis-%03d" 6100a2430fSAndrzej Pietrasiewicz 62d6d22922SAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 63d6d22922SAndrzej Pietrasiewicz 64d6d22922SAndrzej Pietrasiewicz static DEFINE_IDA(rndis_ida); 6500a2430fSAndrzej Pietrasiewicz 6600a2430fSAndrzej Pietrasiewicz /* Driver Version */ 6700a2430fSAndrzej Pietrasiewicz static const __le32 rndis_driver_version = cpu_to_le32(1); 6800a2430fSAndrzej Pietrasiewicz 6900a2430fSAndrzej Pietrasiewicz /* Function Prototypes */ 7083210e59SAndrzej Pietrasiewicz static rndis_resp_t *rndis_add_response(struct rndis_params *params, 7183210e59SAndrzej Pietrasiewicz u32 length); 7200a2430fSAndrzej Pietrasiewicz 73d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 74d6d22922SAndrzej Pietrasiewicz 7597a32539SAlexey Dobriyan static const struct proc_ops rndis_proc_ops; 76d6d22922SAndrzej Pietrasiewicz 77d6d22922SAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 7800a2430fSAndrzej Pietrasiewicz 7900a2430fSAndrzej Pietrasiewicz /* supported OIDs */ 8097f0117bSAnson Jacob static const u32 oid_supported_list[] = { 8100a2430fSAndrzej Pietrasiewicz /* the general stuff */ 8200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_SUPPORTED_LIST, 8300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_HARDWARE_STATUS, 8400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_SUPPORTED, 8500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_IN_USE, 8600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE, 8700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_LINK_SPEED, 8800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE, 8900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE, 9000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_ID, 9100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_DESCRIPTION, 9200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_DRIVER_VERSION, 9300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_CURRENT_PACKET_FILTER, 9400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE, 9500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_CONNECT_STATUS, 9600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_PHYSICAL_MEDIUM, 9700a2430fSAndrzej Pietrasiewicz 9800a2430fSAndrzej Pietrasiewicz /* the statistical stuff */ 9900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_XMIT_OK, 10000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_OK, 10100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_XMIT_ERROR, 10200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_ERROR, 10300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_NO_BUFFER, 10400a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_OPTIONAL_STATS 10500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_BYTES_XMIT, 10600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT, 10700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_BYTES_XMIT, 10800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT, 10900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_BYTES_XMIT, 11000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT, 11100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_BYTES_RCV, 11200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_FRAMES_RCV, 11300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_BYTES_RCV, 11400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_FRAMES_RCV, 11500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_BYTES_RCV, 11600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_FRAMES_RCV, 11700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_CRC_ERROR, 11800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH, 11900a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_OPTIONAL_STATS */ 12000a2430fSAndrzej Pietrasiewicz 12100a2430fSAndrzej Pietrasiewicz /* mandatory 802.3 */ 12200a2430fSAndrzej Pietrasiewicz /* the general stuff */ 12300a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_PERMANENT_ADDRESS, 12400a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_CURRENT_ADDRESS, 12500a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MULTICAST_LIST, 12600a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MAC_OPTIONS, 12700a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MAXIMUM_LIST_SIZE, 12800a2430fSAndrzej Pietrasiewicz 12900a2430fSAndrzej Pietrasiewicz /* the statistical stuff */ 13000a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT, 13100a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_ONE_COLLISION, 13200a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_MORE_COLLISIONS, 13300a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_OPTIONAL_STATS 13400a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_DEFERRED, 13500a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_MAX_COLLISIONS, 13600a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_RCV_OVERRUN, 13700a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_UNDERRUN, 13800a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE, 13900a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST, 14000a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_LATE_COLLISIONS, 14100a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_OPTIONAL_STATS */ 14200a2430fSAndrzej Pietrasiewicz 14300a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_PM 14400a2430fSAndrzej Pietrasiewicz /* PM and wakeup are "mandatory" for USB, but the RNDIS specs 14500a2430fSAndrzej Pietrasiewicz * don't say what they mean ... and the NDIS specs are often 14600a2430fSAndrzej Pietrasiewicz * confusing and/or ambiguous in this context. (That is, more 14700a2430fSAndrzej Pietrasiewicz * so than their specs for the other OIDs.) 14800a2430fSAndrzej Pietrasiewicz * 14900a2430fSAndrzej Pietrasiewicz * FIXME someone who knows what these should do, please 15000a2430fSAndrzej Pietrasiewicz * implement them! 15100a2430fSAndrzej Pietrasiewicz */ 15200a2430fSAndrzej Pietrasiewicz 15300a2430fSAndrzej Pietrasiewicz /* power management */ 15400a2430fSAndrzej Pietrasiewicz OID_PNP_CAPABILITIES, 15500a2430fSAndrzej Pietrasiewicz OID_PNP_QUERY_POWER, 15600a2430fSAndrzej Pietrasiewicz OID_PNP_SET_POWER, 15700a2430fSAndrzej Pietrasiewicz 15800a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_WAKEUP 15900a2430fSAndrzej Pietrasiewicz /* wake up host */ 16000a2430fSAndrzej Pietrasiewicz OID_PNP_ENABLE_WAKE_UP, 16100a2430fSAndrzej Pietrasiewicz OID_PNP_ADD_WAKE_UP_PATTERN, 16200a2430fSAndrzej Pietrasiewicz OID_PNP_REMOVE_WAKE_UP_PATTERN, 16300a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_WAKEUP */ 16400a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_PM */ 16500a2430fSAndrzej Pietrasiewicz }; 16600a2430fSAndrzej Pietrasiewicz 16700a2430fSAndrzej Pietrasiewicz 16800a2430fSAndrzej Pietrasiewicz /* NDIS Functions */ 16983210e59SAndrzej Pietrasiewicz static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf, 17000a2430fSAndrzej Pietrasiewicz unsigned buf_len, rndis_resp_t *r) 17100a2430fSAndrzej Pietrasiewicz { 17200a2430fSAndrzej Pietrasiewicz int retval = -ENOTSUPP; 17300a2430fSAndrzej Pietrasiewicz u32 length = 4; /* usually */ 17400a2430fSAndrzej Pietrasiewicz __le32 *outbuf; 17500a2430fSAndrzej Pietrasiewicz int i, count; 17600a2430fSAndrzej Pietrasiewicz rndis_query_cmplt_type *resp; 17700a2430fSAndrzej Pietrasiewicz struct net_device *net; 17800a2430fSAndrzej Pietrasiewicz struct rtnl_link_stats64 temp; 17900a2430fSAndrzej Pietrasiewicz const struct rtnl_link_stats64 *stats; 18000a2430fSAndrzej Pietrasiewicz 18100a2430fSAndrzej Pietrasiewicz if (!r) return -ENOMEM; 18200a2430fSAndrzej Pietrasiewicz resp = (rndis_query_cmplt_type *)r->buf; 18300a2430fSAndrzej Pietrasiewicz 18400a2430fSAndrzej Pietrasiewicz if (!resp) return -ENOMEM; 18500a2430fSAndrzej Pietrasiewicz 18600a2430fSAndrzej Pietrasiewicz if (buf_len && rndis_debug > 1) { 18700a2430fSAndrzej Pietrasiewicz pr_debug("query OID %08x value, len %d:\n", OID, buf_len); 18800a2430fSAndrzej Pietrasiewicz for (i = 0; i < buf_len; i += 16) { 18900a2430fSAndrzej Pietrasiewicz pr_debug("%03d: %08x %08x %08x %08x\n", i, 19000a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i]), 19100a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 4]), 19200a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 8]), 19300a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 12])); 19400a2430fSAndrzej Pietrasiewicz } 19500a2430fSAndrzej Pietrasiewicz } 19600a2430fSAndrzej Pietrasiewicz 19700a2430fSAndrzej Pietrasiewicz /* response goes here, right after the header */ 19800a2430fSAndrzej Pietrasiewicz outbuf = (__le32 *)&resp[1]; 19900a2430fSAndrzej Pietrasiewicz resp->InformationBufferOffset = cpu_to_le32(16); 20000a2430fSAndrzej Pietrasiewicz 20183210e59SAndrzej Pietrasiewicz net = params->dev; 20200a2430fSAndrzej Pietrasiewicz stats = dev_get_stats(net, &temp); 20300a2430fSAndrzej Pietrasiewicz 20400a2430fSAndrzej Pietrasiewicz switch (OID) { 20500a2430fSAndrzej Pietrasiewicz 20600a2430fSAndrzej Pietrasiewicz /* general oids (table 4-1) */ 20700a2430fSAndrzej Pietrasiewicz 20800a2430fSAndrzej Pietrasiewicz /* mandatory */ 20900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_SUPPORTED_LIST: 21000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__); 21100a2430fSAndrzej Pietrasiewicz length = sizeof(oid_supported_list); 21200a2430fSAndrzej Pietrasiewicz count = length / sizeof(u32); 21300a2430fSAndrzej Pietrasiewicz for (i = 0; i < count; i++) 21400a2430fSAndrzej Pietrasiewicz outbuf[i] = cpu_to_le32(oid_supported_list[i]); 21500a2430fSAndrzej Pietrasiewicz retval = 0; 21600a2430fSAndrzej Pietrasiewicz break; 21700a2430fSAndrzej Pietrasiewicz 21800a2430fSAndrzej Pietrasiewicz /* mandatory */ 21900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_HARDWARE_STATUS: 22000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__); 22100a2430fSAndrzej Pietrasiewicz /* Bogus question! 22200a2430fSAndrzej Pietrasiewicz * Hardware must be ready to receive high level protocols. 22300a2430fSAndrzej Pietrasiewicz * BTW: 22400a2430fSAndrzej Pietrasiewicz * reddite ergo quae sunt Caesaris Caesari 22500a2430fSAndrzej Pietrasiewicz * et quae sunt Dei Deo! 22600a2430fSAndrzej Pietrasiewicz */ 22700a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 22800a2430fSAndrzej Pietrasiewicz retval = 0; 22900a2430fSAndrzej Pietrasiewicz break; 23000a2430fSAndrzej Pietrasiewicz 23100a2430fSAndrzej Pietrasiewicz /* mandatory */ 23200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_SUPPORTED: 23300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__); 23483210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->medium); 23500a2430fSAndrzej Pietrasiewicz retval = 0; 23600a2430fSAndrzej Pietrasiewicz break; 23700a2430fSAndrzej Pietrasiewicz 23800a2430fSAndrzej Pietrasiewicz /* mandatory */ 23900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_IN_USE: 24000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__); 24100a2430fSAndrzej Pietrasiewicz /* one medium, one transport... (maybe you do it better) */ 24283210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->medium); 24300a2430fSAndrzej Pietrasiewicz retval = 0; 24400a2430fSAndrzej Pietrasiewicz break; 24500a2430fSAndrzej Pietrasiewicz 24600a2430fSAndrzej Pietrasiewicz /* mandatory */ 24700a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE: 24800a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__); 24983210e59SAndrzej Pietrasiewicz if (params->dev) { 25083210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 25100a2430fSAndrzej Pietrasiewicz retval = 0; 25200a2430fSAndrzej Pietrasiewicz } 25300a2430fSAndrzej Pietrasiewicz break; 25400a2430fSAndrzej Pietrasiewicz 25500a2430fSAndrzej Pietrasiewicz /* mandatory */ 25600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_LINK_SPEED: 25700a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 25800a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__); 25983210e59SAndrzej Pietrasiewicz if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED) 26000a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 26100a2430fSAndrzej Pietrasiewicz else 26283210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->speed); 26300a2430fSAndrzej Pietrasiewicz retval = 0; 26400a2430fSAndrzej Pietrasiewicz break; 26500a2430fSAndrzej Pietrasiewicz 26600a2430fSAndrzej Pietrasiewicz /* mandatory */ 26700a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE: 26800a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__); 26983210e59SAndrzej Pietrasiewicz if (params->dev) { 27083210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 27100a2430fSAndrzej Pietrasiewicz retval = 0; 27200a2430fSAndrzej Pietrasiewicz } 27300a2430fSAndrzej Pietrasiewicz break; 27400a2430fSAndrzej Pietrasiewicz 27500a2430fSAndrzej Pietrasiewicz /* mandatory */ 27600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE: 27700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__); 27883210e59SAndrzej Pietrasiewicz if (params->dev) { 27983210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 28000a2430fSAndrzej Pietrasiewicz retval = 0; 28100a2430fSAndrzej Pietrasiewicz } 28200a2430fSAndrzej Pietrasiewicz break; 28300a2430fSAndrzej Pietrasiewicz 28400a2430fSAndrzej Pietrasiewicz /* mandatory */ 28500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_ID: 28600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__); 28783210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->vendorID); 28800a2430fSAndrzej Pietrasiewicz retval = 0; 28900a2430fSAndrzej Pietrasiewicz break; 29000a2430fSAndrzej Pietrasiewicz 29100a2430fSAndrzej Pietrasiewicz /* mandatory */ 29200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_DESCRIPTION: 29300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__); 29483210e59SAndrzej Pietrasiewicz if (params->vendorDescr) { 29583210e59SAndrzej Pietrasiewicz length = strlen(params->vendorDescr); 29683210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->vendorDescr, length); 29700a2430fSAndrzej Pietrasiewicz } else { 29800a2430fSAndrzej Pietrasiewicz outbuf[0] = 0; 29900a2430fSAndrzej Pietrasiewicz } 30000a2430fSAndrzej Pietrasiewicz retval = 0; 30100a2430fSAndrzej Pietrasiewicz break; 30200a2430fSAndrzej Pietrasiewicz 30300a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION: 30400a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__); 30500a2430fSAndrzej Pietrasiewicz /* Created as LE */ 30600a2430fSAndrzej Pietrasiewicz *outbuf = rndis_driver_version; 30700a2430fSAndrzej Pietrasiewicz retval = 0; 30800a2430fSAndrzej Pietrasiewicz break; 30900a2430fSAndrzej Pietrasiewicz 31000a2430fSAndrzej Pietrasiewicz /* mandatory */ 31100a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_CURRENT_PACKET_FILTER: 31200a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__); 31383210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(*params->filter); 31400a2430fSAndrzej Pietrasiewicz retval = 0; 31500a2430fSAndrzej Pietrasiewicz break; 31600a2430fSAndrzej Pietrasiewicz 31700a2430fSAndrzej Pietrasiewicz /* mandatory */ 31800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE: 31900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__); 32000a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE); 32100a2430fSAndrzej Pietrasiewicz retval = 0; 32200a2430fSAndrzej Pietrasiewicz break; 32300a2430fSAndrzej Pietrasiewicz 32400a2430fSAndrzej Pietrasiewicz /* mandatory */ 32500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS: 32600a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 32700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__); 32883210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->media_state); 32900a2430fSAndrzej Pietrasiewicz retval = 0; 33000a2430fSAndrzej Pietrasiewicz break; 33100a2430fSAndrzej Pietrasiewicz 33200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_PHYSICAL_MEDIUM: 33300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__); 33400a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 33500a2430fSAndrzej Pietrasiewicz retval = 0; 33600a2430fSAndrzej Pietrasiewicz break; 33700a2430fSAndrzej Pietrasiewicz 33800a2430fSAndrzej Pietrasiewicz /* The RNDIS specification is incomplete/wrong. Some versions 33900a2430fSAndrzej Pietrasiewicz * of MS-Windows expect OIDs that aren't specified there. Other 34000a2430fSAndrzej Pietrasiewicz * versions emit undefined RNDIS messages. DOCUMENT ALL THESE! 34100a2430fSAndrzej Pietrasiewicz */ 34200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */ 34300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__); 34400a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32( 34500a2430fSAndrzej Pietrasiewicz RNDIS_MAC_OPTION_RECEIVE_SERIALIZED 34600a2430fSAndrzej Pietrasiewicz | RNDIS_MAC_OPTION_FULL_DUPLEX); 34700a2430fSAndrzej Pietrasiewicz retval = 0; 34800a2430fSAndrzej Pietrasiewicz break; 34900a2430fSAndrzej Pietrasiewicz 35000a2430fSAndrzej Pietrasiewicz /* statistics OIDs (table 4-2) */ 35100a2430fSAndrzej Pietrasiewicz 35200a2430fSAndrzej Pietrasiewicz /* mandatory */ 35300a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_XMIT_OK: 35400a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 35500a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__); 35600a2430fSAndrzej Pietrasiewicz if (stats) { 35700a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->tx_packets 35800a2430fSAndrzej Pietrasiewicz - stats->tx_errors - stats->tx_dropped); 35900a2430fSAndrzej Pietrasiewicz retval = 0; 36000a2430fSAndrzej Pietrasiewicz } 36100a2430fSAndrzej Pietrasiewicz break; 36200a2430fSAndrzej Pietrasiewicz 36300a2430fSAndrzej Pietrasiewicz /* mandatory */ 36400a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_OK: 36500a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 36600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__); 36700a2430fSAndrzej Pietrasiewicz if (stats) { 36800a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_packets 36900a2430fSAndrzej Pietrasiewicz - stats->rx_errors - stats->rx_dropped); 37000a2430fSAndrzej Pietrasiewicz retval = 0; 37100a2430fSAndrzej Pietrasiewicz } 37200a2430fSAndrzej Pietrasiewicz break; 37300a2430fSAndrzej Pietrasiewicz 37400a2430fSAndrzej Pietrasiewicz /* mandatory */ 37500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_XMIT_ERROR: 37600a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 37700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__); 37800a2430fSAndrzej Pietrasiewicz if (stats) { 37900a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->tx_errors); 38000a2430fSAndrzej Pietrasiewicz retval = 0; 38100a2430fSAndrzej Pietrasiewicz } 38200a2430fSAndrzej Pietrasiewicz break; 38300a2430fSAndrzej Pietrasiewicz 38400a2430fSAndrzej Pietrasiewicz /* mandatory */ 38500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_ERROR: 38600a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 38700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__); 38800a2430fSAndrzej Pietrasiewicz if (stats) { 38900a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_errors); 39000a2430fSAndrzej Pietrasiewicz retval = 0; 39100a2430fSAndrzej Pietrasiewicz } 39200a2430fSAndrzej Pietrasiewicz break; 39300a2430fSAndrzej Pietrasiewicz 39400a2430fSAndrzej Pietrasiewicz /* mandatory */ 39500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_NO_BUFFER: 39600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__); 39700a2430fSAndrzej Pietrasiewicz if (stats) { 39800a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_dropped); 39900a2430fSAndrzej Pietrasiewicz retval = 0; 40000a2430fSAndrzej Pietrasiewicz } 40100a2430fSAndrzej Pietrasiewicz break; 40200a2430fSAndrzej Pietrasiewicz 40300a2430fSAndrzej Pietrasiewicz /* ieee802.3 OIDs (table 4-3) */ 40400a2430fSAndrzej Pietrasiewicz 40500a2430fSAndrzej Pietrasiewicz /* mandatory */ 40600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_PERMANENT_ADDRESS: 40700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__); 40883210e59SAndrzej Pietrasiewicz if (params->dev) { 40900a2430fSAndrzej Pietrasiewicz length = ETH_ALEN; 41083210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->host_mac, length); 41100a2430fSAndrzej Pietrasiewicz retval = 0; 41200a2430fSAndrzej Pietrasiewicz } 41300a2430fSAndrzej Pietrasiewicz break; 41400a2430fSAndrzej Pietrasiewicz 41500a2430fSAndrzej Pietrasiewicz /* mandatory */ 41600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_CURRENT_ADDRESS: 41700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__); 41883210e59SAndrzej Pietrasiewicz if (params->dev) { 41900a2430fSAndrzej Pietrasiewicz length = ETH_ALEN; 42083210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->host_mac, length); 42100a2430fSAndrzej Pietrasiewicz retval = 0; 42200a2430fSAndrzej Pietrasiewicz } 42300a2430fSAndrzej Pietrasiewicz break; 42400a2430fSAndrzej Pietrasiewicz 42500a2430fSAndrzej Pietrasiewicz /* mandatory */ 42600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MULTICAST_LIST: 42700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__); 42800a2430fSAndrzej Pietrasiewicz /* Multicast base address only */ 42900a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0xE0000000); 43000a2430fSAndrzej Pietrasiewicz retval = 0; 43100a2430fSAndrzej Pietrasiewicz break; 43200a2430fSAndrzej Pietrasiewicz 43300a2430fSAndrzej Pietrasiewicz /* mandatory */ 43400a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE: 43500a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__); 43600a2430fSAndrzej Pietrasiewicz /* Multicast base address only */ 43700a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(1); 43800a2430fSAndrzej Pietrasiewicz retval = 0; 43900a2430fSAndrzej Pietrasiewicz break; 44000a2430fSAndrzej Pietrasiewicz 44100a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MAC_OPTIONS: 44200a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__); 44300a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 44400a2430fSAndrzej Pietrasiewicz retval = 0; 44500a2430fSAndrzej Pietrasiewicz break; 44600a2430fSAndrzej Pietrasiewicz 44700a2430fSAndrzej Pietrasiewicz /* ieee802.3 statistics OIDs (table 4-4) */ 44800a2430fSAndrzej Pietrasiewicz 44900a2430fSAndrzej Pietrasiewicz /* mandatory */ 45000a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT: 45100a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__); 45200a2430fSAndrzej Pietrasiewicz if (stats) { 45300a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_frame_errors); 45400a2430fSAndrzej Pietrasiewicz retval = 0; 45500a2430fSAndrzej Pietrasiewicz } 45600a2430fSAndrzej Pietrasiewicz break; 45700a2430fSAndrzej Pietrasiewicz 45800a2430fSAndrzej Pietrasiewicz /* mandatory */ 45900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_XMIT_ONE_COLLISION: 46000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__); 46100a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 46200a2430fSAndrzej Pietrasiewicz retval = 0; 46300a2430fSAndrzej Pietrasiewicz break; 46400a2430fSAndrzej Pietrasiewicz 46500a2430fSAndrzej Pietrasiewicz /* mandatory */ 46600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS: 46700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__); 46800a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 46900a2430fSAndrzej Pietrasiewicz retval = 0; 47000a2430fSAndrzej Pietrasiewicz break; 47100a2430fSAndrzej Pietrasiewicz 47200a2430fSAndrzej Pietrasiewicz default: 4733f5ad864SJoe Perches pr_warn("%s: query unknown OID 0x%08X\n", __func__, OID); 47400a2430fSAndrzej Pietrasiewicz } 47500a2430fSAndrzej Pietrasiewicz if (retval < 0) 47600a2430fSAndrzej Pietrasiewicz length = 0; 47700a2430fSAndrzej Pietrasiewicz 47800a2430fSAndrzej Pietrasiewicz resp->InformationBufferLength = cpu_to_le32(length); 47900a2430fSAndrzej Pietrasiewicz r->length = length + sizeof(*resp); 48000a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(r->length); 48100a2430fSAndrzej Pietrasiewicz return retval; 48200a2430fSAndrzej Pietrasiewicz } 48300a2430fSAndrzej Pietrasiewicz 48483210e59SAndrzej Pietrasiewicz static int gen_ndis_set_resp(struct rndis_params *params, u32 OID, 48583210e59SAndrzej Pietrasiewicz u8 *buf, u32 buf_len, rndis_resp_t *r) 48600a2430fSAndrzej Pietrasiewicz { 48700a2430fSAndrzej Pietrasiewicz rndis_set_cmplt_type *resp; 48800a2430fSAndrzej Pietrasiewicz int i, retval = -ENOTSUPP; 48900a2430fSAndrzej Pietrasiewicz 49000a2430fSAndrzej Pietrasiewicz if (!r) 49100a2430fSAndrzej Pietrasiewicz return -ENOMEM; 49200a2430fSAndrzej Pietrasiewicz resp = (rndis_set_cmplt_type *)r->buf; 49300a2430fSAndrzej Pietrasiewicz if (!resp) 49400a2430fSAndrzej Pietrasiewicz return -ENOMEM; 49500a2430fSAndrzej Pietrasiewicz 49600a2430fSAndrzej Pietrasiewicz if (buf_len && rndis_debug > 1) { 49700a2430fSAndrzej Pietrasiewicz pr_debug("set OID %08x value, len %d:\n", OID, buf_len); 49800a2430fSAndrzej Pietrasiewicz for (i = 0; i < buf_len; i += 16) { 49900a2430fSAndrzej Pietrasiewicz pr_debug("%03d: %08x %08x %08x %08x\n", i, 50000a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i]), 50100a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 4]), 50200a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 8]), 50300a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 12])); 50400a2430fSAndrzej Pietrasiewicz } 50500a2430fSAndrzej Pietrasiewicz } 50600a2430fSAndrzej Pietrasiewicz 50700a2430fSAndrzej Pietrasiewicz switch (OID) { 50800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_CURRENT_PACKET_FILTER: 50900a2430fSAndrzej Pietrasiewicz 51000a2430fSAndrzej Pietrasiewicz /* these NDIS_PACKET_TYPE_* bitflags are shared with 51100a2430fSAndrzej Pietrasiewicz * cdc_filter; it's not RNDIS-specific 51200a2430fSAndrzej Pietrasiewicz * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in: 51300a2430fSAndrzej Pietrasiewicz * PROMISCUOUS, DIRECTED, 51400a2430fSAndrzej Pietrasiewicz * MULTICAST, ALL_MULTICAST, BROADCAST 51500a2430fSAndrzej Pietrasiewicz */ 51600a2430fSAndrzej Pietrasiewicz *params->filter = (u16)get_unaligned_le32(buf); 51700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n", 51800a2430fSAndrzej Pietrasiewicz __func__, *params->filter); 51900a2430fSAndrzej Pietrasiewicz 52000a2430fSAndrzej Pietrasiewicz /* this call has a significant side effect: it's 52100a2430fSAndrzej Pietrasiewicz * what makes the packet flow start and stop, like 52200a2430fSAndrzej Pietrasiewicz * activating the CDC Ethernet altsetting. 52300a2430fSAndrzej Pietrasiewicz */ 52400a2430fSAndrzej Pietrasiewicz retval = 0; 52500a2430fSAndrzej Pietrasiewicz if (*params->filter) { 52600a2430fSAndrzej Pietrasiewicz params->state = RNDIS_DATA_INITIALIZED; 52700a2430fSAndrzej Pietrasiewicz netif_carrier_on(params->dev); 52800a2430fSAndrzej Pietrasiewicz if (netif_running(params->dev)) 52900a2430fSAndrzej Pietrasiewicz netif_wake_queue(params->dev); 53000a2430fSAndrzej Pietrasiewicz } else { 53100a2430fSAndrzej Pietrasiewicz params->state = RNDIS_INITIALIZED; 53200a2430fSAndrzej Pietrasiewicz netif_carrier_off(params->dev); 53300a2430fSAndrzej Pietrasiewicz netif_stop_queue(params->dev); 53400a2430fSAndrzej Pietrasiewicz } 53500a2430fSAndrzej Pietrasiewicz break; 53600a2430fSAndrzej Pietrasiewicz 53700a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MULTICAST_LIST: 53800a2430fSAndrzej Pietrasiewicz /* I think we can ignore this */ 53900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__); 54000a2430fSAndrzej Pietrasiewicz retval = 0; 54100a2430fSAndrzej Pietrasiewicz break; 54200a2430fSAndrzej Pietrasiewicz 54300a2430fSAndrzej Pietrasiewicz default: 5443f5ad864SJoe Perches pr_warn("%s: set unknown OID 0x%08X, size %d\n", 54500a2430fSAndrzej Pietrasiewicz __func__, OID, buf_len); 54600a2430fSAndrzej Pietrasiewicz } 54700a2430fSAndrzej Pietrasiewicz 54800a2430fSAndrzej Pietrasiewicz return retval; 54900a2430fSAndrzej Pietrasiewicz } 55000a2430fSAndrzej Pietrasiewicz 55100a2430fSAndrzej Pietrasiewicz /* 55200a2430fSAndrzej Pietrasiewicz * Response Functions 55300a2430fSAndrzej Pietrasiewicz */ 55400a2430fSAndrzej Pietrasiewicz 55583210e59SAndrzej Pietrasiewicz static int rndis_init_response(struct rndis_params *params, 55683210e59SAndrzej Pietrasiewicz rndis_init_msg_type *buf) 55700a2430fSAndrzej Pietrasiewicz { 55800a2430fSAndrzej Pietrasiewicz rndis_init_cmplt_type *resp; 55900a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 56000a2430fSAndrzej Pietrasiewicz 56100a2430fSAndrzej Pietrasiewicz if (!params->dev) 56200a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 56300a2430fSAndrzej Pietrasiewicz 56483210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_init_cmplt_type)); 56500a2430fSAndrzej Pietrasiewicz if (!r) 56600a2430fSAndrzej Pietrasiewicz return -ENOMEM; 56700a2430fSAndrzej Pietrasiewicz resp = (rndis_init_cmplt_type *)r->buf; 56800a2430fSAndrzej Pietrasiewicz 56900a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C); 57000a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(52); 57100a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 57200a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 57300a2430fSAndrzej Pietrasiewicz resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION); 57400a2430fSAndrzej Pietrasiewicz resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION); 57500a2430fSAndrzej Pietrasiewicz resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS); 57600a2430fSAndrzej Pietrasiewicz resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3); 57700a2430fSAndrzej Pietrasiewicz resp->MaxPacketsPerTransfer = cpu_to_le32(1); 57800a2430fSAndrzej Pietrasiewicz resp->MaxTransferSize = cpu_to_le32( 57900a2430fSAndrzej Pietrasiewicz params->dev->mtu 58000a2430fSAndrzej Pietrasiewicz + sizeof(struct ethhdr) 58100a2430fSAndrzej Pietrasiewicz + sizeof(struct rndis_packet_msg_type) 58200a2430fSAndrzej Pietrasiewicz + 22); 58300a2430fSAndrzej Pietrasiewicz resp->PacketAlignmentFactor = cpu_to_le32(0); 58400a2430fSAndrzej Pietrasiewicz resp->AFListOffset = cpu_to_le32(0); 58500a2430fSAndrzej Pietrasiewicz resp->AFListSize = cpu_to_le32(0); 58600a2430fSAndrzej Pietrasiewicz 58700a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 58800a2430fSAndrzej Pietrasiewicz return 0; 58900a2430fSAndrzej Pietrasiewicz } 59000a2430fSAndrzej Pietrasiewicz 59183210e59SAndrzej Pietrasiewicz static int rndis_query_response(struct rndis_params *params, 59283210e59SAndrzej Pietrasiewicz rndis_query_msg_type *buf) 59300a2430fSAndrzej Pietrasiewicz { 59400a2430fSAndrzej Pietrasiewicz rndis_query_cmplt_type *resp; 59500a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 59600a2430fSAndrzej Pietrasiewicz 59700a2430fSAndrzej Pietrasiewicz /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */ 59800a2430fSAndrzej Pietrasiewicz if (!params->dev) 59900a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 60000a2430fSAndrzej Pietrasiewicz 60100a2430fSAndrzej Pietrasiewicz /* 60200a2430fSAndrzej Pietrasiewicz * we need more memory: 60300a2430fSAndrzej Pietrasiewicz * gen_ndis_query_resp expects enough space for 60400a2430fSAndrzej Pietrasiewicz * rndis_query_cmplt_type followed by data. 60500a2430fSAndrzej Pietrasiewicz * oid_supported_list is the largest data reply 60600a2430fSAndrzej Pietrasiewicz */ 60783210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, 60800a2430fSAndrzej Pietrasiewicz sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type)); 60900a2430fSAndrzej Pietrasiewicz if (!r) 61000a2430fSAndrzej Pietrasiewicz return -ENOMEM; 61100a2430fSAndrzej Pietrasiewicz resp = (rndis_query_cmplt_type *)r->buf; 61200a2430fSAndrzej Pietrasiewicz 61300a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C); 61400a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 61500a2430fSAndrzej Pietrasiewicz 61683210e59SAndrzej Pietrasiewicz if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID), 61700a2430fSAndrzej Pietrasiewicz le32_to_cpu(buf->InformationBufferOffset) 61800a2430fSAndrzej Pietrasiewicz + 8 + (u8 *)buf, 61900a2430fSAndrzej Pietrasiewicz le32_to_cpu(buf->InformationBufferLength), 62000a2430fSAndrzej Pietrasiewicz r)) { 62100a2430fSAndrzej Pietrasiewicz /* OID not supported */ 62200a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED); 62300a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(sizeof *resp); 62400a2430fSAndrzej Pietrasiewicz resp->InformationBufferLength = cpu_to_le32(0); 62500a2430fSAndrzej Pietrasiewicz resp->InformationBufferOffset = cpu_to_le32(0); 62600a2430fSAndrzej Pietrasiewicz } else 62700a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 62800a2430fSAndrzej Pietrasiewicz 62900a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 63000a2430fSAndrzej Pietrasiewicz return 0; 63100a2430fSAndrzej Pietrasiewicz } 63200a2430fSAndrzej Pietrasiewicz 63383210e59SAndrzej Pietrasiewicz static int rndis_set_response(struct rndis_params *params, 63483210e59SAndrzej Pietrasiewicz rndis_set_msg_type *buf) 63500a2430fSAndrzej Pietrasiewicz { 63600a2430fSAndrzej Pietrasiewicz u32 BufLength, BufOffset; 63700a2430fSAndrzej Pietrasiewicz rndis_set_cmplt_type *resp; 63800a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 63900a2430fSAndrzej Pietrasiewicz 640*38ea1eacSGreg Kroah-Hartman BufLength = le32_to_cpu(buf->InformationBufferLength); 641*38ea1eacSGreg Kroah-Hartman BufOffset = le32_to_cpu(buf->InformationBufferOffset); 642*38ea1eacSGreg Kroah-Hartman if ((BufLength > RNDIS_MAX_TOTAL_SIZE) || 643*38ea1eacSGreg Kroah-Hartman (BufOffset + 8 >= RNDIS_MAX_TOTAL_SIZE)) 644*38ea1eacSGreg Kroah-Hartman return -EINVAL; 645*38ea1eacSGreg Kroah-Hartman 64683210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_set_cmplt_type)); 64700a2430fSAndrzej Pietrasiewicz if (!r) 64800a2430fSAndrzej Pietrasiewicz return -ENOMEM; 64900a2430fSAndrzej Pietrasiewicz resp = (rndis_set_cmplt_type *)r->buf; 65000a2430fSAndrzej Pietrasiewicz 65100a2430fSAndrzej Pietrasiewicz #ifdef VERBOSE_DEBUG 65200a2430fSAndrzej Pietrasiewicz pr_debug("%s: Length: %d\n", __func__, BufLength); 65300a2430fSAndrzej Pietrasiewicz pr_debug("%s: Offset: %d\n", __func__, BufOffset); 65400a2430fSAndrzej Pietrasiewicz pr_debug("%s: InfoBuffer: ", __func__); 65500a2430fSAndrzej Pietrasiewicz 65600a2430fSAndrzej Pietrasiewicz for (i = 0; i < BufLength; i++) { 65700a2430fSAndrzej Pietrasiewicz pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset)); 65800a2430fSAndrzej Pietrasiewicz } 65900a2430fSAndrzej Pietrasiewicz 66000a2430fSAndrzej Pietrasiewicz pr_debug("\n"); 66100a2430fSAndrzej Pietrasiewicz #endif 66200a2430fSAndrzej Pietrasiewicz 66300a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C); 66400a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(16); 66500a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 66683210e59SAndrzej Pietrasiewicz if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID), 66700a2430fSAndrzej Pietrasiewicz ((u8 *)buf) + 8 + BufOffset, BufLength, r)) 66800a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED); 66900a2430fSAndrzej Pietrasiewicz else 67000a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 67100a2430fSAndrzej Pietrasiewicz 67200a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 67300a2430fSAndrzej Pietrasiewicz return 0; 67400a2430fSAndrzej Pietrasiewicz } 67500a2430fSAndrzej Pietrasiewicz 67683210e59SAndrzej Pietrasiewicz static int rndis_reset_response(struct rndis_params *params, 67783210e59SAndrzej Pietrasiewicz rndis_reset_msg_type *buf) 67800a2430fSAndrzej Pietrasiewicz { 67900a2430fSAndrzej Pietrasiewicz rndis_reset_cmplt_type *resp; 68000a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 681207707d8SXerox Lin u8 *xbuf; 682207707d8SXerox Lin u32 length; 683207707d8SXerox Lin 684207707d8SXerox Lin /* drain the response queue */ 685207707d8SXerox Lin while ((xbuf = rndis_get_next_response(params, &length))) 686207707d8SXerox Lin rndis_free_response(params, xbuf); 68700a2430fSAndrzej Pietrasiewicz 68883210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type)); 68900a2430fSAndrzej Pietrasiewicz if (!r) 69000a2430fSAndrzej Pietrasiewicz return -ENOMEM; 69100a2430fSAndrzej Pietrasiewicz resp = (rndis_reset_cmplt_type *)r->buf; 69200a2430fSAndrzej Pietrasiewicz 69300a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C); 69400a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(16); 69500a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 69600a2430fSAndrzej Pietrasiewicz /* resent information */ 69700a2430fSAndrzej Pietrasiewicz resp->AddressingReset = cpu_to_le32(1); 69800a2430fSAndrzej Pietrasiewicz 69900a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 70000a2430fSAndrzej Pietrasiewicz return 0; 70100a2430fSAndrzej Pietrasiewicz } 70200a2430fSAndrzej Pietrasiewicz 70383210e59SAndrzej Pietrasiewicz static int rndis_keepalive_response(struct rndis_params *params, 70400a2430fSAndrzej Pietrasiewicz rndis_keepalive_msg_type *buf) 70500a2430fSAndrzej Pietrasiewicz { 70600a2430fSAndrzej Pietrasiewicz rndis_keepalive_cmplt_type *resp; 70700a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 70800a2430fSAndrzej Pietrasiewicz 70900a2430fSAndrzej Pietrasiewicz /* host "should" check only in RNDIS_DATA_INITIALIZED state */ 71000a2430fSAndrzej Pietrasiewicz 71183210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type)); 71200a2430fSAndrzej Pietrasiewicz if (!r) 71300a2430fSAndrzej Pietrasiewicz return -ENOMEM; 71400a2430fSAndrzej Pietrasiewicz resp = (rndis_keepalive_cmplt_type *)r->buf; 71500a2430fSAndrzej Pietrasiewicz 71600a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C); 71700a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(16); 71800a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 71900a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 72000a2430fSAndrzej Pietrasiewicz 72100a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 72200a2430fSAndrzej Pietrasiewicz return 0; 72300a2430fSAndrzej Pietrasiewicz } 72400a2430fSAndrzej Pietrasiewicz 72500a2430fSAndrzej Pietrasiewicz 72600a2430fSAndrzej Pietrasiewicz /* 72700a2430fSAndrzej Pietrasiewicz * Device to Host Comunication 72800a2430fSAndrzej Pietrasiewicz */ 72983210e59SAndrzej Pietrasiewicz static int rndis_indicate_status_msg(struct rndis_params *params, u32 status) 73000a2430fSAndrzej Pietrasiewicz { 73100a2430fSAndrzej Pietrasiewicz rndis_indicate_status_msg_type *resp; 73200a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 73300a2430fSAndrzej Pietrasiewicz 73400a2430fSAndrzej Pietrasiewicz if (params->state == RNDIS_UNINITIALIZED) 73500a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 73600a2430fSAndrzej Pietrasiewicz 73783210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type)); 73800a2430fSAndrzej Pietrasiewicz if (!r) 73900a2430fSAndrzej Pietrasiewicz return -ENOMEM; 74000a2430fSAndrzej Pietrasiewicz resp = (rndis_indicate_status_msg_type *)r->buf; 74100a2430fSAndrzej Pietrasiewicz 74200a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE); 74300a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(20); 74400a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(status); 74500a2430fSAndrzej Pietrasiewicz resp->StatusBufferLength = cpu_to_le32(0); 74600a2430fSAndrzej Pietrasiewicz resp->StatusBufferOffset = cpu_to_le32(0); 74700a2430fSAndrzej Pietrasiewicz 74800a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 74900a2430fSAndrzej Pietrasiewicz return 0; 75000a2430fSAndrzej Pietrasiewicz } 75100a2430fSAndrzej Pietrasiewicz 75283210e59SAndrzej Pietrasiewicz int rndis_signal_connect(struct rndis_params *params) 75300a2430fSAndrzej Pietrasiewicz { 75483210e59SAndrzej Pietrasiewicz params->media_state = RNDIS_MEDIA_STATE_CONNECTED; 75583210e59SAndrzej Pietrasiewicz return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT); 75600a2430fSAndrzej Pietrasiewicz } 75700a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_signal_connect); 75800a2430fSAndrzej Pietrasiewicz 75983210e59SAndrzej Pietrasiewicz int rndis_signal_disconnect(struct rndis_params *params) 76000a2430fSAndrzej Pietrasiewicz { 76183210e59SAndrzej Pietrasiewicz params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED; 76283210e59SAndrzej Pietrasiewicz return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT); 76300a2430fSAndrzej Pietrasiewicz } 76400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_signal_disconnect); 76500a2430fSAndrzej Pietrasiewicz 76683210e59SAndrzej Pietrasiewicz void rndis_uninit(struct rndis_params *params) 76700a2430fSAndrzej Pietrasiewicz { 76800a2430fSAndrzej Pietrasiewicz u8 *buf; 76900a2430fSAndrzej Pietrasiewicz u32 length; 77000a2430fSAndrzej Pietrasiewicz 77183210e59SAndrzej Pietrasiewicz if (!params) 77200a2430fSAndrzej Pietrasiewicz return; 77383210e59SAndrzej Pietrasiewicz params->state = RNDIS_UNINITIALIZED; 77400a2430fSAndrzej Pietrasiewicz 77500a2430fSAndrzej Pietrasiewicz /* drain the response queue */ 77683210e59SAndrzej Pietrasiewicz while ((buf = rndis_get_next_response(params, &length))) 77783210e59SAndrzej Pietrasiewicz rndis_free_response(params, buf); 77800a2430fSAndrzej Pietrasiewicz } 77900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_uninit); 78000a2430fSAndrzej Pietrasiewicz 78183210e59SAndrzej Pietrasiewicz void rndis_set_host_mac(struct rndis_params *params, const u8 *addr) 78200a2430fSAndrzej Pietrasiewicz { 78383210e59SAndrzej Pietrasiewicz params->host_mac = addr; 78400a2430fSAndrzej Pietrasiewicz } 78500a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_host_mac); 78600a2430fSAndrzej Pietrasiewicz 78700a2430fSAndrzej Pietrasiewicz /* 78800a2430fSAndrzej Pietrasiewicz * Message Parser 78900a2430fSAndrzej Pietrasiewicz */ 79083210e59SAndrzej Pietrasiewicz int rndis_msg_parser(struct rndis_params *params, u8 *buf) 79100a2430fSAndrzej Pietrasiewicz { 79200a2430fSAndrzej Pietrasiewicz u32 MsgType, MsgLength; 79300a2430fSAndrzej Pietrasiewicz __le32 *tmp; 79400a2430fSAndrzej Pietrasiewicz 79500a2430fSAndrzej Pietrasiewicz if (!buf) 79600a2430fSAndrzej Pietrasiewicz return -ENOMEM; 79700a2430fSAndrzej Pietrasiewicz 79800a2430fSAndrzej Pietrasiewicz tmp = (__le32 *)buf; 79900a2430fSAndrzej Pietrasiewicz MsgType = get_unaligned_le32(tmp++); 80000a2430fSAndrzej Pietrasiewicz MsgLength = get_unaligned_le32(tmp++); 80100a2430fSAndrzej Pietrasiewicz 80283210e59SAndrzej Pietrasiewicz if (!params) 80300a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 80400a2430fSAndrzej Pietrasiewicz 80500a2430fSAndrzej Pietrasiewicz /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for 80600a2430fSAndrzej Pietrasiewicz * rx/tx statistics and link status, in addition to KEEPALIVE traffic 80700a2430fSAndrzej Pietrasiewicz * and normal HC level polling to see if there's any IN traffic. 80800a2430fSAndrzej Pietrasiewicz */ 80900a2430fSAndrzej Pietrasiewicz 81000a2430fSAndrzej Pietrasiewicz /* For USB: responses may take up to 10 seconds */ 81100a2430fSAndrzej Pietrasiewicz switch (MsgType) { 81200a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_INIT: 81300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_MSG_INIT\n", 81400a2430fSAndrzej Pietrasiewicz __func__); 81500a2430fSAndrzej Pietrasiewicz params->state = RNDIS_INITIALIZED; 81683210e59SAndrzej Pietrasiewicz return rndis_init_response(params, (rndis_init_msg_type *)buf); 81700a2430fSAndrzej Pietrasiewicz 81800a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_HALT: 81900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_MSG_HALT\n", 82000a2430fSAndrzej Pietrasiewicz __func__); 82100a2430fSAndrzej Pietrasiewicz params->state = RNDIS_UNINITIALIZED; 82200a2430fSAndrzej Pietrasiewicz if (params->dev) { 82300a2430fSAndrzej Pietrasiewicz netif_carrier_off(params->dev); 82400a2430fSAndrzej Pietrasiewicz netif_stop_queue(params->dev); 82500a2430fSAndrzej Pietrasiewicz } 82600a2430fSAndrzej Pietrasiewicz return 0; 82700a2430fSAndrzej Pietrasiewicz 82800a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_QUERY: 82983210e59SAndrzej Pietrasiewicz return rndis_query_response(params, 83000a2430fSAndrzej Pietrasiewicz (rndis_query_msg_type *)buf); 83100a2430fSAndrzej Pietrasiewicz 83200a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_SET: 83383210e59SAndrzej Pietrasiewicz return rndis_set_response(params, (rndis_set_msg_type *)buf); 83400a2430fSAndrzej Pietrasiewicz 83500a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_RESET: 83600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_MSG_RESET\n", 83700a2430fSAndrzej Pietrasiewicz __func__); 83883210e59SAndrzej Pietrasiewicz return rndis_reset_response(params, 83900a2430fSAndrzej Pietrasiewicz (rndis_reset_msg_type *)buf); 84000a2430fSAndrzej Pietrasiewicz 84100a2430fSAndrzej Pietrasiewicz case RNDIS_MSG_KEEPALIVE: 84200a2430fSAndrzej Pietrasiewicz /* For USB: host does this every 5 seconds */ 84300a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 84400a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_MSG_KEEPALIVE\n", 84500a2430fSAndrzej Pietrasiewicz __func__); 84683210e59SAndrzej Pietrasiewicz return rndis_keepalive_response(params, 84700a2430fSAndrzej Pietrasiewicz (rndis_keepalive_msg_type *) 84800a2430fSAndrzej Pietrasiewicz buf); 84900a2430fSAndrzej Pietrasiewicz 85000a2430fSAndrzej Pietrasiewicz default: 85100a2430fSAndrzej Pietrasiewicz /* At least Windows XP emits some undefined RNDIS messages. 85200a2430fSAndrzej Pietrasiewicz * In one case those messages seemed to relate to the host 85300a2430fSAndrzej Pietrasiewicz * suspending itself. 85400a2430fSAndrzej Pietrasiewicz */ 8553f5ad864SJoe Perches pr_warn("%s: unknown RNDIS message 0x%08X len %d\n", 85600a2430fSAndrzej Pietrasiewicz __func__, MsgType, MsgLength); 8571ca532e9SMichel Pollet /* Garbled message can be huge, so limit what we display */ 8581ca532e9SMichel Pollet if (MsgLength > 16) 8591ca532e9SMichel Pollet MsgLength = 16; 86000a2430fSAndrzej Pietrasiewicz print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET, 86100a2430fSAndrzej Pietrasiewicz buf, MsgLength); 86200a2430fSAndrzej Pietrasiewicz break; 86300a2430fSAndrzej Pietrasiewicz } 86400a2430fSAndrzej Pietrasiewicz 86500a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 86600a2430fSAndrzej Pietrasiewicz } 86700a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_msg_parser); 86800a2430fSAndrzej Pietrasiewicz 869d6d22922SAndrzej Pietrasiewicz static inline int rndis_get_nr(void) 870d6d22922SAndrzej Pietrasiewicz { 871d6d22922SAndrzej Pietrasiewicz return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL); 872d6d22922SAndrzej Pietrasiewicz } 873d6d22922SAndrzej Pietrasiewicz 874d6d22922SAndrzej Pietrasiewicz static inline void rndis_put_nr(int nr) 875d6d22922SAndrzej Pietrasiewicz { 876d6d22922SAndrzej Pietrasiewicz ida_simple_remove(&rndis_ida, nr); 877d6d22922SAndrzej Pietrasiewicz } 878d6d22922SAndrzej Pietrasiewicz 87983210e59SAndrzej Pietrasiewicz struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v) 88000a2430fSAndrzej Pietrasiewicz { 881d6d22922SAndrzej Pietrasiewicz struct rndis_params *params; 88281dff869SAndrzej Pietrasiewicz int i; 88300a2430fSAndrzej Pietrasiewicz 88400a2430fSAndrzej Pietrasiewicz if (!resp_avail) 88583210e59SAndrzej Pietrasiewicz return ERR_PTR(-EINVAL); 88600a2430fSAndrzej Pietrasiewicz 887d6d22922SAndrzej Pietrasiewicz i = rndis_get_nr(); 888d6d22922SAndrzej Pietrasiewicz if (i < 0) { 88900a2430fSAndrzej Pietrasiewicz pr_debug("failed\n"); 89000a2430fSAndrzej Pietrasiewicz 89183210e59SAndrzej Pietrasiewicz return ERR_PTR(-ENODEV); 89200a2430fSAndrzej Pietrasiewicz } 893d6d22922SAndrzej Pietrasiewicz 894d6d22922SAndrzej Pietrasiewicz params = kzalloc(sizeof(*params), GFP_KERNEL); 895d6d22922SAndrzej Pietrasiewicz if (!params) { 896d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 897d6d22922SAndrzej Pietrasiewicz 898d6d22922SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 899d6d22922SAndrzej Pietrasiewicz } 900d6d22922SAndrzej Pietrasiewicz 901d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 902d6d22922SAndrzej Pietrasiewicz { 903d6d22922SAndrzej Pietrasiewicz struct proc_dir_entry *proc_entry; 904d6d22922SAndrzej Pietrasiewicz char name[20]; 905d6d22922SAndrzej Pietrasiewicz 906d6d22922SAndrzej Pietrasiewicz sprintf(name, NAME_TEMPLATE, i); 907d6d22922SAndrzej Pietrasiewicz proc_entry = proc_create_data(name, 0660, NULL, 90897a32539SAlexey Dobriyan &rndis_proc_ops, params); 909d6d22922SAndrzej Pietrasiewicz if (!proc_entry) { 910d6d22922SAndrzej Pietrasiewicz kfree(params); 911d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 912d6d22922SAndrzej Pietrasiewicz 913d6d22922SAndrzej Pietrasiewicz return ERR_PTR(-EIO); 914d6d22922SAndrzej Pietrasiewicz } 915d6d22922SAndrzej Pietrasiewicz } 916d6d22922SAndrzej Pietrasiewicz #endif 917d6d22922SAndrzej Pietrasiewicz 918d6d22922SAndrzej Pietrasiewicz params->confignr = i; 919d6d22922SAndrzej Pietrasiewicz params->used = 1; 920d6d22922SAndrzej Pietrasiewicz params->state = RNDIS_UNINITIALIZED; 921d6d22922SAndrzej Pietrasiewicz params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED; 922d6d22922SAndrzej Pietrasiewicz params->resp_avail = resp_avail; 923d6d22922SAndrzej Pietrasiewicz params->v = v; 924f6281af9SGeliang Tang INIT_LIST_HEAD(¶ms->resp_queue); 925d6d22922SAndrzej Pietrasiewicz pr_debug("%s: configNr = %d\n", __func__, i); 926d6d22922SAndrzej Pietrasiewicz 927d6d22922SAndrzej Pietrasiewicz return params; 928d6d22922SAndrzej Pietrasiewicz } 92900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_register); 93000a2430fSAndrzej Pietrasiewicz 93183210e59SAndrzej Pietrasiewicz void rndis_deregister(struct rndis_params *params) 93200a2430fSAndrzej Pietrasiewicz { 93381dff869SAndrzej Pietrasiewicz int i; 934d6d22922SAndrzej Pietrasiewicz 93500a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 93600a2430fSAndrzej Pietrasiewicz 93783210e59SAndrzej Pietrasiewicz if (!params) 93883210e59SAndrzej Pietrasiewicz return; 939d6d22922SAndrzej Pietrasiewicz 940d6d22922SAndrzej Pietrasiewicz i = params->confignr; 941d6d22922SAndrzej Pietrasiewicz 942d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 943d6d22922SAndrzej Pietrasiewicz { 944d6d22922SAndrzej Pietrasiewicz char name[20]; 945d6d22922SAndrzej Pietrasiewicz 946d6d22922SAndrzej Pietrasiewicz sprintf(name, NAME_TEMPLATE, i); 947d6d22922SAndrzej Pietrasiewicz remove_proc_entry(name, NULL); 948d6d22922SAndrzej Pietrasiewicz } 949d6d22922SAndrzej Pietrasiewicz #endif 950d6d22922SAndrzej Pietrasiewicz 951d6d22922SAndrzej Pietrasiewicz kfree(params); 952d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 95300a2430fSAndrzej Pietrasiewicz } 95400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_deregister); 95583210e59SAndrzej Pietrasiewicz int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev, 95683210e59SAndrzej Pietrasiewicz u16 *cdc_filter) 95700a2430fSAndrzej Pietrasiewicz { 95800a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 95900a2430fSAndrzej Pietrasiewicz if (!dev) 96000a2430fSAndrzej Pietrasiewicz return -EINVAL; 96183210e59SAndrzej Pietrasiewicz if (!params) 96283210e59SAndrzej Pietrasiewicz return -1; 96300a2430fSAndrzej Pietrasiewicz 96483210e59SAndrzej Pietrasiewicz params->dev = dev; 96583210e59SAndrzej Pietrasiewicz params->filter = cdc_filter; 96600a2430fSAndrzej Pietrasiewicz 96700a2430fSAndrzej Pietrasiewicz return 0; 96800a2430fSAndrzej Pietrasiewicz } 96900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_dev); 97000a2430fSAndrzej Pietrasiewicz 97183210e59SAndrzej Pietrasiewicz int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID, 97283210e59SAndrzej Pietrasiewicz const char *vendorDescr) 97300a2430fSAndrzej Pietrasiewicz { 97400a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 97500a2430fSAndrzej Pietrasiewicz if (!vendorDescr) return -1; 97683210e59SAndrzej Pietrasiewicz if (!params) 97783210e59SAndrzej Pietrasiewicz return -1; 97800a2430fSAndrzej Pietrasiewicz 97983210e59SAndrzej Pietrasiewicz params->vendorID = vendorID; 98083210e59SAndrzej Pietrasiewicz params->vendorDescr = vendorDescr; 98100a2430fSAndrzej Pietrasiewicz 98200a2430fSAndrzej Pietrasiewicz return 0; 98300a2430fSAndrzej Pietrasiewicz } 98400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_vendor); 98500a2430fSAndrzej Pietrasiewicz 98683210e59SAndrzej Pietrasiewicz int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed) 98700a2430fSAndrzej Pietrasiewicz { 98800a2430fSAndrzej Pietrasiewicz pr_debug("%s: %u %u\n", __func__, medium, speed); 98983210e59SAndrzej Pietrasiewicz if (!params) 99083210e59SAndrzej Pietrasiewicz return -1; 99100a2430fSAndrzej Pietrasiewicz 99283210e59SAndrzej Pietrasiewicz params->medium = medium; 99383210e59SAndrzej Pietrasiewicz params->speed = speed; 99400a2430fSAndrzej Pietrasiewicz 99500a2430fSAndrzej Pietrasiewicz return 0; 99600a2430fSAndrzej Pietrasiewicz } 99700a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_medium); 99800a2430fSAndrzej Pietrasiewicz 99900a2430fSAndrzej Pietrasiewicz void rndis_add_hdr(struct sk_buff *skb) 100000a2430fSAndrzej Pietrasiewicz { 100100a2430fSAndrzej Pietrasiewicz struct rndis_packet_msg_type *header; 100200a2430fSAndrzej Pietrasiewicz 100300a2430fSAndrzej Pietrasiewicz if (!skb) 100400a2430fSAndrzej Pietrasiewicz return; 1005d58ff351SJohannes Berg header = skb_push(skb, sizeof(*header)); 100600a2430fSAndrzej Pietrasiewicz memset(header, 0, sizeof *header); 100700a2430fSAndrzej Pietrasiewicz header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET); 100800a2430fSAndrzej Pietrasiewicz header->MessageLength = cpu_to_le32(skb->len); 100900a2430fSAndrzej Pietrasiewicz header->DataOffset = cpu_to_le32(36); 101000a2430fSAndrzej Pietrasiewicz header->DataLength = cpu_to_le32(skb->len - sizeof(*header)); 101100a2430fSAndrzej Pietrasiewicz } 101200a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_add_hdr); 101300a2430fSAndrzej Pietrasiewicz 101483210e59SAndrzej Pietrasiewicz void rndis_free_response(struct rndis_params *params, u8 *buf) 101500a2430fSAndrzej Pietrasiewicz { 1016f6281af9SGeliang Tang rndis_resp_t *r, *n; 101700a2430fSAndrzej Pietrasiewicz 1018f6281af9SGeliang Tang list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { 10191c17a353SJulia Lawall if (r->buf == buf) { 102000a2430fSAndrzej Pietrasiewicz list_del(&r->list); 102100a2430fSAndrzej Pietrasiewicz kfree(r); 102200a2430fSAndrzej Pietrasiewicz } 102300a2430fSAndrzej Pietrasiewicz } 102400a2430fSAndrzej Pietrasiewicz } 102500a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_free_response); 102600a2430fSAndrzej Pietrasiewicz 102783210e59SAndrzej Pietrasiewicz u8 *rndis_get_next_response(struct rndis_params *params, u32 *length) 102800a2430fSAndrzej Pietrasiewicz { 1029f6281af9SGeliang Tang rndis_resp_t *r, *n; 103000a2430fSAndrzej Pietrasiewicz 103100a2430fSAndrzej Pietrasiewicz if (!length) return NULL; 103200a2430fSAndrzej Pietrasiewicz 1033f6281af9SGeliang Tang list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { 103400a2430fSAndrzej Pietrasiewicz if (!r->send) { 103500a2430fSAndrzej Pietrasiewicz r->send = 1; 103600a2430fSAndrzej Pietrasiewicz *length = r->length; 103700a2430fSAndrzej Pietrasiewicz return r->buf; 103800a2430fSAndrzej Pietrasiewicz } 103900a2430fSAndrzej Pietrasiewicz } 104000a2430fSAndrzej Pietrasiewicz 104100a2430fSAndrzej Pietrasiewicz return NULL; 104200a2430fSAndrzej Pietrasiewicz } 104300a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_get_next_response); 104400a2430fSAndrzej Pietrasiewicz 104583210e59SAndrzej Pietrasiewicz static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length) 104600a2430fSAndrzej Pietrasiewicz { 104700a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 104800a2430fSAndrzej Pietrasiewicz 104900a2430fSAndrzej Pietrasiewicz /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */ 105000a2430fSAndrzej Pietrasiewicz r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC); 105100a2430fSAndrzej Pietrasiewicz if (!r) return NULL; 105200a2430fSAndrzej Pietrasiewicz 105300a2430fSAndrzej Pietrasiewicz r->buf = (u8 *)(r + 1); 105400a2430fSAndrzej Pietrasiewicz r->length = length; 105500a2430fSAndrzej Pietrasiewicz r->send = 0; 105600a2430fSAndrzej Pietrasiewicz 1057f6281af9SGeliang Tang list_add_tail(&r->list, ¶ms->resp_queue); 105800a2430fSAndrzej Pietrasiewicz return r; 105900a2430fSAndrzej Pietrasiewicz } 106000a2430fSAndrzej Pietrasiewicz 106100a2430fSAndrzej Pietrasiewicz int rndis_rm_hdr(struct gether *port, 106200a2430fSAndrzej Pietrasiewicz struct sk_buff *skb, 106300a2430fSAndrzej Pietrasiewicz struct sk_buff_head *list) 106400a2430fSAndrzej Pietrasiewicz { 106500a2430fSAndrzej Pietrasiewicz /* tmp points to a struct rndis_packet_msg_type */ 106600a2430fSAndrzej Pietrasiewicz __le32 *tmp = (void *)skb->data; 106700a2430fSAndrzej Pietrasiewicz 106800a2430fSAndrzej Pietrasiewicz /* MessageType, MessageLength */ 106900a2430fSAndrzej Pietrasiewicz if (cpu_to_le32(RNDIS_MSG_PACKET) 107000a2430fSAndrzej Pietrasiewicz != get_unaligned(tmp++)) { 107100a2430fSAndrzej Pietrasiewicz dev_kfree_skb_any(skb); 107200a2430fSAndrzej Pietrasiewicz return -EINVAL; 107300a2430fSAndrzej Pietrasiewicz } 107400a2430fSAndrzej Pietrasiewicz tmp++; 107500a2430fSAndrzej Pietrasiewicz 107600a2430fSAndrzej Pietrasiewicz /* DataOffset, DataLength */ 107700a2430fSAndrzej Pietrasiewicz if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) { 107800a2430fSAndrzej Pietrasiewicz dev_kfree_skb_any(skb); 107900a2430fSAndrzej Pietrasiewicz return -EOVERFLOW; 108000a2430fSAndrzej Pietrasiewicz } 108100a2430fSAndrzej Pietrasiewicz skb_trim(skb, get_unaligned_le32(tmp++)); 108200a2430fSAndrzej Pietrasiewicz 108300a2430fSAndrzej Pietrasiewicz skb_queue_tail(list, skb); 108400a2430fSAndrzej Pietrasiewicz return 0; 108500a2430fSAndrzej Pietrasiewicz } 108600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_rm_hdr); 108700a2430fSAndrzej Pietrasiewicz 108800a2430fSAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 108900a2430fSAndrzej Pietrasiewicz 109000a2430fSAndrzej Pietrasiewicz static int rndis_proc_show(struct seq_file *m, void *v) 109100a2430fSAndrzej Pietrasiewicz { 109200a2430fSAndrzej Pietrasiewicz rndis_params *param = m->private; 109300a2430fSAndrzej Pietrasiewicz 109400a2430fSAndrzej Pietrasiewicz seq_printf(m, 109500a2430fSAndrzej Pietrasiewicz "Config Nr. %d\n" 109600a2430fSAndrzej Pietrasiewicz "used : %s\n" 109700a2430fSAndrzej Pietrasiewicz "state : %s\n" 109800a2430fSAndrzej Pietrasiewicz "medium : 0x%08X\n" 109900a2430fSAndrzej Pietrasiewicz "speed : %d\n" 110000a2430fSAndrzej Pietrasiewicz "cable : %s\n" 110100a2430fSAndrzej Pietrasiewicz "vendor ID : 0x%08X\n" 110200a2430fSAndrzej Pietrasiewicz "vendor : %s\n", 110300a2430fSAndrzej Pietrasiewicz param->confignr, (param->used) ? "y" : "n", 110400a2430fSAndrzej Pietrasiewicz ({ char *s = "?"; 110500a2430fSAndrzej Pietrasiewicz switch (param->state) { 110600a2430fSAndrzej Pietrasiewicz case RNDIS_UNINITIALIZED: 110700a2430fSAndrzej Pietrasiewicz s = "RNDIS_UNINITIALIZED"; break; 110800a2430fSAndrzej Pietrasiewicz case RNDIS_INITIALIZED: 110900a2430fSAndrzej Pietrasiewicz s = "RNDIS_INITIALIZED"; break; 111000a2430fSAndrzej Pietrasiewicz case RNDIS_DATA_INITIALIZED: 111100a2430fSAndrzej Pietrasiewicz s = "RNDIS_DATA_INITIALIZED"; break; 111200a2430fSAndrzej Pietrasiewicz } s; }), 111300a2430fSAndrzej Pietrasiewicz param->medium, 111400a2430fSAndrzej Pietrasiewicz (param->media_state) ? 0 : param->speed*100, 111500a2430fSAndrzej Pietrasiewicz (param->media_state) ? "disconnected" : "connected", 111600a2430fSAndrzej Pietrasiewicz param->vendorID, param->vendorDescr); 111700a2430fSAndrzej Pietrasiewicz return 0; 111800a2430fSAndrzej Pietrasiewicz } 111900a2430fSAndrzej Pietrasiewicz 112000a2430fSAndrzej Pietrasiewicz static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, 112100a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ppos) 112200a2430fSAndrzej Pietrasiewicz { 1123359745d7SMuchun Song rndis_params *p = pde_data(file_inode(file)); 112400a2430fSAndrzej Pietrasiewicz u32 speed = 0; 112500a2430fSAndrzej Pietrasiewicz int i, fl_speed = 0; 112600a2430fSAndrzej Pietrasiewicz 112700a2430fSAndrzej Pietrasiewicz for (i = 0; i < count; i++) { 112800a2430fSAndrzej Pietrasiewicz char c; 112900a2430fSAndrzej Pietrasiewicz if (get_user(c, buffer)) 113000a2430fSAndrzej Pietrasiewicz return -EFAULT; 113100a2430fSAndrzej Pietrasiewicz switch (c) { 113200a2430fSAndrzej Pietrasiewicz case '0': 113300a2430fSAndrzej Pietrasiewicz case '1': 113400a2430fSAndrzej Pietrasiewicz case '2': 113500a2430fSAndrzej Pietrasiewicz case '3': 113600a2430fSAndrzej Pietrasiewicz case '4': 113700a2430fSAndrzej Pietrasiewicz case '5': 113800a2430fSAndrzej Pietrasiewicz case '6': 113900a2430fSAndrzej Pietrasiewicz case '7': 114000a2430fSAndrzej Pietrasiewicz case '8': 114100a2430fSAndrzej Pietrasiewicz case '9': 114200a2430fSAndrzej Pietrasiewicz fl_speed = 1; 114300a2430fSAndrzej Pietrasiewicz speed = speed * 10 + c - '0'; 114400a2430fSAndrzej Pietrasiewicz break; 114500a2430fSAndrzej Pietrasiewicz case 'C': 114600a2430fSAndrzej Pietrasiewicz case 'c': 1147868055fdSAndrzej Pietrasiewicz rndis_signal_connect(p); 114800a2430fSAndrzej Pietrasiewicz break; 114900a2430fSAndrzej Pietrasiewicz case 'D': 115000a2430fSAndrzej Pietrasiewicz case 'd': 1151868055fdSAndrzej Pietrasiewicz rndis_signal_disconnect(p); 115200a2430fSAndrzej Pietrasiewicz break; 115300a2430fSAndrzej Pietrasiewicz default: 115400a2430fSAndrzej Pietrasiewicz if (fl_speed) p->speed = speed; 115500a2430fSAndrzej Pietrasiewicz else pr_debug("%c is not valid\n", c); 115600a2430fSAndrzej Pietrasiewicz break; 115700a2430fSAndrzej Pietrasiewicz } 115800a2430fSAndrzej Pietrasiewicz 115900a2430fSAndrzej Pietrasiewicz buffer++; 116000a2430fSAndrzej Pietrasiewicz } 116100a2430fSAndrzej Pietrasiewicz 116200a2430fSAndrzej Pietrasiewicz return count; 116300a2430fSAndrzej Pietrasiewicz } 116400a2430fSAndrzej Pietrasiewicz 116500a2430fSAndrzej Pietrasiewicz static int rndis_proc_open(struct inode *inode, struct file *file) 116600a2430fSAndrzej Pietrasiewicz { 1167359745d7SMuchun Song return single_open(file, rndis_proc_show, pde_data(inode)); 116800a2430fSAndrzej Pietrasiewicz } 116900a2430fSAndrzej Pietrasiewicz 117097a32539SAlexey Dobriyan static const struct proc_ops rndis_proc_ops = { 117197a32539SAlexey Dobriyan .proc_open = rndis_proc_open, 117297a32539SAlexey Dobriyan .proc_read = seq_read, 117397a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 117497a32539SAlexey Dobriyan .proc_release = single_release, 117597a32539SAlexey Dobriyan .proc_write = rndis_proc_write, 117600a2430fSAndrzej Pietrasiewicz }; 117700a2430fSAndrzej Pietrasiewicz 117800a2430fSAndrzej Pietrasiewicz #define NAME_TEMPLATE "driver/rndis-%03d" 117900a2430fSAndrzej Pietrasiewicz 118000a2430fSAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1181