Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * f_qc_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 |
| 7 | * Copyright (C) 2009 Samsung Electronics |
| 8 | * Author: Michal Nazarewicz (mina86@mina86.com) |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 9 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* #define VERBOSE_DEBUG */ |
| 25 | |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/etherdevice.h> |
| 30 | |
| 31 | #include <linux/atomic.h> |
| 32 | |
| 33 | #include "u_ether.h" |
| 34 | #include "u_qc_ether.h" |
| 35 | #include "rndis.h" |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * This function is an RNDIS Ethernet port -- a Microsoft protocol that's |
| 40 | * been promoted instead of the standard CDC Ethernet. The published RNDIS |
| 41 | * spec is ambiguous, incomplete, and needlessly complex. Variants such as |
| 42 | * ActiveSync have even worse status in terms of specification. |
| 43 | * |
| 44 | * In short: it's a protocol controlled by (and for) Microsoft, not for an |
| 45 | * Open ecosystem or markets. Linux supports it *only* because Microsoft |
| 46 | * doesn't support the CDC Ethernet standard. |
| 47 | * |
| 48 | * The RNDIS data transfer model is complex, with multiple Ethernet packets |
| 49 | * per USB message, and out of band data. The control model is built around |
| 50 | * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM |
| 51 | * (modem, not Ethernet) veneer, with those ACM descriptors being entirely |
| 52 | * useless (they're ignored). RNDIS expects to be the only function in its |
| 53 | * configuration, so it's no real help if you need composite devices; and |
| 54 | * it expects to be the first configuration too. |
| 55 | * |
| 56 | * There is a single technical advantage of RNDIS over CDC Ethernet, if you |
| 57 | * discount the fluff that its RPC can be made to deliver: it doesn't need |
| 58 | * a NOP altsetting for the data interface. That lets it work on some of the |
| 59 | * "so smart it's stupid" hardware which takes over configuration changes |
| 60 | * from the software, and adds restrictions like "no altsettings". |
| 61 | * |
| 62 | * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and |
| 63 | * have all sorts of contrary-to-specification oddities that can prevent |
| 64 | * them from working sanely. Since bugfixes (or accurate specs, letting |
| 65 | * Linux work around those bugs) are unlikely to ever come from MSFT, you |
| 66 | * may want to avoid using RNDIS on purely operational grounds. |
| 67 | * |
| 68 | * Omissions from the RNDIS 1.0 specification include: |
| 69 | * |
| 70 | * - Power management ... references data that's scattered around lots |
| 71 | * of other documentation, which is incorrect/incomplete there too. |
| 72 | * |
| 73 | * - There are various undocumented protocol requirements, like the need |
| 74 | * to send garbage in some control-OUT messages. |
| 75 | * |
| 76 | * - MS-Windows drivers sometimes emit undocumented requests. |
| 77 | * |
| 78 | * This function is based on RNDIS link function driver and |
| 79 | * contains MSM specific implementation. |
| 80 | */ |
| 81 | |
| 82 | struct f_rndis_qc { |
| 83 | struct qc_gether port; |
| 84 | u8 ctrl_id, data_id; |
| 85 | u8 ethaddr[ETH_ALEN]; |
| 86 | u32 vendorID; |
| 87 | u8 max_pkt_per_xfer; |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 88 | u32 max_pkt_size; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 89 | const char *manufacturer; |
| 90 | int config; |
| 91 | atomic_t ioctl_excl; |
| 92 | atomic_t open_excl; |
| 93 | |
| 94 | struct usb_ep *notify; |
| 95 | struct usb_request *notify_req; |
| 96 | atomic_t notify_count; |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 97 | struct data_port bam_port; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static inline struct f_rndis_qc *func_to_rndis_qc(struct usb_function *f) |
| 101 | { |
| 102 | return container_of(f, struct f_rndis_qc, port.func); |
| 103 | } |
| 104 | |
| 105 | /* peak (theoretical) bulk transfer rate in bits-per-second */ |
| 106 | static unsigned int rndis_qc_bitrate(struct usb_gadget *g) |
| 107 | { |
| 108 | if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER) |
| 109 | return 13 * 1024 * 8 * 1000 * 8; |
| 110 | else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 111 | return 13 * 512 * 8 * 1000 * 8; |
| 112 | else |
| 113 | return 19 * 64 * 1 * 1000 * 8; |
| 114 | } |
| 115 | |
| 116 | /*-------------------------------------------------------------------------*/ |
| 117 | |
| 118 | #define RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ |
| 119 | #define RNDIS_QC_STATUS_BYTECOUNT 8 /* 8 bytes data */ |
| 120 | |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 121 | /* currently only one rndis instance is supported - port |
| 122 | * index 0. |
| 123 | */ |
| 124 | #define RNDIS_QC_NO_PORTS 1 |
| 125 | #define RNDIS_QC_ACTIVE_PORT 0 |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 126 | |
| 127 | /* default max packets per tarnsfer value */ |
| 128 | #define DEFAULT_MAX_PKT_PER_XFER 15 |
| 129 | |
| 130 | |
| 131 | #define RNDIS_QC_IOCTL_MAGIC 'i' |
| 132 | #define RNDIS_QC_GET_MAX_PKT_PER_XFER _IOR(RNDIS_QC_IOCTL_MAGIC, 1, u8) |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 133 | #define RNDIS_QC_GET_MAX_PKT_SIZE _IOR(RNDIS_QC_IOCTL_MAGIC, 2, u32) |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 134 | |
| 135 | |
| 136 | /* interface descriptor: */ |
| 137 | |
| 138 | static struct usb_interface_descriptor rndis_qc_control_intf = { |
| 139 | .bLength = sizeof rndis_qc_control_intf, |
| 140 | .bDescriptorType = USB_DT_INTERFACE, |
| 141 | |
| 142 | /* .bInterfaceNumber = DYNAMIC */ |
| 143 | /* status endpoint is optional; this could be patched later */ |
| 144 | .bNumEndpoints = 1, |
| 145 | .bInterfaceClass = USB_CLASS_COMM, |
| 146 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 147 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, |
| 148 | /* .iInterface = DYNAMIC */ |
| 149 | }; |
| 150 | |
| 151 | static struct usb_cdc_header_desc rndis_qc_header_desc = { |
| 152 | .bLength = sizeof rndis_qc_header_desc, |
| 153 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 154 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 155 | |
| 156 | .bcdCDC = cpu_to_le16(0x0110), |
| 157 | }; |
| 158 | |
| 159 | static struct usb_cdc_call_mgmt_descriptor rndis_qc_call_mgmt_descriptor = { |
| 160 | .bLength = sizeof rndis_qc_call_mgmt_descriptor, |
| 161 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 162 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 163 | |
| 164 | .bmCapabilities = 0x00, |
| 165 | .bDataInterface = 0x01, |
| 166 | }; |
| 167 | |
| 168 | static struct usb_cdc_acm_descriptor rndis_qc_acm_descriptor = { |
| 169 | .bLength = sizeof rndis_qc_acm_descriptor, |
| 170 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 171 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 172 | |
| 173 | .bmCapabilities = 0x00, |
| 174 | }; |
| 175 | |
| 176 | static struct usb_cdc_union_desc rndis_qc_union_desc = { |
| 177 | .bLength = sizeof(rndis_qc_union_desc), |
| 178 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 179 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 180 | /* .bMasterInterface0 = DYNAMIC */ |
| 181 | /* .bSlaveInterface0 = DYNAMIC */ |
| 182 | }; |
| 183 | |
| 184 | /* the data interface has two bulk endpoints */ |
| 185 | |
| 186 | static struct usb_interface_descriptor rndis_qc_data_intf = { |
| 187 | .bLength = sizeof rndis_qc_data_intf, |
| 188 | .bDescriptorType = USB_DT_INTERFACE, |
| 189 | |
| 190 | /* .bInterfaceNumber = DYNAMIC */ |
| 191 | .bNumEndpoints = 2, |
| 192 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 193 | .bInterfaceSubClass = 0, |
| 194 | .bInterfaceProtocol = 0, |
| 195 | /* .iInterface = DYNAMIC */ |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | static struct usb_interface_assoc_descriptor |
| 200 | rndis_qc_iad_descriptor = { |
| 201 | .bLength = sizeof rndis_qc_iad_descriptor, |
| 202 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, |
| 203 | .bFirstInterface = 0, /* XXX, hardcoded */ |
| 204 | .bInterfaceCount = 2, /* control + data */ |
| 205 | .bFunctionClass = USB_CLASS_COMM, |
| 206 | .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET, |
| 207 | .bFunctionProtocol = USB_CDC_PROTO_NONE, |
| 208 | /* .iFunction = DYNAMIC */ |
| 209 | }; |
| 210 | |
| 211 | /* full speed support: */ |
| 212 | |
| 213 | static struct usb_endpoint_descriptor rndis_qc_fs_notify_desc = { |
| 214 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 215 | .bDescriptorType = USB_DT_ENDPOINT, |
| 216 | |
| 217 | .bEndpointAddress = USB_DIR_IN, |
| 218 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 219 | .wMaxPacketSize = cpu_to_le16(RNDIS_QC_STATUS_BYTECOUNT), |
| 220 | .bInterval = 1 << RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC, |
| 221 | }; |
| 222 | |
| 223 | static struct usb_endpoint_descriptor rndis_qc_fs_in_desc = { |
| 224 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 225 | .bDescriptorType = USB_DT_ENDPOINT, |
| 226 | |
| 227 | .bEndpointAddress = USB_DIR_IN, |
| 228 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 229 | }; |
| 230 | |
| 231 | static struct usb_endpoint_descriptor rndis_qc_fs_out_desc = { |
| 232 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 233 | .bDescriptorType = USB_DT_ENDPOINT, |
| 234 | |
| 235 | .bEndpointAddress = USB_DIR_OUT, |
| 236 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 237 | }; |
| 238 | |
| 239 | static struct usb_descriptor_header *eth_qc_fs_function[] = { |
| 240 | (struct usb_descriptor_header *) &rndis_qc_iad_descriptor, |
| 241 | /* control interface matches ACM, not Ethernet */ |
| 242 | (struct usb_descriptor_header *) &rndis_qc_control_intf, |
| 243 | (struct usb_descriptor_header *) &rndis_qc_header_desc, |
| 244 | (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor, |
| 245 | (struct usb_descriptor_header *) &rndis_qc_acm_descriptor, |
| 246 | (struct usb_descriptor_header *) &rndis_qc_union_desc, |
| 247 | (struct usb_descriptor_header *) &rndis_qc_fs_notify_desc, |
| 248 | /* data interface has no altsetting */ |
| 249 | (struct usb_descriptor_header *) &rndis_qc_data_intf, |
| 250 | (struct usb_descriptor_header *) &rndis_qc_fs_in_desc, |
| 251 | (struct usb_descriptor_header *) &rndis_qc_fs_out_desc, |
| 252 | NULL, |
| 253 | }; |
| 254 | |
| 255 | /* high speed support: */ |
| 256 | |
| 257 | static struct usb_endpoint_descriptor rndis_qc_hs_notify_desc = { |
| 258 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 259 | .bDescriptorType = USB_DT_ENDPOINT, |
| 260 | |
| 261 | .bEndpointAddress = USB_DIR_IN, |
| 262 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 263 | .wMaxPacketSize = cpu_to_le16(RNDIS_QC_STATUS_BYTECOUNT), |
| 264 | .bInterval = RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC + 4, |
| 265 | }; |
| 266 | static struct usb_endpoint_descriptor rndis_qc_hs_in_desc = { |
| 267 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 268 | .bDescriptorType = USB_DT_ENDPOINT, |
| 269 | |
| 270 | .bEndpointAddress = USB_DIR_IN, |
| 271 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 272 | .wMaxPacketSize = cpu_to_le16(512), |
| 273 | }; |
| 274 | |
| 275 | static struct usb_endpoint_descriptor rndis_qc_hs_out_desc = { |
| 276 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 277 | .bDescriptorType = USB_DT_ENDPOINT, |
| 278 | |
| 279 | .bEndpointAddress = USB_DIR_OUT, |
| 280 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 281 | .wMaxPacketSize = cpu_to_le16(512), |
| 282 | }; |
| 283 | |
| 284 | static struct usb_descriptor_header *eth_qc_hs_function[] = { |
| 285 | (struct usb_descriptor_header *) &rndis_qc_iad_descriptor, |
| 286 | /* control interface matches ACM, not Ethernet */ |
| 287 | (struct usb_descriptor_header *) &rndis_qc_control_intf, |
| 288 | (struct usb_descriptor_header *) &rndis_qc_header_desc, |
| 289 | (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor, |
| 290 | (struct usb_descriptor_header *) &rndis_qc_acm_descriptor, |
| 291 | (struct usb_descriptor_header *) &rndis_qc_union_desc, |
| 292 | (struct usb_descriptor_header *) &rndis_qc_hs_notify_desc, |
| 293 | /* data interface has no altsetting */ |
| 294 | (struct usb_descriptor_header *) &rndis_qc_data_intf, |
| 295 | (struct usb_descriptor_header *) &rndis_qc_hs_in_desc, |
| 296 | (struct usb_descriptor_header *) &rndis_qc_hs_out_desc, |
| 297 | NULL, |
| 298 | }; |
| 299 | |
| 300 | /* super speed support: */ |
| 301 | |
| 302 | static struct usb_endpoint_descriptor rndis_qc_ss_notify_desc = { |
| 303 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 304 | .bDescriptorType = USB_DT_ENDPOINT, |
| 305 | |
| 306 | .bEndpointAddress = USB_DIR_IN, |
| 307 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 308 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
| 309 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, |
| 310 | }; |
| 311 | |
| 312 | static struct usb_ss_ep_comp_descriptor rndis_qc_ss_intr_comp_desc = { |
| 313 | .bLength = sizeof ss_intr_comp_desc, |
| 314 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 315 | |
| 316 | /* the following 3 values can be tweaked if necessary */ |
| 317 | /* .bMaxBurst = 0, */ |
| 318 | /* .bmAttributes = 0, */ |
| 319 | .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT), |
| 320 | }; |
| 321 | |
| 322 | static struct usb_endpoint_descriptor rndis_qc_ss_in_desc = { |
| 323 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 324 | .bDescriptorType = USB_DT_ENDPOINT, |
| 325 | |
| 326 | .bEndpointAddress = USB_DIR_IN, |
| 327 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 328 | .wMaxPacketSize = cpu_to_le16(1024), |
| 329 | }; |
| 330 | |
| 331 | static struct usb_endpoint_descriptor rndis_qc_ss_out_desc = { |
| 332 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 333 | .bDescriptorType = USB_DT_ENDPOINT, |
| 334 | |
| 335 | .bEndpointAddress = USB_DIR_OUT, |
| 336 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 337 | .wMaxPacketSize = cpu_to_le16(1024), |
| 338 | }; |
| 339 | |
| 340 | static struct usb_ss_ep_comp_descriptor rndis_qc_ss_bulk_comp_desc = { |
| 341 | .bLength = sizeof ss_bulk_comp_desc, |
| 342 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 343 | |
| 344 | /* the following 2 values can be tweaked if necessary */ |
| 345 | /* .bMaxBurst = 0, */ |
| 346 | /* .bmAttributes = 0, */ |
| 347 | }; |
| 348 | |
| 349 | static struct usb_descriptor_header *eth_qc_ss_function[] = { |
| 350 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
| 351 | |
| 352 | /* control interface matches ACM, not Ethernet */ |
| 353 | (struct usb_descriptor_header *) &rndis_qc_control_intf, |
| 354 | (struct usb_descriptor_header *) &rndis_qc_header_desc, |
| 355 | (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor, |
| 356 | (struct usb_descriptor_header *) &rndis_qc_acm_descriptor, |
| 357 | (struct usb_descriptor_header *) &rndis_qc_union_desc, |
| 358 | (struct usb_descriptor_header *) &rndis_qc_ss_notify_desc, |
| 359 | (struct usb_descriptor_header *) &rndis_qc_ss_intr_comp_desc, |
| 360 | |
| 361 | /* data interface has no altsetting */ |
| 362 | (struct usb_descriptor_header *) &rndis_qc_data_intf, |
| 363 | (struct usb_descriptor_header *) &rndis_qc_ss_in_desc, |
| 364 | (struct usb_descriptor_header *) &rndis_qc_ss_bulk_comp_desc, |
| 365 | (struct usb_descriptor_header *) &rndis_qc_ss_out_desc, |
| 366 | (struct usb_descriptor_header *) &rndis_qc_ss_bulk_comp_desc, |
| 367 | NULL, |
| 368 | }; |
| 369 | |
| 370 | /* string descriptors: */ |
| 371 | |
| 372 | static struct usb_string rndis_qc_string_defs[] = { |
| 373 | [0].s = "RNDIS Communications Control", |
| 374 | [1].s = "RNDIS Ethernet Data", |
| 375 | [2].s = "RNDIS", |
| 376 | { } /* end of list */ |
| 377 | }; |
| 378 | |
| 379 | static struct usb_gadget_strings rndis_qc_string_table = { |
| 380 | .language = 0x0409, /* en-us */ |
| 381 | .strings = rndis_qc_string_defs, |
| 382 | }; |
| 383 | |
| 384 | static struct usb_gadget_strings *rndis_qc_strings[] = { |
| 385 | &rndis_qc_string_table, |
| 386 | NULL, |
| 387 | }; |
| 388 | |
| 389 | struct f_rndis_qc *_rndis_qc; |
| 390 | |
| 391 | static inline int rndis_qc_lock(atomic_t *excl) |
| 392 | { |
| 393 | if (atomic_inc_return(excl) == 1) { |
| 394 | return 0; |
| 395 | } else { |
| 396 | atomic_dec(excl); |
| 397 | return -EBUSY; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static inline void rndis_qc_unlock(atomic_t *excl) |
| 402 | { |
| 403 | atomic_dec(excl); |
| 404 | } |
| 405 | |
| 406 | /* MSM bam support */ |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 407 | |
| 408 | static int rndis_qc_bam_setup(void) |
| 409 | { |
| 410 | int ret; |
| 411 | |
| 412 | ret = bam_data_setup(RNDIS_QC_NO_PORTS); |
| 413 | if (ret) { |
| 414 | pr_err("bam_data_setup failed err: %d\n", ret); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int rndis_qc_bam_connect(struct f_rndis_qc *dev) |
| 422 | { |
| 423 | int ret; |
| 424 | |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 425 | dev->bam_port.cdev = dev->port.func.config->cdev; |
| 426 | dev->bam_port.in = dev->port.in_ep; |
| 427 | dev->bam_port.out = dev->port.out_ep; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 428 | |
| 429 | /* currently we use the first connection */ |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 430 | ret = bam_data_connect(&dev->bam_port, 0, 0); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 431 | if (ret) { |
| 432 | pr_err("bam_data_connect failed: err:%d\n", |
| 433 | ret); |
| 434 | return ret; |
| 435 | } else { |
| 436 | pr_info("rndis bam connected\n"); |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int rndis_qc_bam_disconnect(struct f_rndis_qc *dev) |
| 443 | { |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 444 | pr_debug("dev:%p. %s Disconnect BAM.\n", dev, __func__); |
| 445 | |
| 446 | bam_data_disconnect(&dev->bam_port, 0); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /*-------------------------------------------------------------------------*/ |
| 452 | |
| 453 | static struct sk_buff *rndis_qc_add_header(struct qc_gether *port, |
| 454 | struct sk_buff *skb) |
| 455 | { |
| 456 | struct sk_buff *skb2; |
| 457 | |
| 458 | skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); |
| 459 | if (skb2) |
| 460 | rndis_add_hdr(skb2); |
| 461 | |
| 462 | dev_kfree_skb_any(skb); |
| 463 | return skb2; |
| 464 | } |
| 465 | |
| 466 | int rndis_qc_rm_hdr(struct qc_gether *port, |
| 467 | struct sk_buff *skb, |
| 468 | struct sk_buff_head *list) |
| 469 | { |
| 470 | /* tmp points to a struct rndis_packet_msg_type */ |
| 471 | __le32 *tmp = (void *)skb->data; |
| 472 | |
| 473 | /* MessageType, MessageLength */ |
| 474 | if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG) |
| 475 | != get_unaligned(tmp++)) { |
| 476 | dev_kfree_skb_any(skb); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | tmp++; |
| 480 | |
| 481 | /* DataOffset, DataLength */ |
| 482 | if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) { |
| 483 | dev_kfree_skb_any(skb); |
| 484 | return -EOVERFLOW; |
| 485 | } |
| 486 | skb_trim(skb, get_unaligned_le32(tmp++)); |
| 487 | |
| 488 | skb_queue_tail(list, skb); |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | static void rndis_qc_response_available(void *_rndis) |
| 494 | { |
| 495 | struct f_rndis_qc *rndis = _rndis; |
| 496 | struct usb_request *req = rndis->notify_req; |
| 497 | __le32 *data = req->buf; |
| 498 | int status; |
| 499 | |
| 500 | if (atomic_inc_return(&rndis->notify_count) != 1) |
| 501 | return; |
| 502 | |
| 503 | /* Send RNDIS RESPONSE_AVAILABLE notification; a |
| 504 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too |
| 505 | * |
| 506 | * This is the only notification defined by RNDIS. |
| 507 | */ |
| 508 | data[0] = cpu_to_le32(1); |
| 509 | data[1] = cpu_to_le32(0); |
| 510 | |
| 511 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); |
| 512 | if (status) { |
| 513 | atomic_dec(&rndis->notify_count); |
| 514 | pr_info("notify/0 --> %d\n", status); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static void rndis_qc_response_complete(struct usb_ep *ep, |
| 519 | struct usb_request *req) |
| 520 | { |
Jack Pham | 0ad82e6 | 2012-09-27 17:31:08 -0700 | [diff] [blame] | 521 | struct f_rndis_qc *rndis = req->context; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 522 | int status = req->status; |
Jack Pham | 0ad82e6 | 2012-09-27 17:31:08 -0700 | [diff] [blame] | 523 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 524 | |
| 525 | /* after TX: |
| 526 | * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control) |
| 527 | * - RNDIS_RESPONSE_AVAILABLE (status/irq) |
| 528 | */ |
| 529 | switch (status) { |
| 530 | case -ECONNRESET: |
| 531 | case -ESHUTDOWN: |
| 532 | /* connection gone */ |
| 533 | atomic_set(&rndis->notify_count, 0); |
| 534 | break; |
| 535 | default: |
| 536 | pr_info("RNDIS %s response error %d, %d/%d\n", |
| 537 | ep->name, status, |
| 538 | req->actual, req->length); |
| 539 | /* FALLTHROUGH */ |
| 540 | case 0: |
| 541 | if (ep != rndis->notify) |
| 542 | break; |
| 543 | |
| 544 | /* handle multiple pending RNDIS_RESPONSE_AVAILABLE |
| 545 | * notifications by resending until we're done |
| 546 | */ |
| 547 | if (atomic_dec_and_test(&rndis->notify_count)) |
| 548 | break; |
| 549 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); |
| 550 | if (status) { |
| 551 | atomic_dec(&rndis->notify_count); |
| 552 | DBG(cdev, "notify/1 --> %d\n", status); |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static void rndis_qc_command_complete(struct usb_ep *ep, |
| 559 | struct usb_request *req) |
| 560 | { |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 561 | struct f_rndis_qc *rndis = req->context; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 562 | int status; |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 563 | rndis_init_msg_type *buf; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 564 | |
| 565 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ |
| 566 | status = rndis_msg_parser(rndis->config, (u8 *) req->buf); |
| 567 | if (status < 0) |
| 568 | pr_err("RNDIS command error %d, %d/%d\n", |
| 569 | status, req->actual, req->length); |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 570 | |
| 571 | buf = (rndis_init_msg_type *)req->buf; |
| 572 | |
| 573 | if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) { |
| 574 | rndis->max_pkt_size = buf->MaxTransferSize; |
| 575 | pr_debug("MaxTransferSize: %d\n", buf->MaxTransferSize); |
| 576 | } |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static int |
| 580 | rndis_qc_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 581 | { |
| 582 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 583 | struct usb_composite_dev *cdev = f->config->cdev; |
| 584 | struct usb_request *req = cdev->req; |
| 585 | int value = -EOPNOTSUPP; |
| 586 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 587 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 588 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 589 | |
| 590 | /* composite driver infrastructure handles everything except |
| 591 | * CDC class messages; interface activation uses set_alt(). |
| 592 | */ |
| 593 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 594 | |
| 595 | /* RNDIS uses the CDC command encapsulation mechanism to implement |
| 596 | * an RPC scheme, with much getting/setting of attributes by OID. |
| 597 | */ |
| 598 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 599 | | USB_CDC_SEND_ENCAPSULATED_COMMAND: |
| 600 | if (w_value || w_index != rndis->ctrl_id) |
| 601 | goto invalid; |
| 602 | /* read the request; process it later */ |
| 603 | value = w_length; |
| 604 | req->complete = rndis_qc_command_complete; |
| 605 | req->context = rndis; |
| 606 | /* later, rndis_response_available() sends a notification */ |
| 607 | break; |
| 608 | |
| 609 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 610 | | USB_CDC_GET_ENCAPSULATED_RESPONSE: |
| 611 | if (w_value || w_index != rndis->ctrl_id) |
| 612 | goto invalid; |
| 613 | else { |
| 614 | u8 *buf; |
| 615 | u32 n; |
| 616 | |
| 617 | /* return the result */ |
| 618 | buf = rndis_get_next_response(rndis->config, &n); |
| 619 | if (buf) { |
| 620 | memcpy(req->buf, buf, n); |
| 621 | req->complete = rndis_qc_response_complete; |
| 622 | rndis_free_response(rndis->config, buf); |
| 623 | value = n; |
| 624 | } |
| 625 | /* else stalls ... spec says to avoid that */ |
| 626 | } |
| 627 | break; |
| 628 | |
| 629 | default: |
| 630 | invalid: |
| 631 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 632 | ctrl->bRequestType, ctrl->bRequest, |
| 633 | w_value, w_index, w_length); |
| 634 | } |
| 635 | |
| 636 | /* respond with data transfer or status phase? */ |
| 637 | if (value >= 0) { |
| 638 | DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n", |
| 639 | ctrl->bRequestType, ctrl->bRequest, |
| 640 | w_value, w_index, w_length); |
| 641 | req->zero = (value < w_length); |
| 642 | req->length = value; |
| 643 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 644 | if (value < 0) |
| 645 | pr_err("rndis response on err %d\n", value); |
| 646 | } |
| 647 | |
| 648 | /* device either stalls (value < 0) or reports success */ |
| 649 | return value; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | static int rndis_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 654 | { |
| 655 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 656 | struct usb_composite_dev *cdev = f->config->cdev; |
| 657 | |
| 658 | /* we know alt == 0 */ |
| 659 | |
| 660 | if (intf == rndis->ctrl_id) { |
| 661 | if (rndis->notify->driver_data) { |
| 662 | VDBG(cdev, "reset rndis control %d\n", intf); |
| 663 | usb_ep_disable(rndis->notify); |
| 664 | } |
| 665 | if (!rndis->notify->desc) { |
| 666 | VDBG(cdev, "init rndis ctrl %d\n", intf); |
| 667 | if (config_ep_by_speed(cdev->gadget, f, rndis->notify)) |
| 668 | goto fail; |
| 669 | } |
| 670 | usb_ep_enable(rndis->notify); |
| 671 | rndis->notify->driver_data = rndis; |
| 672 | |
| 673 | } else if (intf == rndis->data_id) { |
| 674 | struct net_device *net; |
| 675 | |
| 676 | if (rndis->port.in_ep->driver_data) { |
| 677 | DBG(cdev, "reset rndis\n"); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 678 | /* rndis->port is needed for disconnecting the BAM data |
| 679 | * path. Only after the BAM data path is disconnected, |
| 680 | * we can disconnect the port from the network layer. |
| 681 | */ |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 682 | rndis_qc_bam_disconnect(rndis); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 683 | gether_qc_disconnect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) { |
| 687 | DBG(cdev, "init rndis\n"); |
| 688 | if (config_ep_by_speed(cdev->gadget, f, |
| 689 | rndis->port.in_ep) || |
| 690 | config_ep_by_speed(cdev->gadget, f, |
| 691 | rndis->port.out_ep)) { |
| 692 | rndis->port.in_ep->desc = NULL; |
| 693 | rndis->port.out_ep->desc = NULL; |
| 694 | goto fail; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* Avoid ZLPs; they can be troublesome. */ |
| 699 | rndis->port.is_zlp_ok = false; |
| 700 | |
| 701 | /* RNDIS should be in the "RNDIS uninitialized" state, |
| 702 | * either never activated or after rndis_uninit(). |
| 703 | * |
| 704 | * We don't want data to flow here until a nonzero packet |
| 705 | * filter is set, at which point it enters "RNDIS data |
| 706 | * initialized" state ... but we do want the endpoints |
| 707 | * to be activated. It's a strange little state. |
| 708 | * |
| 709 | * REVISIT the RNDIS gadget code has done this wrong for a |
| 710 | * very long time. We need another call to the link layer |
| 711 | * code -- gether_updown(...bool) maybe -- to do it right. |
| 712 | */ |
| 713 | rndis->port.cdc_filter = 0; |
| 714 | |
| 715 | DBG(cdev, "RNDIS RX/TX early activation ...\n"); |
Amit Blay | d6d690a | 2012-10-16 13:37:42 +0200 | [diff] [blame] | 716 | net = gether_qc_connect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 717 | if (IS_ERR(net)) |
| 718 | return PTR_ERR(net); |
| 719 | |
| 720 | if (rndis_qc_bam_connect(rndis)) |
| 721 | goto fail; |
| 722 | |
| 723 | rndis_set_param_dev(rndis->config, net, |
| 724 | &rndis->port.cdc_filter); |
| 725 | } else |
| 726 | goto fail; |
| 727 | |
| 728 | return 0; |
| 729 | fail: |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | static void rndis_qc_disable(struct usb_function *f) |
| 734 | { |
| 735 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 736 | |
| 737 | if (!rndis->notify->driver_data) |
| 738 | return; |
| 739 | |
| 740 | pr_info("rndis deactivated\n"); |
| 741 | |
| 742 | rndis_uninit(rndis->config); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 743 | rndis_qc_bam_disconnect(rndis); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 744 | gether_qc_disconnect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 745 | |
| 746 | usb_ep_disable(rndis->notify); |
| 747 | rndis->notify->driver_data = NULL; |
| 748 | } |
| 749 | |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 750 | static void rndis_qc_suspend(struct usb_function *f) |
| 751 | { |
| 752 | pr_debug("%s: rndis suspended\n", __func__); |
| 753 | |
| 754 | bam_data_suspend(RNDIS_QC_ACTIVE_PORT); |
| 755 | } |
| 756 | |
| 757 | static void rndis_qc_resume(struct usb_function *f) |
| 758 | { |
| 759 | pr_debug("%s: rndis resumed\n", __func__); |
| 760 | |
| 761 | bam_data_resume(RNDIS_QC_ACTIVE_PORT); |
| 762 | } |
| 763 | |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 764 | /*-------------------------------------------------------------------------*/ |
| 765 | |
| 766 | /* |
| 767 | * This isn't quite the same mechanism as CDC Ethernet, since the |
| 768 | * notification scheme passes less data, but the same set of link |
| 769 | * states must be tested. A key difference is that altsettings are |
| 770 | * not used to tell whether the link should send packets or not. |
| 771 | */ |
| 772 | |
| 773 | static void rndis_qc_open(struct qc_gether *geth) |
| 774 | { |
| 775 | struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func); |
| 776 | struct usb_composite_dev *cdev = geth->func.config->cdev; |
| 777 | |
| 778 | DBG(cdev, "%s\n", __func__); |
| 779 | |
| 780 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, |
| 781 | rndis_qc_bitrate(cdev->gadget) / 100); |
| 782 | rndis_signal_connect(rndis->config); |
| 783 | } |
| 784 | |
| 785 | static void rndis_qc_close(struct qc_gether *geth) |
| 786 | { |
| 787 | struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func); |
| 788 | |
| 789 | DBG(geth->func.config->cdev, "%s\n", __func__); |
| 790 | |
| 791 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); |
| 792 | rndis_signal_disconnect(rndis->config); |
| 793 | } |
| 794 | |
| 795 | /*-------------------------------------------------------------------------*/ |
| 796 | |
| 797 | /* ethernet function driver setup/binding */ |
| 798 | |
| 799 | static int |
| 800 | rndis_qc_bind(struct usb_configuration *c, struct usb_function *f) |
| 801 | { |
| 802 | struct usb_composite_dev *cdev = c->cdev; |
| 803 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 804 | int status; |
| 805 | struct usb_ep *ep; |
| 806 | |
| 807 | /* allocate instance-specific interface IDs */ |
| 808 | status = usb_interface_id(c, f); |
| 809 | if (status < 0) |
| 810 | goto fail; |
| 811 | rndis->ctrl_id = status; |
| 812 | rndis_qc_iad_descriptor.bFirstInterface = status; |
| 813 | |
| 814 | rndis_qc_control_intf.bInterfaceNumber = status; |
| 815 | rndis_qc_union_desc.bMasterInterface0 = status; |
| 816 | |
| 817 | status = usb_interface_id(c, f); |
| 818 | if (status < 0) |
| 819 | goto fail; |
| 820 | rndis->data_id = status; |
| 821 | |
| 822 | rndis_qc_data_intf.bInterfaceNumber = status; |
| 823 | rndis_qc_union_desc.bSlaveInterface0 = status; |
| 824 | |
| 825 | status = -ENODEV; |
| 826 | |
| 827 | /* allocate instance-specific endpoints */ |
| 828 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_in_desc); |
| 829 | if (!ep) |
| 830 | goto fail; |
| 831 | rndis->port.in_ep = ep; |
| 832 | ep->driver_data = cdev; /* claim */ |
| 833 | |
| 834 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_out_desc); |
| 835 | if (!ep) |
| 836 | goto fail; |
| 837 | rndis->port.out_ep = ep; |
| 838 | ep->driver_data = cdev; /* claim */ |
| 839 | |
| 840 | /* NOTE: a status/notification endpoint is, strictly speaking, |
| 841 | * optional. We don't treat it that way though! It's simpler, |
| 842 | * and some newer profiles don't treat it as optional. |
| 843 | */ |
| 844 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_notify_desc); |
| 845 | if (!ep) |
| 846 | goto fail; |
| 847 | rndis->notify = ep; |
| 848 | ep->driver_data = cdev; /* claim */ |
| 849 | |
| 850 | status = -ENOMEM; |
| 851 | |
| 852 | /* allocate notification request and buffer */ |
| 853 | rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 854 | if (!rndis->notify_req) |
| 855 | goto fail; |
| 856 | rndis->notify_req->buf = kmalloc(RNDIS_QC_STATUS_BYTECOUNT, GFP_KERNEL); |
| 857 | if (!rndis->notify_req->buf) |
| 858 | goto fail; |
| 859 | rndis->notify_req->length = RNDIS_QC_STATUS_BYTECOUNT; |
| 860 | rndis->notify_req->context = rndis; |
| 861 | rndis->notify_req->complete = rndis_qc_response_complete; |
| 862 | |
| 863 | /* copy descriptors, and track endpoint copies */ |
| 864 | f->descriptors = usb_copy_descriptors(eth_qc_fs_function); |
| 865 | if (!f->descriptors) |
| 866 | goto fail; |
| 867 | |
| 868 | /* support all relevant hardware speeds... we expect that when |
| 869 | * hardware is dual speed, all bulk-capable endpoints work at |
| 870 | * both speeds |
| 871 | */ |
| 872 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 873 | rndis_qc_hs_in_desc.bEndpointAddress = |
| 874 | rndis_qc_fs_in_desc.bEndpointAddress; |
| 875 | rndis_qc_hs_out_desc.bEndpointAddress = |
| 876 | rndis_qc_fs_out_desc.bEndpointAddress; |
| 877 | rndis_qc_hs_notify_desc.bEndpointAddress = |
| 878 | rndis_qc_fs_notify_desc.bEndpointAddress; |
| 879 | |
| 880 | /* copy descriptors, and track endpoint copies */ |
| 881 | f->hs_descriptors = usb_copy_descriptors(eth_qc_hs_function); |
| 882 | |
| 883 | if (!f->hs_descriptors) |
| 884 | goto fail; |
| 885 | } |
| 886 | |
| 887 | if (gadget_is_superspeed(c->cdev->gadget)) { |
| 888 | rndis_qc_ss_in_desc.bEndpointAddress = |
| 889 | rndis_qc_fs_in_desc.bEndpointAddress; |
| 890 | rndis_qc_ss_out_desc.bEndpointAddress = |
| 891 | rndis_qc_fs_out_desc.bEndpointAddress; |
| 892 | rndis_qc_ss_notify_desc.bEndpointAddress = |
| 893 | rndis_qc_fs_notify_desc.bEndpointAddress; |
| 894 | |
| 895 | /* copy descriptors, and track endpoint copies */ |
| 896 | f->ss_descriptors = usb_copy_descriptors(eth_qc_ss_function); |
| 897 | if (!f->ss_descriptors) |
| 898 | goto fail; |
| 899 | } |
| 900 | |
| 901 | rndis->port.open = rndis_qc_open; |
| 902 | rndis->port.close = rndis_qc_close; |
| 903 | |
| 904 | status = rndis_register(rndis_qc_response_available, rndis); |
| 905 | if (status < 0) |
| 906 | goto fail; |
| 907 | rndis->config = status; |
| 908 | |
| 909 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); |
| 910 | rndis_set_host_mac(rndis->config, rndis->ethaddr); |
| 911 | |
| 912 | if (rndis_set_param_vendor(rndis->config, rndis->vendorID, |
| 913 | rndis->manufacturer)) |
| 914 | goto fail; |
| 915 | |
| 916 | rndis_set_max_pkt_xfer(rndis->config, rndis->max_pkt_per_xfer); |
| 917 | |
Ofir Cohen | 76624ed | 2012-09-09 10:27:58 +0300 | [diff] [blame] | 918 | /* In case of aggregated packets QC device will request |
| 919 | * aliment to 4 (2^2). |
| 920 | */ |
| 921 | rndis_set_pkt_alignment_factor(rndis->config, 2); |
| 922 | |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 923 | /* NOTE: all that is done without knowing or caring about |
| 924 | * the network link ... which is unavailable to this code |
| 925 | * until we're activated via set_alt(). |
| 926 | */ |
| 927 | |
| 928 | DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n", |
| 929 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
| 930 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 931 | rndis->port.in_ep->name, rndis->port.out_ep->name, |
| 932 | rndis->notify->name); |
| 933 | return 0; |
| 934 | |
| 935 | fail: |
| 936 | if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors) |
| 937 | usb_free_descriptors(f->ss_descriptors); |
| 938 | if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors) |
| 939 | usb_free_descriptors(f->hs_descriptors); |
| 940 | if (f->descriptors) |
| 941 | usb_free_descriptors(f->descriptors); |
| 942 | |
| 943 | if (rndis->notify_req) { |
| 944 | kfree(rndis->notify_req->buf); |
| 945 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
| 946 | } |
| 947 | |
| 948 | /* we might as well release our claims on endpoints */ |
| 949 | if (rndis->notify) |
| 950 | rndis->notify->driver_data = NULL; |
| 951 | if (rndis->port.out_ep->desc) |
| 952 | rndis->port.out_ep->driver_data = NULL; |
| 953 | if (rndis->port.in_ep->desc) |
| 954 | rndis->port.in_ep->driver_data = NULL; |
| 955 | |
| 956 | pr_err("%s: can't bind, err %d\n", f->name, status); |
| 957 | |
| 958 | return status; |
| 959 | } |
| 960 | |
| 961 | static void |
| 962 | rndis_qc_unbind(struct usb_configuration *c, struct usb_function *f) |
| 963 | { |
| 964 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 965 | |
| 966 | rndis_deregister(rndis->config); |
| 967 | rndis_exit(); |
| 968 | |
| 969 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 970 | usb_free_descriptors(f->hs_descriptors); |
| 971 | usb_free_descriptors(f->descriptors); |
| 972 | |
| 973 | kfree(rndis->notify_req->buf); |
| 974 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
| 975 | |
| 976 | kfree(rndis); |
| 977 | } |
| 978 | |
| 979 | /* Some controllers can't support RNDIS ... */ |
| 980 | static inline bool can_support_rndis_qc(struct usb_configuration *c) |
| 981 | { |
| 982 | /* everything else is *presumably* fine */ |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * rndis_qc_bind_config - add RNDIS network link to a configuration |
| 988 | * @c: the configuration to support the network link |
| 989 | * @ethaddr: a buffer in which the ethernet address of the host side |
| 990 | * side of the link was recorded |
| 991 | * Context: single threaded during gadget setup |
| 992 | * |
| 993 | * Returns zero on success, else negative errno. |
| 994 | * |
| 995 | * Caller must have called @gether_setup(). Caller is also responsible |
| 996 | * for calling @gether_cleanup() before module unload. |
| 997 | */ |
| 998 | int |
| 999 | rndis_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) |
| 1000 | { |
| 1001 | return rndis_qc_bind_config_vendor(c, ethaddr, 0, NULL, 1); |
| 1002 | } |
| 1003 | |
| 1004 | int |
| 1005 | rndis_qc_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 1006 | u32 vendorID, const char *manufacturer, |
| 1007 | u8 max_pkt_per_xfer) |
| 1008 | { |
| 1009 | struct f_rndis_qc *rndis; |
| 1010 | int status; |
| 1011 | |
| 1012 | if (!can_support_rndis_qc(c) || !ethaddr) |
| 1013 | return -EINVAL; |
| 1014 | |
| 1015 | /* setup RNDIS itself */ |
| 1016 | status = rndis_init(); |
| 1017 | if (status < 0) |
| 1018 | return status; |
| 1019 | |
| 1020 | status = rndis_qc_bam_setup(); |
| 1021 | if (status) { |
| 1022 | pr_err("bam setup failed"); |
| 1023 | return status; |
| 1024 | } |
| 1025 | |
| 1026 | /* maybe allocate device-global string IDs */ |
| 1027 | if (rndis_qc_string_defs[0].id == 0) { |
| 1028 | |
| 1029 | /* control interface label */ |
| 1030 | status = usb_string_id(c->cdev); |
| 1031 | if (status < 0) |
| 1032 | return status; |
| 1033 | rndis_qc_string_defs[0].id = status; |
| 1034 | rndis_qc_control_intf.iInterface = status; |
| 1035 | |
| 1036 | /* data interface label */ |
| 1037 | status = usb_string_id(c->cdev); |
| 1038 | if (status < 0) |
| 1039 | return status; |
| 1040 | rndis_qc_string_defs[1].id = status; |
| 1041 | rndis_qc_data_intf.iInterface = status; |
| 1042 | |
| 1043 | /* IAD iFunction label */ |
| 1044 | status = usb_string_id(c->cdev); |
| 1045 | if (status < 0) |
| 1046 | return status; |
| 1047 | rndis_qc_string_defs[2].id = status; |
| 1048 | rndis_qc_iad_descriptor.iFunction = status; |
| 1049 | } |
| 1050 | |
| 1051 | /* allocate and initialize one new instance */ |
| 1052 | status = -ENOMEM; |
| 1053 | rndis = kzalloc(sizeof *rndis, GFP_KERNEL); |
| 1054 | if (!rndis) |
| 1055 | goto fail; |
| 1056 | |
| 1057 | memcpy(rndis->ethaddr, ethaddr, ETH_ALEN); |
| 1058 | rndis->vendorID = vendorID; |
| 1059 | rndis->manufacturer = manufacturer; |
| 1060 | |
| 1061 | /* if max_pkt_per_xfer was not configured set to default value */ |
| 1062 | rndis->max_pkt_per_xfer = |
| 1063 | max_pkt_per_xfer ? max_pkt_per_xfer : DEFAULT_MAX_PKT_PER_XFER; |
| 1064 | |
| 1065 | /* RNDIS activates when the host changes this filter */ |
| 1066 | rndis->port.cdc_filter = 0; |
| 1067 | |
| 1068 | /* RNDIS has special (and complex) framing */ |
| 1069 | rndis->port.header_len = sizeof(struct rndis_packet_msg_type); |
| 1070 | rndis->port.wrap = rndis_qc_add_header; |
| 1071 | rndis->port.unwrap = rndis_qc_rm_hdr; |
| 1072 | |
| 1073 | rndis->port.func.name = "rndis"; |
| 1074 | rndis->port.func.strings = rndis_qc_strings; |
| 1075 | /* descriptors are per-instance copies */ |
| 1076 | rndis->port.func.bind = rndis_qc_bind; |
| 1077 | rndis->port.func.unbind = rndis_qc_unbind; |
| 1078 | rndis->port.func.set_alt = rndis_qc_set_alt; |
| 1079 | rndis->port.func.setup = rndis_qc_setup; |
| 1080 | rndis->port.func.disable = rndis_qc_disable; |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 1081 | rndis->port.func.suspend = rndis_qc_suspend; |
| 1082 | rndis->port.func.resume = rndis_qc_resume; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 1083 | |
| 1084 | _rndis_qc = rndis; |
| 1085 | |
| 1086 | status = usb_add_function(c, &rndis->port.func); |
| 1087 | if (status) { |
| 1088 | kfree(rndis); |
| 1089 | fail: |
| 1090 | rndis_exit(); |
| 1091 | } |
| 1092 | return status; |
| 1093 | } |
| 1094 | |
| 1095 | static int rndis_qc_open_dev(struct inode *ip, struct file *fp) |
| 1096 | { |
| 1097 | pr_info("Open rndis QC driver\n"); |
| 1098 | |
| 1099 | if (!_rndis_qc) { |
| 1100 | pr_err("rndis_qc_dev not created yet\n"); |
| 1101 | return -ENODEV; |
| 1102 | } |
| 1103 | |
| 1104 | if (rndis_qc_lock(&_rndis_qc->open_excl)) { |
| 1105 | pr_err("Already opened\n"); |
| 1106 | return -EBUSY; |
| 1107 | } |
| 1108 | |
| 1109 | fp->private_data = _rndis_qc; |
| 1110 | pr_info("rndis QC file opened\n"); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int rndis_qc_release_dev(struct inode *ip, struct file *fp) |
| 1116 | { |
| 1117 | struct f_rndis_qc *rndis = fp->private_data; |
| 1118 | |
| 1119 | pr_info("Close rndis QC file"); |
| 1120 | rndis_qc_unlock(&rndis->open_excl); |
| 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | static long rndis_qc_ioctl(struct file *fp, unsigned cmd, unsigned long arg) |
| 1126 | { |
| 1127 | struct f_rndis_qc *rndis = fp->private_data; |
| 1128 | int ret = 0; |
| 1129 | |
| 1130 | pr_info("Received command %d", cmd); |
| 1131 | |
| 1132 | if (rndis_qc_lock(&rndis->ioctl_excl)) |
| 1133 | return -EBUSY; |
| 1134 | |
| 1135 | switch (cmd) { |
| 1136 | case RNDIS_QC_GET_MAX_PKT_PER_XFER: |
| 1137 | ret = copy_to_user((void __user *)arg, |
| 1138 | &rndis->max_pkt_per_xfer, |
| 1139 | sizeof(rndis->max_pkt_per_xfer)); |
| 1140 | if (ret) { |
| 1141 | pr_err("copying to user space failed"); |
| 1142 | ret = -EFAULT; |
| 1143 | } |
| 1144 | pr_info("Sent max packets per xfer %d", |
| 1145 | rndis->max_pkt_per_xfer); |
| 1146 | break; |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 1147 | case RNDIS_QC_GET_MAX_PKT_SIZE: |
| 1148 | ret = copy_to_user((void __user *)arg, |
| 1149 | &rndis->max_pkt_size, |
| 1150 | sizeof(rndis->max_pkt_size)); |
| 1151 | if (ret) { |
| 1152 | pr_err("copying to user space failed"); |
| 1153 | ret = -EFAULT; |
| 1154 | } |
| 1155 | pr_debug("Sent max packet size %d", |
| 1156 | rndis->max_pkt_size); |
| 1157 | break; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 1158 | default: |
| 1159 | pr_err("Unsupported IOCTL"); |
| 1160 | ret = -EINVAL; |
| 1161 | } |
| 1162 | |
| 1163 | rndis_qc_unlock(&rndis->ioctl_excl); |
| 1164 | |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | static const struct file_operations rndis_qc_fops = { |
| 1169 | .owner = THIS_MODULE, |
| 1170 | .open = rndis_qc_open_dev, |
| 1171 | .release = rndis_qc_release_dev, |
| 1172 | .unlocked_ioctl = rndis_qc_ioctl, |
| 1173 | }; |
| 1174 | |
| 1175 | static struct miscdevice rndis_qc_device = { |
| 1176 | .minor = MISC_DYNAMIC_MINOR, |
| 1177 | .name = "android_rndis_qc", |
| 1178 | .fops = &rndis_qc_fops, |
| 1179 | }; |
| 1180 | |
| 1181 | static int rndis_qc_init(void) |
| 1182 | { |
| 1183 | int ret; |
| 1184 | |
| 1185 | pr_info("initialize rndis QC instance\n"); |
| 1186 | |
| 1187 | ret = misc_register(&rndis_qc_device); |
| 1188 | if (ret) |
| 1189 | pr_err("rndis QC driver failed to register"); |
| 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | static void rndis_qc_cleanup(void) |
| 1195 | { |
| 1196 | pr_info("rndis QC cleanup"); |
| 1197 | |
| 1198 | misc_deregister(&rndis_qc_device); |
| 1199 | _rndis_qc = NULL; |
| 1200 | } |
| 1201 | |
| 1202 | |