100a2430fSAndrzej Pietrasiewicz /* 200a2430fSAndrzej Pietrasiewicz * RNDIS MSG parser 300a2430fSAndrzej Pietrasiewicz * 400a2430fSAndrzej Pietrasiewicz * Authors: Benedikt Spranger, Pengutronix 500a2430fSAndrzej Pietrasiewicz * Robert Schwebel, Pengutronix 600a2430fSAndrzej Pietrasiewicz * 700a2430fSAndrzej Pietrasiewicz * This program is free software; you can redistribute it and/or 800a2430fSAndrzej Pietrasiewicz * modify it under the terms of the GNU General Public License 900a2430fSAndrzej Pietrasiewicz * version 2, as published by the Free Software Foundation. 1000a2430fSAndrzej Pietrasiewicz * 1100a2430fSAndrzej Pietrasiewicz * This software was originally developed in conformance with 1200a2430fSAndrzej Pietrasiewicz * Microsoft's Remote NDIS Specification License Agreement. 1300a2430fSAndrzej Pietrasiewicz * 1400a2430fSAndrzej Pietrasiewicz * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de> 1500a2430fSAndrzej Pietrasiewicz * Fixed message length bug in init_response 1600a2430fSAndrzej Pietrasiewicz * 1700a2430fSAndrzej Pietrasiewicz * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de> 1800a2430fSAndrzej Pietrasiewicz * Fixed rndis_rm_hdr length bug. 1900a2430fSAndrzej Pietrasiewicz * 2000a2430fSAndrzej Pietrasiewicz * Copyright (C) 2004 by David Brownell 2100a2430fSAndrzej Pietrasiewicz * updates to merge with Linux 2.6, better match RNDIS spec 2200a2430fSAndrzej Pietrasiewicz */ 2300a2430fSAndrzej Pietrasiewicz 2400a2430fSAndrzej Pietrasiewicz #include <linux/module.h> 2500a2430fSAndrzej Pietrasiewicz #include <linux/moduleparam.h> 2600a2430fSAndrzej Pietrasiewicz #include <linux/kernel.h> 2700a2430fSAndrzej Pietrasiewicz #include <linux/errno.h> 28d6d22922SAndrzej Pietrasiewicz #include <linux/idr.h> 2900a2430fSAndrzej Pietrasiewicz #include <linux/list.h> 3000a2430fSAndrzej Pietrasiewicz #include <linux/proc_fs.h> 3100a2430fSAndrzej Pietrasiewicz #include <linux/slab.h> 3200a2430fSAndrzej Pietrasiewicz #include <linux/seq_file.h> 3300a2430fSAndrzej Pietrasiewicz #include <linux/netdevice.h> 3400a2430fSAndrzej Pietrasiewicz 3500a2430fSAndrzej Pietrasiewicz #include <asm/io.h> 3600a2430fSAndrzej Pietrasiewicz #include <asm/byteorder.h> 3700a2430fSAndrzej Pietrasiewicz #include <asm/unaligned.h> 3800a2430fSAndrzej Pietrasiewicz 3900a2430fSAndrzej Pietrasiewicz #include "u_rndis.h" 4000a2430fSAndrzej Pietrasiewicz 4100a2430fSAndrzej Pietrasiewicz #undef VERBOSE_DEBUG 4200a2430fSAndrzej Pietrasiewicz 4300a2430fSAndrzej Pietrasiewicz #include "rndis.h" 4400a2430fSAndrzej Pietrasiewicz 4500a2430fSAndrzej Pietrasiewicz 4600a2430fSAndrzej Pietrasiewicz /* The driver for your USB chip needs to support ep0 OUT to work with 4700a2430fSAndrzej Pietrasiewicz * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional). 4800a2430fSAndrzej Pietrasiewicz * 4900a2430fSAndrzej Pietrasiewicz * Windows hosts need an INF file like Documentation/usb/linux.inf 5000a2430fSAndrzej Pietrasiewicz * and will be happier if you provide the host_addr module parameter. 5100a2430fSAndrzej Pietrasiewicz */ 5200a2430fSAndrzej Pietrasiewicz 5300a2430fSAndrzej Pietrasiewicz #if 0 5400a2430fSAndrzej Pietrasiewicz static int rndis_debug = 0; 5500a2430fSAndrzej Pietrasiewicz module_param (rndis_debug, int, 0); 5600a2430fSAndrzej Pietrasiewicz MODULE_PARM_DESC (rndis_debug, "enable debugging"); 5700a2430fSAndrzej Pietrasiewicz #else 5800a2430fSAndrzej Pietrasiewicz #define rndis_debug 0 5900a2430fSAndrzej Pietrasiewicz #endif 6000a2430fSAndrzej Pietrasiewicz 61d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 6200a2430fSAndrzej Pietrasiewicz 63d6d22922SAndrzej Pietrasiewicz #define NAME_TEMPLATE "driver/rndis-%03d" 6400a2430fSAndrzej Pietrasiewicz 65d6d22922SAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 66d6d22922SAndrzej Pietrasiewicz 67d6d22922SAndrzej Pietrasiewicz static DEFINE_IDA(rndis_ida); 6800a2430fSAndrzej Pietrasiewicz 6900a2430fSAndrzej Pietrasiewicz /* Driver Version */ 7000a2430fSAndrzej Pietrasiewicz static const __le32 rndis_driver_version = cpu_to_le32(1); 7100a2430fSAndrzej Pietrasiewicz 7200a2430fSAndrzej Pietrasiewicz /* Function Prototypes */ 7383210e59SAndrzej Pietrasiewicz static rndis_resp_t *rndis_add_response(struct rndis_params *params, 7483210e59SAndrzej Pietrasiewicz u32 length); 7500a2430fSAndrzej Pietrasiewicz 76d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 77d6d22922SAndrzej Pietrasiewicz 78d6d22922SAndrzej Pietrasiewicz static const struct file_operations rndis_proc_fops; 79d6d22922SAndrzej Pietrasiewicz 80d6d22922SAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 8100a2430fSAndrzej Pietrasiewicz 8200a2430fSAndrzej Pietrasiewicz /* supported OIDs */ 8397f0117bSAnson Jacob static const u32 oid_supported_list[] = { 8400a2430fSAndrzej Pietrasiewicz /* the general stuff */ 8500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_SUPPORTED_LIST, 8600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_HARDWARE_STATUS, 8700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_SUPPORTED, 8800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_IN_USE, 8900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE, 9000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_LINK_SPEED, 9100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE, 9200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE, 9300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_ID, 9400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_DESCRIPTION, 9500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_VENDOR_DRIVER_VERSION, 9600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_CURRENT_PACKET_FILTER, 9700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE, 9800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MEDIA_CONNECT_STATUS, 9900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_PHYSICAL_MEDIUM, 10000a2430fSAndrzej Pietrasiewicz 10100a2430fSAndrzej Pietrasiewicz /* the statistical stuff */ 10200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_XMIT_OK, 10300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_OK, 10400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_XMIT_ERROR, 10500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_ERROR, 10600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_NO_BUFFER, 10700a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_OPTIONAL_STATS 10800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_BYTES_XMIT, 10900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT, 11000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_BYTES_XMIT, 11100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT, 11200a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_BYTES_XMIT, 11300a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT, 11400a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_BYTES_RCV, 11500a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_DIRECTED_FRAMES_RCV, 11600a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_BYTES_RCV, 11700a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_MULTICAST_FRAMES_RCV, 11800a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_BYTES_RCV, 11900a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_BROADCAST_FRAMES_RCV, 12000a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_RCV_CRC_ERROR, 12100a2430fSAndrzej Pietrasiewicz RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH, 12200a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_OPTIONAL_STATS */ 12300a2430fSAndrzej Pietrasiewicz 12400a2430fSAndrzej Pietrasiewicz /* mandatory 802.3 */ 12500a2430fSAndrzej Pietrasiewicz /* the general stuff */ 12600a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_PERMANENT_ADDRESS, 12700a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_CURRENT_ADDRESS, 12800a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MULTICAST_LIST, 12900a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MAC_OPTIONS, 13000a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_MAXIMUM_LIST_SIZE, 13100a2430fSAndrzej Pietrasiewicz 13200a2430fSAndrzej Pietrasiewicz /* the statistical stuff */ 13300a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT, 13400a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_ONE_COLLISION, 13500a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_MORE_COLLISIONS, 13600a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_OPTIONAL_STATS 13700a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_DEFERRED, 13800a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_MAX_COLLISIONS, 13900a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_RCV_OVERRUN, 14000a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_UNDERRUN, 14100a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE, 14200a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST, 14300a2430fSAndrzej Pietrasiewicz RNDIS_OID_802_3_XMIT_LATE_COLLISIONS, 14400a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_OPTIONAL_STATS */ 14500a2430fSAndrzej Pietrasiewicz 14600a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_PM 14700a2430fSAndrzej Pietrasiewicz /* PM and wakeup are "mandatory" for USB, but the RNDIS specs 14800a2430fSAndrzej Pietrasiewicz * don't say what they mean ... and the NDIS specs are often 14900a2430fSAndrzej Pietrasiewicz * confusing and/or ambiguous in this context. (That is, more 15000a2430fSAndrzej Pietrasiewicz * so than their specs for the other OIDs.) 15100a2430fSAndrzej Pietrasiewicz * 15200a2430fSAndrzej Pietrasiewicz * FIXME someone who knows what these should do, please 15300a2430fSAndrzej Pietrasiewicz * implement them! 15400a2430fSAndrzej Pietrasiewicz */ 15500a2430fSAndrzej Pietrasiewicz 15600a2430fSAndrzej Pietrasiewicz /* power management */ 15700a2430fSAndrzej Pietrasiewicz OID_PNP_CAPABILITIES, 15800a2430fSAndrzej Pietrasiewicz OID_PNP_QUERY_POWER, 15900a2430fSAndrzej Pietrasiewicz OID_PNP_SET_POWER, 16000a2430fSAndrzej Pietrasiewicz 16100a2430fSAndrzej Pietrasiewicz #ifdef RNDIS_WAKEUP 16200a2430fSAndrzej Pietrasiewicz /* wake up host */ 16300a2430fSAndrzej Pietrasiewicz OID_PNP_ENABLE_WAKE_UP, 16400a2430fSAndrzej Pietrasiewicz OID_PNP_ADD_WAKE_UP_PATTERN, 16500a2430fSAndrzej Pietrasiewicz OID_PNP_REMOVE_WAKE_UP_PATTERN, 16600a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_WAKEUP */ 16700a2430fSAndrzej Pietrasiewicz #endif /* RNDIS_PM */ 16800a2430fSAndrzej Pietrasiewicz }; 16900a2430fSAndrzej Pietrasiewicz 17000a2430fSAndrzej Pietrasiewicz 17100a2430fSAndrzej Pietrasiewicz /* NDIS Functions */ 17283210e59SAndrzej Pietrasiewicz static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf, 17300a2430fSAndrzej Pietrasiewicz unsigned buf_len, rndis_resp_t *r) 17400a2430fSAndrzej Pietrasiewicz { 17500a2430fSAndrzej Pietrasiewicz int retval = -ENOTSUPP; 17600a2430fSAndrzej Pietrasiewicz u32 length = 4; /* usually */ 17700a2430fSAndrzej Pietrasiewicz __le32 *outbuf; 17800a2430fSAndrzej Pietrasiewicz int i, count; 17900a2430fSAndrzej Pietrasiewicz rndis_query_cmplt_type *resp; 18000a2430fSAndrzej Pietrasiewicz struct net_device *net; 18100a2430fSAndrzej Pietrasiewicz struct rtnl_link_stats64 temp; 18200a2430fSAndrzej Pietrasiewicz const struct rtnl_link_stats64 *stats; 18300a2430fSAndrzej Pietrasiewicz 18400a2430fSAndrzej Pietrasiewicz if (!r) return -ENOMEM; 18500a2430fSAndrzej Pietrasiewicz resp = (rndis_query_cmplt_type *)r->buf; 18600a2430fSAndrzej Pietrasiewicz 18700a2430fSAndrzej Pietrasiewicz if (!resp) return -ENOMEM; 18800a2430fSAndrzej Pietrasiewicz 18900a2430fSAndrzej Pietrasiewicz if (buf_len && rndis_debug > 1) { 19000a2430fSAndrzej Pietrasiewicz pr_debug("query OID %08x value, len %d:\n", OID, buf_len); 19100a2430fSAndrzej Pietrasiewicz for (i = 0; i < buf_len; i += 16) { 19200a2430fSAndrzej Pietrasiewicz pr_debug("%03d: %08x %08x %08x %08x\n", i, 19300a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i]), 19400a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 4]), 19500a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 8]), 19600a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 12])); 19700a2430fSAndrzej Pietrasiewicz } 19800a2430fSAndrzej Pietrasiewicz } 19900a2430fSAndrzej Pietrasiewicz 20000a2430fSAndrzej Pietrasiewicz /* response goes here, right after the header */ 20100a2430fSAndrzej Pietrasiewicz outbuf = (__le32 *)&resp[1]; 20200a2430fSAndrzej Pietrasiewicz resp->InformationBufferOffset = cpu_to_le32(16); 20300a2430fSAndrzej Pietrasiewicz 20483210e59SAndrzej Pietrasiewicz net = params->dev; 20500a2430fSAndrzej Pietrasiewicz stats = dev_get_stats(net, &temp); 20600a2430fSAndrzej Pietrasiewicz 20700a2430fSAndrzej Pietrasiewicz switch (OID) { 20800a2430fSAndrzej Pietrasiewicz 20900a2430fSAndrzej Pietrasiewicz /* general oids (table 4-1) */ 21000a2430fSAndrzej Pietrasiewicz 21100a2430fSAndrzej Pietrasiewicz /* mandatory */ 21200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_SUPPORTED_LIST: 21300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__); 21400a2430fSAndrzej Pietrasiewicz length = sizeof(oid_supported_list); 21500a2430fSAndrzej Pietrasiewicz count = length / sizeof(u32); 21600a2430fSAndrzej Pietrasiewicz for (i = 0; i < count; i++) 21700a2430fSAndrzej Pietrasiewicz outbuf[i] = cpu_to_le32(oid_supported_list[i]); 21800a2430fSAndrzej Pietrasiewicz retval = 0; 21900a2430fSAndrzej Pietrasiewicz break; 22000a2430fSAndrzej Pietrasiewicz 22100a2430fSAndrzej Pietrasiewicz /* mandatory */ 22200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_HARDWARE_STATUS: 22300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__); 22400a2430fSAndrzej Pietrasiewicz /* Bogus question! 22500a2430fSAndrzej Pietrasiewicz * Hardware must be ready to receive high level protocols. 22600a2430fSAndrzej Pietrasiewicz * BTW: 22700a2430fSAndrzej Pietrasiewicz * reddite ergo quae sunt Caesaris Caesari 22800a2430fSAndrzej Pietrasiewicz * et quae sunt Dei Deo! 22900a2430fSAndrzej Pietrasiewicz */ 23000a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 23100a2430fSAndrzej Pietrasiewicz retval = 0; 23200a2430fSAndrzej Pietrasiewicz break; 23300a2430fSAndrzej Pietrasiewicz 23400a2430fSAndrzej Pietrasiewicz /* mandatory */ 23500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_SUPPORTED: 23600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__); 23783210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->medium); 23800a2430fSAndrzej Pietrasiewicz retval = 0; 23900a2430fSAndrzej Pietrasiewicz break; 24000a2430fSAndrzej Pietrasiewicz 24100a2430fSAndrzej Pietrasiewicz /* mandatory */ 24200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_IN_USE: 24300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__); 24400a2430fSAndrzej Pietrasiewicz /* one medium, one transport... (maybe you do it better) */ 24583210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->medium); 24600a2430fSAndrzej Pietrasiewicz retval = 0; 24700a2430fSAndrzej Pietrasiewicz break; 24800a2430fSAndrzej Pietrasiewicz 24900a2430fSAndrzej Pietrasiewicz /* mandatory */ 25000a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE: 25100a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__); 25283210e59SAndrzej Pietrasiewicz if (params->dev) { 25383210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 25400a2430fSAndrzej Pietrasiewicz retval = 0; 25500a2430fSAndrzej Pietrasiewicz } 25600a2430fSAndrzej Pietrasiewicz break; 25700a2430fSAndrzej Pietrasiewicz 25800a2430fSAndrzej Pietrasiewicz /* mandatory */ 25900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_LINK_SPEED: 26000a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 26100a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__); 26283210e59SAndrzej Pietrasiewicz if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED) 26300a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 26400a2430fSAndrzej Pietrasiewicz else 26583210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->speed); 26600a2430fSAndrzej Pietrasiewicz retval = 0; 26700a2430fSAndrzej Pietrasiewicz break; 26800a2430fSAndrzej Pietrasiewicz 26900a2430fSAndrzej Pietrasiewicz /* mandatory */ 27000a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE: 27100a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__); 27283210e59SAndrzej Pietrasiewicz if (params->dev) { 27383210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 27400a2430fSAndrzej Pietrasiewicz retval = 0; 27500a2430fSAndrzej Pietrasiewicz } 27600a2430fSAndrzej Pietrasiewicz break; 27700a2430fSAndrzej Pietrasiewicz 27800a2430fSAndrzej Pietrasiewicz /* mandatory */ 27900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE: 28000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__); 28183210e59SAndrzej Pietrasiewicz if (params->dev) { 28283210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->dev->mtu); 28300a2430fSAndrzej Pietrasiewicz retval = 0; 28400a2430fSAndrzej Pietrasiewicz } 28500a2430fSAndrzej Pietrasiewicz break; 28600a2430fSAndrzej Pietrasiewicz 28700a2430fSAndrzej Pietrasiewicz /* mandatory */ 28800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_ID: 28900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__); 29083210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->vendorID); 29100a2430fSAndrzej Pietrasiewicz retval = 0; 29200a2430fSAndrzej Pietrasiewicz break; 29300a2430fSAndrzej Pietrasiewicz 29400a2430fSAndrzej Pietrasiewicz /* mandatory */ 29500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_DESCRIPTION: 29600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__); 29783210e59SAndrzej Pietrasiewicz if (params->vendorDescr) { 29883210e59SAndrzej Pietrasiewicz length = strlen(params->vendorDescr); 29983210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->vendorDescr, length); 30000a2430fSAndrzej Pietrasiewicz } else { 30100a2430fSAndrzej Pietrasiewicz outbuf[0] = 0; 30200a2430fSAndrzej Pietrasiewicz } 30300a2430fSAndrzej Pietrasiewicz retval = 0; 30400a2430fSAndrzej Pietrasiewicz break; 30500a2430fSAndrzej Pietrasiewicz 30600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION: 30700a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__); 30800a2430fSAndrzej Pietrasiewicz /* Created as LE */ 30900a2430fSAndrzej Pietrasiewicz *outbuf = rndis_driver_version; 31000a2430fSAndrzej Pietrasiewicz retval = 0; 31100a2430fSAndrzej Pietrasiewicz break; 31200a2430fSAndrzej Pietrasiewicz 31300a2430fSAndrzej Pietrasiewicz /* mandatory */ 31400a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_CURRENT_PACKET_FILTER: 31500a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__); 31683210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(*params->filter); 31700a2430fSAndrzej Pietrasiewicz retval = 0; 31800a2430fSAndrzej Pietrasiewicz break; 31900a2430fSAndrzej Pietrasiewicz 32000a2430fSAndrzej Pietrasiewicz /* mandatory */ 32100a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE: 32200a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__); 32300a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE); 32400a2430fSAndrzej Pietrasiewicz retval = 0; 32500a2430fSAndrzej Pietrasiewicz break; 32600a2430fSAndrzej Pietrasiewicz 32700a2430fSAndrzej Pietrasiewicz /* mandatory */ 32800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS: 32900a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 33000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__); 33183210e59SAndrzej Pietrasiewicz *outbuf = cpu_to_le32(params->media_state); 33200a2430fSAndrzej Pietrasiewicz retval = 0; 33300a2430fSAndrzej Pietrasiewicz break; 33400a2430fSAndrzej Pietrasiewicz 33500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_PHYSICAL_MEDIUM: 33600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__); 33700a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 33800a2430fSAndrzej Pietrasiewicz retval = 0; 33900a2430fSAndrzej Pietrasiewicz break; 34000a2430fSAndrzej Pietrasiewicz 34100a2430fSAndrzej Pietrasiewicz /* The RNDIS specification is incomplete/wrong. Some versions 34200a2430fSAndrzej Pietrasiewicz * of MS-Windows expect OIDs that aren't specified there. Other 34300a2430fSAndrzej Pietrasiewicz * versions emit undefined RNDIS messages. DOCUMENT ALL THESE! 34400a2430fSAndrzej Pietrasiewicz */ 34500a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */ 34600a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__); 34700a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32( 34800a2430fSAndrzej Pietrasiewicz RNDIS_MAC_OPTION_RECEIVE_SERIALIZED 34900a2430fSAndrzej Pietrasiewicz | RNDIS_MAC_OPTION_FULL_DUPLEX); 35000a2430fSAndrzej Pietrasiewicz retval = 0; 35100a2430fSAndrzej Pietrasiewicz break; 35200a2430fSAndrzej Pietrasiewicz 35300a2430fSAndrzej Pietrasiewicz /* statistics OIDs (table 4-2) */ 35400a2430fSAndrzej Pietrasiewicz 35500a2430fSAndrzej Pietrasiewicz /* mandatory */ 35600a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_XMIT_OK: 35700a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 35800a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__); 35900a2430fSAndrzej Pietrasiewicz if (stats) { 36000a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->tx_packets 36100a2430fSAndrzej Pietrasiewicz - stats->tx_errors - stats->tx_dropped); 36200a2430fSAndrzej Pietrasiewicz retval = 0; 36300a2430fSAndrzej Pietrasiewicz } 36400a2430fSAndrzej Pietrasiewicz break; 36500a2430fSAndrzej Pietrasiewicz 36600a2430fSAndrzej Pietrasiewicz /* mandatory */ 36700a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_OK: 36800a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 36900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__); 37000a2430fSAndrzej Pietrasiewicz if (stats) { 37100a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_packets 37200a2430fSAndrzej Pietrasiewicz - stats->rx_errors - stats->rx_dropped); 37300a2430fSAndrzej Pietrasiewicz retval = 0; 37400a2430fSAndrzej Pietrasiewicz } 37500a2430fSAndrzej Pietrasiewicz break; 37600a2430fSAndrzej Pietrasiewicz 37700a2430fSAndrzej Pietrasiewicz /* mandatory */ 37800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_XMIT_ERROR: 37900a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 38000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__); 38100a2430fSAndrzej Pietrasiewicz if (stats) { 38200a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->tx_errors); 38300a2430fSAndrzej Pietrasiewicz retval = 0; 38400a2430fSAndrzej Pietrasiewicz } 38500a2430fSAndrzej Pietrasiewicz break; 38600a2430fSAndrzej Pietrasiewicz 38700a2430fSAndrzej Pietrasiewicz /* mandatory */ 38800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_ERROR: 38900a2430fSAndrzej Pietrasiewicz if (rndis_debug > 1) 39000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__); 39100a2430fSAndrzej Pietrasiewicz if (stats) { 39200a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_errors); 39300a2430fSAndrzej Pietrasiewicz retval = 0; 39400a2430fSAndrzej Pietrasiewicz } 39500a2430fSAndrzej Pietrasiewicz break; 39600a2430fSAndrzej Pietrasiewicz 39700a2430fSAndrzej Pietrasiewicz /* mandatory */ 39800a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_RCV_NO_BUFFER: 39900a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__); 40000a2430fSAndrzej Pietrasiewicz if (stats) { 40100a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_dropped); 40200a2430fSAndrzej Pietrasiewicz retval = 0; 40300a2430fSAndrzej Pietrasiewicz } 40400a2430fSAndrzej Pietrasiewicz break; 40500a2430fSAndrzej Pietrasiewicz 40600a2430fSAndrzej Pietrasiewicz /* ieee802.3 OIDs (table 4-3) */ 40700a2430fSAndrzej Pietrasiewicz 40800a2430fSAndrzej Pietrasiewicz /* mandatory */ 40900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_PERMANENT_ADDRESS: 41000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__); 41183210e59SAndrzej Pietrasiewicz if (params->dev) { 41200a2430fSAndrzej Pietrasiewicz length = ETH_ALEN; 41383210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->host_mac, length); 41400a2430fSAndrzej Pietrasiewicz retval = 0; 41500a2430fSAndrzej Pietrasiewicz } 41600a2430fSAndrzej Pietrasiewicz break; 41700a2430fSAndrzej Pietrasiewicz 41800a2430fSAndrzej Pietrasiewicz /* mandatory */ 41900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_CURRENT_ADDRESS: 42000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__); 42183210e59SAndrzej Pietrasiewicz if (params->dev) { 42200a2430fSAndrzej Pietrasiewicz length = ETH_ALEN; 42383210e59SAndrzej Pietrasiewicz memcpy(outbuf, params->host_mac, length); 42400a2430fSAndrzej Pietrasiewicz retval = 0; 42500a2430fSAndrzej Pietrasiewicz } 42600a2430fSAndrzej Pietrasiewicz break; 42700a2430fSAndrzej Pietrasiewicz 42800a2430fSAndrzej Pietrasiewicz /* mandatory */ 42900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MULTICAST_LIST: 43000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__); 43100a2430fSAndrzej Pietrasiewicz /* Multicast base address only */ 43200a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0xE0000000); 43300a2430fSAndrzej Pietrasiewicz retval = 0; 43400a2430fSAndrzej Pietrasiewicz break; 43500a2430fSAndrzej Pietrasiewicz 43600a2430fSAndrzej Pietrasiewicz /* mandatory */ 43700a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE: 43800a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__); 43900a2430fSAndrzej Pietrasiewicz /* Multicast base address only */ 44000a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(1); 44100a2430fSAndrzej Pietrasiewicz retval = 0; 44200a2430fSAndrzej Pietrasiewicz break; 44300a2430fSAndrzej Pietrasiewicz 44400a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MAC_OPTIONS: 44500a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__); 44600a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 44700a2430fSAndrzej Pietrasiewicz retval = 0; 44800a2430fSAndrzej Pietrasiewicz break; 44900a2430fSAndrzej Pietrasiewicz 45000a2430fSAndrzej Pietrasiewicz /* ieee802.3 statistics OIDs (table 4-4) */ 45100a2430fSAndrzej Pietrasiewicz 45200a2430fSAndrzej Pietrasiewicz /* mandatory */ 45300a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT: 45400a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__); 45500a2430fSAndrzej Pietrasiewicz if (stats) { 45600a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(stats->rx_frame_errors); 45700a2430fSAndrzej Pietrasiewicz retval = 0; 45800a2430fSAndrzej Pietrasiewicz } 45900a2430fSAndrzej Pietrasiewicz break; 46000a2430fSAndrzej Pietrasiewicz 46100a2430fSAndrzej Pietrasiewicz /* mandatory */ 46200a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_XMIT_ONE_COLLISION: 46300a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__); 46400a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 46500a2430fSAndrzej Pietrasiewicz retval = 0; 46600a2430fSAndrzej Pietrasiewicz break; 46700a2430fSAndrzej Pietrasiewicz 46800a2430fSAndrzej Pietrasiewicz /* mandatory */ 46900a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS: 47000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__); 47100a2430fSAndrzej Pietrasiewicz *outbuf = cpu_to_le32(0); 47200a2430fSAndrzej Pietrasiewicz retval = 0; 47300a2430fSAndrzej Pietrasiewicz break; 47400a2430fSAndrzej Pietrasiewicz 47500a2430fSAndrzej Pietrasiewicz default: 4763f5ad864SJoe Perches pr_warn("%s: query unknown OID 0x%08X\n", __func__, OID); 47700a2430fSAndrzej Pietrasiewicz } 47800a2430fSAndrzej Pietrasiewicz if (retval < 0) 47900a2430fSAndrzej Pietrasiewicz length = 0; 48000a2430fSAndrzej Pietrasiewicz 48100a2430fSAndrzej Pietrasiewicz resp->InformationBufferLength = cpu_to_le32(length); 48200a2430fSAndrzej Pietrasiewicz r->length = length + sizeof(*resp); 48300a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(r->length); 48400a2430fSAndrzej Pietrasiewicz return retval; 48500a2430fSAndrzej Pietrasiewicz } 48600a2430fSAndrzej Pietrasiewicz 48783210e59SAndrzej Pietrasiewicz static int gen_ndis_set_resp(struct rndis_params *params, u32 OID, 48883210e59SAndrzej Pietrasiewicz u8 *buf, u32 buf_len, rndis_resp_t *r) 48900a2430fSAndrzej Pietrasiewicz { 49000a2430fSAndrzej Pietrasiewicz rndis_set_cmplt_type *resp; 49100a2430fSAndrzej Pietrasiewicz int i, retval = -ENOTSUPP; 49200a2430fSAndrzej Pietrasiewicz 49300a2430fSAndrzej Pietrasiewicz if (!r) 49400a2430fSAndrzej Pietrasiewicz return -ENOMEM; 49500a2430fSAndrzej Pietrasiewicz resp = (rndis_set_cmplt_type *)r->buf; 49600a2430fSAndrzej Pietrasiewicz if (!resp) 49700a2430fSAndrzej Pietrasiewicz return -ENOMEM; 49800a2430fSAndrzej Pietrasiewicz 49900a2430fSAndrzej Pietrasiewicz if (buf_len && rndis_debug > 1) { 50000a2430fSAndrzej Pietrasiewicz pr_debug("set OID %08x value, len %d:\n", OID, buf_len); 50100a2430fSAndrzej Pietrasiewicz for (i = 0; i < buf_len; i += 16) { 50200a2430fSAndrzej Pietrasiewicz pr_debug("%03d: %08x %08x %08x %08x\n", i, 50300a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i]), 50400a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 4]), 50500a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 8]), 50600a2430fSAndrzej Pietrasiewicz get_unaligned_le32(&buf[i + 12])); 50700a2430fSAndrzej Pietrasiewicz } 50800a2430fSAndrzej Pietrasiewicz } 50900a2430fSAndrzej Pietrasiewicz 51000a2430fSAndrzej Pietrasiewicz switch (OID) { 51100a2430fSAndrzej Pietrasiewicz case RNDIS_OID_GEN_CURRENT_PACKET_FILTER: 51200a2430fSAndrzej Pietrasiewicz 51300a2430fSAndrzej Pietrasiewicz /* these NDIS_PACKET_TYPE_* bitflags are shared with 51400a2430fSAndrzej Pietrasiewicz * cdc_filter; it's not RNDIS-specific 51500a2430fSAndrzej Pietrasiewicz * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in: 51600a2430fSAndrzej Pietrasiewicz * PROMISCUOUS, DIRECTED, 51700a2430fSAndrzej Pietrasiewicz * MULTICAST, ALL_MULTICAST, BROADCAST 51800a2430fSAndrzej Pietrasiewicz */ 51900a2430fSAndrzej Pietrasiewicz *params->filter = (u16)get_unaligned_le32(buf); 52000a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n", 52100a2430fSAndrzej Pietrasiewicz __func__, *params->filter); 52200a2430fSAndrzej Pietrasiewicz 52300a2430fSAndrzej Pietrasiewicz /* this call has a significant side effect: it's 52400a2430fSAndrzej Pietrasiewicz * what makes the packet flow start and stop, like 52500a2430fSAndrzej Pietrasiewicz * activating the CDC Ethernet altsetting. 52600a2430fSAndrzej Pietrasiewicz */ 52700a2430fSAndrzej Pietrasiewicz retval = 0; 52800a2430fSAndrzej Pietrasiewicz if (*params->filter) { 52900a2430fSAndrzej Pietrasiewicz params->state = RNDIS_DATA_INITIALIZED; 53000a2430fSAndrzej Pietrasiewicz netif_carrier_on(params->dev); 53100a2430fSAndrzej Pietrasiewicz if (netif_running(params->dev)) 53200a2430fSAndrzej Pietrasiewicz netif_wake_queue(params->dev); 53300a2430fSAndrzej Pietrasiewicz } else { 53400a2430fSAndrzej Pietrasiewicz params->state = RNDIS_INITIALIZED; 53500a2430fSAndrzej Pietrasiewicz netif_carrier_off(params->dev); 53600a2430fSAndrzej Pietrasiewicz netif_stop_queue(params->dev); 53700a2430fSAndrzej Pietrasiewicz } 53800a2430fSAndrzej Pietrasiewicz break; 53900a2430fSAndrzej Pietrasiewicz 54000a2430fSAndrzej Pietrasiewicz case RNDIS_OID_802_3_MULTICAST_LIST: 54100a2430fSAndrzej Pietrasiewicz /* I think we can ignore this */ 54200a2430fSAndrzej Pietrasiewicz pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__); 54300a2430fSAndrzej Pietrasiewicz retval = 0; 54400a2430fSAndrzej Pietrasiewicz break; 54500a2430fSAndrzej Pietrasiewicz 54600a2430fSAndrzej Pietrasiewicz default: 5473f5ad864SJoe Perches pr_warn("%s: set unknown OID 0x%08X, size %d\n", 54800a2430fSAndrzej Pietrasiewicz __func__, OID, buf_len); 54900a2430fSAndrzej Pietrasiewicz } 55000a2430fSAndrzej Pietrasiewicz 55100a2430fSAndrzej Pietrasiewicz return retval; 55200a2430fSAndrzej Pietrasiewicz } 55300a2430fSAndrzej Pietrasiewicz 55400a2430fSAndrzej Pietrasiewicz /* 55500a2430fSAndrzej Pietrasiewicz * Response Functions 55600a2430fSAndrzej Pietrasiewicz */ 55700a2430fSAndrzej Pietrasiewicz 55883210e59SAndrzej Pietrasiewicz static int rndis_init_response(struct rndis_params *params, 55983210e59SAndrzej Pietrasiewicz rndis_init_msg_type *buf) 56000a2430fSAndrzej Pietrasiewicz { 56100a2430fSAndrzej Pietrasiewicz rndis_init_cmplt_type *resp; 56200a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 56300a2430fSAndrzej Pietrasiewicz 56400a2430fSAndrzej Pietrasiewicz if (!params->dev) 56500a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 56600a2430fSAndrzej Pietrasiewicz 56783210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_init_cmplt_type)); 56800a2430fSAndrzej Pietrasiewicz if (!r) 56900a2430fSAndrzej Pietrasiewicz return -ENOMEM; 57000a2430fSAndrzej Pietrasiewicz resp = (rndis_init_cmplt_type *)r->buf; 57100a2430fSAndrzej Pietrasiewicz 57200a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C); 57300a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(52); 57400a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 57500a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 57600a2430fSAndrzej Pietrasiewicz resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION); 57700a2430fSAndrzej Pietrasiewicz resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION); 57800a2430fSAndrzej Pietrasiewicz resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS); 57900a2430fSAndrzej Pietrasiewicz resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3); 58000a2430fSAndrzej Pietrasiewicz resp->MaxPacketsPerTransfer = cpu_to_le32(1); 58100a2430fSAndrzej Pietrasiewicz resp->MaxTransferSize = cpu_to_le32( 58200a2430fSAndrzej Pietrasiewicz params->dev->mtu 58300a2430fSAndrzej Pietrasiewicz + sizeof(struct ethhdr) 58400a2430fSAndrzej Pietrasiewicz + sizeof(struct rndis_packet_msg_type) 58500a2430fSAndrzej Pietrasiewicz + 22); 58600a2430fSAndrzej Pietrasiewicz resp->PacketAlignmentFactor = cpu_to_le32(0); 58700a2430fSAndrzej Pietrasiewicz resp->AFListOffset = cpu_to_le32(0); 58800a2430fSAndrzej Pietrasiewicz resp->AFListSize = cpu_to_le32(0); 58900a2430fSAndrzej Pietrasiewicz 59000a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 59100a2430fSAndrzej Pietrasiewicz return 0; 59200a2430fSAndrzej Pietrasiewicz } 59300a2430fSAndrzej Pietrasiewicz 59483210e59SAndrzej Pietrasiewicz static int rndis_query_response(struct rndis_params *params, 59583210e59SAndrzej Pietrasiewicz rndis_query_msg_type *buf) 59600a2430fSAndrzej Pietrasiewicz { 59700a2430fSAndrzej Pietrasiewicz rndis_query_cmplt_type *resp; 59800a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 59900a2430fSAndrzej Pietrasiewicz 60000a2430fSAndrzej Pietrasiewicz /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */ 60100a2430fSAndrzej Pietrasiewicz if (!params->dev) 60200a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 60300a2430fSAndrzej Pietrasiewicz 60400a2430fSAndrzej Pietrasiewicz /* 60500a2430fSAndrzej Pietrasiewicz * we need more memory: 60600a2430fSAndrzej Pietrasiewicz * gen_ndis_query_resp expects enough space for 60700a2430fSAndrzej Pietrasiewicz * rndis_query_cmplt_type followed by data. 60800a2430fSAndrzej Pietrasiewicz * oid_supported_list is the largest data reply 60900a2430fSAndrzej Pietrasiewicz */ 61083210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, 61100a2430fSAndrzej Pietrasiewicz sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type)); 61200a2430fSAndrzej Pietrasiewicz if (!r) 61300a2430fSAndrzej Pietrasiewicz return -ENOMEM; 61400a2430fSAndrzej Pietrasiewicz resp = (rndis_query_cmplt_type *)r->buf; 61500a2430fSAndrzej Pietrasiewicz 61600a2430fSAndrzej Pietrasiewicz resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C); 61700a2430fSAndrzej Pietrasiewicz resp->RequestID = buf->RequestID; /* Still LE in msg buffer */ 61800a2430fSAndrzej Pietrasiewicz 61983210e59SAndrzej Pietrasiewicz if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID), 62000a2430fSAndrzej Pietrasiewicz le32_to_cpu(buf->InformationBufferOffset) 62100a2430fSAndrzej Pietrasiewicz + 8 + (u8 *)buf, 62200a2430fSAndrzej Pietrasiewicz le32_to_cpu(buf->InformationBufferLength), 62300a2430fSAndrzej Pietrasiewicz r)) { 62400a2430fSAndrzej Pietrasiewicz /* OID not supported */ 62500a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED); 62600a2430fSAndrzej Pietrasiewicz resp->MessageLength = cpu_to_le32(sizeof *resp); 62700a2430fSAndrzej Pietrasiewicz resp->InformationBufferLength = cpu_to_le32(0); 62800a2430fSAndrzej Pietrasiewicz resp->InformationBufferOffset = cpu_to_le32(0); 62900a2430fSAndrzej Pietrasiewicz } else 63000a2430fSAndrzej Pietrasiewicz resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 63100a2430fSAndrzej Pietrasiewicz 63200a2430fSAndrzej Pietrasiewicz params->resp_avail(params->v); 63300a2430fSAndrzej Pietrasiewicz return 0; 63400a2430fSAndrzej Pietrasiewicz } 63500a2430fSAndrzej Pietrasiewicz 63683210e59SAndrzej Pietrasiewicz static int rndis_set_response(struct rndis_params *params, 63783210e59SAndrzej Pietrasiewicz rndis_set_msg_type *buf) 63800a2430fSAndrzej Pietrasiewicz { 63900a2430fSAndrzej Pietrasiewicz u32 BufLength, BufOffset; 64000a2430fSAndrzej Pietrasiewicz rndis_set_cmplt_type *resp; 64100a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 64200a2430fSAndrzej Pietrasiewicz 64383210e59SAndrzej Pietrasiewicz r = rndis_add_response(params, sizeof(rndis_set_cmplt_type)); 64400a2430fSAndrzej Pietrasiewicz if (!r) 64500a2430fSAndrzej Pietrasiewicz return -ENOMEM; 64600a2430fSAndrzej Pietrasiewicz resp = (rndis_set_cmplt_type *)r->buf; 64700a2430fSAndrzej Pietrasiewicz 64800a2430fSAndrzej Pietrasiewicz BufLength = le32_to_cpu(buf->InformationBufferLength); 64900a2430fSAndrzej Pietrasiewicz BufOffset = le32_to_cpu(buf->InformationBufferOffset); 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); 85700a2430fSAndrzej Pietrasiewicz print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET, 85800a2430fSAndrzej Pietrasiewicz buf, MsgLength); 85900a2430fSAndrzej Pietrasiewicz break; 86000a2430fSAndrzej Pietrasiewicz } 86100a2430fSAndrzej Pietrasiewicz 86200a2430fSAndrzej Pietrasiewicz return -ENOTSUPP; 86300a2430fSAndrzej Pietrasiewicz } 86400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_msg_parser); 86500a2430fSAndrzej Pietrasiewicz 866d6d22922SAndrzej Pietrasiewicz static inline int rndis_get_nr(void) 867d6d22922SAndrzej Pietrasiewicz { 868d6d22922SAndrzej Pietrasiewicz return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL); 869d6d22922SAndrzej Pietrasiewicz } 870d6d22922SAndrzej Pietrasiewicz 871d6d22922SAndrzej Pietrasiewicz static inline void rndis_put_nr(int nr) 872d6d22922SAndrzej Pietrasiewicz { 873d6d22922SAndrzej Pietrasiewicz ida_simple_remove(&rndis_ida, nr); 874d6d22922SAndrzej Pietrasiewicz } 875d6d22922SAndrzej Pietrasiewicz 87683210e59SAndrzej Pietrasiewicz struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v) 87700a2430fSAndrzej Pietrasiewicz { 878d6d22922SAndrzej Pietrasiewicz struct rndis_params *params; 87981dff869SAndrzej Pietrasiewicz int i; 88000a2430fSAndrzej Pietrasiewicz 88100a2430fSAndrzej Pietrasiewicz if (!resp_avail) 88283210e59SAndrzej Pietrasiewicz return ERR_PTR(-EINVAL); 88300a2430fSAndrzej Pietrasiewicz 884d6d22922SAndrzej Pietrasiewicz i = rndis_get_nr(); 885d6d22922SAndrzej Pietrasiewicz if (i < 0) { 88600a2430fSAndrzej Pietrasiewicz pr_debug("failed\n"); 88700a2430fSAndrzej Pietrasiewicz 88883210e59SAndrzej Pietrasiewicz return ERR_PTR(-ENODEV); 88900a2430fSAndrzej Pietrasiewicz } 890d6d22922SAndrzej Pietrasiewicz 891d6d22922SAndrzej Pietrasiewicz params = kzalloc(sizeof(*params), GFP_KERNEL); 892d6d22922SAndrzej Pietrasiewicz if (!params) { 893d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 894d6d22922SAndrzej Pietrasiewicz 895d6d22922SAndrzej Pietrasiewicz return ERR_PTR(-ENOMEM); 896d6d22922SAndrzej Pietrasiewicz } 897d6d22922SAndrzej Pietrasiewicz 898d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 899d6d22922SAndrzej Pietrasiewicz { 900d6d22922SAndrzej Pietrasiewicz struct proc_dir_entry *proc_entry; 901d6d22922SAndrzej Pietrasiewicz char name[20]; 902d6d22922SAndrzej Pietrasiewicz 903d6d22922SAndrzej Pietrasiewicz sprintf(name, NAME_TEMPLATE, i); 904d6d22922SAndrzej Pietrasiewicz proc_entry = proc_create_data(name, 0660, NULL, 905d6d22922SAndrzej Pietrasiewicz &rndis_proc_fops, params); 906d6d22922SAndrzej Pietrasiewicz if (!proc_entry) { 907d6d22922SAndrzej Pietrasiewicz kfree(params); 908d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 909d6d22922SAndrzej Pietrasiewicz 910d6d22922SAndrzej Pietrasiewicz return ERR_PTR(-EIO); 911d6d22922SAndrzej Pietrasiewicz } 912d6d22922SAndrzej Pietrasiewicz } 913d6d22922SAndrzej Pietrasiewicz #endif 914d6d22922SAndrzej Pietrasiewicz 915d6d22922SAndrzej Pietrasiewicz params->confignr = i; 916d6d22922SAndrzej Pietrasiewicz params->used = 1; 917d6d22922SAndrzej Pietrasiewicz params->state = RNDIS_UNINITIALIZED; 918d6d22922SAndrzej Pietrasiewicz params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED; 919d6d22922SAndrzej Pietrasiewicz params->resp_avail = resp_avail; 920d6d22922SAndrzej Pietrasiewicz params->v = v; 921f6281af9SGeliang Tang INIT_LIST_HEAD(¶ms->resp_queue); 922d6d22922SAndrzej Pietrasiewicz pr_debug("%s: configNr = %d\n", __func__, i); 923d6d22922SAndrzej Pietrasiewicz 924d6d22922SAndrzej Pietrasiewicz return params; 925d6d22922SAndrzej Pietrasiewicz } 92600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_register); 92700a2430fSAndrzej Pietrasiewicz 92883210e59SAndrzej Pietrasiewicz void rndis_deregister(struct rndis_params *params) 92900a2430fSAndrzej Pietrasiewicz { 93081dff869SAndrzej Pietrasiewicz int i; 931d6d22922SAndrzej Pietrasiewicz 93200a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 93300a2430fSAndrzej Pietrasiewicz 93483210e59SAndrzej Pietrasiewicz if (!params) 93583210e59SAndrzej Pietrasiewicz return; 936d6d22922SAndrzej Pietrasiewicz 937d6d22922SAndrzej Pietrasiewicz i = params->confignr; 938d6d22922SAndrzej Pietrasiewicz 939d6d22922SAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 940d6d22922SAndrzej Pietrasiewicz { 941d6d22922SAndrzej Pietrasiewicz char name[20]; 942d6d22922SAndrzej Pietrasiewicz 943d6d22922SAndrzej Pietrasiewicz sprintf(name, NAME_TEMPLATE, i); 944d6d22922SAndrzej Pietrasiewicz remove_proc_entry(name, NULL); 945d6d22922SAndrzej Pietrasiewicz } 946d6d22922SAndrzej Pietrasiewicz #endif 947d6d22922SAndrzej Pietrasiewicz 948d6d22922SAndrzej Pietrasiewicz kfree(params); 949d6d22922SAndrzej Pietrasiewicz rndis_put_nr(i); 95000a2430fSAndrzej Pietrasiewicz } 95100a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_deregister); 95283210e59SAndrzej Pietrasiewicz int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev, 95383210e59SAndrzej Pietrasiewicz u16 *cdc_filter) 95400a2430fSAndrzej Pietrasiewicz { 95500a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 95600a2430fSAndrzej Pietrasiewicz if (!dev) 95700a2430fSAndrzej Pietrasiewicz return -EINVAL; 95883210e59SAndrzej Pietrasiewicz if (!params) 95983210e59SAndrzej Pietrasiewicz return -1; 96000a2430fSAndrzej Pietrasiewicz 96183210e59SAndrzej Pietrasiewicz params->dev = dev; 96283210e59SAndrzej Pietrasiewicz params->filter = cdc_filter; 96300a2430fSAndrzej Pietrasiewicz 96400a2430fSAndrzej Pietrasiewicz return 0; 96500a2430fSAndrzej Pietrasiewicz } 96600a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_dev); 96700a2430fSAndrzej Pietrasiewicz 96883210e59SAndrzej Pietrasiewicz int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID, 96983210e59SAndrzej Pietrasiewicz const char *vendorDescr) 97000a2430fSAndrzej Pietrasiewicz { 97100a2430fSAndrzej Pietrasiewicz pr_debug("%s:\n", __func__); 97200a2430fSAndrzej Pietrasiewicz if (!vendorDescr) return -1; 97383210e59SAndrzej Pietrasiewicz if (!params) 97483210e59SAndrzej Pietrasiewicz return -1; 97500a2430fSAndrzej Pietrasiewicz 97683210e59SAndrzej Pietrasiewicz params->vendorID = vendorID; 97783210e59SAndrzej Pietrasiewicz params->vendorDescr = vendorDescr; 97800a2430fSAndrzej Pietrasiewicz 97900a2430fSAndrzej Pietrasiewicz return 0; 98000a2430fSAndrzej Pietrasiewicz } 98100a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_vendor); 98200a2430fSAndrzej Pietrasiewicz 98383210e59SAndrzej Pietrasiewicz int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed) 98400a2430fSAndrzej Pietrasiewicz { 98500a2430fSAndrzej Pietrasiewicz pr_debug("%s: %u %u\n", __func__, medium, speed); 98683210e59SAndrzej Pietrasiewicz if (!params) 98783210e59SAndrzej Pietrasiewicz return -1; 98800a2430fSAndrzej Pietrasiewicz 98983210e59SAndrzej Pietrasiewicz params->medium = medium; 99083210e59SAndrzej Pietrasiewicz params->speed = speed; 99100a2430fSAndrzej Pietrasiewicz 99200a2430fSAndrzej Pietrasiewicz return 0; 99300a2430fSAndrzej Pietrasiewicz } 99400a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_set_param_medium); 99500a2430fSAndrzej Pietrasiewicz 99600a2430fSAndrzej Pietrasiewicz void rndis_add_hdr(struct sk_buff *skb) 99700a2430fSAndrzej Pietrasiewicz { 99800a2430fSAndrzej Pietrasiewicz struct rndis_packet_msg_type *header; 99900a2430fSAndrzej Pietrasiewicz 100000a2430fSAndrzej Pietrasiewicz if (!skb) 100100a2430fSAndrzej Pietrasiewicz return; 1002*d58ff351SJohannes Berg header = skb_push(skb, sizeof(*header)); 100300a2430fSAndrzej Pietrasiewicz memset(header, 0, sizeof *header); 100400a2430fSAndrzej Pietrasiewicz header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET); 100500a2430fSAndrzej Pietrasiewicz header->MessageLength = cpu_to_le32(skb->len); 100600a2430fSAndrzej Pietrasiewicz header->DataOffset = cpu_to_le32(36); 100700a2430fSAndrzej Pietrasiewicz header->DataLength = cpu_to_le32(skb->len - sizeof(*header)); 100800a2430fSAndrzej Pietrasiewicz } 100900a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_add_hdr); 101000a2430fSAndrzej Pietrasiewicz 101183210e59SAndrzej Pietrasiewicz void rndis_free_response(struct rndis_params *params, u8 *buf) 101200a2430fSAndrzej Pietrasiewicz { 1013f6281af9SGeliang Tang rndis_resp_t *r, *n; 101400a2430fSAndrzej Pietrasiewicz 1015f6281af9SGeliang Tang list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { 10161c17a353SJulia Lawall if (r->buf == buf) { 101700a2430fSAndrzej Pietrasiewicz list_del(&r->list); 101800a2430fSAndrzej Pietrasiewicz kfree(r); 101900a2430fSAndrzej Pietrasiewicz } 102000a2430fSAndrzej Pietrasiewicz } 102100a2430fSAndrzej Pietrasiewicz } 102200a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_free_response); 102300a2430fSAndrzej Pietrasiewicz 102483210e59SAndrzej Pietrasiewicz u8 *rndis_get_next_response(struct rndis_params *params, u32 *length) 102500a2430fSAndrzej Pietrasiewicz { 1026f6281af9SGeliang Tang rndis_resp_t *r, *n; 102700a2430fSAndrzej Pietrasiewicz 102800a2430fSAndrzej Pietrasiewicz if (!length) return NULL; 102900a2430fSAndrzej Pietrasiewicz 1030f6281af9SGeliang Tang list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { 103100a2430fSAndrzej Pietrasiewicz if (!r->send) { 103200a2430fSAndrzej Pietrasiewicz r->send = 1; 103300a2430fSAndrzej Pietrasiewicz *length = r->length; 103400a2430fSAndrzej Pietrasiewicz return r->buf; 103500a2430fSAndrzej Pietrasiewicz } 103600a2430fSAndrzej Pietrasiewicz } 103700a2430fSAndrzej Pietrasiewicz 103800a2430fSAndrzej Pietrasiewicz return NULL; 103900a2430fSAndrzej Pietrasiewicz } 104000a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_get_next_response); 104100a2430fSAndrzej Pietrasiewicz 104283210e59SAndrzej Pietrasiewicz static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length) 104300a2430fSAndrzej Pietrasiewicz { 104400a2430fSAndrzej Pietrasiewicz rndis_resp_t *r; 104500a2430fSAndrzej Pietrasiewicz 104600a2430fSAndrzej Pietrasiewicz /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */ 104700a2430fSAndrzej Pietrasiewicz r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC); 104800a2430fSAndrzej Pietrasiewicz if (!r) return NULL; 104900a2430fSAndrzej Pietrasiewicz 105000a2430fSAndrzej Pietrasiewicz r->buf = (u8 *)(r + 1); 105100a2430fSAndrzej Pietrasiewicz r->length = length; 105200a2430fSAndrzej Pietrasiewicz r->send = 0; 105300a2430fSAndrzej Pietrasiewicz 1054f6281af9SGeliang Tang list_add_tail(&r->list, ¶ms->resp_queue); 105500a2430fSAndrzej Pietrasiewicz return r; 105600a2430fSAndrzej Pietrasiewicz } 105700a2430fSAndrzej Pietrasiewicz 105800a2430fSAndrzej Pietrasiewicz int rndis_rm_hdr(struct gether *port, 105900a2430fSAndrzej Pietrasiewicz struct sk_buff *skb, 106000a2430fSAndrzej Pietrasiewicz struct sk_buff_head *list) 106100a2430fSAndrzej Pietrasiewicz { 106200a2430fSAndrzej Pietrasiewicz /* tmp points to a struct rndis_packet_msg_type */ 106300a2430fSAndrzej Pietrasiewicz __le32 *tmp = (void *)skb->data; 106400a2430fSAndrzej Pietrasiewicz 106500a2430fSAndrzej Pietrasiewicz /* MessageType, MessageLength */ 106600a2430fSAndrzej Pietrasiewicz if (cpu_to_le32(RNDIS_MSG_PACKET) 106700a2430fSAndrzej Pietrasiewicz != get_unaligned(tmp++)) { 106800a2430fSAndrzej Pietrasiewicz dev_kfree_skb_any(skb); 106900a2430fSAndrzej Pietrasiewicz return -EINVAL; 107000a2430fSAndrzej Pietrasiewicz } 107100a2430fSAndrzej Pietrasiewicz tmp++; 107200a2430fSAndrzej Pietrasiewicz 107300a2430fSAndrzej Pietrasiewicz /* DataOffset, DataLength */ 107400a2430fSAndrzej Pietrasiewicz if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) { 107500a2430fSAndrzej Pietrasiewicz dev_kfree_skb_any(skb); 107600a2430fSAndrzej Pietrasiewicz return -EOVERFLOW; 107700a2430fSAndrzej Pietrasiewicz } 107800a2430fSAndrzej Pietrasiewicz skb_trim(skb, get_unaligned_le32(tmp++)); 107900a2430fSAndrzej Pietrasiewicz 108000a2430fSAndrzej Pietrasiewicz skb_queue_tail(list, skb); 108100a2430fSAndrzej Pietrasiewicz return 0; 108200a2430fSAndrzej Pietrasiewicz } 108300a2430fSAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(rndis_rm_hdr); 108400a2430fSAndrzej Pietrasiewicz 108500a2430fSAndrzej Pietrasiewicz #ifdef CONFIG_USB_GADGET_DEBUG_FILES 108600a2430fSAndrzej Pietrasiewicz 108700a2430fSAndrzej Pietrasiewicz static int rndis_proc_show(struct seq_file *m, void *v) 108800a2430fSAndrzej Pietrasiewicz { 108900a2430fSAndrzej Pietrasiewicz rndis_params *param = m->private; 109000a2430fSAndrzej Pietrasiewicz 109100a2430fSAndrzej Pietrasiewicz seq_printf(m, 109200a2430fSAndrzej Pietrasiewicz "Config Nr. %d\n" 109300a2430fSAndrzej Pietrasiewicz "used : %s\n" 109400a2430fSAndrzej Pietrasiewicz "state : %s\n" 109500a2430fSAndrzej Pietrasiewicz "medium : 0x%08X\n" 109600a2430fSAndrzej Pietrasiewicz "speed : %d\n" 109700a2430fSAndrzej Pietrasiewicz "cable : %s\n" 109800a2430fSAndrzej Pietrasiewicz "vendor ID : 0x%08X\n" 109900a2430fSAndrzej Pietrasiewicz "vendor : %s\n", 110000a2430fSAndrzej Pietrasiewicz param->confignr, (param->used) ? "y" : "n", 110100a2430fSAndrzej Pietrasiewicz ({ char *s = "?"; 110200a2430fSAndrzej Pietrasiewicz switch (param->state) { 110300a2430fSAndrzej Pietrasiewicz case RNDIS_UNINITIALIZED: 110400a2430fSAndrzej Pietrasiewicz s = "RNDIS_UNINITIALIZED"; break; 110500a2430fSAndrzej Pietrasiewicz case RNDIS_INITIALIZED: 110600a2430fSAndrzej Pietrasiewicz s = "RNDIS_INITIALIZED"; break; 110700a2430fSAndrzej Pietrasiewicz case RNDIS_DATA_INITIALIZED: 110800a2430fSAndrzej Pietrasiewicz s = "RNDIS_DATA_INITIALIZED"; break; 110900a2430fSAndrzej Pietrasiewicz } s; }), 111000a2430fSAndrzej Pietrasiewicz param->medium, 111100a2430fSAndrzej Pietrasiewicz (param->media_state) ? 0 : param->speed*100, 111200a2430fSAndrzej Pietrasiewicz (param->media_state) ? "disconnected" : "connected", 111300a2430fSAndrzej Pietrasiewicz param->vendorID, param->vendorDescr); 111400a2430fSAndrzej Pietrasiewicz return 0; 111500a2430fSAndrzej Pietrasiewicz } 111600a2430fSAndrzej Pietrasiewicz 111700a2430fSAndrzej Pietrasiewicz static ssize_t rndis_proc_write(struct file *file, const char __user *buffer, 111800a2430fSAndrzej Pietrasiewicz size_t count, loff_t *ppos) 111900a2430fSAndrzej Pietrasiewicz { 112000a2430fSAndrzej Pietrasiewicz rndis_params *p = PDE_DATA(file_inode(file)); 112100a2430fSAndrzej Pietrasiewicz u32 speed = 0; 112200a2430fSAndrzej Pietrasiewicz int i, fl_speed = 0; 112300a2430fSAndrzej Pietrasiewicz 112400a2430fSAndrzej Pietrasiewicz for (i = 0; i < count; i++) { 112500a2430fSAndrzej Pietrasiewicz char c; 112600a2430fSAndrzej Pietrasiewicz if (get_user(c, buffer)) 112700a2430fSAndrzej Pietrasiewicz return -EFAULT; 112800a2430fSAndrzej Pietrasiewicz switch (c) { 112900a2430fSAndrzej Pietrasiewicz case '0': 113000a2430fSAndrzej Pietrasiewicz case '1': 113100a2430fSAndrzej Pietrasiewicz case '2': 113200a2430fSAndrzej Pietrasiewicz case '3': 113300a2430fSAndrzej Pietrasiewicz case '4': 113400a2430fSAndrzej Pietrasiewicz case '5': 113500a2430fSAndrzej Pietrasiewicz case '6': 113600a2430fSAndrzej Pietrasiewicz case '7': 113700a2430fSAndrzej Pietrasiewicz case '8': 113800a2430fSAndrzej Pietrasiewicz case '9': 113900a2430fSAndrzej Pietrasiewicz fl_speed = 1; 114000a2430fSAndrzej Pietrasiewicz speed = speed * 10 + c - '0'; 114100a2430fSAndrzej Pietrasiewicz break; 114200a2430fSAndrzej Pietrasiewicz case 'C': 114300a2430fSAndrzej Pietrasiewicz case 'c': 1144868055fdSAndrzej Pietrasiewicz rndis_signal_connect(p); 114500a2430fSAndrzej Pietrasiewicz break; 114600a2430fSAndrzej Pietrasiewicz case 'D': 114700a2430fSAndrzej Pietrasiewicz case 'd': 1148868055fdSAndrzej Pietrasiewicz rndis_signal_disconnect(p); 114900a2430fSAndrzej Pietrasiewicz break; 115000a2430fSAndrzej Pietrasiewicz default: 115100a2430fSAndrzej Pietrasiewicz if (fl_speed) p->speed = speed; 115200a2430fSAndrzej Pietrasiewicz else pr_debug("%c is not valid\n", c); 115300a2430fSAndrzej Pietrasiewicz break; 115400a2430fSAndrzej Pietrasiewicz } 115500a2430fSAndrzej Pietrasiewicz 115600a2430fSAndrzej Pietrasiewicz buffer++; 115700a2430fSAndrzej Pietrasiewicz } 115800a2430fSAndrzej Pietrasiewicz 115900a2430fSAndrzej Pietrasiewicz return count; 116000a2430fSAndrzej Pietrasiewicz } 116100a2430fSAndrzej Pietrasiewicz 116200a2430fSAndrzej Pietrasiewicz static int rndis_proc_open(struct inode *inode, struct file *file) 116300a2430fSAndrzej Pietrasiewicz { 116400a2430fSAndrzej Pietrasiewicz return single_open(file, rndis_proc_show, PDE_DATA(inode)); 116500a2430fSAndrzej Pietrasiewicz } 116600a2430fSAndrzej Pietrasiewicz 116700a2430fSAndrzej Pietrasiewicz static const struct file_operations rndis_proc_fops = { 116800a2430fSAndrzej Pietrasiewicz .owner = THIS_MODULE, 116900a2430fSAndrzej Pietrasiewicz .open = rndis_proc_open, 117000a2430fSAndrzej Pietrasiewicz .read = seq_read, 117100a2430fSAndrzej Pietrasiewicz .llseek = seq_lseek, 117200a2430fSAndrzej Pietrasiewicz .release = single_release, 117300a2430fSAndrzej Pietrasiewicz .write = rndis_proc_write, 117400a2430fSAndrzej Pietrasiewicz }; 117500a2430fSAndrzej Pietrasiewicz 117600a2430fSAndrzej Pietrasiewicz #define NAME_TEMPLATE "driver/rndis-%03d" 117700a2430fSAndrzej Pietrasiewicz 117800a2430fSAndrzej Pietrasiewicz #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1179