aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorBrian Niebuhr <bniebuhr@efjohnson.com>2009-08-14 11:04:22 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2009-09-23 09:46:35 -0400
commit9b39e9ddedeef48569f8aac60a7b4c1fbb127c7d (patch)
treed2cc583190e18fa03298e84093c3346e1646007c /drivers/usb/gadget
parent877accca79b706afe5d78b9a92cf4f22919fb2b0 (diff)
USB: gadget: Add EEM gadget driver
This patch adds a CDC EEM ethernet gadget driver. CDC EEM is a newer USB ethernet specification that uses a simpler interface than the older CDC ECM. This makes CDC EEM usable by a wider set of USB hardware. By default the ethernet gadget will still use CDC ECM/Subset, but kernel configuration and/or a module parameter will allow alternative use of the CDC EEM protocol. Changes since last version: - Brought in missing RNDIS changes that caused compile error - Modified 'sentinel CRC' checking to match EEM host driver Signed-off-by: Brian Niebuhr <bniebuhr@efjohnson.com> Cc: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig26
-rw-r--r--drivers/usb/gadget/ether.c31
-rw-r--r--drivers/usb/gadget/f_eem.c562
-rw-r--r--drivers/usb/gadget/f_rndis.c15
-rw-r--r--drivers/usb/gadget/rndis.c13
-rw-r--r--drivers/usb/gadget/rndis.h3
-rw-r--r--drivers/usb/gadget/u_ether.c85
-rw-r--r--drivers/usb/gadget/u_ether.h12
8 files changed, 699 insertions, 48 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 3537d915d275..4d8ab470f087 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -628,8 +628,8 @@ config USB_ETH
628 tristate "Ethernet Gadget (with CDC Ethernet support)" 628 tristate "Ethernet Gadget (with CDC Ethernet support)"
629 depends on NET 629 depends on NET
630 help 630 help
631 This driver implements Ethernet style communication, in either 631 This driver implements Ethernet style communication, in one of
632 of two ways: 632 several ways:
633 633
634 - The "Communication Device Class" (CDC) Ethernet Control Model. 634 - The "Communication Device Class" (CDC) Ethernet Control Model.
635 That protocol is often avoided with pure Ethernet adapters, in 635 That protocol is often avoided with pure Ethernet adapters, in
@@ -639,7 +639,11 @@ config USB_ETH
639 - On hardware can't implement that protocol, a simple CDC subset 639 - On hardware can't implement that protocol, a simple CDC subset
640 is used, placing fewer demands on USB. 640 is used, placing fewer demands on USB.
641 641
642 RNDIS support is a third option, more demanding than that subset. 642 - CDC Ethernet Emulation Model (EEM) is a newer standard that has
643 a simpler interface that can be used by more USB hardware.
644
645 RNDIS support is an additional option, more demanding than than
646 subset.
643 647
644 Within the USB device, this gadget driver exposes a network device 648 Within the USB device, this gadget driver exposes a network device
645 "usbX", where X depends on what other networking devices you have. 649 "usbX", where X depends on what other networking devices you have.
@@ -672,6 +676,22 @@ config USB_ETH_RNDIS
672 XP, you'll need to download drivers from Microsoft's website; a URL 676 XP, you'll need to download drivers from Microsoft's website; a URL
673 is given in comments found in that info file. 677 is given in comments found in that info file.
674 678
679config USB_ETH_EEM
680 bool "Ethernet Emulation Model (EEM) support"
681 depends on USB_ETH
682 default n
683 help
684 CDC EEM is a newer USB standard that is somewhat simpler than CDC ECM
685 and therefore can be supported by more hardware. Technically ECM and
686 EEM are designed for different applications. The ECM model extends
687 the network interface to the target (e.g. a USB cable modem), and the
688 EEM model is for mobile devices to communicate with hosts using
689 ethernet over USB. For Linux gadgets, however, the interface with
690 the host is the same (a usbX device), so the differences are minimal.
691
692 If you say "y" here, the Ethernet gadget driver will use the EEM
693 protocol rather than ECM. If unsure, say "n".
694
675config USB_GADGETFS 695config USB_GADGETFS
676 tristate "Gadget Filesystem (EXPERIMENTAL)" 696 tristate "Gadget Filesystem (EXPERIMENTAL)"
677 depends on EXPERIMENTAL 697 depends on EXPERIMENTAL
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index bd102f5052ba..f37de283d0ab 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -61,6 +61,11 @@
61 * simpler, Microsoft pushes their own approach: RNDIS. The published 61 * simpler, Microsoft pushes their own approach: RNDIS. The published
62 * RNDIS specs are ambiguous and appear to be incomplete, and are also 62 * RNDIS specs are ambiguous and appear to be incomplete, and are also
63 * needlessly complex. They borrow more from CDC ACM than CDC ECM. 63 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
64 *
65 * While CDC ECM, CDC Subset, and RNDIS are designed to extend the ethernet
66 * interface to the target, CDC EEM was designed to use ethernet over the USB
67 * link between the host and target. CDC EEM is implemented as an alternative
68 * to those other protocols when that communication model is more appropriate
64 */ 69 */
65 70
66#define DRIVER_DESC "Ethernet Gadget" 71#define DRIVER_DESC "Ethernet Gadget"
@@ -114,6 +119,7 @@ static inline bool has_rndis(void)
114#include "f_rndis.c" 119#include "f_rndis.c"
115#include "rndis.c" 120#include "rndis.c"
116#endif 121#endif
122#include "f_eem.c"
117#include "u_ether.c" 123#include "u_ether.c"
118 124
119/*-------------------------------------------------------------------------*/ 125/*-------------------------------------------------------------------------*/
@@ -150,6 +156,10 @@ static inline bool has_rndis(void)
150#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ 156#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
151#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ 157#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
152 158
159/* For EEM gadgets */
160#define EEM_VENDOR_NUM 0x0525 /* INVALID - NEEDS TO BE ALLOCATED */
161#define EEM_PRODUCT_NUM 0xa4a1 /* INVALID - NEEDS TO BE ALLOCATED */
162
153/*-------------------------------------------------------------------------*/ 163/*-------------------------------------------------------------------------*/
154 164
155static struct usb_device_descriptor device_desc = { 165static struct usb_device_descriptor device_desc = {
@@ -246,8 +256,16 @@ static struct usb_configuration rndis_config_driver = {
246 256
247/*-------------------------------------------------------------------------*/ 257/*-------------------------------------------------------------------------*/
248 258
259#ifdef CONFIG_USB_ETH_EEM
260static int use_eem = 1;
261#else
262static int use_eem;
263#endif
264module_param(use_eem, bool, 0);
265MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
266
249/* 267/*
250 * We _always_ have an ECM or CDC Subset configuration. 268 * We _always_ have an ECM, CDC Subset, or EEM configuration.
251 */ 269 */
252static int __init eth_do_config(struct usb_configuration *c) 270static int __init eth_do_config(struct usb_configuration *c)
253{ 271{
@@ -258,7 +276,9 @@ static int __init eth_do_config(struct usb_configuration *c)
258 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 276 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
259 } 277 }
260 278
261 if (can_support_ecm(c->cdev->gadget)) 279 if (use_eem)
280 return eem_bind_config(c);
281 else if (can_support_ecm(c->cdev->gadget))
262 return ecm_bind_config(c, hostaddr); 282 return ecm_bind_config(c, hostaddr);
263 else 283 else
264 return geth_bind_config(c, hostaddr); 284 return geth_bind_config(c, hostaddr);
@@ -286,7 +306,12 @@ static int __init eth_bind(struct usb_composite_dev *cdev)
286 return status; 306 return status;
287 307
288 /* set up main config label and device descriptor */ 308 /* set up main config label and device descriptor */
289 if (can_support_ecm(cdev->gadget)) { 309 if (use_eem) {
310 /* EEM */
311 eth_config_driver.label = "CDC Ethernet (EEM)";
312 device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
313 device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
314 } else if (can_support_ecm(cdev->gadget)) {
290 /* ECM */ 315 /* ECM */
291 eth_config_driver.label = "CDC Ethernet (ECM)"; 316 eth_config_driver.label = "CDC Ethernet (ECM)";
292 } else { 317 } else {
diff --git a/drivers/usb/gadget/f_eem.c b/drivers/usb/gadget/f_eem.c
new file mode 100644
index 000000000000..0a577d5694fd
--- /dev/null
+++ b/drivers/usb/gadget/f_eem.c
@@ -0,0 +1,562 @@
1/*
2 * f_eem.c -- USB CDC Ethernet (EEM) link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 EF Johnson Technologies
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/kernel.h>
24#include <linux/device.h>
25#include <linux/etherdevice.h>
26#include <linux/crc32.h>
27
28#include "u_ether.h"
29
30#define EEM_HLEN 2
31
32/*
33 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
34 * Ethernet link.
35 */
36
37struct eem_ep_descs {
38 struct usb_endpoint_descriptor *in;
39 struct usb_endpoint_descriptor *out;
40};
41
42struct f_eem {
43 struct gether port;
44 u8 ctrl_id;
45
46 struct eem_ep_descs fs;
47 struct eem_ep_descs hs;
48};
49
50static inline struct f_eem *func_to_eem(struct usb_function *f)
51{
52 return container_of(f, struct f_eem, port.func);
53}
54
55/*-------------------------------------------------------------------------*/
56
57/* interface descriptor: */
58
59static struct usb_interface_descriptor eem_intf __initdata = {
60 .bLength = sizeof eem_intf,
61 .bDescriptorType = USB_DT_INTERFACE,
62
63 /* .bInterfaceNumber = DYNAMIC */
64 .bNumEndpoints = 2,
65 .bInterfaceClass = USB_CLASS_COMM,
66 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
67 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
68 /* .iInterface = DYNAMIC */
69};
70
71/* full speed support: */
72
73static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = {
74 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
76
77 .bEndpointAddress = USB_DIR_IN,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
79};
80
81static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
84
85 .bEndpointAddress = USB_DIR_OUT,
86 .bmAttributes = USB_ENDPOINT_XFER_BULK,
87};
88
89static struct usb_descriptor_header *eem_fs_function[] __initdata = {
90 /* CDC EEM control descriptors */
91 (struct usb_descriptor_header *) &eem_intf,
92 (struct usb_descriptor_header *) &eem_fs_in_desc,
93 (struct usb_descriptor_header *) &eem_fs_out_desc,
94 NULL,
95};
96
97/* high speed support: */
98
99static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102
103 .bEndpointAddress = USB_DIR_IN,
104 .bmAttributes = USB_ENDPOINT_XFER_BULK,
105 .wMaxPacketSize = cpu_to_le16(512),
106};
107
108static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = {
109 .bLength = USB_DT_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_ENDPOINT,
111
112 .bEndpointAddress = USB_DIR_OUT,
113 .bmAttributes = USB_ENDPOINT_XFER_BULK,
114 .wMaxPacketSize = cpu_to_le16(512),
115};
116
117static struct usb_descriptor_header *eem_hs_function[] __initdata = {
118 /* CDC EEM control descriptors */
119 (struct usb_descriptor_header *) &eem_intf,
120 (struct usb_descriptor_header *) &eem_hs_in_desc,
121 (struct usb_descriptor_header *) &eem_hs_out_desc,
122 NULL,
123};
124
125/* string descriptors: */
126
127static struct usb_string eem_string_defs[] = {
128 [0].s = "CDC Ethernet Emulation Model (EEM)",
129 { } /* end of list */
130};
131
132static struct usb_gadget_strings eem_string_table = {
133 .language = 0x0409, /* en-us */
134 .strings = eem_string_defs,
135};
136
137static struct usb_gadget_strings *eem_strings[] = {
138 &eem_string_table,
139 NULL,
140};
141
142/*-------------------------------------------------------------------------*/
143
144static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
145{
146 struct usb_composite_dev *cdev = f->config->cdev;
147 int value = -EOPNOTSUPP;
148 u16 w_index = le16_to_cpu(ctrl->wIndex);
149 u16 w_value = le16_to_cpu(ctrl->wValue);
150 u16 w_length = le16_to_cpu(ctrl->wLength);
151
152 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
153 ctrl->bRequestType, ctrl->bRequest,
154 w_value, w_index, w_length);
155
156 /* device either stalls (value < 0) or reports success */
157 return value;
158}
159
160
161static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
162{
163 struct f_eem *eem = func_to_eem(f);
164 struct usb_composite_dev *cdev = f->config->cdev;
165 struct net_device *net;
166
167 /* we know alt == 0, so this is an activation or a reset */
168 if (alt != 0)
169 goto fail;
170
171 if (intf == eem->ctrl_id) {
172
173 if (eem->port.in_ep->driver_data) {
174 DBG(cdev, "reset eem\n");
175 gether_disconnect(&eem->port);
176 }
177
178 if (!eem->port.in) {
179 DBG(cdev, "init eem\n");
180 eem->port.in = ep_choose(cdev->gadget,
181 eem->hs.in, eem->fs.in);
182 eem->port.out = ep_choose(cdev->gadget,
183 eem->hs.out, eem->fs.out);
184 }
185
186 /* zlps should not occur because zero-length EEM packets
187 * will be inserted in those cases where they would occur
188 */
189 eem->port.is_zlp_ok = 1;
190 eem->port.cdc_filter = DEFAULT_FILTER;
191 DBG(cdev, "activate eem\n");
192 net = gether_connect(&eem->port);
193 if (IS_ERR(net))
194 return PTR_ERR(net);
195 } else
196 goto fail;
197
198 return 0;
199fail:
200 return -EINVAL;
201}
202
203static void eem_disable(struct usb_function *f)
204{
205 struct f_eem *eem = func_to_eem(f);
206 struct usb_composite_dev *cdev = f->config->cdev;
207
208 DBG(cdev, "eem deactivated\n");
209
210 if (eem->port.in_ep->driver_data)
211 gether_disconnect(&eem->port);
212}
213
214/*-------------------------------------------------------------------------*/
215
216/* EEM function driver setup/binding */
217
218static int __init
219eem_bind(struct usb_configuration *c, struct usb_function *f)
220{
221 struct usb_composite_dev *cdev = c->cdev;
222 struct f_eem *eem = func_to_eem(f);
223 int status;
224 struct usb_ep *ep;
225
226 /* allocate instance-specific interface IDs */
227 status = usb_interface_id(c, f);
228 if (status < 0)
229 goto fail;
230 eem->ctrl_id = status;
231 eem_intf.bInterfaceNumber = status;
232
233 status = -ENODEV;
234
235 /* allocate instance-specific endpoints */
236 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
237 if (!ep)
238 goto fail;
239 eem->port.in_ep = ep;
240 ep->driver_data = cdev; /* claim */
241
242 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
243 if (!ep)
244 goto fail;
245 eem->port.out_ep = ep;
246 ep->driver_data = cdev; /* claim */
247
248 status = -ENOMEM;
249
250 /* copy descriptors, and track endpoint copies */
251 f->descriptors = usb_copy_descriptors(eem_fs_function);
252 if (!f->descriptors)
253 goto fail;
254
255 eem->fs.in = usb_find_endpoint(eem_fs_function,
256 f->descriptors, &eem_fs_in_desc);
257 eem->fs.out = usb_find_endpoint(eem_fs_function,
258 f->descriptors, &eem_fs_out_desc);
259
260 /* support all relevant hardware speeds... we expect that when
261 * hardware is dual speed, all bulk-capable endpoints work at
262 * both speeds
263 */
264 if (gadget_is_dualspeed(c->cdev->gadget)) {
265 eem_hs_in_desc.bEndpointAddress =
266 eem_fs_in_desc.bEndpointAddress;
267 eem_hs_out_desc.bEndpointAddress =
268 eem_fs_out_desc.bEndpointAddress;
269
270 /* copy descriptors, and track endpoint copies */
271 f->hs_descriptors = usb_copy_descriptors(eem_hs_function);
272 if (!f->hs_descriptors)
273 goto fail;
274
275 eem->hs.in = usb_find_endpoint(eem_hs_function,
276 f->hs_descriptors, &eem_hs_in_desc);
277 eem->hs.out = usb_find_endpoint(eem_hs_function,
278 f->hs_descriptors, &eem_hs_out_desc);
279 }
280
281 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
282 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
283 eem->port.in_ep->name, eem->port.out_ep->name);
284 return 0;
285
286fail:
287 if (f->descriptors)
288 usb_free_descriptors(f->descriptors);
289
290 /* we might as well release our claims on endpoints */
291 if (eem->port.out)
292 eem->port.out_ep->driver_data = NULL;
293 if (eem->port.in)
294 eem->port.in_ep->driver_data = NULL;
295
296 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
297
298 return status;
299}
300
301static void
302eem_unbind(struct usb_configuration *c, struct usb_function *f)
303{
304 struct f_eem *eem = func_to_eem(f);
305
306 DBG(c->cdev, "eem unbind\n");
307
308 if (gadget_is_dualspeed(c->cdev->gadget))
309 usb_free_descriptors(f->hs_descriptors);
310 usb_free_descriptors(f->descriptors);
311 kfree(eem);
312}
313
314static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
315{
316}
317
318/*
319 * Add the EEM header and ethernet checksum.
320 * We currently do not attempt to put multiple ethernet frames
321 * into a single USB transfer
322 */
323static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
324{
325 struct sk_buff *skb2 = NULL;
326 struct usb_ep *in = port->in_ep;
327 int padlen = 0;
328 u16 len = skb->len;
329
330 if (!skb_cloned(skb)) {
331 int headroom = skb_headroom(skb);
332 int tailroom = skb_tailroom(skb);
333
334 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
335 * stick two bytes of zero-length EEM packet on the end.
336 */
337 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
338 padlen += 2;
339
340 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
341 (headroom >= EEM_HLEN))
342 goto done;
343 }
344
345 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
346 dev_kfree_skb_any(skb);
347 skb = skb2;
348 if (!skb)
349 return skb;
350
351done:
352 /* use the "no CRC" option */
353 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
354
355 /* EEM packet header format:
356 * b0..13: length of ethernet frame
357 * b14: bmCRC (0 == sentinel CRC)
358 * b15: bmType (0 == data)
359 */
360 len = skb->len;
361 put_unaligned_le16((len & 0x3FFF) | BIT(14), skb_push(skb, 2));
362
363 /* add a zero-length EEM packet, if needed */
364 if (padlen)
365 put_unaligned_le16(0, skb_put(skb, 2));
366
367 return skb;
368}
369
370/*
371 * Remove the EEM header. Note that there can be many EEM packets in a single
372 * USB transfer, so we need to break them out and handle them independently.
373 */
374static int eem_unwrap(struct gether *port,
375 struct sk_buff *skb,
376 struct sk_buff_head *list)
377{
378 struct usb_composite_dev *cdev = port->func.config->cdev;
379 int status = 0;
380
381 do {
382 struct sk_buff *skb2;
383 u16 header;
384 u16 len = 0;
385
386 if (skb->len < EEM_HLEN) {
387 status = -EINVAL;
388 DBG(cdev, "invalid EEM header\n");
389 goto error;
390 }
391
392 /* remove the EEM header */
393 header = get_unaligned_le16(skb->data);
394 skb_pull(skb, EEM_HLEN);
395
396 /* EEM packet header format:
397 * b0..14: EEM type dependent (data or command)
398 * b15: bmType (0 == data, 1 == command)
399 */
400 if (header & BIT(15)) {
401 struct usb_request *req = cdev->req;
402 u16 bmEEMCmd;
403
404 /* EEM command packet format:
405 * b0..10: bmEEMCmdParam
406 * b11..13: bmEEMCmd
407 * b14: reserved (must be zero)
408 * b15: bmType (1 == command)
409 */
410 if (header & BIT(14))
411 continue;
412
413 bmEEMCmd = (header >> 11) & 0x7;
414 switch (bmEEMCmd) {
415 case 0: /* echo */
416 len = header & 0x7FF;
417 if (skb->len < len) {
418 status = -EOVERFLOW;
419 goto error;
420 }
421
422 skb2 = skb_clone(skb, GFP_ATOMIC);
423 if (unlikely(!skb2)) {
424 DBG(cdev, "EEM echo response error\n");
425 goto next;
426 }
427 skb_trim(skb2, len);
428 put_unaligned_le16(BIT(15) | BIT(11) | len,
429 skb_push(skb2, 2));
430 skb_copy_bits(skb, 0, req->buf, skb->len);
431 req->length = skb->len;
432 req->complete = eem_cmd_complete;
433 req->zero = 1;
434 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
435 DBG(cdev, "echo response queue fail\n");
436 break;
437
438 case 1: /* echo response */
439 case 2: /* suspend hint */
440 case 3: /* response hint */
441 case 4: /* response complete hint */
442 case 5: /* tickle */
443 default: /* reserved */
444 continue;
445 }
446 } else {
447 u32 crc, crc2;
448 struct sk_buff *skb3;
449
450 /* check for zero-length EEM packet */
451 if (header == 0)
452 continue;
453
454 /* EEM data packet format:
455 * b0..13: length of ethernet frame
456 * b14: bmCRC (0 == sentinel, 1 == calculated)
457 * b15: bmType (0 == data)
458 */
459 len = header & 0x3FFF;
460 if ((skb->len < len)
461 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
462 status = -EINVAL;
463 goto error;
464 }
465
466 /* validate CRC */
467 crc = get_unaligned_le32(skb->data + len - ETH_FCS_LEN);
468 if (header & BIT(14)) {
469 crc = get_unaligned_le32(skb->data + len
470 - ETH_FCS_LEN);
471 crc2 = ~crc32_le(~0,
472 skb->data,
473 skb->len - ETH_FCS_LEN);
474 } else {
475 crc = get_unaligned_be32(skb->data + len
476 - ETH_FCS_LEN);
477 crc2 = 0xdeadbeef;
478 }
479 if (crc != crc2) {
480 DBG(cdev, "invalid EEM CRC\n");
481 goto next;
482 }
483
484 skb2 = skb_clone(skb, GFP_ATOMIC);
485 if (unlikely(!skb2)) {
486 DBG(cdev, "unable to unframe EEM packet\n");
487 continue;
488 }
489 skb_trim(skb2, len - ETH_FCS_LEN);
490
491 skb3 = skb_copy_expand(skb2,
492 NET_IP_ALIGN,
493 0,
494 GFP_ATOMIC);
495 if (unlikely(!skb3)) {
496 DBG(cdev, "unable to realign EEM packet\n");
497 dev_kfree_skb_any(skb2);
498 continue;
499 }
500 dev_kfree_skb_any(skb2);
501 skb_queue_tail(list, skb3);
502 }
503next:
504 skb_pull(skb, len);
505 } while (skb->len);
506
507error:
508 dev_kfree_skb_any(skb);
509 return status;
510}
511
512/**
513 * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
514 * @c: the configuration to support the network link
515 * Context: single threaded during gadget setup
516 *
517 * Returns zero on success, else negative errno.
518 *
519 * Caller must have called @gether_setup(). Caller is also responsible
520 * for calling @gether_cleanup() before module unload.
521 */
522int __init eem_bind_config(struct usb_configuration *c)
523{
524 struct f_eem *eem;
525 int status;
526
527 /* maybe allocate device-global string IDs */
528 if (eem_string_defs[0].id == 0) {
529
530 /* control interface label */
531 status = usb_string_id(c->cdev);
532 if (status < 0)
533 return status;
534 eem_string_defs[0].id = status;
535 eem_intf.iInterface = status;
536 }
537
538 /* allocate and initialize one new instance */
539 eem = kzalloc(sizeof *eem, GFP_KERNEL);
540 if (!eem)
541 return -ENOMEM;
542
543 eem->port.cdc_filter = DEFAULT_FILTER;
544
545 eem->port.func.name = "cdc_eem";
546 eem->port.func.strings = eem_strings;
547 /* descriptors are per-instance copies */
548 eem->port.func.bind = eem_bind;
549 eem->port.func.unbind = eem_unbind;
550 eem->port.func.set_alt = eem_set_alt;
551 eem->port.func.setup = eem_setup;
552 eem->port.func.disable = eem_disable;
553 eem->port.wrap = eem_wrap;
554 eem->port.unwrap = eem_unwrap;
555 eem->port.header_len = EEM_HLEN;
556
557 status = usb_add_function(c, &eem->port.func);
558 if (status)
559 kfree(eem);
560 return status;
561}
562
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index 424a37c5773f..c9966cc07d3a 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -286,12 +286,17 @@ static struct usb_gadget_strings *rndis_strings[] = {
286 286
287/*-------------------------------------------------------------------------*/ 287/*-------------------------------------------------------------------------*/
288 288
289static struct sk_buff *rndis_add_header(struct sk_buff *skb) 289static struct sk_buff *rndis_add_header(struct gether *port,
290 struct sk_buff *skb)
290{ 291{
291 skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); 292 struct sk_buff *skb2;
292 if (skb) 293
293 rndis_add_hdr(skb); 294 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
294 return skb; 295 if (skb2)
296 rndis_add_hdr(skb2);
297
298 dev_kfree_skb_any(skb);
299 return skb2;
295} 300}
296 301
297static void rndis_response_available(void *_rndis) 302static void rndis_response_available(void *_rndis)
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c
index ca41b0b5afb3..48267bc0b2e0 100644
--- a/drivers/usb/gadget/rndis.c
+++ b/drivers/usb/gadget/rndis.c
@@ -1022,22 +1022,29 @@ static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1022 return r; 1022 return r;
1023} 1023}
1024 1024
1025int rndis_rm_hdr(struct sk_buff *skb) 1025int rndis_rm_hdr(struct gether *port,
1026 struct sk_buff *skb,
1027 struct sk_buff_head *list)
1026{ 1028{
1027 /* tmp points to a struct rndis_packet_msg_type */ 1029 /* tmp points to a struct rndis_packet_msg_type */
1028 __le32 *tmp = (void *) skb->data; 1030 __le32 *tmp = (void *) skb->data;
1029 1031
1030 /* MessageType, MessageLength */ 1032 /* MessageType, MessageLength */
1031 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG) 1033 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1032 != get_unaligned(tmp++)) 1034 != get_unaligned(tmp++)) {
1035 dev_kfree_skb_any(skb);
1033 return -EINVAL; 1036 return -EINVAL;
1037 }
1034 tmp++; 1038 tmp++;
1035 1039
1036 /* DataOffset, DataLength */ 1040 /* DataOffset, DataLength */
1037 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) 1041 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1042 dev_kfree_skb_any(skb);
1038 return -EOVERFLOW; 1043 return -EOVERFLOW;
1044 }
1039 skb_trim(skb, get_unaligned_le32(tmp++)); 1045 skb_trim(skb, get_unaligned_le32(tmp++));
1040 1046
1047 skb_queue_tail(list, skb);
1041 return 0; 1048 return 0;
1042} 1049}
1043 1050
diff --git a/drivers/usb/gadget/rndis.h b/drivers/usb/gadget/rndis.h
index aac61dfe0f03..c236aaa9dcd1 100644
--- a/drivers/usb/gadget/rndis.h
+++ b/drivers/usb/gadget/rndis.h
@@ -251,7 +251,8 @@ int rndis_set_param_vendor (u8 configNr, u32 vendorID,
251 const char *vendorDescr); 251 const char *vendorDescr);
252int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed); 252int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed);
253void rndis_add_hdr (struct sk_buff *skb); 253void rndis_add_hdr (struct sk_buff *skb);
254int rndis_rm_hdr (struct sk_buff *skb); 254int rndis_rm_hdr(struct gether *port, struct sk_buff *skb,
255 struct sk_buff_head *list);
255u8 *rndis_get_next_response (int configNr, u32 *length); 256u8 *rndis_get_next_response (int configNr, u32 *length);
256void rndis_free_response (int configNr, u8 *buf); 257void rndis_free_response (int configNr, u8 *buf);
257 258
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index c66521953917..f8751ff863cd 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -37,8 +37,9 @@
37 * one (!) network link through the USB gadget stack, normally "usb0". 37 * one (!) network link through the USB gadget stack, normally "usb0".
38 * 38 *
39 * The control and data models are handled by the function driver which 39 * The control and data models are handled by the function driver which
40 * connects to this code; such as CDC Ethernet, "CDC Subset", or RNDIS. 40 * connects to this code; such as CDC Ethernet (ECM or EEM),
41 * That includes all descriptor and endpoint management. 41 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
42 * management.
42 * 43 *
43 * Link level addressing is handled by this component using module 44 * Link level addressing is handled by this component using module
44 * parameters; if no such parameters are provided, random link level 45 * parameters; if no such parameters are provided, random link level
@@ -68,9 +69,13 @@ struct eth_dev {
68 struct list_head tx_reqs, rx_reqs; 69 struct list_head tx_reqs, rx_reqs;
69 atomic_t tx_qlen; 70 atomic_t tx_qlen;
70 71
72 struct sk_buff_head rx_frames;
73
71 unsigned header_len; 74 unsigned header_len;
72 struct sk_buff *(*wrap)(struct sk_buff *skb); 75 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
73 int (*unwrap)(struct sk_buff *skb); 76 int (*unwrap)(struct gether *,
77 struct sk_buff *skb,
78 struct sk_buff_head *list);
74 79
75 struct work_struct work; 80 struct work_struct work;
76 81
@@ -269,7 +274,7 @@ enomem:
269 274
270static void rx_complete(struct usb_ep *ep, struct usb_request *req) 275static void rx_complete(struct usb_ep *ep, struct usb_request *req)
271{ 276{
272 struct sk_buff *skb = req->context; 277 struct sk_buff *skb = req->context, *skb2;
273 struct eth_dev *dev = ep->driver_data; 278 struct eth_dev *dev = ep->driver_data;
274 int status = req->status; 279 int status = req->status;
275 280
@@ -278,26 +283,47 @@ static void rx_complete(struct usb_ep *ep, struct usb_request *req)
278 /* normal completion */ 283 /* normal completion */
279 case 0: 284 case 0:
280 skb_put(skb, req->actual); 285 skb_put(skb, req->actual);
281 if (dev->unwrap)
282 status = dev->unwrap(skb);
283 if (status < 0
284 || ETH_HLEN > skb->len
285 || skb->len > ETH_FRAME_LEN) {
286 dev->net->stats.rx_errors++;
287 dev->net->stats.rx_length_errors++;
288 DBG(dev, "rx length %d\n", skb->len);
289 break;
290 }
291 286
292 skb->protocol = eth_type_trans(skb, dev->net); 287 if (dev->unwrap) {
293 dev->net->stats.rx_packets++; 288 unsigned long flags;
294 dev->net->stats.rx_bytes += skb->len;
295 289
296 /* no buffer copies needed, unless hardware can't 290 spin_lock_irqsave(&dev->lock, flags);
297 * use skb buffers. 291 if (dev->port_usb) {
298 */ 292 status = dev->unwrap(dev->port_usb,
299 status = netif_rx(skb); 293 skb,
294 &dev->rx_frames);
295 } else {
296 dev_kfree_skb_any(skb);
297 status = -ENOTCONN;
298 }
299 spin_unlock_irqrestore(&dev->lock, flags);
300 } else {
301 skb_queue_tail(&dev->rx_frames, skb);
302 }
300 skb = NULL; 303 skb = NULL;
304
305 skb2 = skb_dequeue(&dev->rx_frames);
306 while (skb2) {
307 if (status < 0
308 || ETH_HLEN > skb2->len
309 || skb2->len > ETH_FRAME_LEN) {
310 dev->net->stats.rx_errors++;
311 dev->net->stats.rx_length_errors++;
312 DBG(dev, "rx length %d\n", skb2->len);
313 dev_kfree_skb_any(skb2);
314 goto next_frame;
315 }
316 skb2->protocol = eth_type_trans(skb2, dev->net);
317 dev->net->stats.rx_packets++;
318 dev->net->stats.rx_bytes += skb2->len;
319
320 /* no buffer copies needed, unless hardware can't
321 * use skb buffers.
322 */
323 status = netif_rx(skb2);
324next_frame:
325 skb2 = skb_dequeue(&dev->rx_frames);
326 }
301 break; 327 break;
302 328
303 /* software-driven interface shutdown */ 329 /* software-driven interface shutdown */
@@ -537,14 +563,15 @@ static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
537 * or there's not enough space for extra headers we need 563 * or there's not enough space for extra headers we need
538 */ 564 */
539 if (dev->wrap) { 565 if (dev->wrap) {
540 struct sk_buff *skb_new; 566 unsigned long flags;
541 567
542 skb_new = dev->wrap(skb); 568 spin_lock_irqsave(&dev->lock, flags);
543 if (!skb_new) 569 if (dev->port_usb)
570 skb = dev->wrap(dev->port_usb, skb);
571 spin_unlock_irqrestore(&dev->lock, flags);
572 if (!skb)
544 goto drop; 573 goto drop;
545 574
546 dev_kfree_skb_any(skb);
547 skb = skb_new;
548 length = skb->len; 575 length = skb->len;
549 } 576 }
550 req->buf = skb->data; 577 req->buf = skb->data;
@@ -578,9 +605,9 @@ static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
578 } 605 }
579 606
580 if (retval) { 607 if (retval) {
608 dev_kfree_skb_any(skb);
581drop: 609drop:
582 dev->net->stats.tx_dropped++; 610 dev->net->stats.tx_dropped++;
583 dev_kfree_skb_any(skb);
584 spin_lock_irqsave(&dev->req_lock, flags); 611 spin_lock_irqsave(&dev->req_lock, flags);
585 if (list_empty(&dev->tx_reqs)) 612 if (list_empty(&dev->tx_reqs))
586 netif_start_queue(net); 613 netif_start_queue(net);
@@ -753,6 +780,8 @@ int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
753 INIT_LIST_HEAD(&dev->tx_reqs); 780 INIT_LIST_HEAD(&dev->tx_reqs);
754 INIT_LIST_HEAD(&dev->rx_reqs); 781 INIT_LIST_HEAD(&dev->rx_reqs);
755 782
783 skb_queue_head_init(&dev->rx_frames);
784
756 /* network device setup */ 785 /* network device setup */
757 dev->net = net; 786 dev->net = net;
758 strcpy(net->name, "usb%d"); 787 strcpy(net->name, "usb%d");
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 0d1f7ae3b071..91b39ffdf6ea 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -60,12 +60,13 @@ struct gether {
60 60
61 u16 cdc_filter; 61 u16 cdc_filter;
62 62
63 /* hooks for added framing, as needed for RNDIS and EEM. 63 /* hooks for added framing, as needed for RNDIS and EEM. */
64 * we currently don't support multiple frames per SKB.
65 */
66 u32 header_len; 64 u32 header_len;
67 struct sk_buff *(*wrap)(struct sk_buff *skb); 65 struct sk_buff *(*wrap)(struct gether *port,
68 int (*unwrap)(struct sk_buff *skb); 66 struct sk_buff *skb);
67 int (*unwrap)(struct gether *port,
68 struct sk_buff *skb,
69 struct sk_buff_head *list);
69 70
70 /* called on network open/close */ 71 /* called on network open/close */
71 void (*open)(struct gether *); 72 void (*open)(struct gether *);
@@ -109,6 +110,7 @@ static inline bool can_support_ecm(struct usb_gadget *gadget)
109/* each configuration may bind one instance of an ethernet link */ 110/* each configuration may bind one instance of an ethernet link */
110int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 111int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
111int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 112int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
113int eem_bind_config(struct usb_configuration *c);
112 114
113#ifdef CONFIG_USB_ETH_RNDIS 115#ifdef CONFIG_USB_ETH_RNDIS
114 116