David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * f_rndis.c -- RNDIS link function driver |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 6 | * Copyright (C) 2008 Nokia Corporation |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 7 | * Copyright (C) 2009 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 8 | * Author: Michal Nazarewicz (mina86@mina86.com) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* #define VERBOSE_DEBUG */ |
| 17 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 20 | #include <linux/module.h> |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 21 | #include <linux/device.h> |
| 22 | #include <linux/etherdevice.h> |
| 23 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 24 | #include <linux/atomic.h> |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 25 | |
| 26 | #include "u_ether.h" |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 27 | #include "u_ether_configfs.h" |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 28 | #include "u_rndis.h" |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 29 | #include "rndis.h" |
Andrzej Pietrasiewicz | a747b09 | 2014-05-08 14:06:27 +0200 | [diff] [blame] | 30 | #include "configfs.h" |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 31 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 32 | /* |
| 33 | * This function is an RNDIS Ethernet port -- a Microsoft protocol that's |
| 34 | * been promoted instead of the standard CDC Ethernet. The published RNDIS |
| 35 | * spec is ambiguous, incomplete, and needlessly complex. Variants such as |
| 36 | * ActiveSync have even worse status in terms of specification. |
| 37 | * |
| 38 | * In short: it's a protocol controlled by (and for) Microsoft, not for an |
| 39 | * Open ecosystem or markets. Linux supports it *only* because Microsoft |
| 40 | * doesn't support the CDC Ethernet standard. |
| 41 | * |
| 42 | * The RNDIS data transfer model is complex, with multiple Ethernet packets |
| 43 | * per USB message, and out of band data. The control model is built around |
| 44 | * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM |
| 45 | * (modem, not Ethernet) veneer, with those ACM descriptors being entirely |
| 46 | * useless (they're ignored). RNDIS expects to be the only function in its |
| 47 | * configuration, so it's no real help if you need composite devices; and |
| 48 | * it expects to be the first configuration too. |
| 49 | * |
| 50 | * There is a single technical advantage of RNDIS over CDC Ethernet, if you |
| 51 | * discount the fluff that its RPC can be made to deliver: it doesn't need |
| 52 | * a NOP altsetting for the data interface. That lets it work on some of the |
| 53 | * "so smart it's stupid" hardware which takes over configuration changes |
| 54 | * from the software, and adds restrictions like "no altsettings". |
| 55 | * |
| 56 | * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and |
| 57 | * have all sorts of contrary-to-specification oddities that can prevent |
| 58 | * them from working sanely. Since bugfixes (or accurate specs, letting |
| 59 | * Linux work around those bugs) are unlikely to ever come from MSFT, you |
| 60 | * may want to avoid using RNDIS on purely operational grounds. |
| 61 | * |
| 62 | * Omissions from the RNDIS 1.0 specification include: |
| 63 | * |
| 64 | * - Power management ... references data that's scattered around lots |
| 65 | * of other documentation, which is incorrect/incomplete there too. |
| 66 | * |
| 67 | * - There are various undocumented protocol requirements, like the need |
| 68 | * to send garbage in some control-OUT messages. |
| 69 | * |
| 70 | * - MS-Windows drivers sometimes emit undocumented requests. |
| 71 | */ |
| 72 | |
xerox_lin | f053900 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 73 | static unsigned int rndis_dl_max_pkt_per_xfer = 3; |
| 74 | module_param(rndis_dl_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR); |
| 75 | MODULE_PARM_DESC(rndis_dl_max_pkt_per_xfer, |
| 76 | "Maximum packets per transfer for DL aggregation"); |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 77 | |
| 78 | static unsigned int rndis_ul_max_pkt_per_xfer = 3; |
| 79 | module_param(rndis_ul_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR); |
| 80 | MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer, |
| 81 | "Maximum packets per transfer for UL aggregation"); |
| 82 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 83 | struct f_rndis { |
| 84 | struct gether port; |
| 85 | u8 ctrl_id, data_id; |
| 86 | u8 ethaddr[ETH_ALEN]; |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 87 | u32 vendorID; |
| 88 | const char *manufacturer; |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 89 | struct rndis_params *params; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 90 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 91 | struct usb_ep *notify; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 92 | struct usb_request *notify_req; |
| 93 | atomic_t notify_count; |
| 94 | }; |
| 95 | |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 96 | static struct f_rndis *__rndis; |
| 97 | static spinlock_t _rndis_lock; |
| 98 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 99 | static inline struct f_rndis *func_to_rndis(struct usb_function *f) |
| 100 | { |
| 101 | return container_of(f, struct f_rndis, port.func); |
| 102 | } |
| 103 | |
| 104 | /* peak (theoretical) bulk transfer rate in bits-per-second */ |
| 105 | static unsigned int bitrate(struct usb_gadget *g) |
| 106 | { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 107 | if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER) |
| 108 | return 13 * 1024 * 8 * 1000 * 8; |
| 109 | else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 110 | return 13 * 512 * 8 * 1000 * 8; |
| 111 | else |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 112 | return 19 * 64 * 1 * 1000 * 8; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /*-------------------------------------------------------------------------*/ |
| 116 | |
| 117 | /* |
| 118 | */ |
| 119 | |
Sebastian Andrzej Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 120 | #define RNDIS_STATUS_INTERVAL_MS 32 |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 121 | #define STATUS_BYTECOUNT 8 /* 8 bytes data */ |
| 122 | |
| 123 | |
| 124 | /* interface descriptor: */ |
| 125 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 126 | static struct usb_interface_descriptor rndis_control_intf = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 127 | .bLength = sizeof rndis_control_intf, |
| 128 | .bDescriptorType = USB_DT_INTERFACE, |
| 129 | |
| 130 | /* .bInterfaceNumber = DYNAMIC */ |
| 131 | /* status endpoint is optional; this could be patched later */ |
| 132 | .bNumEndpoints = 1, |
| 133 | .bInterfaceClass = USB_CLASS_COMM, |
| 134 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 135 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, |
| 136 | /* .iInterface = DYNAMIC */ |
| 137 | }; |
| 138 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 139 | static struct usb_cdc_header_desc header_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 140 | .bLength = sizeof header_desc, |
| 141 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 142 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 143 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 144 | .bcdCDC = cpu_to_le16(0x0110), |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 147 | static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 148 | .bLength = sizeof call_mgmt_descriptor, |
| 149 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 150 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 151 | |
| 152 | .bmCapabilities = 0x00, |
| 153 | .bDataInterface = 0x01, |
| 154 | }; |
| 155 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 156 | static struct usb_cdc_acm_descriptor rndis_acm_descriptor = { |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 157 | .bLength = sizeof rndis_acm_descriptor, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 158 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 159 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 160 | |
| 161 | .bmCapabilities = 0x00, |
| 162 | }; |
| 163 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 164 | static struct usb_cdc_union_desc rndis_union_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 165 | .bLength = sizeof(rndis_union_desc), |
| 166 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 167 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 168 | /* .bMasterInterface0 = DYNAMIC */ |
| 169 | /* .bSlaveInterface0 = DYNAMIC */ |
| 170 | }; |
| 171 | |
| 172 | /* the data interface has two bulk endpoints */ |
| 173 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 174 | static struct usb_interface_descriptor rndis_data_intf = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 175 | .bLength = sizeof rndis_data_intf, |
| 176 | .bDescriptorType = USB_DT_INTERFACE, |
| 177 | |
| 178 | /* .bInterfaceNumber = DYNAMIC */ |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 179 | .bNumEndpoints = 2, |
| 180 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 181 | .bInterfaceSubClass = 0, |
| 182 | .bInterfaceProtocol = 0, |
| 183 | /* .iInterface = DYNAMIC */ |
| 184 | }; |
| 185 | |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 186 | |
| 187 | static struct usb_interface_assoc_descriptor |
| 188 | rndis_iad_descriptor = { |
| 189 | .bLength = sizeof rndis_iad_descriptor, |
| 190 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 191 | |
| 192 | .bFirstInterface = 0, /* XXX, hardcoded */ |
| 193 | .bInterfaceCount = 2, // control + data |
| 194 | .bFunctionClass = USB_CLASS_COMM, |
| 195 | .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET, |
| 196 | .bFunctionProtocol = USB_CDC_PROTO_NONE, |
| 197 | /* .iFunction = DYNAMIC */ |
| 198 | }; |
| 199 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 200 | /* full speed support: */ |
| 201 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 202 | static struct usb_endpoint_descriptor fs_notify_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 203 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 204 | .bDescriptorType = USB_DT_ENDPOINT, |
| 205 | |
| 206 | .bEndpointAddress = USB_DIR_IN, |
| 207 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 208 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
Sebastian Andrzej Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 209 | .bInterval = RNDIS_STATUS_INTERVAL_MS, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 212 | static struct usb_endpoint_descriptor fs_in_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 213 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 214 | .bDescriptorType = USB_DT_ENDPOINT, |
| 215 | |
| 216 | .bEndpointAddress = USB_DIR_IN, |
| 217 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 218 | }; |
| 219 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 220 | static struct usb_endpoint_descriptor fs_out_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 221 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 222 | .bDescriptorType = USB_DT_ENDPOINT, |
| 223 | |
| 224 | .bEndpointAddress = USB_DIR_OUT, |
| 225 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 226 | }; |
| 227 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 228 | static struct usb_descriptor_header *eth_fs_function[] = { |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 229 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 230 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 231 | /* control interface matches ACM, not Ethernet */ |
| 232 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 233 | (struct usb_descriptor_header *) &header_desc, |
| 234 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 235 | (struct usb_descriptor_header *) &rndis_acm_descriptor, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 236 | (struct usb_descriptor_header *) &rndis_union_desc, |
| 237 | (struct usb_descriptor_header *) &fs_notify_desc, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 238 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 239 | /* data interface has no altsetting */ |
| 240 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 241 | (struct usb_descriptor_header *) &fs_in_desc, |
| 242 | (struct usb_descriptor_header *) &fs_out_desc, |
| 243 | NULL, |
| 244 | }; |
| 245 | |
| 246 | /* high speed support: */ |
| 247 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 248 | static struct usb_endpoint_descriptor hs_notify_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 249 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 250 | .bDescriptorType = USB_DT_ENDPOINT, |
| 251 | |
| 252 | .bEndpointAddress = USB_DIR_IN, |
| 253 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 254 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
Sebastian Andrzej Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 255 | .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 256 | }; |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 257 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 258 | static struct usb_endpoint_descriptor hs_in_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 259 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 260 | .bDescriptorType = USB_DT_ENDPOINT, |
| 261 | |
| 262 | .bEndpointAddress = USB_DIR_IN, |
| 263 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 264 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 267 | static struct usb_endpoint_descriptor hs_out_desc = { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 268 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 269 | .bDescriptorType = USB_DT_ENDPOINT, |
| 270 | |
| 271 | .bEndpointAddress = USB_DIR_OUT, |
| 272 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 273 | .wMaxPacketSize = cpu_to_le16(512), |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 276 | static struct usb_descriptor_header *eth_hs_function[] = { |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 277 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 278 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 279 | /* control interface matches ACM, not Ethernet */ |
| 280 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 281 | (struct usb_descriptor_header *) &header_desc, |
| 282 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 283 | (struct usb_descriptor_header *) &rndis_acm_descriptor, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 284 | (struct usb_descriptor_header *) &rndis_union_desc, |
| 285 | (struct usb_descriptor_header *) &hs_notify_desc, |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 286 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 287 | /* data interface has no altsetting */ |
| 288 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 289 | (struct usb_descriptor_header *) &hs_in_desc, |
| 290 | (struct usb_descriptor_header *) &hs_out_desc, |
| 291 | NULL, |
| 292 | }; |
| 293 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 294 | /* super speed support: */ |
| 295 | |
| 296 | static struct usb_endpoint_descriptor ss_notify_desc = { |
| 297 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 298 | .bDescriptorType = USB_DT_ENDPOINT, |
| 299 | |
| 300 | .bEndpointAddress = USB_DIR_IN, |
| 301 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 302 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
Sebastian Andrzej Siewior | bcb2f99 | 2012-10-22 22:14:57 +0200 | [diff] [blame] | 303 | .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS) |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = { |
| 307 | .bLength = sizeof ss_intr_comp_desc, |
| 308 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 309 | |
| 310 | /* the following 3 values can be tweaked if necessary */ |
| 311 | /* .bMaxBurst = 0, */ |
| 312 | /* .bmAttributes = 0, */ |
| 313 | .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT), |
| 314 | }; |
| 315 | |
| 316 | static struct usb_endpoint_descriptor ss_in_desc = { |
| 317 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 318 | .bDescriptorType = USB_DT_ENDPOINT, |
| 319 | |
| 320 | .bEndpointAddress = USB_DIR_IN, |
| 321 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 322 | .wMaxPacketSize = cpu_to_le16(1024), |
| 323 | }; |
| 324 | |
| 325 | static struct usb_endpoint_descriptor ss_out_desc = { |
| 326 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 327 | .bDescriptorType = USB_DT_ENDPOINT, |
| 328 | |
| 329 | .bEndpointAddress = USB_DIR_OUT, |
| 330 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 331 | .wMaxPacketSize = cpu_to_le16(1024), |
| 332 | }; |
| 333 | |
| 334 | static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = { |
| 335 | .bLength = sizeof ss_bulk_comp_desc, |
| 336 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 337 | |
| 338 | /* the following 2 values can be tweaked if necessary */ |
| 339 | /* .bMaxBurst = 0, */ |
| 340 | /* .bmAttributes = 0, */ |
| 341 | }; |
| 342 | |
| 343 | static struct usb_descriptor_header *eth_ss_function[] = { |
| 344 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
| 345 | |
| 346 | /* control interface matches ACM, not Ethernet */ |
| 347 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 348 | (struct usb_descriptor_header *) &header_desc, |
| 349 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
| 350 | (struct usb_descriptor_header *) &rndis_acm_descriptor, |
| 351 | (struct usb_descriptor_header *) &rndis_union_desc, |
| 352 | (struct usb_descriptor_header *) &ss_notify_desc, |
| 353 | (struct usb_descriptor_header *) &ss_intr_comp_desc, |
| 354 | |
| 355 | /* data interface has no altsetting */ |
| 356 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 357 | (struct usb_descriptor_header *) &ss_in_desc, |
| 358 | (struct usb_descriptor_header *) &ss_bulk_comp_desc, |
| 359 | (struct usb_descriptor_header *) &ss_out_desc, |
| 360 | (struct usb_descriptor_header *) &ss_bulk_comp_desc, |
| 361 | NULL, |
| 362 | }; |
| 363 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 364 | /* string descriptors: */ |
| 365 | |
| 366 | static struct usb_string rndis_string_defs[] = { |
| 367 | [0].s = "RNDIS Communications Control", |
| 368 | [1].s = "RNDIS Ethernet Data", |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 369 | [2].s = "RNDIS", |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 370 | { } /* end of list */ |
| 371 | }; |
| 372 | |
| 373 | static struct usb_gadget_strings rndis_string_table = { |
| 374 | .language = 0x0409, /* en-us */ |
| 375 | .strings = rndis_string_defs, |
| 376 | }; |
| 377 | |
| 378 | static struct usb_gadget_strings *rndis_strings[] = { |
| 379 | &rndis_string_table, |
| 380 | NULL, |
| 381 | }; |
| 382 | |
| 383 | /*-------------------------------------------------------------------------*/ |
| 384 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 385 | static struct sk_buff *rndis_add_header(struct gether *port, |
| 386 | struct sk_buff *skb) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 387 | { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 388 | struct sk_buff *skb2; |
| 389 | |
Peter Chen | 80d1642 | 2016-08-11 15:51:46 +0800 | [diff] [blame] | 390 | if (!skb) |
| 391 | return NULL; |
| 392 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 393 | skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); |
Markus Elfring | 7b0f000 | 2014-11-21 14:51:43 +0100 | [diff] [blame] | 394 | rndis_add_hdr(skb2); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 395 | |
Macpaul Lin | 2656c9e | 2014-04-15 16:09:33 +0800 | [diff] [blame] | 396 | dev_kfree_skb(skb); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 397 | return skb2; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void rndis_response_available(void *_rndis) |
| 401 | { |
| 402 | struct f_rndis *rndis = _rndis; |
| 403 | struct usb_request *req = rndis->notify_req; |
| 404 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; |
| 405 | __le32 *data = req->buf; |
| 406 | int status; |
| 407 | |
Richard Röjfors | ff34950 | 2008-11-15 19:53:24 -0800 | [diff] [blame] | 408 | if (atomic_inc_return(&rndis->notify_count) != 1) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 409 | return; |
| 410 | |
| 411 | /* Send RNDIS RESPONSE_AVAILABLE notification; a |
| 412 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too |
| 413 | * |
| 414 | * This is the only notification defined by RNDIS. |
| 415 | */ |
| 416 | data[0] = cpu_to_le32(1); |
| 417 | data[1] = cpu_to_le32(0); |
| 418 | |
| 419 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); |
| 420 | if (status) { |
| 421 | atomic_dec(&rndis->notify_count); |
| 422 | DBG(cdev, "notify/0 --> %d\n", status); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) |
| 427 | { |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 428 | struct f_rndis *rndis; |
| 429 | struct usb_composite_dev *cdev; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 430 | int status = req->status; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 431 | struct usb_ep *notify_ep; |
| 432 | |
| 433 | spin_lock(&_rndis_lock); |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 434 | rndis = __rndis; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 435 | if (!rndis || !rndis->notify) { |
| 436 | spin_unlock(&_rndis_lock); |
| 437 | return; |
| 438 | } |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 439 | |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 440 | cdev = rndis->port.func.config->cdev; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 441 | /* after TX: |
| 442 | * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control) |
| 443 | * - RNDIS_RESPONSE_AVAILABLE (status/irq) |
| 444 | */ |
| 445 | switch (status) { |
| 446 | case -ECONNRESET: |
| 447 | case -ESHUTDOWN: |
| 448 | /* connection gone */ |
| 449 | atomic_set(&rndis->notify_count, 0); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 450 | goto out; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 451 | default: |
| 452 | DBG(cdev, "RNDIS %s response error %d, %d/%d\n", |
| 453 | ep->name, status, |
| 454 | req->actual, req->length); |
| 455 | /* FALLTHROUGH */ |
| 456 | case 0: |
| 457 | if (ep != rndis->notify) |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 458 | goto out; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 459 | |
| 460 | /* handle multiple pending RNDIS_RESPONSE_AVAILABLE |
| 461 | * notifications by resending until we're done |
| 462 | */ |
| 463 | if (atomic_dec_and_test(&rndis->notify_count)) |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 464 | goto out; |
| 465 | notify_ep = rndis->notify; |
| 466 | spin_unlock(&_rndis_lock); |
| 467 | status = usb_ep_queue(notify_ep, req, GFP_ATOMIC); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 468 | if (status) { |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 469 | spin_lock(&_rndis_lock); |
| 470 | if (!__rndis) |
| 471 | goto out; |
| 472 | atomic_dec(&__rndis->notify_count); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 473 | DBG(cdev, "notify/1 --> %d\n", status); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 474 | spin_unlock(&_rndis_lock); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 475 | } |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 476 | } |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 477 | |
| 478 | return; |
| 479 | |
| 480 | out: |
| 481 | spin_unlock(&_rndis_lock); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) |
| 485 | { |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 486 | struct f_rndis *rndis; |
| 487 | struct usb_composite_dev *cdev; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 488 | int status; |
Badhri Jagan Sridharan | b7a343a | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 489 | rndis_init_msg_type *buf; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 490 | |
Sriharsha Allenki | e1cc61d | 2017-07-27 11:12:09 +0530 | [diff] [blame] | 491 | if (req->status != 0) { |
| 492 | pr_err("%s: RNDIS command completion error:%d\n", |
| 493 | __func__, req->status); |
| 494 | return; |
| 495 | } |
| 496 | |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 497 | spin_lock(&_rndis_lock); |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 498 | rndis = __rndis; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 499 | if (!rndis || !rndis->notify) { |
| 500 | spin_unlock(&_rndis_lock); |
Vijayavardhan Vennapusa | c23003a | 2016-03-04 10:09:39 +0530 | [diff] [blame] | 501 | return; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 502 | } |
Vijayavardhan Vennapusa | c23003a | 2016-03-04 10:09:39 +0530 | [diff] [blame] | 503 | |
Vijayavardhan Vennapusa | d251f8b | 2016-03-17 10:39:56 +0530 | [diff] [blame] | 504 | cdev = rndis->port.func.config->cdev; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 505 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ |
| 506 | // spin_lock(&dev->lock); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 507 | status = rndis_msg_parser(rndis->params, (u8 *) req->buf); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 508 | if (status < 0) |
Truls Bengtsson | 967baed | 2013-03-20 14:02:25 +0100 | [diff] [blame] | 509 | pr_err("RNDIS command error %d, %d/%d\n", |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 510 | status, req->actual, req->length); |
Badhri Jagan Sridharan | b7a343a | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 511 | |
| 512 | buf = (rndis_init_msg_type *)req->buf; |
| 513 | |
| 514 | if (buf->MessageType == RNDIS_MSG_INIT) { |
| 515 | if (buf->MaxTransferSize > 2048) |
| 516 | rndis->port.multi_pkt_xfer = 1; |
| 517 | else |
| 518 | rndis->port.multi_pkt_xfer = 0; |
| 519 | DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n", |
| 520 | __func__, buf->MaxTransferSize, |
| 521 | rndis->port.multi_pkt_xfer ? "enabled" : |
| 522 | "disabled"); |
xerox_lin | f053900 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 523 | if (rndis_dl_max_pkt_per_xfer <= 1) |
Badhri Jagan Sridharan | ccf7707 | 2014-09-18 10:48:48 -0700 | [diff] [blame] | 524 | rndis->port.multi_pkt_xfer = 0; |
Badhri Jagan Sridharan | b7a343a | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 525 | } |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 526 | // spin_unlock(&dev->lock); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 527 | spin_unlock(&_rndis_lock); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static int |
| 531 | rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 532 | { |
| 533 | struct f_rndis *rndis = func_to_rndis(f); |
| 534 | struct usb_composite_dev *cdev = f->config->cdev; |
| 535 | struct usb_request *req = cdev->req; |
| 536 | int value = -EOPNOTSUPP; |
| 537 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 538 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 539 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 540 | |
| 541 | /* composite driver infrastructure handles everything except |
| 542 | * CDC class messages; interface activation uses set_alt(). |
| 543 | */ |
| 544 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 545 | |
| 546 | /* RNDIS uses the CDC command encapsulation mechanism to implement |
| 547 | * an RPC scheme, with much getting/setting of attributes by OID. |
| 548 | */ |
| 549 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 550 | | USB_CDC_SEND_ENCAPSULATED_COMMAND: |
Felipe Balbi | 472b912 | 2011-05-13 16:53:48 +0300 | [diff] [blame] | 551 | if (w_value || w_index != rndis->ctrl_id) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 552 | goto invalid; |
| 553 | /* read the request; process it later */ |
| 554 | value = w_length; |
| 555 | req->complete = rndis_command_complete; |
| 556 | req->context = rndis; |
| 557 | /* later, rndis_response_available() sends a notification */ |
| 558 | break; |
| 559 | |
| 560 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 561 | | USB_CDC_GET_ENCAPSULATED_RESPONSE: |
| 562 | if (w_value || w_index != rndis->ctrl_id) |
| 563 | goto invalid; |
| 564 | else { |
| 565 | u8 *buf; |
| 566 | u32 n; |
| 567 | |
| 568 | /* return the result */ |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 569 | buf = rndis_get_next_response(rndis->params, &n); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 570 | if (buf) { |
| 571 | memcpy(req->buf, buf, n); |
| 572 | req->complete = rndis_response_complete; |
Lukasz Majewski | f135617 | 2012-02-10 09:54:51 +0100 | [diff] [blame] | 573 | req->context = rndis; |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 574 | rndis_free_response(rndis->params, buf); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 575 | value = n; |
| 576 | } |
| 577 | /* else stalls ... spec says to avoid that */ |
| 578 | } |
| 579 | break; |
| 580 | |
| 581 | default: |
| 582 | invalid: |
| 583 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 584 | ctrl->bRequestType, ctrl->bRequest, |
| 585 | w_value, w_index, w_length); |
| 586 | } |
| 587 | |
| 588 | /* respond with data transfer or status phase? */ |
| 589 | if (value >= 0) { |
| 590 | DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n", |
| 591 | ctrl->bRequestType, ctrl->bRequest, |
| 592 | w_value, w_index, w_length); |
David Brownell | 090b901 | 2009-03-20 01:08:20 -0700 | [diff] [blame] | 593 | req->zero = (value < w_length); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 594 | req->length = value; |
| 595 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 596 | if (value < 0) |
| 597 | ERROR(cdev, "rndis response on err %d\n", value); |
| 598 | } |
| 599 | |
| 600 | /* device either stalls (value < 0) or reports success */ |
| 601 | return value; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 606 | { |
| 607 | struct f_rndis *rndis = func_to_rndis(f); |
| 608 | struct usb_composite_dev *cdev = f->config->cdev; |
| 609 | |
| 610 | /* we know alt == 0 */ |
| 611 | |
| 612 | if (intf == rndis->ctrl_id) { |
Robert Baldyga | bbc474f | 2015-09-16 12:10:54 +0200 | [diff] [blame] | 613 | VDBG(cdev, "reset rndis control %d\n", intf); |
| 614 | usb_ep_disable(rndis->notify); |
| 615 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 616 | if (!rndis->notify->desc) { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 617 | VDBG(cdev, "init rndis ctrl %d\n", intf); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 618 | if (config_ep_by_speed(cdev->gadget, f, rndis->notify)) |
| 619 | goto fail; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 620 | } |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 621 | usb_ep_enable(rndis->notify); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 622 | |
| 623 | } else if (intf == rndis->data_id) { |
| 624 | struct net_device *net; |
| 625 | |
Robert Baldyga | bbc474f | 2015-09-16 12:10:54 +0200 | [diff] [blame] | 626 | if (rndis->port.in_ep->enabled) { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 627 | DBG(cdev, "reset rndis\n"); |
| 628 | gether_disconnect(&rndis->port); |
Maulik Mankad | 830d1b1 | 2009-05-29 18:34:40 +0530 | [diff] [blame] | 629 | } |
| 630 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 631 | if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) { |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 632 | DBG(cdev, "init rndis\n"); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 633 | if (config_ep_by_speed(cdev->gadget, f, |
| 634 | rndis->port.in_ep) || |
| 635 | config_ep_by_speed(cdev->gadget, f, |
| 636 | rndis->port.out_ep)) { |
| 637 | rndis->port.in_ep->desc = NULL; |
| 638 | rndis->port.out_ep->desc = NULL; |
| 639 | goto fail; |
| 640 | } |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Avoid ZLPs; they can be troublesome. */ |
| 644 | rndis->port.is_zlp_ok = false; |
| 645 | |
| 646 | /* RNDIS should be in the "RNDIS uninitialized" state, |
| 647 | * either never activated or after rndis_uninit(). |
| 648 | * |
| 649 | * We don't want data to flow here until a nonzero packet |
| 650 | * filter is set, at which point it enters "RNDIS data |
| 651 | * initialized" state ... but we do want the endpoints |
| 652 | * to be activated. It's a strange little state. |
| 653 | * |
| 654 | * REVISIT the RNDIS gadget code has done this wrong for a |
| 655 | * very long time. We need another call to the link layer |
| 656 | * code -- gether_updown(...bool) maybe -- to do it right. |
| 657 | */ |
| 658 | rndis->port.cdc_filter = 0; |
| 659 | |
| 660 | DBG(cdev, "RNDIS RX/TX early activation ... \n"); |
| 661 | net = gether_connect(&rndis->port); |
| 662 | if (IS_ERR(net)) |
| 663 | return PTR_ERR(net); |
| 664 | |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 665 | rndis_set_param_dev(rndis->params, net, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 666 | &rndis->port.cdc_filter); |
| 667 | } else |
| 668 | goto fail; |
| 669 | |
| 670 | return 0; |
| 671 | fail: |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | |
| 675 | static void rndis_disable(struct usb_function *f) |
| 676 | { |
| 677 | struct f_rndis *rndis = func_to_rndis(f); |
| 678 | struct usb_composite_dev *cdev = f->config->cdev; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 679 | unsigned long flags; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 680 | |
Robert Baldyga | bbc474f | 2015-09-16 12:10:54 +0200 | [diff] [blame] | 681 | if (!rndis->notify->enabled) |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 682 | return; |
| 683 | |
| 684 | DBG(cdev, "rndis deactivated\n"); |
| 685 | |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 686 | spin_lock_irqsave(&_rndis_lock, flags); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 687 | rndis_uninit(rndis->params); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 688 | spin_unlock_irqrestore(&_rndis_lock, flags); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 689 | gether_disconnect(&rndis->port); |
| 690 | |
| 691 | usb_ep_disable(rndis->notify); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /*-------------------------------------------------------------------------*/ |
| 695 | |
| 696 | /* |
| 697 | * This isn't quite the same mechanism as CDC Ethernet, since the |
| 698 | * notification scheme passes less data, but the same set of link |
| 699 | * states must be tested. A key difference is that altsettings are |
| 700 | * not used to tell whether the link should send packets or not. |
| 701 | */ |
| 702 | |
| 703 | static void rndis_open(struct gether *geth) |
| 704 | { |
| 705 | struct f_rndis *rndis = func_to_rndis(&geth->func); |
| 706 | struct usb_composite_dev *cdev = geth->func.config->cdev; |
| 707 | |
| 708 | DBG(cdev, "%s\n", __func__); |
| 709 | |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 710 | rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 711 | bitrate(cdev->gadget) / 100); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 712 | rndis_signal_connect(rndis->params); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void rndis_close(struct gether *geth) |
| 716 | { |
| 717 | struct f_rndis *rndis = func_to_rndis(&geth->func); |
| 718 | |
| 719 | DBG(geth->func.config->cdev, "%s\n", __func__); |
| 720 | |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 721 | rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0); |
| 722 | rndis_signal_disconnect(rndis->params); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /*-------------------------------------------------------------------------*/ |
| 726 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 727 | /* Some controllers can't support RNDIS ... */ |
| 728 | static inline bool can_support_rndis(struct usb_configuration *c) |
| 729 | { |
| 730 | /* everything else is *presumably* fine */ |
| 731 | return true; |
| 732 | } |
| 733 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 734 | /* ethernet function driver setup/binding */ |
| 735 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 736 | static int |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 737 | rndis_bind(struct usb_configuration *c, struct usb_function *f) |
| 738 | { |
| 739 | struct usb_composite_dev *cdev = c->cdev; |
| 740 | struct f_rndis *rndis = func_to_rndis(f); |
Andrzej Pietrasiewicz | 4e75e72 | 2013-05-28 09:16:00 +0200 | [diff] [blame] | 741 | struct usb_string *us; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 742 | int status; |
| 743 | struct usb_ep *ep; |
| 744 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 745 | struct f_rndis_opts *rndis_opts; |
| 746 | |
| 747 | if (!can_support_rndis(c)) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst); |
| 751 | |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 752 | if (cdev->use_os_string) { |
| 753 | f->os_desc_table = kzalloc(sizeof(*f->os_desc_table), |
| 754 | GFP_KERNEL); |
| 755 | if (!f->os_desc_table) |
Dan Carpenter | 4683ae8 | 2014-05-21 15:26:55 +0300 | [diff] [blame] | 756 | return -ENOMEM; |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 757 | f->os_desc_n = 1; |
| 758 | f->os_desc_table[0].os_desc = &rndis_opts->rndis_os_desc; |
| 759 | } |
| 760 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 761 | /* |
| 762 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() |
| 763 | * configurations are bound in sequence with list_for_each_entry, |
| 764 | * in each configuration its functions are bound in sequence |
| 765 | * with list_for_each_entry, so we assume no race condition |
| 766 | * with regard to rndis_opts->bound access |
| 767 | */ |
| 768 | if (!rndis_opts->bound) { |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 769 | mutex_lock(&rndis_opts->lock); |
Ajay Agarwal | a3cd980 | 2018-05-15 14:47:49 +0530 | [diff] [blame] | 770 | rndis_opts->net = gether_setup_name_default("rndis"); |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 771 | if (IS_ERR(rndis_opts->net)) { |
| 772 | status = PTR_ERR(rndis_opts->net); |
| 773 | mutex_unlock(&rndis_opts->lock); |
| 774 | goto error; |
| 775 | } |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 776 | gether_set_gadget(rndis_opts->net, cdev->gadget); |
| 777 | status = gether_register_netdev(rndis_opts->net); |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 778 | mutex_unlock(&rndis_opts->lock); |
| 779 | if (status) { |
| 780 | free_netdev(rndis_opts->net); |
| 781 | goto error; |
| 782 | } |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 783 | rndis_opts->bound = true; |
| 784 | } |
Andrzej Pietrasiewicz | 31f89db | 2013-12-03 15:15:28 +0100 | [diff] [blame] | 785 | |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 786 | gether_get_host_addr_u8(rndis_opts->net, rndis->ethaddr); |
| 787 | rndis->port.ioport = netdev_priv(rndis_opts->net); |
| 788 | |
Andrzej Pietrasiewicz | 4e75e72 | 2013-05-28 09:16:00 +0200 | [diff] [blame] | 789 | us = usb_gstrings_attach(cdev, rndis_strings, |
| 790 | ARRAY_SIZE(rndis_string_defs)); |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 791 | if (IS_ERR(us)) { |
| 792 | status = PTR_ERR(us); |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 793 | goto netdev_cleanup; |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 794 | } |
Andrzej Pietrasiewicz | 4e75e72 | 2013-05-28 09:16:00 +0200 | [diff] [blame] | 795 | rndis_control_intf.iInterface = us[0].id; |
| 796 | rndis_data_intf.iInterface = us[1].id; |
| 797 | rndis_iad_descriptor.iFunction = us[2].id; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 798 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 799 | /* allocate instance-specific interface IDs */ |
| 800 | status = usb_interface_id(c, f); |
| 801 | if (status < 0) |
| 802 | goto fail; |
| 803 | rndis->ctrl_id = status; |
Michal Nazarewicz | b97503f | 2009-10-28 16:57:30 +0100 | [diff] [blame] | 804 | rndis_iad_descriptor.bFirstInterface = status; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 805 | |
| 806 | rndis_control_intf.bInterfaceNumber = status; |
| 807 | rndis_union_desc.bMasterInterface0 = status; |
| 808 | |
Andrzej Pietrasiewicz | 4546527 | 2014-07-01 15:47:47 +0200 | [diff] [blame] | 809 | if (cdev->use_os_string) |
| 810 | f->os_desc_table[0].if_id = |
| 811 | rndis_iad_descriptor.bFirstInterface; |
| 812 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 813 | status = usb_interface_id(c, f); |
| 814 | if (status < 0) |
| 815 | goto fail; |
| 816 | rndis->data_id = status; |
| 817 | |
| 818 | rndis_data_intf.bInterfaceNumber = status; |
| 819 | rndis_union_desc.bSlaveInterface0 = status; |
| 820 | |
| 821 | status = -ENODEV; |
| 822 | |
| 823 | /* allocate instance-specific endpoints */ |
| 824 | ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc); |
| 825 | if (!ep) |
| 826 | goto fail; |
| 827 | rndis->port.in_ep = ep; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 828 | |
| 829 | ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc); |
| 830 | if (!ep) |
| 831 | goto fail; |
| 832 | rndis->port.out_ep = ep; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 833 | |
| 834 | /* NOTE: a status/notification endpoint is, strictly speaking, |
| 835 | * optional. We don't treat it that way though! It's simpler, |
| 836 | * and some newer profiles don't treat it as optional. |
| 837 | */ |
| 838 | ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc); |
| 839 | if (!ep) |
| 840 | goto fail; |
| 841 | rndis->notify = ep; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 842 | |
| 843 | status = -ENOMEM; |
| 844 | |
| 845 | /* allocate notification request and buffer */ |
| 846 | rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 847 | if (!rndis->notify_req) |
| 848 | goto fail; |
ChandanaKishori Chiluveru | 2b5ed57 | 2015-08-05 15:30:40 +0530 | [diff] [blame] | 849 | rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT + |
| 850 | cdev->gadget->extra_buf_alloc, GFP_KERNEL); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 851 | if (!rndis->notify_req->buf) |
| 852 | goto fail; |
| 853 | rndis->notify_req->length = STATUS_BYTECOUNT; |
| 854 | rndis->notify_req->context = rndis; |
| 855 | rndis->notify_req->complete = rndis_response_complete; |
| 856 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 857 | /* support all relevant hardware speeds... we expect that when |
| 858 | * hardware is dual speed, all bulk-capable endpoints work at |
| 859 | * both speeds |
| 860 | */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 861 | hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress; |
| 862 | hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress; |
| 863 | hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 864 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 865 | ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress; |
| 866 | ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress; |
| 867 | ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 868 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 869 | status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function, |
John Youn | eaef50c | 2016-02-05 17:06:07 -0800 | [diff] [blame] | 870 | eth_ss_function, NULL); |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 871 | if (status) |
| 872 | goto fail; |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 873 | |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 874 | rndis->port.open = rndis_open; |
| 875 | rndis->port.close = rndis_close; |
| 876 | |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 877 | rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0); |
| 878 | rndis_set_host_mac(rndis->params, rndis->ethaddr); |
Amit Pundir | d29237a | 2015-12-21 14:03:15 +0530 | [diff] [blame] | 879 | rndis_set_max_pkt_xfer(rndis->params, rndis_ul_max_pkt_per_xfer); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 880 | |
Benoit Goby | 1fbfeff | 2012-05-10 10:08:04 +0200 | [diff] [blame] | 881 | if (rndis->manufacturer && rndis->vendorID && |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 882 | rndis_set_param_vendor(rndis->params, rndis->vendorID, |
Pavitrakumar Managutte | 9b17635 | 2014-10-22 19:33:22 +0530 | [diff] [blame] | 883 | rndis->manufacturer)) { |
| 884 | status = -EINVAL; |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 885 | goto fail_free_descs; |
Pavitrakumar Managutte | 9b17635 | 2014-10-22 19:33:22 +0530 | [diff] [blame] | 886 | } |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 887 | |
| 888 | /* NOTE: all that is done without knowing or caring about |
| 889 | * the network link ... which is unavailable to this code |
| 890 | * until we're activated via set_alt(). |
| 891 | */ |
| 892 | |
| 893 | DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n", |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 894 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 895 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 896 | rndis->port.in_ep->name, rndis->port.out_ep->name, |
| 897 | rndis->notify->name); |
| 898 | return 0; |
| 899 | |
Pavitrakumar Managutte | d12a872 | 2014-10-22 19:24:58 +0530 | [diff] [blame] | 900 | fail_free_descs: |
| 901 | usb_free_all_descriptors(f); |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 902 | fail: |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 903 | kfree(f->os_desc_table); |
| 904 | f->os_desc_n = 0; |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 905 | |
| 906 | if (rndis->notify_req) { |
| 907 | kfree(rndis->notify_req->buf); |
| 908 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
| 909 | } |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 910 | netdev_cleanup: |
| 911 | gether_cleanup(rndis->port.ioport); |
| 912 | error: |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 913 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 914 | |
| 915 | return status; |
| 916 | } |
| 917 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 918 | void rndis_borrow_net(struct usb_function_instance *f, struct net_device *net) |
| 919 | { |
| 920 | struct f_rndis_opts *opts; |
| 921 | |
| 922 | opts = container_of(f, struct f_rndis_opts, func_inst); |
| 923 | if (opts->bound) |
| 924 | gether_cleanup(netdev_priv(opts->net)); |
| 925 | else |
| 926 | free_netdev(opts->net); |
| 927 | opts->borrowed_net = opts->bound = true; |
| 928 | opts->net = net; |
| 929 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 930 | EXPORT_SYMBOL_GPL(rndis_borrow_net); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 931 | |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 932 | static inline struct f_rndis_opts *to_f_rndis_opts(struct config_item *item) |
| 933 | { |
| 934 | return container_of(to_config_group(item), struct f_rndis_opts, |
| 935 | func_inst.group); |
| 936 | } |
| 937 | |
| 938 | /* f_rndis_item_ops */ |
| 939 | USB_ETHERNET_CONFIGFS_ITEM(rndis); |
| 940 | |
| 941 | /* f_rndis_opts_dev_addr */ |
| 942 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(rndis); |
| 943 | |
| 944 | /* f_rndis_opts_host_addr */ |
| 945 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(rndis); |
| 946 | |
| 947 | /* f_rndis_opts_qmult */ |
| 948 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(rndis); |
| 949 | |
| 950 | /* f_rndis_opts_ifname */ |
| 951 | USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(rndis); |
| 952 | |
| 953 | static struct configfs_attribute *rndis_attrs[] = { |
Christoph Hellwig | f9a63da | 2015-10-03 15:32:42 +0200 | [diff] [blame] | 954 | &rndis_opts_attr_dev_addr, |
| 955 | &rndis_opts_attr_host_addr, |
| 956 | &rndis_opts_attr_qmult, |
| 957 | &rndis_opts_attr_ifname, |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 958 | NULL, |
| 959 | }; |
| 960 | |
| 961 | static struct config_item_type rndis_func_type = { |
| 962 | .ct_item_ops = &rndis_item_ops, |
| 963 | .ct_attrs = rndis_attrs, |
| 964 | .ct_owner = THIS_MODULE, |
| 965 | }; |
| 966 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 967 | static void rndis_free_inst(struct usb_function_instance *f) |
| 968 | { |
| 969 | struct f_rndis_opts *opts; |
| 970 | |
| 971 | opts = container_of(f, struct f_rndis_opts, func_inst); |
| 972 | if (!opts->borrowed_net) { |
| 973 | if (opts->bound) |
| 974 | gether_cleanup(netdev_priv(opts->net)); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 975 | } |
Andrzej Pietrasiewicz | a747b09 | 2014-05-08 14:06:27 +0200 | [diff] [blame] | 976 | |
Andrew Gabbasov | 3c57f9d | 2017-09-30 08:54:52 -0700 | [diff] [blame] | 977 | kfree(opts->rndis_interf_group); /* single VLA chunk */ |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 978 | kfree(opts); |
| 979 | } |
| 980 | |
| 981 | static struct usb_function_instance *rndis_alloc_inst(void) |
| 982 | { |
| 983 | struct f_rndis_opts *opts; |
Andrzej Pietrasiewicz | a747b09 | 2014-05-08 14:06:27 +0200 | [diff] [blame] | 984 | struct usb_os_desc *descs[1]; |
Andrzej Pietrasiewicz | 14574b5 | 2014-06-18 14:24:49 +0200 | [diff] [blame] | 985 | char *names[1]; |
Andrew Gabbasov | 3c57f9d | 2017-09-30 08:54:52 -0700 | [diff] [blame] | 986 | struct config_group *rndis_interf_group; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 987 | |
| 988 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 989 | if (!opts) |
| 990 | return ERR_PTR(-ENOMEM); |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 991 | opts->rndis_os_desc.ext_compat_id = opts->rndis_ext_compat_id; |
| 992 | |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 993 | mutex_init(&opts->lock); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 994 | spin_lock_init(&_rndis_lock); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 995 | opts->func_inst.free_func_inst = rndis_free_inst; |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 996 | INIT_LIST_HEAD(&opts->rndis_os_desc.ext_prop); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 997 | |
Andrzej Pietrasiewicz | a747b09 | 2014-05-08 14:06:27 +0200 | [diff] [blame] | 998 | descs[0] = &opts->rndis_os_desc; |
Andrzej Pietrasiewicz | 14574b5 | 2014-06-18 14:24:49 +0200 | [diff] [blame] | 999 | names[0] = "rndis"; |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1000 | config_group_init_type_name(&opts->func_inst.group, "", |
| 1001 | &rndis_func_type); |
Andrew Gabbasov | 3c57f9d | 2017-09-30 08:54:52 -0700 | [diff] [blame] | 1002 | rndis_interf_group = |
| 1003 | usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs, |
| 1004 | names, THIS_MODULE); |
| 1005 | if (IS_ERR(rndis_interf_group)) { |
| 1006 | rndis_free_inst(&opts->func_inst); |
| 1007 | return ERR_CAST(rndis_interf_group); |
| 1008 | } |
| 1009 | opts->rndis_interf_group = rndis_interf_group; |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1010 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1011 | return &opts->func_inst; |
| 1012 | } |
| 1013 | |
| 1014 | static void rndis_free(struct usb_function *f) |
| 1015 | { |
| 1016 | struct f_rndis *rndis; |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1017 | struct f_rndis_opts *opts; |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 1018 | unsigned long flags; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1019 | |
| 1020 | rndis = func_to_rndis(f); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 1021 | rndis_deregister(rndis->params); |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1022 | opts = container_of(f->fi, struct f_rndis_opts, func_inst); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 1023 | spin_lock_irqsave(&_rndis_lock, flags); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1024 | kfree(rndis); |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 1025 | __rndis = NULL; |
| 1026 | spin_unlock_irqrestore(&_rndis_lock, flags); |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1027 | mutex_lock(&opts->lock); |
| 1028 | opts->refcnt--; |
| 1029 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static void rndis_unbind(struct usb_configuration *c, struct usb_function *f) |
| 1033 | { |
| 1034 | struct f_rndis *rndis = func_to_rndis(f); |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 1035 | struct f_rndis_opts *opts = container_of(f->fi, struct f_rndis_opts, |
| 1036 | func_inst); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1037 | |
Andrzej Pietrasiewicz | de7a8d2 | 2014-05-08 14:06:24 +0200 | [diff] [blame] | 1038 | kfree(f->os_desc_table); |
| 1039 | f->os_desc_n = 0; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1040 | usb_free_all_descriptors(f); |
| 1041 | |
| 1042 | kfree(rndis->notify_req->buf); |
| 1043 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
Ajay Agarwal | 200f484 | 2018-04-26 16:44:27 +0530 | [diff] [blame] | 1044 | gether_cleanup(rndis->port.ioport); |
| 1045 | opts->bound = false; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | static struct usb_function *rndis_alloc(struct usb_function_instance *fi) |
| 1049 | { |
| 1050 | struct f_rndis *rndis; |
| 1051 | struct f_rndis_opts *opts; |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 1052 | struct rndis_params *params; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1053 | |
| 1054 | /* allocate and initialize one new instance */ |
| 1055 | rndis = kzalloc(sizeof(*rndis), GFP_KERNEL); |
Andrzej Pietrasiewicz | 76da66d | 2013-05-28 09:15:59 +0200 | [diff] [blame] | 1056 | if (!rndis) |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1057 | return ERR_PTR(-ENOMEM); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1058 | |
Vijayavardhan Vennapusa | 341fa07 | 2016-03-09 13:57:02 +0530 | [diff] [blame] | 1059 | __rndis = rndis; |
| 1060 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1061 | opts = container_of(fi, struct f_rndis_opts, func_inst); |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1062 | mutex_lock(&opts->lock); |
| 1063 | opts->refcnt++; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1064 | |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1065 | rndis->vendorID = opts->vendor_id; |
| 1066 | rndis->manufacturer = opts->manufacturer; |
| 1067 | |
Andrzej Pietrasiewicz | b3df2fa | 2013-05-28 09:16:01 +0200 | [diff] [blame] | 1068 | mutex_unlock(&opts->lock); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1069 | /* RNDIS activates when the host changes this filter */ |
| 1070 | rndis->port.cdc_filter = 0; |
| 1071 | |
| 1072 | /* RNDIS has special (and complex) framing */ |
| 1073 | rndis->port.header_len = sizeof(struct rndis_packet_msg_type); |
| 1074 | rndis->port.wrap = rndis_add_header; |
| 1075 | rndis->port.unwrap = rndis_rm_hdr; |
xerox_lin | 72ffa18 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 1076 | rndis->port.ul_max_pkts_per_xfer = rndis_ul_max_pkt_per_xfer; |
xerox_lin | f053900 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 1077 | rndis->port.dl_max_pkts_per_xfer = rndis_dl_max_pkt_per_xfer; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1078 | |
| 1079 | rndis->port.func.name = "rndis"; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1080 | /* descriptors are per-instance copies */ |
| 1081 | rndis->port.func.bind = rndis_bind; |
| 1082 | rndis->port.func.unbind = rndis_unbind; |
| 1083 | rndis->port.func.set_alt = rndis_set_alt; |
| 1084 | rndis->port.func.setup = rndis_setup; |
| 1085 | rndis->port.func.disable = rndis_disable; |
| 1086 | rndis->port.func.free_func = rndis_free; |
| 1087 | |
Hemant Kumar | 8b06029 | 2016-12-06 15:58:20 -0800 | [diff] [blame] | 1088 | params = rndis_register(rndis_response_available, rndis, NULL); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 1089 | if (IS_ERR(params)) { |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1090 | kfree(rndis); |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 1091 | return ERR_CAST(params); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1092 | } |
Andrzej Pietrasiewicz | 83210e5 | 2015-03-20 08:18:47 +0100 | [diff] [blame] | 1093 | rndis->params = params; |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1094 | |
| 1095 | return &rndis->port.func; |
| 1096 | } |
| 1097 | |
Andrzej Pietrasiewicz | d6d2292 | 2015-02-06 13:43:30 +0100 | [diff] [blame] | 1098 | DECLARE_USB_FUNCTION_INIT(rndis, rndis_alloc_inst, rndis_alloc); |
Andrzej Pietrasiewicz | f466c63 | 2013-05-28 09:15:57 +0200 | [diff] [blame] | 1099 | MODULE_LICENSE("GPL"); |
| 1100 | MODULE_AUTHOR("David Brownell"); |