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) |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2012-2013, 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 */ |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame^] | 430 | ret = bam_data_connect(&dev->bam_port, 0, USB_GADGET_XPORT_BAM2BAM, |
| 431 | 0, USB_FUNC_RNDIS); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 432 | if (ret) { |
| 433 | pr_err("bam_data_connect failed: err:%d\n", |
| 434 | ret); |
| 435 | return ret; |
| 436 | } else { |
| 437 | pr_info("rndis bam connected\n"); |
| 438 | } |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int rndis_qc_bam_disconnect(struct f_rndis_qc *dev) |
| 444 | { |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 445 | pr_debug("dev:%p. %s Disconnect BAM.\n", dev, __func__); |
| 446 | |
| 447 | bam_data_disconnect(&dev->bam_port, 0); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /*-------------------------------------------------------------------------*/ |
| 453 | |
| 454 | static struct sk_buff *rndis_qc_add_header(struct qc_gether *port, |
| 455 | struct sk_buff *skb) |
| 456 | { |
| 457 | struct sk_buff *skb2; |
| 458 | |
| 459 | skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); |
| 460 | if (skb2) |
| 461 | rndis_add_hdr(skb2); |
| 462 | |
| 463 | dev_kfree_skb_any(skb); |
| 464 | return skb2; |
| 465 | } |
| 466 | |
| 467 | int rndis_qc_rm_hdr(struct qc_gether *port, |
| 468 | struct sk_buff *skb, |
| 469 | struct sk_buff_head *list) |
| 470 | { |
| 471 | /* tmp points to a struct rndis_packet_msg_type */ |
| 472 | __le32 *tmp = (void *)skb->data; |
| 473 | |
| 474 | /* MessageType, MessageLength */ |
| 475 | if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG) |
| 476 | != get_unaligned(tmp++)) { |
| 477 | dev_kfree_skb_any(skb); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | tmp++; |
| 481 | |
| 482 | /* DataOffset, DataLength */ |
| 483 | if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) { |
| 484 | dev_kfree_skb_any(skb); |
| 485 | return -EOVERFLOW; |
| 486 | } |
| 487 | skb_trim(skb, get_unaligned_le32(tmp++)); |
| 488 | |
| 489 | skb_queue_tail(list, skb); |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | static void rndis_qc_response_available(void *_rndis) |
| 495 | { |
| 496 | struct f_rndis_qc *rndis = _rndis; |
| 497 | struct usb_request *req = rndis->notify_req; |
| 498 | __le32 *data = req->buf; |
| 499 | int status; |
| 500 | |
| 501 | if (atomic_inc_return(&rndis->notify_count) != 1) |
| 502 | return; |
| 503 | |
| 504 | /* Send RNDIS RESPONSE_AVAILABLE notification; a |
| 505 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too |
| 506 | * |
| 507 | * This is the only notification defined by RNDIS. |
| 508 | */ |
| 509 | data[0] = cpu_to_le32(1); |
| 510 | data[1] = cpu_to_le32(0); |
| 511 | |
| 512 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); |
| 513 | if (status) { |
| 514 | atomic_dec(&rndis->notify_count); |
| 515 | pr_info("notify/0 --> %d\n", status); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static void rndis_qc_response_complete(struct usb_ep *ep, |
| 520 | struct usb_request *req) |
| 521 | { |
Jack Pham | 0ad82e6 | 2012-09-27 17:31:08 -0700 | [diff] [blame] | 522 | struct f_rndis_qc *rndis = req->context; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 523 | int status = req->status; |
Jack Pham | 0ad82e6 | 2012-09-27 17:31:08 -0700 | [diff] [blame] | 524 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 525 | |
| 526 | /* after TX: |
| 527 | * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control) |
| 528 | * - RNDIS_RESPONSE_AVAILABLE (status/irq) |
| 529 | */ |
| 530 | switch (status) { |
| 531 | case -ECONNRESET: |
| 532 | case -ESHUTDOWN: |
| 533 | /* connection gone */ |
| 534 | atomic_set(&rndis->notify_count, 0); |
| 535 | break; |
| 536 | default: |
| 537 | pr_info("RNDIS %s response error %d, %d/%d\n", |
| 538 | ep->name, status, |
| 539 | req->actual, req->length); |
| 540 | /* FALLTHROUGH */ |
| 541 | case 0: |
| 542 | if (ep != rndis->notify) |
| 543 | break; |
| 544 | |
| 545 | /* handle multiple pending RNDIS_RESPONSE_AVAILABLE |
| 546 | * notifications by resending until we're done |
| 547 | */ |
| 548 | if (atomic_dec_and_test(&rndis->notify_count)) |
| 549 | break; |
| 550 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); |
| 551 | if (status) { |
| 552 | atomic_dec(&rndis->notify_count); |
| 553 | DBG(cdev, "notify/1 --> %d\n", status); |
| 554 | } |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | static void rndis_qc_command_complete(struct usb_ep *ep, |
| 560 | struct usb_request *req) |
| 561 | { |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 562 | struct f_rndis_qc *rndis = req->context; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 563 | int status; |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 564 | rndis_init_msg_type *buf; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 565 | |
| 566 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ |
| 567 | status = rndis_msg_parser(rndis->config, (u8 *) req->buf); |
| 568 | if (status < 0) |
| 569 | pr_err("RNDIS command error %d, %d/%d\n", |
| 570 | status, req->actual, req->length); |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 571 | |
| 572 | buf = (rndis_init_msg_type *)req->buf; |
| 573 | |
| 574 | if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) { |
| 575 | rndis->max_pkt_size = buf->MaxTransferSize; |
| 576 | pr_debug("MaxTransferSize: %d\n", buf->MaxTransferSize); |
| 577 | } |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static int |
| 581 | rndis_qc_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 582 | { |
| 583 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 584 | struct usb_composite_dev *cdev = f->config->cdev; |
| 585 | struct usb_request *req = cdev->req; |
| 586 | int value = -EOPNOTSUPP; |
| 587 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 588 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 589 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 590 | |
| 591 | /* composite driver infrastructure handles everything except |
| 592 | * CDC class messages; interface activation uses set_alt(). |
| 593 | */ |
| 594 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { |
| 595 | |
| 596 | /* RNDIS uses the CDC command encapsulation mechanism to implement |
| 597 | * an RPC scheme, with much getting/setting of attributes by OID. |
| 598 | */ |
| 599 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 600 | | USB_CDC_SEND_ENCAPSULATED_COMMAND: |
| 601 | if (w_value || w_index != rndis->ctrl_id) |
| 602 | goto invalid; |
| 603 | /* read the request; process it later */ |
| 604 | value = w_length; |
| 605 | req->complete = rndis_qc_command_complete; |
| 606 | req->context = rndis; |
| 607 | /* later, rndis_response_available() sends a notification */ |
| 608 | break; |
| 609 | |
| 610 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) |
| 611 | | USB_CDC_GET_ENCAPSULATED_RESPONSE: |
| 612 | if (w_value || w_index != rndis->ctrl_id) |
| 613 | goto invalid; |
| 614 | else { |
| 615 | u8 *buf; |
| 616 | u32 n; |
| 617 | |
| 618 | /* return the result */ |
| 619 | buf = rndis_get_next_response(rndis->config, &n); |
| 620 | if (buf) { |
| 621 | memcpy(req->buf, buf, n); |
| 622 | req->complete = rndis_qc_response_complete; |
| 623 | rndis_free_response(rndis->config, buf); |
| 624 | value = n; |
| 625 | } |
| 626 | /* else stalls ... spec says to avoid that */ |
| 627 | } |
| 628 | break; |
| 629 | |
| 630 | default: |
| 631 | invalid: |
| 632 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 633 | ctrl->bRequestType, ctrl->bRequest, |
| 634 | w_value, w_index, w_length); |
| 635 | } |
| 636 | |
| 637 | /* respond with data transfer or status phase? */ |
| 638 | if (value >= 0) { |
| 639 | DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n", |
| 640 | ctrl->bRequestType, ctrl->bRequest, |
| 641 | w_value, w_index, w_length); |
| 642 | req->zero = (value < w_length); |
| 643 | req->length = value; |
| 644 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); |
| 645 | if (value < 0) |
| 646 | pr_err("rndis response on err %d\n", value); |
| 647 | } |
| 648 | |
| 649 | /* device either stalls (value < 0) or reports success */ |
| 650 | return value; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | static int rndis_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 655 | { |
| 656 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 657 | struct usb_composite_dev *cdev = f->config->cdev; |
| 658 | |
| 659 | /* we know alt == 0 */ |
| 660 | |
| 661 | if (intf == rndis->ctrl_id) { |
| 662 | if (rndis->notify->driver_data) { |
| 663 | VDBG(cdev, "reset rndis control %d\n", intf); |
| 664 | usb_ep_disable(rndis->notify); |
| 665 | } |
| 666 | if (!rndis->notify->desc) { |
| 667 | VDBG(cdev, "init rndis ctrl %d\n", intf); |
| 668 | if (config_ep_by_speed(cdev->gadget, f, rndis->notify)) |
| 669 | goto fail; |
| 670 | } |
| 671 | usb_ep_enable(rndis->notify); |
| 672 | rndis->notify->driver_data = rndis; |
| 673 | |
| 674 | } else if (intf == rndis->data_id) { |
| 675 | struct net_device *net; |
| 676 | |
| 677 | if (rndis->port.in_ep->driver_data) { |
| 678 | DBG(cdev, "reset rndis\n"); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 679 | /* rndis->port is needed for disconnecting the BAM data |
| 680 | * path. Only after the BAM data path is disconnected, |
| 681 | * we can disconnect the port from the network layer. |
| 682 | */ |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 683 | rndis_qc_bam_disconnect(rndis); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 684 | gether_qc_disconnect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) { |
| 688 | DBG(cdev, "init rndis\n"); |
| 689 | if (config_ep_by_speed(cdev->gadget, f, |
| 690 | rndis->port.in_ep) || |
| 691 | config_ep_by_speed(cdev->gadget, f, |
| 692 | rndis->port.out_ep)) { |
| 693 | rndis->port.in_ep->desc = NULL; |
| 694 | rndis->port.out_ep->desc = NULL; |
| 695 | goto fail; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* Avoid ZLPs; they can be troublesome. */ |
| 700 | rndis->port.is_zlp_ok = false; |
| 701 | |
| 702 | /* RNDIS should be in the "RNDIS uninitialized" state, |
| 703 | * either never activated or after rndis_uninit(). |
| 704 | * |
| 705 | * We don't want data to flow here until a nonzero packet |
| 706 | * filter is set, at which point it enters "RNDIS data |
| 707 | * initialized" state ... but we do want the endpoints |
| 708 | * to be activated. It's a strange little state. |
| 709 | * |
| 710 | * REVISIT the RNDIS gadget code has done this wrong for a |
| 711 | * very long time. We need another call to the link layer |
| 712 | * code -- gether_updown(...bool) maybe -- to do it right. |
| 713 | */ |
| 714 | rndis->port.cdc_filter = 0; |
| 715 | |
| 716 | DBG(cdev, "RNDIS RX/TX early activation ...\n"); |
Amit Blay | d6d690a | 2012-10-16 13:37:42 +0200 | [diff] [blame] | 717 | net = gether_qc_connect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 718 | if (IS_ERR(net)) |
| 719 | return PTR_ERR(net); |
| 720 | |
| 721 | if (rndis_qc_bam_connect(rndis)) |
| 722 | goto fail; |
| 723 | |
| 724 | rndis_set_param_dev(rndis->config, net, |
| 725 | &rndis->port.cdc_filter); |
| 726 | } else |
| 727 | goto fail; |
| 728 | |
| 729 | return 0; |
| 730 | fail: |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | |
| 734 | static void rndis_qc_disable(struct usb_function *f) |
| 735 | { |
| 736 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 737 | |
| 738 | if (!rndis->notify->driver_data) |
| 739 | return; |
| 740 | |
| 741 | pr_info("rndis deactivated\n"); |
| 742 | |
| 743 | rndis_uninit(rndis->config); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 744 | rndis_qc_bam_disconnect(rndis); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 745 | gether_qc_disconnect_name(&rndis->port, "rndis0"); |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 746 | |
| 747 | usb_ep_disable(rndis->notify); |
| 748 | rndis->notify->driver_data = NULL; |
| 749 | } |
| 750 | |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 751 | static void rndis_qc_suspend(struct usb_function *f) |
| 752 | { |
| 753 | pr_debug("%s: rndis suspended\n", __func__); |
| 754 | |
| 755 | bam_data_suspend(RNDIS_QC_ACTIVE_PORT); |
| 756 | } |
| 757 | |
| 758 | static void rndis_qc_resume(struct usb_function *f) |
| 759 | { |
| 760 | pr_debug("%s: rndis resumed\n", __func__); |
| 761 | |
| 762 | bam_data_resume(RNDIS_QC_ACTIVE_PORT); |
| 763 | } |
| 764 | |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 765 | /*-------------------------------------------------------------------------*/ |
| 766 | |
| 767 | /* |
| 768 | * This isn't quite the same mechanism as CDC Ethernet, since the |
| 769 | * notification scheme passes less data, but the same set of link |
| 770 | * states must be tested. A key difference is that altsettings are |
| 771 | * not used to tell whether the link should send packets or not. |
| 772 | */ |
| 773 | |
| 774 | static void rndis_qc_open(struct qc_gether *geth) |
| 775 | { |
| 776 | struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func); |
| 777 | struct usb_composite_dev *cdev = geth->func.config->cdev; |
| 778 | |
| 779 | DBG(cdev, "%s\n", __func__); |
| 780 | |
| 781 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, |
| 782 | rndis_qc_bitrate(cdev->gadget) / 100); |
| 783 | rndis_signal_connect(rndis->config); |
| 784 | } |
| 785 | |
| 786 | static void rndis_qc_close(struct qc_gether *geth) |
| 787 | { |
| 788 | struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func); |
| 789 | |
| 790 | DBG(geth->func.config->cdev, "%s\n", __func__); |
| 791 | |
| 792 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); |
| 793 | rndis_signal_disconnect(rndis->config); |
| 794 | } |
| 795 | |
| 796 | /*-------------------------------------------------------------------------*/ |
| 797 | |
| 798 | /* ethernet function driver setup/binding */ |
| 799 | |
| 800 | static int |
| 801 | rndis_qc_bind(struct usb_configuration *c, struct usb_function *f) |
| 802 | { |
| 803 | struct usb_composite_dev *cdev = c->cdev; |
| 804 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 805 | int status; |
| 806 | struct usb_ep *ep; |
| 807 | |
| 808 | /* allocate instance-specific interface IDs */ |
| 809 | status = usb_interface_id(c, f); |
| 810 | if (status < 0) |
| 811 | goto fail; |
| 812 | rndis->ctrl_id = status; |
| 813 | rndis_qc_iad_descriptor.bFirstInterface = status; |
| 814 | |
| 815 | rndis_qc_control_intf.bInterfaceNumber = status; |
| 816 | rndis_qc_union_desc.bMasterInterface0 = status; |
| 817 | |
| 818 | status = usb_interface_id(c, f); |
| 819 | if (status < 0) |
| 820 | goto fail; |
| 821 | rndis->data_id = status; |
| 822 | |
| 823 | rndis_qc_data_intf.bInterfaceNumber = status; |
| 824 | rndis_qc_union_desc.bSlaveInterface0 = status; |
| 825 | |
| 826 | status = -ENODEV; |
| 827 | |
| 828 | /* allocate instance-specific endpoints */ |
| 829 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_in_desc); |
| 830 | if (!ep) |
| 831 | goto fail; |
| 832 | rndis->port.in_ep = ep; |
| 833 | ep->driver_data = cdev; /* claim */ |
| 834 | |
| 835 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_out_desc); |
| 836 | if (!ep) |
| 837 | goto fail; |
| 838 | rndis->port.out_ep = ep; |
| 839 | ep->driver_data = cdev; /* claim */ |
| 840 | |
| 841 | /* NOTE: a status/notification endpoint is, strictly speaking, |
| 842 | * optional. We don't treat it that way though! It's simpler, |
| 843 | * and some newer profiles don't treat it as optional. |
| 844 | */ |
| 845 | ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_notify_desc); |
| 846 | if (!ep) |
| 847 | goto fail; |
| 848 | rndis->notify = ep; |
| 849 | ep->driver_data = cdev; /* claim */ |
| 850 | |
| 851 | status = -ENOMEM; |
| 852 | |
| 853 | /* allocate notification request and buffer */ |
| 854 | rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 855 | if (!rndis->notify_req) |
| 856 | goto fail; |
| 857 | rndis->notify_req->buf = kmalloc(RNDIS_QC_STATUS_BYTECOUNT, GFP_KERNEL); |
| 858 | if (!rndis->notify_req->buf) |
| 859 | goto fail; |
| 860 | rndis->notify_req->length = RNDIS_QC_STATUS_BYTECOUNT; |
| 861 | rndis->notify_req->context = rndis; |
| 862 | rndis->notify_req->complete = rndis_qc_response_complete; |
| 863 | |
| 864 | /* copy descriptors, and track endpoint copies */ |
| 865 | f->descriptors = usb_copy_descriptors(eth_qc_fs_function); |
| 866 | if (!f->descriptors) |
| 867 | goto fail; |
| 868 | |
| 869 | /* support all relevant hardware speeds... we expect that when |
| 870 | * hardware is dual speed, all bulk-capable endpoints work at |
| 871 | * both speeds |
| 872 | */ |
| 873 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 874 | rndis_qc_hs_in_desc.bEndpointAddress = |
| 875 | rndis_qc_fs_in_desc.bEndpointAddress; |
| 876 | rndis_qc_hs_out_desc.bEndpointAddress = |
| 877 | rndis_qc_fs_out_desc.bEndpointAddress; |
| 878 | rndis_qc_hs_notify_desc.bEndpointAddress = |
| 879 | rndis_qc_fs_notify_desc.bEndpointAddress; |
| 880 | |
| 881 | /* copy descriptors, and track endpoint copies */ |
| 882 | f->hs_descriptors = usb_copy_descriptors(eth_qc_hs_function); |
| 883 | |
| 884 | if (!f->hs_descriptors) |
| 885 | goto fail; |
| 886 | } |
| 887 | |
| 888 | if (gadget_is_superspeed(c->cdev->gadget)) { |
| 889 | rndis_qc_ss_in_desc.bEndpointAddress = |
| 890 | rndis_qc_fs_in_desc.bEndpointAddress; |
| 891 | rndis_qc_ss_out_desc.bEndpointAddress = |
| 892 | rndis_qc_fs_out_desc.bEndpointAddress; |
| 893 | rndis_qc_ss_notify_desc.bEndpointAddress = |
| 894 | rndis_qc_fs_notify_desc.bEndpointAddress; |
| 895 | |
| 896 | /* copy descriptors, and track endpoint copies */ |
| 897 | f->ss_descriptors = usb_copy_descriptors(eth_qc_ss_function); |
| 898 | if (!f->ss_descriptors) |
| 899 | goto fail; |
| 900 | } |
| 901 | |
| 902 | rndis->port.open = rndis_qc_open; |
| 903 | rndis->port.close = rndis_qc_close; |
| 904 | |
| 905 | status = rndis_register(rndis_qc_response_available, rndis); |
| 906 | if (status < 0) |
| 907 | goto fail; |
| 908 | rndis->config = status; |
| 909 | |
| 910 | rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0); |
| 911 | rndis_set_host_mac(rndis->config, rndis->ethaddr); |
| 912 | |
| 913 | if (rndis_set_param_vendor(rndis->config, rndis->vendorID, |
| 914 | rndis->manufacturer)) |
| 915 | goto fail; |
| 916 | |
| 917 | rndis_set_max_pkt_xfer(rndis->config, rndis->max_pkt_per_xfer); |
| 918 | |
Ofir Cohen | 76624ed | 2012-09-09 10:27:58 +0300 | [diff] [blame] | 919 | /* In case of aggregated packets QC device will request |
| 920 | * aliment to 4 (2^2). |
| 921 | */ |
| 922 | rndis_set_pkt_alignment_factor(rndis->config, 2); |
| 923 | |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 924 | /* NOTE: all that is done without knowing or caring about |
| 925 | * the network link ... which is unavailable to this code |
| 926 | * until we're activated via set_alt(). |
| 927 | */ |
| 928 | |
| 929 | DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n", |
| 930 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
| 931 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 932 | rndis->port.in_ep->name, rndis->port.out_ep->name, |
| 933 | rndis->notify->name); |
| 934 | return 0; |
| 935 | |
| 936 | fail: |
| 937 | if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors) |
| 938 | usb_free_descriptors(f->ss_descriptors); |
| 939 | if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors) |
| 940 | usb_free_descriptors(f->hs_descriptors); |
| 941 | if (f->descriptors) |
| 942 | usb_free_descriptors(f->descriptors); |
| 943 | |
| 944 | if (rndis->notify_req) { |
| 945 | kfree(rndis->notify_req->buf); |
| 946 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
| 947 | } |
| 948 | |
| 949 | /* we might as well release our claims on endpoints */ |
| 950 | if (rndis->notify) |
| 951 | rndis->notify->driver_data = NULL; |
| 952 | if (rndis->port.out_ep->desc) |
| 953 | rndis->port.out_ep->driver_data = NULL; |
| 954 | if (rndis->port.in_ep->desc) |
| 955 | rndis->port.in_ep->driver_data = NULL; |
| 956 | |
| 957 | pr_err("%s: can't bind, err %d\n", f->name, status); |
| 958 | |
| 959 | return status; |
| 960 | } |
| 961 | |
| 962 | static void |
| 963 | rndis_qc_unbind(struct usb_configuration *c, struct usb_function *f) |
| 964 | { |
| 965 | struct f_rndis_qc *rndis = func_to_rndis_qc(f); |
| 966 | |
| 967 | rndis_deregister(rndis->config); |
| 968 | rndis_exit(); |
| 969 | |
| 970 | if (gadget_is_dualspeed(c->cdev->gadget)) |
| 971 | usb_free_descriptors(f->hs_descriptors); |
| 972 | usb_free_descriptors(f->descriptors); |
| 973 | |
| 974 | kfree(rndis->notify_req->buf); |
| 975 | usb_ep_free_request(rndis->notify, rndis->notify_req); |
| 976 | |
| 977 | kfree(rndis); |
| 978 | } |
| 979 | |
| 980 | /* Some controllers can't support RNDIS ... */ |
| 981 | static inline bool can_support_rndis_qc(struct usb_configuration *c) |
| 982 | { |
| 983 | /* everything else is *presumably* fine */ |
| 984 | return true; |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * rndis_qc_bind_config - add RNDIS network link to a configuration |
| 989 | * @c: the configuration to support the network link |
| 990 | * @ethaddr: a buffer in which the ethernet address of the host side |
| 991 | * side of the link was recorded |
| 992 | * Context: single threaded during gadget setup |
| 993 | * |
| 994 | * Returns zero on success, else negative errno. |
| 995 | * |
| 996 | * Caller must have called @gether_setup(). Caller is also responsible |
| 997 | * for calling @gether_cleanup() before module unload. |
| 998 | */ |
| 999 | int |
| 1000 | rndis_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) |
| 1001 | { |
| 1002 | return rndis_qc_bind_config_vendor(c, ethaddr, 0, NULL, 1); |
| 1003 | } |
| 1004 | |
| 1005 | int |
| 1006 | rndis_qc_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
| 1007 | u32 vendorID, const char *manufacturer, |
| 1008 | u8 max_pkt_per_xfer) |
| 1009 | { |
| 1010 | struct f_rndis_qc *rndis; |
| 1011 | int status; |
| 1012 | |
| 1013 | if (!can_support_rndis_qc(c) || !ethaddr) |
| 1014 | return -EINVAL; |
| 1015 | |
| 1016 | /* setup RNDIS itself */ |
| 1017 | status = rndis_init(); |
| 1018 | if (status < 0) |
| 1019 | return status; |
| 1020 | |
| 1021 | status = rndis_qc_bam_setup(); |
| 1022 | if (status) { |
| 1023 | pr_err("bam setup failed"); |
| 1024 | return status; |
| 1025 | } |
| 1026 | |
| 1027 | /* maybe allocate device-global string IDs */ |
| 1028 | if (rndis_qc_string_defs[0].id == 0) { |
| 1029 | |
| 1030 | /* control interface label */ |
| 1031 | status = usb_string_id(c->cdev); |
| 1032 | if (status < 0) |
| 1033 | return status; |
| 1034 | rndis_qc_string_defs[0].id = status; |
| 1035 | rndis_qc_control_intf.iInterface = status; |
| 1036 | |
| 1037 | /* data interface label */ |
| 1038 | status = usb_string_id(c->cdev); |
| 1039 | if (status < 0) |
| 1040 | return status; |
| 1041 | rndis_qc_string_defs[1].id = status; |
| 1042 | rndis_qc_data_intf.iInterface = status; |
| 1043 | |
| 1044 | /* IAD iFunction label */ |
| 1045 | status = usb_string_id(c->cdev); |
| 1046 | if (status < 0) |
| 1047 | return status; |
| 1048 | rndis_qc_string_defs[2].id = status; |
| 1049 | rndis_qc_iad_descriptor.iFunction = status; |
| 1050 | } |
| 1051 | |
| 1052 | /* allocate and initialize one new instance */ |
| 1053 | status = -ENOMEM; |
| 1054 | rndis = kzalloc(sizeof *rndis, GFP_KERNEL); |
| 1055 | if (!rndis) |
| 1056 | goto fail; |
| 1057 | |
| 1058 | memcpy(rndis->ethaddr, ethaddr, ETH_ALEN); |
| 1059 | rndis->vendorID = vendorID; |
| 1060 | rndis->manufacturer = manufacturer; |
| 1061 | |
| 1062 | /* if max_pkt_per_xfer was not configured set to default value */ |
| 1063 | rndis->max_pkt_per_xfer = |
| 1064 | max_pkt_per_xfer ? max_pkt_per_xfer : DEFAULT_MAX_PKT_PER_XFER; |
| 1065 | |
| 1066 | /* RNDIS activates when the host changes this filter */ |
| 1067 | rndis->port.cdc_filter = 0; |
| 1068 | |
| 1069 | /* RNDIS has special (and complex) framing */ |
| 1070 | rndis->port.header_len = sizeof(struct rndis_packet_msg_type); |
| 1071 | rndis->port.wrap = rndis_qc_add_header; |
| 1072 | rndis->port.unwrap = rndis_qc_rm_hdr; |
| 1073 | |
| 1074 | rndis->port.func.name = "rndis"; |
| 1075 | rndis->port.func.strings = rndis_qc_strings; |
| 1076 | /* descriptors are per-instance copies */ |
| 1077 | rndis->port.func.bind = rndis_qc_bind; |
| 1078 | rndis->port.func.unbind = rndis_qc_unbind; |
| 1079 | rndis->port.func.set_alt = rndis_qc_set_alt; |
| 1080 | rndis->port.func.setup = rndis_qc_setup; |
| 1081 | rndis->port.func.disable = rndis_qc_disable; |
Anna Perel | a6d73074 | 2012-12-17 09:43:16 +0200 | [diff] [blame] | 1082 | rndis->port.func.suspend = rndis_qc_suspend; |
| 1083 | rndis->port.func.resume = rndis_qc_resume; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 1084 | |
| 1085 | _rndis_qc = rndis; |
| 1086 | |
| 1087 | status = usb_add_function(c, &rndis->port.func); |
| 1088 | if (status) { |
| 1089 | kfree(rndis); |
| 1090 | fail: |
| 1091 | rndis_exit(); |
| 1092 | } |
| 1093 | return status; |
| 1094 | } |
| 1095 | |
| 1096 | static int rndis_qc_open_dev(struct inode *ip, struct file *fp) |
| 1097 | { |
| 1098 | pr_info("Open rndis QC driver\n"); |
| 1099 | |
| 1100 | if (!_rndis_qc) { |
| 1101 | pr_err("rndis_qc_dev not created yet\n"); |
| 1102 | return -ENODEV; |
| 1103 | } |
| 1104 | |
| 1105 | if (rndis_qc_lock(&_rndis_qc->open_excl)) { |
| 1106 | pr_err("Already opened\n"); |
| 1107 | return -EBUSY; |
| 1108 | } |
| 1109 | |
| 1110 | fp->private_data = _rndis_qc; |
| 1111 | pr_info("rndis QC file opened\n"); |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | static int rndis_qc_release_dev(struct inode *ip, struct file *fp) |
| 1117 | { |
| 1118 | struct f_rndis_qc *rndis = fp->private_data; |
| 1119 | |
| 1120 | pr_info("Close rndis QC file"); |
| 1121 | rndis_qc_unlock(&rndis->open_excl); |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static long rndis_qc_ioctl(struct file *fp, unsigned cmd, unsigned long arg) |
| 1127 | { |
| 1128 | struct f_rndis_qc *rndis = fp->private_data; |
| 1129 | int ret = 0; |
| 1130 | |
| 1131 | pr_info("Received command %d", cmd); |
| 1132 | |
| 1133 | if (rndis_qc_lock(&rndis->ioctl_excl)) |
| 1134 | return -EBUSY; |
| 1135 | |
| 1136 | switch (cmd) { |
| 1137 | case RNDIS_QC_GET_MAX_PKT_PER_XFER: |
| 1138 | ret = copy_to_user((void __user *)arg, |
| 1139 | &rndis->max_pkt_per_xfer, |
| 1140 | sizeof(rndis->max_pkt_per_xfer)); |
| 1141 | if (ret) { |
| 1142 | pr_err("copying to user space failed"); |
| 1143 | ret = -EFAULT; |
| 1144 | } |
| 1145 | pr_info("Sent max packets per xfer %d", |
| 1146 | rndis->max_pkt_per_xfer); |
| 1147 | break; |
Anna Perel | ce47ed4 | 2012-12-05 14:31:07 +0200 | [diff] [blame] | 1148 | case RNDIS_QC_GET_MAX_PKT_SIZE: |
| 1149 | ret = copy_to_user((void __user *)arg, |
| 1150 | &rndis->max_pkt_size, |
| 1151 | sizeof(rndis->max_pkt_size)); |
| 1152 | if (ret) { |
| 1153 | pr_err("copying to user space failed"); |
| 1154 | ret = -EFAULT; |
| 1155 | } |
| 1156 | pr_debug("Sent max packet size %d", |
| 1157 | rndis->max_pkt_size); |
| 1158 | break; |
Ofir Cohen | aef90b7 | 2012-07-31 12:37:04 +0200 | [diff] [blame] | 1159 | default: |
| 1160 | pr_err("Unsupported IOCTL"); |
| 1161 | ret = -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | rndis_qc_unlock(&rndis->ioctl_excl); |
| 1165 | |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | static const struct file_operations rndis_qc_fops = { |
| 1170 | .owner = THIS_MODULE, |
| 1171 | .open = rndis_qc_open_dev, |
| 1172 | .release = rndis_qc_release_dev, |
| 1173 | .unlocked_ioctl = rndis_qc_ioctl, |
| 1174 | }; |
| 1175 | |
| 1176 | static struct miscdevice rndis_qc_device = { |
| 1177 | .minor = MISC_DYNAMIC_MINOR, |
| 1178 | .name = "android_rndis_qc", |
| 1179 | .fops = &rndis_qc_fops, |
| 1180 | }; |
| 1181 | |
| 1182 | static int rndis_qc_init(void) |
| 1183 | { |
| 1184 | int ret; |
| 1185 | |
| 1186 | pr_info("initialize rndis QC instance\n"); |
| 1187 | |
| 1188 | ret = misc_register(&rndis_qc_device); |
| 1189 | if (ret) |
| 1190 | pr_err("rndis QC driver failed to register"); |
| 1191 | |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | static void rndis_qc_cleanup(void) |
| 1196 | { |
| 1197 | pr_info("rndis QC cleanup"); |
| 1198 | |
| 1199 | misc_deregister(&rndis_qc_device); |
| 1200 | _rndis_qc = NULL; |
| 1201 | } |
| 1202 | |
| 1203 | |