blob: 59ff8d7b69a72cc8bee53166b2ab570e50c25b69 [file] [log] [blame]
David Brownell45fe3b82008-06-19 18:20:04 -07001/*
2 * f_rndis.c -- RNDIS link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01007 * Copyright (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01008 * Author: Michal Nazarewicz (mina86@mina86.com)
David Brownell45fe3b82008-06-19 18:20:04 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
David Brownell45fe3b82008-06-19 18:20:04 -070014 */
15
16/* #define VERBOSE_DEBUG */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Brownell45fe3b82008-06-19 18:20:04 -070019#include <linux/kernel.h>
20#include <linux/device.h>
21#include <linux/etherdevice.h>
22
Arun Sharma60063492011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Brownell45fe3b82008-06-19 18:20:04 -070024
25#include "u_ether.h"
26#include "rndis.h"
27
28
29/*
30 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
31 * been promoted instead of the standard CDC Ethernet. The published RNDIS
32 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
33 * ActiveSync have even worse status in terms of specification.
34 *
35 * In short: it's a protocol controlled by (and for) Microsoft, not for an
36 * Open ecosystem or markets. Linux supports it *only* because Microsoft
37 * doesn't support the CDC Ethernet standard.
38 *
39 * The RNDIS data transfer model is complex, with multiple Ethernet packets
40 * per USB message, and out of band data. The control model is built around
41 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
42 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
43 * useless (they're ignored). RNDIS expects to be the only function in its
44 * configuration, so it's no real help if you need composite devices; and
45 * it expects to be the first configuration too.
46 *
47 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
48 * discount the fluff that its RPC can be made to deliver: it doesn't need
49 * a NOP altsetting for the data interface. That lets it work on some of the
50 * "so smart it's stupid" hardware which takes over configuration changes
51 * from the software, and adds restrictions like "no altsettings".
52 *
53 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
54 * have all sorts of contrary-to-specification oddities that can prevent
55 * them from working sanely. Since bugfixes (or accurate specs, letting
56 * Linux work around those bugs) are unlikely to ever come from MSFT, you
57 * may want to avoid using RNDIS on purely operational grounds.
58 *
59 * Omissions from the RNDIS 1.0 specification include:
60 *
61 * - Power management ... references data that's scattered around lots
62 * of other documentation, which is incorrect/incomplete there too.
63 *
64 * - There are various undocumented protocol requirements, like the need
65 * to send garbage in some control-OUT messages.
66 *
67 * - MS-Windows drivers sometimes emit undocumented requests.
68 */
69
David Brownell45fe3b82008-06-19 18:20:04 -070070struct f_rndis {
71 struct gether port;
72 u8 ctrl_id, data_id;
73 u8 ethaddr[ETH_ALEN];
Benoit Gobyda8a44a2011-12-16 20:00:01 -080074 u32 vendorID;
75 const char *manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -070076 int config;
77
David Brownell45fe3b82008-06-19 18:20:04 -070078 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070079 struct usb_request *notify_req;
80 atomic_t notify_count;
81};
82
83static inline struct f_rndis *func_to_rndis(struct usb_function *f)
84{
85 return container_of(f, struct f_rndis, port.func);
86}
87
88/* peak (theoretical) bulk transfer rate in bits-per-second */
89static unsigned int bitrate(struct usb_gadget *g)
90{
Paul Zimmerman04617db2011-06-27 14:13:18 -070091 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
92 return 13 * 1024 * 8 * 1000 * 8;
93 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownell45fe3b82008-06-19 18:20:04 -070094 return 13 * 512 * 8 * 1000 * 8;
95 else
Paul Zimmerman04617db2011-06-27 14:13:18 -070096 return 19 * 64 * 1 * 1000 * 8;
David Brownell45fe3b82008-06-19 18:20:04 -070097}
98
99/*-------------------------------------------------------------------------*/
100
101/*
102 */
103
104#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
105#define STATUS_BYTECOUNT 8 /* 8 bytes data */
106
107
108/* interface descriptor: */
109
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200110static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700111 .bLength = sizeof rndis_control_intf,
112 .bDescriptorType = USB_DT_INTERFACE,
113
114 /* .bInterfaceNumber = DYNAMIC */
115 /* status endpoint is optional; this could be patched later */
116 .bNumEndpoints = 1,
117 .bInterfaceClass = USB_CLASS_COMM,
118 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
119 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
120 /* .iInterface = DYNAMIC */
121};
122
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200123static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700124 .bLength = sizeof header_desc,
125 .bDescriptorType = USB_DT_CS_INTERFACE,
126 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
127
Harvey Harrison551509d2009-02-11 14:11:36 -0800128 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700129};
130
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200131static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700132 .bLength = sizeof call_mgmt_descriptor,
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
135
136 .bmCapabilities = 0x00,
137 .bDataInterface = 0x01,
138};
139
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200140static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100141 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_ACM_TYPE,
144
145 .bmCapabilities = 0x00,
146};
147
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200148static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700149 .bLength = sizeof(rndis_union_desc),
150 .bDescriptorType = USB_DT_CS_INTERFACE,
151 .bDescriptorSubType = USB_CDC_UNION_TYPE,
152 /* .bMasterInterface0 = DYNAMIC */
153 /* .bSlaveInterface0 = DYNAMIC */
154};
155
156/* the data interface has two bulk endpoints */
157
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200158static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700159 .bLength = sizeof rndis_data_intf,
160 .bDescriptorType = USB_DT_INTERFACE,
161
162 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700163 .bNumEndpoints = 2,
164 .bInterfaceClass = USB_CLASS_CDC_DATA,
165 .bInterfaceSubClass = 0,
166 .bInterfaceProtocol = 0,
167 /* .iInterface = DYNAMIC */
168};
169
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100170
171static struct usb_interface_assoc_descriptor
172rndis_iad_descriptor = {
173 .bLength = sizeof rndis_iad_descriptor,
174 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
175
176 .bFirstInterface = 0, /* XXX, hardcoded */
177 .bInterfaceCount = 2, // control + data
178 .bFunctionClass = USB_CLASS_COMM,
179 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
180 .bFunctionProtocol = USB_CDC_PROTO_NONE,
181 /* .iFunction = DYNAMIC */
182};
183
David Brownell45fe3b82008-06-19 18:20:04 -0700184/* full speed support: */
185
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200186static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
189
190 .bEndpointAddress = USB_DIR_IN,
191 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800192 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700193 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
194};
195
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200196static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700197 .bLength = USB_DT_ENDPOINT_SIZE,
198 .bDescriptorType = USB_DT_ENDPOINT,
199
200 .bEndpointAddress = USB_DIR_IN,
201 .bmAttributes = USB_ENDPOINT_XFER_BULK,
202};
203
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200204static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700205 .bLength = USB_DT_ENDPOINT_SIZE,
206 .bDescriptorType = USB_DT_ENDPOINT,
207
208 .bEndpointAddress = USB_DIR_OUT,
209 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210};
211
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200212static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100213 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700214
David Brownell45fe3b82008-06-19 18:20:04 -0700215 /* control interface matches ACM, not Ethernet */
216 (struct usb_descriptor_header *) &rndis_control_intf,
217 (struct usb_descriptor_header *) &header_desc,
218 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100219 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700220 (struct usb_descriptor_header *) &rndis_union_desc,
221 (struct usb_descriptor_header *) &fs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700222
David Brownell45fe3b82008-06-19 18:20:04 -0700223 /* data interface has no altsetting */
224 (struct usb_descriptor_header *) &rndis_data_intf,
225 (struct usb_descriptor_header *) &fs_in_desc,
226 (struct usb_descriptor_header *) &fs_out_desc,
227 NULL,
228};
229
230/* high speed support: */
231
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200232static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700233 .bLength = USB_DT_ENDPOINT_SIZE,
234 .bDescriptorType = USB_DT_ENDPOINT,
235
236 .bEndpointAddress = USB_DIR_IN,
237 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800238 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700239 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
240};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700241
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200242static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT,
245
246 .bEndpointAddress = USB_DIR_IN,
247 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800248 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700249};
250
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200251static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700252 .bLength = USB_DT_ENDPOINT_SIZE,
253 .bDescriptorType = USB_DT_ENDPOINT,
254
255 .bEndpointAddress = USB_DIR_OUT,
256 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800257 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700258};
259
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200260static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100261 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700262
David Brownell45fe3b82008-06-19 18:20:04 -0700263 /* control interface matches ACM, not Ethernet */
264 (struct usb_descriptor_header *) &rndis_control_intf,
265 (struct usb_descriptor_header *) &header_desc,
266 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100267 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700268 (struct usb_descriptor_header *) &rndis_union_desc,
269 (struct usb_descriptor_header *) &hs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700270
David Brownell45fe3b82008-06-19 18:20:04 -0700271 /* data interface has no altsetting */
272 (struct usb_descriptor_header *) &rndis_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
275 NULL,
276};
277
Paul Zimmerman04617db2011-06-27 14:13:18 -0700278/* super speed support: */
279
280static struct usb_endpoint_descriptor ss_notify_desc = {
281 .bLength = USB_DT_ENDPOINT_SIZE,
282 .bDescriptorType = USB_DT_ENDPOINT,
283
284 .bEndpointAddress = USB_DIR_IN,
285 .bmAttributes = USB_ENDPOINT_XFER_INT,
286 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
287 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
288};
289
290static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
291 .bLength = sizeof ss_intr_comp_desc,
292 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
293
294 /* the following 3 values can be tweaked if necessary */
295 /* .bMaxBurst = 0, */
296 /* .bmAttributes = 0, */
297 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
298};
299
300static struct usb_endpoint_descriptor ss_in_desc = {
301 .bLength = USB_DT_ENDPOINT_SIZE,
302 .bDescriptorType = USB_DT_ENDPOINT,
303
304 .bEndpointAddress = USB_DIR_IN,
305 .bmAttributes = USB_ENDPOINT_XFER_BULK,
306 .wMaxPacketSize = cpu_to_le16(1024),
307};
308
309static struct usb_endpoint_descriptor ss_out_desc = {
310 .bLength = USB_DT_ENDPOINT_SIZE,
311 .bDescriptorType = USB_DT_ENDPOINT,
312
313 .bEndpointAddress = USB_DIR_OUT,
314 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 .wMaxPacketSize = cpu_to_le16(1024),
316};
317
318static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
319 .bLength = sizeof ss_bulk_comp_desc,
320 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
321
322 /* the following 2 values can be tweaked if necessary */
323 /* .bMaxBurst = 0, */
324 /* .bmAttributes = 0, */
325};
326
327static struct usb_descriptor_header *eth_ss_function[] = {
328 (struct usb_descriptor_header *) &rndis_iad_descriptor,
329
330 /* control interface matches ACM, not Ethernet */
331 (struct usb_descriptor_header *) &rndis_control_intf,
332 (struct usb_descriptor_header *) &header_desc,
333 (struct usb_descriptor_header *) &call_mgmt_descriptor,
334 (struct usb_descriptor_header *) &rndis_acm_descriptor,
335 (struct usb_descriptor_header *) &rndis_union_desc,
336 (struct usb_descriptor_header *) &ss_notify_desc,
337 (struct usb_descriptor_header *) &ss_intr_comp_desc,
338
339 /* data interface has no altsetting */
340 (struct usb_descriptor_header *) &rndis_data_intf,
341 (struct usb_descriptor_header *) &ss_in_desc,
342 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
343 (struct usb_descriptor_header *) &ss_out_desc,
344 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
345 NULL,
346};
347
David Brownell45fe3b82008-06-19 18:20:04 -0700348/* string descriptors: */
349
350static struct usb_string rndis_string_defs[] = {
351 [0].s = "RNDIS Communications Control",
352 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100353 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700354 { } /* end of list */
355};
356
357static struct usb_gadget_strings rndis_string_table = {
358 .language = 0x0409, /* en-us */
359 .strings = rndis_string_defs,
360};
361
362static struct usb_gadget_strings *rndis_strings[] = {
363 &rndis_string_table,
364 NULL,
365};
366
367/*-------------------------------------------------------------------------*/
368
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500369static struct sk_buff *rndis_add_header(struct gether *port,
370 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700371{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500372 struct sk_buff *skb2;
373
374 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
375 if (skb2)
376 rndis_add_hdr(skb2);
377
378 dev_kfree_skb_any(skb);
379 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700380}
381
382static void rndis_response_available(void *_rndis)
383{
384 struct f_rndis *rndis = _rndis;
385 struct usb_request *req = rndis->notify_req;
386 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
387 __le32 *data = req->buf;
388 int status;
389
Richard Röjforsff349502008-11-15 19:53:24 -0800390 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700391 return;
392
393 /* Send RNDIS RESPONSE_AVAILABLE notification; a
394 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
395 *
396 * This is the only notification defined by RNDIS.
397 */
398 data[0] = cpu_to_le32(1);
399 data[1] = cpu_to_le32(0);
400
401 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
402 if (status) {
403 atomic_dec(&rndis->notify_count);
404 DBG(cdev, "notify/0 --> %d\n", status);
405 }
406}
407
408static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
409{
410 struct f_rndis *rndis = req->context;
411 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
412 int status = req->status;
413
414 /* after TX:
415 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
416 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
417 */
418 switch (status) {
419 case -ECONNRESET:
420 case -ESHUTDOWN:
421 /* connection gone */
422 atomic_set(&rndis->notify_count, 0);
423 break;
424 default:
425 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
426 ep->name, status,
427 req->actual, req->length);
428 /* FALLTHROUGH */
429 case 0:
430 if (ep != rndis->notify)
431 break;
432
433 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
434 * notifications by resending until we're done
435 */
436 if (atomic_dec_and_test(&rndis->notify_count))
437 break;
438 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
439 if (status) {
440 atomic_dec(&rndis->notify_count);
441 DBG(cdev, "notify/1 --> %d\n", status);
442 }
443 break;
444 }
445}
446
447static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
448{
449 struct f_rndis *rndis = req->context;
450 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
451 int status;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530452 rndis_init_msg_type *buf;
David Brownell45fe3b82008-06-19 18:20:04 -0700453
454 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
455// spin_lock(&dev->lock);
456 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
457 if (status < 0)
458 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
459 status, req->actual, req->length);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530460
461 buf = (rndis_init_msg_type *)req->buf;
462
463 if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
464 if (buf->MaxTransferSize > 2048)
465 rndis->port.multi_pkt_xfer = 1;
466 else
467 rndis->port.multi_pkt_xfer = 0;
468 DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
469 __func__, buf->MaxTransferSize,
470 rndis->port.multi_pkt_xfer ? "enabled" :
471 "disabled");
472 }
David Brownell45fe3b82008-06-19 18:20:04 -0700473// spin_unlock(&dev->lock);
474}
475
476static int
477rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
478{
479 struct f_rndis *rndis = func_to_rndis(f);
480 struct usb_composite_dev *cdev = f->config->cdev;
481 struct usb_request *req = cdev->req;
482 int value = -EOPNOTSUPP;
483 u16 w_index = le16_to_cpu(ctrl->wIndex);
484 u16 w_value = le16_to_cpu(ctrl->wValue);
485 u16 w_length = le16_to_cpu(ctrl->wLength);
486
487 /* composite driver infrastructure handles everything except
488 * CDC class messages; interface activation uses set_alt().
489 */
490 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
491
492 /* RNDIS uses the CDC command encapsulation mechanism to implement
493 * an RPC scheme, with much getting/setting of attributes by OID.
494 */
495 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
496 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300497 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700498 goto invalid;
499 /* read the request; process it later */
500 value = w_length;
501 req->complete = rndis_command_complete;
502 req->context = rndis;
503 /* later, rndis_response_available() sends a notification */
504 break;
505
506 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
507 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
508 if (w_value || w_index != rndis->ctrl_id)
509 goto invalid;
510 else {
511 u8 *buf;
512 u32 n;
513
514 /* return the result */
515 buf = rndis_get_next_response(rndis->config, &n);
516 if (buf) {
517 memcpy(req->buf, buf, n);
518 req->complete = rndis_response_complete;
Lukasz Majewskif1356172012-02-10 09:54:51 +0100519 req->context = rndis;
David Brownell45fe3b82008-06-19 18:20:04 -0700520 rndis_free_response(rndis->config, buf);
521 value = n;
522 }
523 /* else stalls ... spec says to avoid that */
524 }
525 break;
526
527 default:
528invalid:
529 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
530 ctrl->bRequestType, ctrl->bRequest,
531 w_value, w_index, w_length);
532 }
533
534 /* respond with data transfer or status phase? */
535 if (value >= 0) {
536 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
537 ctrl->bRequestType, ctrl->bRequest,
538 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700539 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700540 req->length = value;
541 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
542 if (value < 0)
543 ERROR(cdev, "rndis response on err %d\n", value);
544 }
545
546 /* device either stalls (value < 0) or reports success */
547 return value;
548}
549
550
551static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
552{
553 struct f_rndis *rndis = func_to_rndis(f);
554 struct usb_composite_dev *cdev = f->config->cdev;
555
556 /* we know alt == 0 */
557
558 if (intf == rndis->ctrl_id) {
559 if (rndis->notify->driver_data) {
560 VDBG(cdev, "reset rndis control %d\n", intf);
561 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300562 }
563 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700564 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300565 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
566 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700567 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300568 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700569 rndis->notify->driver_data = rndis;
570
571 } else if (intf == rndis->data_id) {
572 struct net_device *net;
573
574 if (rndis->port.in_ep->driver_data) {
575 DBG(cdev, "reset rndis\n");
576 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530577 }
578
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300579 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700580 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300581 if (config_ep_by_speed(cdev->gadget, f,
582 rndis->port.in_ep) ||
583 config_ep_by_speed(cdev->gadget, f,
584 rndis->port.out_ep)) {
585 rndis->port.in_ep->desc = NULL;
586 rndis->port.out_ep->desc = NULL;
587 goto fail;
588 }
David Brownell45fe3b82008-06-19 18:20:04 -0700589 }
590
591 /* Avoid ZLPs; they can be troublesome. */
592 rndis->port.is_zlp_ok = false;
593
594 /* RNDIS should be in the "RNDIS uninitialized" state,
595 * either never activated or after rndis_uninit().
596 *
597 * We don't want data to flow here until a nonzero packet
598 * filter is set, at which point it enters "RNDIS data
599 * initialized" state ... but we do want the endpoints
600 * to be activated. It's a strange little state.
601 *
602 * REVISIT the RNDIS gadget code has done this wrong for a
603 * very long time. We need another call to the link layer
604 * code -- gether_updown(...bool) maybe -- to do it right.
605 */
606 rndis->port.cdc_filter = 0;
607
608 DBG(cdev, "RNDIS RX/TX early activation ... \n");
609 net = gether_connect(&rndis->port);
610 if (IS_ERR(net))
611 return PTR_ERR(net);
612
613 rndis_set_param_dev(rndis->config, net,
614 &rndis->port.cdc_filter);
615 } else
616 goto fail;
617
618 return 0;
619fail:
620 return -EINVAL;
621}
622
623static void rndis_disable(struct usb_function *f)
624{
625 struct f_rndis *rndis = func_to_rndis(f);
626 struct usb_composite_dev *cdev = f->config->cdev;
627
628 if (!rndis->notify->driver_data)
629 return;
630
631 DBG(cdev, "rndis deactivated\n");
632
633 rndis_uninit(rndis->config);
634 gether_disconnect(&rndis->port);
635
636 usb_ep_disable(rndis->notify);
637 rndis->notify->driver_data = NULL;
638}
639
640/*-------------------------------------------------------------------------*/
641
642/*
643 * This isn't quite the same mechanism as CDC Ethernet, since the
644 * notification scheme passes less data, but the same set of link
645 * states must be tested. A key difference is that altsettings are
646 * not used to tell whether the link should send packets or not.
647 */
648
649static void rndis_open(struct gether *geth)
650{
651 struct f_rndis *rndis = func_to_rndis(&geth->func);
652 struct usb_composite_dev *cdev = geth->func.config->cdev;
653
654 DBG(cdev, "%s\n", __func__);
655
656 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
657 bitrate(cdev->gadget) / 100);
658 rndis_signal_connect(rndis->config);
659}
660
661static void rndis_close(struct gether *geth)
662{
663 struct f_rndis *rndis = func_to_rndis(&geth->func);
664
665 DBG(geth->func.config->cdev, "%s\n", __func__);
666
667 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
668 rndis_signal_disconnect(rndis->config);
669}
670
671/*-------------------------------------------------------------------------*/
672
673/* ethernet function driver setup/binding */
674
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200675static int
David Brownell45fe3b82008-06-19 18:20:04 -0700676rndis_bind(struct usb_configuration *c, struct usb_function *f)
677{
678 struct usb_composite_dev *cdev = c->cdev;
679 struct f_rndis *rndis = func_to_rndis(f);
680 int status;
681 struct usb_ep *ep;
682
683 /* allocate instance-specific interface IDs */
684 status = usb_interface_id(c, f);
685 if (status < 0)
686 goto fail;
687 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100688 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700689
690 rndis_control_intf.bInterfaceNumber = status;
691 rndis_union_desc.bMasterInterface0 = status;
692
693 status = usb_interface_id(c, f);
694 if (status < 0)
695 goto fail;
696 rndis->data_id = status;
697
698 rndis_data_intf.bInterfaceNumber = status;
699 rndis_union_desc.bSlaveInterface0 = status;
700
701 status = -ENODEV;
702
703 /* allocate instance-specific endpoints */
704 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
705 if (!ep)
706 goto fail;
707 rndis->port.in_ep = ep;
708 ep->driver_data = cdev; /* claim */
709
710 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
711 if (!ep)
712 goto fail;
713 rndis->port.out_ep = ep;
714 ep->driver_data = cdev; /* claim */
715
716 /* NOTE: a status/notification endpoint is, strictly speaking,
717 * optional. We don't treat it that way though! It's simpler,
718 * and some newer profiles don't treat it as optional.
719 */
720 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
721 if (!ep)
722 goto fail;
723 rndis->notify = ep;
724 ep->driver_data = cdev; /* claim */
725
726 status = -ENOMEM;
727
728 /* allocate notification request and buffer */
729 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
730 if (!rndis->notify_req)
731 goto fail;
732 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
733 if (!rndis->notify_req->buf)
734 goto fail;
735 rndis->notify_req->length = STATUS_BYTECOUNT;
736 rndis->notify_req->context = rndis;
737 rndis->notify_req->complete = rndis_response_complete;
738
739 /* copy descriptors, and track endpoint copies */
740 f->descriptors = usb_copy_descriptors(eth_fs_function);
741 if (!f->descriptors)
742 goto fail;
743
David Brownell45fe3b82008-06-19 18:20:04 -0700744 /* support all relevant hardware speeds... we expect that when
745 * hardware is dual speed, all bulk-capable endpoints work at
746 * both speeds
747 */
748 if (gadget_is_dualspeed(c->cdev->gadget)) {
749 hs_in_desc.bEndpointAddress =
750 fs_in_desc.bEndpointAddress;
751 hs_out_desc.bEndpointAddress =
752 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800753 hs_notify_desc.bEndpointAddress =
754 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700755
756 /* copy descriptors, and track endpoint copies */
757 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
David Brownell45fe3b82008-06-19 18:20:04 -0700758 if (!f->hs_descriptors)
759 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700760 }
761
Paul Zimmerman04617db2011-06-27 14:13:18 -0700762 if (gadget_is_superspeed(c->cdev->gadget)) {
763 ss_in_desc.bEndpointAddress =
764 fs_in_desc.bEndpointAddress;
765 ss_out_desc.bEndpointAddress =
766 fs_out_desc.bEndpointAddress;
767 ss_notify_desc.bEndpointAddress =
768 fs_notify_desc.bEndpointAddress;
769
770 /* copy descriptors, and track endpoint copies */
771 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
772 if (!f->ss_descriptors)
773 goto fail;
774 }
775
David Brownell45fe3b82008-06-19 18:20:04 -0700776 rndis->port.open = rndis_open;
777 rndis->port.close = rndis_close;
778
779 status = rndis_register(rndis_response_available, rndis);
780 if (status < 0)
781 goto fail;
782 rndis->config = status;
783
784 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
785 rndis_set_host_mac(rndis->config, rndis->ethaddr);
786
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800787 if (rndis->manufacturer && rndis->vendorID &&
788 rndis_set_param_vendor(rndis->config, rndis->vendorID,
789 rndis->manufacturer))
790 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700791
792 /* NOTE: all that is done without knowing or caring about
793 * the network link ... which is unavailable to this code
794 * until we're activated via set_alt().
795 */
796
797 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700798 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700799 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
800 rndis->port.in_ep->name, rndis->port.out_ep->name,
801 rndis->notify->name);
802 return 0;
803
804fail:
Paul Zimmerman04617db2011-06-27 14:13:18 -0700805 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
806 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700807 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
808 usb_free_descriptors(f->hs_descriptors);
809 if (f->descriptors)
810 usb_free_descriptors(f->descriptors);
811
812 if (rndis->notify_req) {
813 kfree(rndis->notify_req->buf);
814 usb_ep_free_request(rndis->notify, rndis->notify_req);
815 }
816
817 /* we might as well release our claims on endpoints */
818 if (rndis->notify)
819 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300820 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700821 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300822 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700823 rndis->port.in_ep->driver_data = NULL;
824
825 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
826
827 return status;
828}
829
830static void
831rndis_unbind(struct usb_configuration *c, struct usb_function *f)
832{
833 struct f_rndis *rndis = func_to_rndis(f);
834
835 rndis_deregister(rndis->config);
836 rndis_exit();
Benoit Goby7f141c52011-12-16 20:00:38 -0800837 rndis_string_defs[0].id = 0;
David Brownell45fe3b82008-06-19 18:20:04 -0700838
Paul Zimmerman04617db2011-06-27 14:13:18 -0700839 if (gadget_is_superspeed(c->cdev->gadget))
840 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700841 if (gadget_is_dualspeed(c->cdev->gadget))
842 usb_free_descriptors(f->hs_descriptors);
843 usb_free_descriptors(f->descriptors);
844
845 kfree(rndis->notify_req->buf);
846 usb_ep_free_request(rndis->notify, rndis->notify_req);
847
848 kfree(rndis);
849}
850
851/* Some controllers can't support RNDIS ... */
852static inline bool can_support_rndis(struct usb_configuration *c)
853{
David Brownell45fe3b82008-06-19 18:20:04 -0700854 /* everything else is *presumably* fine */
855 return true;
856}
857
858/**
859 * rndis_bind_config - add RNDIS network link to a configuration
860 * @c: the configuration to support the network link
861 * @ethaddr: a buffer in which the ethernet address of the host side
862 * side of the link was recorded
863 * Context: single threaded during gadget setup
864 *
865 * Returns zero on success, else negative errno.
866 *
867 * Caller must have called @gether_setup(). Caller is also responsible
868 * for calling @gether_cleanup() before module unload.
869 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200870int
871rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700872{
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800873 return rndis_bind_config_vendor(c, ethaddr, 0, NULL);
874}
875
876int
877rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
878 u32 vendorID, const char *manufacturer)
879{
David Brownell45fe3b82008-06-19 18:20:04 -0700880 struct f_rndis *rndis;
881 int status;
882
883 if (!can_support_rndis(c) || !ethaddr)
884 return -EINVAL;
885
Benoit Gobya6ccb732012-01-20 14:42:41 -0800886 /* setup RNDIS itself */
887 status = rndis_init();
888 if (status < 0)
889 return status;
890
David Brownell45fe3b82008-06-19 18:20:04 -0700891 /* maybe allocate device-global string IDs */
892 if (rndis_string_defs[0].id == 0) {
893
David Brownell45fe3b82008-06-19 18:20:04 -0700894 /* control interface label */
895 status = usb_string_id(c->cdev);
896 if (status < 0)
897 return status;
898 rndis_string_defs[0].id = status;
899 rndis_control_intf.iInterface = status;
900
901 /* data interface label */
902 status = usb_string_id(c->cdev);
903 if (status < 0)
904 return status;
905 rndis_string_defs[1].id = status;
906 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100907
908 /* IAD iFunction label */
909 status = usb_string_id(c->cdev);
910 if (status < 0)
911 return status;
912 rndis_string_defs[2].id = status;
913 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700914 }
915
916 /* allocate and initialize one new instance */
917 status = -ENOMEM;
918 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
919 if (!rndis)
920 goto fail;
921
922 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800923 rndis->vendorID = vendorID;
924 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700925
926 /* RNDIS activates when the host changes this filter */
927 rndis->port.cdc_filter = 0;
928
929 /* RNDIS has special (and complex) framing */
930 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
931 rndis->port.wrap = rndis_add_header;
932 rndis->port.unwrap = rndis_rm_hdr;
933
934 rndis->port.func.name = "rndis";
935 rndis->port.func.strings = rndis_strings;
936 /* descriptors are per-instance copies */
937 rndis->port.func.bind = rndis_bind;
938 rndis->port.func.unbind = rndis_unbind;
939 rndis->port.func.set_alt = rndis_set_alt;
940 rndis->port.func.setup = rndis_setup;
941 rndis->port.func.disable = rndis_disable;
942
943 status = usb_add_function(c, &rndis->port.func);
944 if (status) {
945 kfree(rndis);
946fail:
947 rndis_exit();
948 }
949 return status;
950}