Loading...
Note: File does not exist in v6.8.
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#include <linux/slab.h>
28
29#include "u_ether.h"
30
31#define EEM_HLEN 2
32
33/*
34 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
35 * Ethernet link.
36 */
37
38struct f_eem {
39 struct gether port;
40 u8 ctrl_id;
41};
42
43static inline struct f_eem *func_to_eem(struct usb_function *f)
44{
45 return container_of(f, struct f_eem, port.func);
46}
47
48/*-------------------------------------------------------------------------*/
49
50/* interface descriptor: */
51
52static struct usb_interface_descriptor eem_intf __initdata = {
53 .bLength = sizeof eem_intf,
54 .bDescriptorType = USB_DT_INTERFACE,
55
56 /* .bInterfaceNumber = DYNAMIC */
57 .bNumEndpoints = 2,
58 .bInterfaceClass = USB_CLASS_COMM,
59 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
60 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
61 /* .iInterface = DYNAMIC */
62};
63
64/* full speed support: */
65
66static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = {
67 .bLength = USB_DT_ENDPOINT_SIZE,
68 .bDescriptorType = USB_DT_ENDPOINT,
69
70 .bEndpointAddress = USB_DIR_IN,
71 .bmAttributes = USB_ENDPOINT_XFER_BULK,
72};
73
74static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = {
75 .bLength = USB_DT_ENDPOINT_SIZE,
76 .bDescriptorType = USB_DT_ENDPOINT,
77
78 .bEndpointAddress = USB_DIR_OUT,
79 .bmAttributes = USB_ENDPOINT_XFER_BULK,
80};
81
82static struct usb_descriptor_header *eem_fs_function[] __initdata = {
83 /* CDC EEM control descriptors */
84 (struct usb_descriptor_header *) &eem_intf,
85 (struct usb_descriptor_header *) &eem_fs_in_desc,
86 (struct usb_descriptor_header *) &eem_fs_out_desc,
87 NULL,
88};
89
90/* high speed support: */
91
92static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = {
93 .bLength = USB_DT_ENDPOINT_SIZE,
94 .bDescriptorType = USB_DT_ENDPOINT,
95
96 .bEndpointAddress = USB_DIR_IN,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 .wMaxPacketSize = cpu_to_le16(512),
99};
100
101static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = {
102 .bLength = USB_DT_ENDPOINT_SIZE,
103 .bDescriptorType = USB_DT_ENDPOINT,
104
105 .bEndpointAddress = USB_DIR_OUT,
106 .bmAttributes = USB_ENDPOINT_XFER_BULK,
107 .wMaxPacketSize = cpu_to_le16(512),
108};
109
110static struct usb_descriptor_header *eem_hs_function[] __initdata = {
111 /* CDC EEM control descriptors */
112 (struct usb_descriptor_header *) &eem_intf,
113 (struct usb_descriptor_header *) &eem_hs_in_desc,
114 (struct usb_descriptor_header *) &eem_hs_out_desc,
115 NULL,
116};
117
118/* super speed support: */
119
120static struct usb_endpoint_descriptor eem_ss_in_desc __initdata = {
121 .bLength = USB_DT_ENDPOINT_SIZE,
122 .bDescriptorType = USB_DT_ENDPOINT,
123
124 .bEndpointAddress = USB_DIR_IN,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = cpu_to_le16(1024),
127};
128
129static struct usb_endpoint_descriptor eem_ss_out_desc __initdata = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132
133 .bEndpointAddress = USB_DIR_OUT,
134 .bmAttributes = USB_ENDPOINT_XFER_BULK,
135 .wMaxPacketSize = cpu_to_le16(1024),
136};
137
138static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc __initdata = {
139 .bLength = sizeof eem_ss_bulk_comp_desc,
140 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
141
142 /* the following 2 values can be tweaked if necessary */
143 /* .bMaxBurst = 0, */
144 /* .bmAttributes = 0, */
145};
146
147static struct usb_descriptor_header *eem_ss_function[] __initdata = {
148 /* CDC EEM control descriptors */
149 (struct usb_descriptor_header *) &eem_intf,
150 (struct usb_descriptor_header *) &eem_ss_in_desc,
151 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
152 (struct usb_descriptor_header *) &eem_ss_out_desc,
153 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
154 NULL,
155};
156
157/* string descriptors: */
158
159static struct usb_string eem_string_defs[] = {
160 [0].s = "CDC Ethernet Emulation Model (EEM)",
161 { } /* end of list */
162};
163
164static struct usb_gadget_strings eem_string_table = {
165 .language = 0x0409, /* en-us */
166 .strings = eem_string_defs,
167};
168
169static struct usb_gadget_strings *eem_strings[] = {
170 &eem_string_table,
171 NULL,
172};
173
174/*-------------------------------------------------------------------------*/
175
176static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
177{
178 struct usb_composite_dev *cdev = f->config->cdev;
179 int value = -EOPNOTSUPP;
180 u16 w_index = le16_to_cpu(ctrl->wIndex);
181 u16 w_value = le16_to_cpu(ctrl->wValue);
182 u16 w_length = le16_to_cpu(ctrl->wLength);
183
184 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
185 ctrl->bRequestType, ctrl->bRequest,
186 w_value, w_index, w_length);
187
188 /* device either stalls (value < 0) or reports success */
189 return value;
190}
191
192
193static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
194{
195 struct f_eem *eem = func_to_eem(f);
196 struct usb_composite_dev *cdev = f->config->cdev;
197 struct net_device *net;
198
199 /* we know alt == 0, so this is an activation or a reset */
200 if (alt != 0)
201 goto fail;
202
203 if (intf == eem->ctrl_id) {
204
205 if (eem->port.in_ep->driver_data) {
206 DBG(cdev, "reset eem\n");
207 gether_disconnect(&eem->port);
208 }
209
210 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
211 DBG(cdev, "init eem\n");
212 if (config_ep_by_speed(cdev->gadget, f,
213 eem->port.in_ep) ||
214 config_ep_by_speed(cdev->gadget, f,
215 eem->port.out_ep)) {
216 eem->port.in_ep->desc = NULL;
217 eem->port.out_ep->desc = NULL;
218 goto fail;
219 }
220 }
221
222 /* zlps should not occur because zero-length EEM packets
223 * will be inserted in those cases where they would occur
224 */
225 eem->port.is_zlp_ok = 1;
226 eem->port.cdc_filter = DEFAULT_FILTER;
227 DBG(cdev, "activate eem\n");
228 net = gether_connect(&eem->port);
229 if (IS_ERR(net))
230 return PTR_ERR(net);
231 } else
232 goto fail;
233
234 return 0;
235fail:
236 return -EINVAL;
237}
238
239static void eem_disable(struct usb_function *f)
240{
241 struct f_eem *eem = func_to_eem(f);
242 struct usb_composite_dev *cdev = f->config->cdev;
243
244 DBG(cdev, "eem deactivated\n");
245
246 if (eem->port.in_ep->driver_data)
247 gether_disconnect(&eem->port);
248}
249
250/*-------------------------------------------------------------------------*/
251
252/* EEM function driver setup/binding */
253
254static int __init
255eem_bind(struct usb_configuration *c, struct usb_function *f)
256{
257 struct usb_composite_dev *cdev = c->cdev;
258 struct f_eem *eem = func_to_eem(f);
259 int status;
260 struct usb_ep *ep;
261
262 /* allocate instance-specific interface IDs */
263 status = usb_interface_id(c, f);
264 if (status < 0)
265 goto fail;
266 eem->ctrl_id = status;
267 eem_intf.bInterfaceNumber = status;
268
269 status = -ENODEV;
270
271 /* allocate instance-specific endpoints */
272 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
273 if (!ep)
274 goto fail;
275 eem->port.in_ep = ep;
276 ep->driver_data = cdev; /* claim */
277
278 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
279 if (!ep)
280 goto fail;
281 eem->port.out_ep = ep;
282 ep->driver_data = cdev; /* claim */
283
284 status = -ENOMEM;
285
286 /* copy descriptors, and track endpoint copies */
287 f->descriptors = usb_copy_descriptors(eem_fs_function);
288 if (!f->descriptors)
289 goto fail;
290
291 /* support all relevant hardware speeds... we expect that when
292 * hardware is dual speed, all bulk-capable endpoints work at
293 * both speeds
294 */
295 if (gadget_is_dualspeed(c->cdev->gadget)) {
296 eem_hs_in_desc.bEndpointAddress =
297 eem_fs_in_desc.bEndpointAddress;
298 eem_hs_out_desc.bEndpointAddress =
299 eem_fs_out_desc.bEndpointAddress;
300
301 /* copy descriptors, and track endpoint copies */
302 f->hs_descriptors = usb_copy_descriptors(eem_hs_function);
303 if (!f->hs_descriptors)
304 goto fail;
305 }
306
307 if (gadget_is_superspeed(c->cdev->gadget)) {
308 eem_ss_in_desc.bEndpointAddress =
309 eem_fs_in_desc.bEndpointAddress;
310 eem_ss_out_desc.bEndpointAddress =
311 eem_fs_out_desc.bEndpointAddress;
312
313 /* copy descriptors, and track endpoint copies */
314 f->ss_descriptors = usb_copy_descriptors(eem_ss_function);
315 if (!f->ss_descriptors)
316 goto fail;
317 }
318
319 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
320 gadget_is_superspeed(c->cdev->gadget) ? "super" :
321 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
322 eem->port.in_ep->name, eem->port.out_ep->name);
323 return 0;
324
325fail:
326 if (f->descriptors)
327 usb_free_descriptors(f->descriptors);
328 if (f->hs_descriptors)
329 usb_free_descriptors(f->hs_descriptors);
330
331 /* we might as well release our claims on endpoints */
332 if (eem->port.out_ep->desc)
333 eem->port.out_ep->driver_data = NULL;
334 if (eem->port.in_ep->desc)
335 eem->port.in_ep->driver_data = NULL;
336
337 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
338
339 return status;
340}
341
342static void
343eem_unbind(struct usb_configuration *c, struct usb_function *f)
344{
345 struct f_eem *eem = func_to_eem(f);
346
347 DBG(c->cdev, "eem unbind\n");
348
349 if (gadget_is_superspeed(c->cdev->gadget))
350 usb_free_descriptors(f->ss_descriptors);
351 if (gadget_is_dualspeed(c->cdev->gadget))
352 usb_free_descriptors(f->hs_descriptors);
353 usb_free_descriptors(f->descriptors);
354 kfree(eem);
355}
356
357static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
358{
359 struct sk_buff *skb = (struct sk_buff *)req->context;
360
361 dev_kfree_skb_any(skb);
362}
363
364/*
365 * Add the EEM header and ethernet checksum.
366 * We currently do not attempt to put multiple ethernet frames
367 * into a single USB transfer
368 */
369static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
370{
371 struct sk_buff *skb2 = NULL;
372 struct usb_ep *in = port->in_ep;
373 int padlen = 0;
374 u16 len = skb->len;
375
376 if (!skb_cloned(skb)) {
377 int headroom = skb_headroom(skb);
378 int tailroom = skb_tailroom(skb);
379
380 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
381 * stick two bytes of zero-length EEM packet on the end.
382 */
383 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
384 padlen += 2;
385
386 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
387 (headroom >= EEM_HLEN))
388 goto done;
389 }
390
391 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
392 dev_kfree_skb_any(skb);
393 skb = skb2;
394 if (!skb)
395 return skb;
396
397done:
398 /* use the "no CRC" option */
399 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
400
401 /* EEM packet header format:
402 * b0..13: length of ethernet frame
403 * b14: bmCRC (0 == sentinel CRC)
404 * b15: bmType (0 == data)
405 */
406 len = skb->len;
407 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
408
409 /* add a zero-length EEM packet, if needed */
410 if (padlen)
411 put_unaligned_le16(0, skb_put(skb, 2));
412
413 return skb;
414}
415
416/*
417 * Remove the EEM header. Note that there can be many EEM packets in a single
418 * USB transfer, so we need to break them out and handle them independently.
419 */
420static int eem_unwrap(struct gether *port,
421 struct sk_buff *skb,
422 struct sk_buff_head *list)
423{
424 struct usb_composite_dev *cdev = port->func.config->cdev;
425 int status = 0;
426
427 do {
428 struct sk_buff *skb2;
429 u16 header;
430 u16 len = 0;
431
432 if (skb->len < EEM_HLEN) {
433 status = -EINVAL;
434 DBG(cdev, "invalid EEM header\n");
435 goto error;
436 }
437
438 /* remove the EEM header */
439 header = get_unaligned_le16(skb->data);
440 skb_pull(skb, EEM_HLEN);
441
442 /* EEM packet header format:
443 * b0..14: EEM type dependent (data or command)
444 * b15: bmType (0 == data, 1 == command)
445 */
446 if (header & BIT(15)) {
447 struct usb_request *req = cdev->req;
448 u16 bmEEMCmd;
449
450 /* EEM command packet format:
451 * b0..10: bmEEMCmdParam
452 * b11..13: bmEEMCmd
453 * b14: reserved (must be zero)
454 * b15: bmType (1 == command)
455 */
456 if (header & BIT(14))
457 continue;
458
459 bmEEMCmd = (header >> 11) & 0x7;
460 switch (bmEEMCmd) {
461 case 0: /* echo */
462 len = header & 0x7FF;
463 if (skb->len < len) {
464 status = -EOVERFLOW;
465 goto error;
466 }
467
468 skb2 = skb_clone(skb, GFP_ATOMIC);
469 if (unlikely(!skb2)) {
470 DBG(cdev, "EEM echo response error\n");
471 goto next;
472 }
473 skb_trim(skb2, len);
474 put_unaligned_le16(BIT(15) | BIT(11) | len,
475 skb_push(skb2, 2));
476 skb_copy_bits(skb2, 0, req->buf, skb2->len);
477 req->length = skb2->len;
478 req->complete = eem_cmd_complete;
479 req->zero = 1;
480 req->context = skb2;
481 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
482 DBG(cdev, "echo response queue fail\n");
483 break;
484
485 case 1: /* echo response */
486 case 2: /* suspend hint */
487 case 3: /* response hint */
488 case 4: /* response complete hint */
489 case 5: /* tickle */
490 default: /* reserved */
491 continue;
492 }
493 } else {
494 u32 crc, crc2;
495 struct sk_buff *skb3;
496
497 /* check for zero-length EEM packet */
498 if (header == 0)
499 continue;
500
501 /* EEM data packet format:
502 * b0..13: length of ethernet frame
503 * b14: bmCRC (0 == sentinel, 1 == calculated)
504 * b15: bmType (0 == data)
505 */
506 len = header & 0x3FFF;
507 if ((skb->len < len)
508 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
509 status = -EINVAL;
510 goto error;
511 }
512
513 /* validate CRC */
514 if (header & BIT(14)) {
515 crc = get_unaligned_le32(skb->data + len
516 - ETH_FCS_LEN);
517 crc2 = ~crc32_le(~0,
518 skb->data, len - ETH_FCS_LEN);
519 } else {
520 crc = get_unaligned_be32(skb->data + len
521 - ETH_FCS_LEN);
522 crc2 = 0xdeadbeef;
523 }
524 if (crc != crc2) {
525 DBG(cdev, "invalid EEM CRC\n");
526 goto next;
527 }
528
529 skb2 = skb_clone(skb, GFP_ATOMIC);
530 if (unlikely(!skb2)) {
531 DBG(cdev, "unable to unframe EEM packet\n");
532 continue;
533 }
534 skb_trim(skb2, len - ETH_FCS_LEN);
535
536 skb3 = skb_copy_expand(skb2,
537 NET_IP_ALIGN,
538 0,
539 GFP_ATOMIC);
540 if (unlikely(!skb3)) {
541 DBG(cdev, "unable to realign EEM packet\n");
542 dev_kfree_skb_any(skb2);
543 continue;
544 }
545 dev_kfree_skb_any(skb2);
546 skb_queue_tail(list, skb3);
547 }
548next:
549 skb_pull(skb, len);
550 } while (skb->len);
551
552error:
553 dev_kfree_skb_any(skb);
554 return status;
555}
556
557/**
558 * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
559 * @c: the configuration to support the network link
560 * Context: single threaded during gadget setup
561 *
562 * Returns zero on success, else negative errno.
563 *
564 * Caller must have called @gether_setup(). Caller is also responsible
565 * for calling @gether_cleanup() before module unload.
566 */
567int __init eem_bind_config(struct usb_configuration *c)
568{
569 struct f_eem *eem;
570 int status;
571
572 /* maybe allocate device-global string IDs */
573 if (eem_string_defs[0].id == 0) {
574
575 /* control interface label */
576 status = usb_string_id(c->cdev);
577 if (status < 0)
578 return status;
579 eem_string_defs[0].id = status;
580 eem_intf.iInterface = status;
581 }
582
583 /* allocate and initialize one new instance */
584 eem = kzalloc(sizeof *eem, GFP_KERNEL);
585 if (!eem)
586 return -ENOMEM;
587
588 eem->port.cdc_filter = DEFAULT_FILTER;
589
590 eem->port.func.name = "cdc_eem";
591 eem->port.func.strings = eem_strings;
592 /* descriptors are per-instance copies */
593 eem->port.func.bind = eem_bind;
594 eem->port.func.unbind = eem_unbind;
595 eem->port.func.set_alt = eem_set_alt;
596 eem->port.func.setup = eem_setup;
597 eem->port.func.disable = eem_disable;
598 eem->port.wrap = eem_wrap;
599 eem->port.unwrap = eem_unwrap;
600 eem->port.header_len = EEM_HLEN;
601
602 status = usb_add_function(c, &eem->port.func);
603 if (status)
604 kfree(eem);
605 return status;
606}
607