1 // SPDX-License-Identifier: GPL-2.0+
3 * f_eem.c -- USB CDC Ethernet (EEM) link function driver
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2009 EF Johnson Technologies
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/etherdevice.h>
14 #include <linux/crc32.h>
15 #include <linux/slab.h>
18 #include "u_ether_configfs.h"
24 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
38 static inline struct f_eem *func_to_eem(struct usb_function *f)
40 return container_of(f, struct f_eem, port.func);
43 /*-------------------------------------------------------------------------*/
45 /* interface descriptor: */
47 static struct usb_interface_descriptor eem_intf = {
48 .bLength = sizeof eem_intf,
49 .bDescriptorType = USB_DT_INTERFACE,
51 /* .bInterfaceNumber = DYNAMIC */
53 .bInterfaceClass = USB_CLASS_COMM,
54 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
55 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
56 /* .iInterface = DYNAMIC */
59 /* full speed support: */
61 static struct usb_endpoint_descriptor eem_fs_in_desc = {
62 .bLength = USB_DT_ENDPOINT_SIZE,
63 .bDescriptorType = USB_DT_ENDPOINT,
65 .bEndpointAddress = USB_DIR_IN,
66 .bmAttributes = USB_ENDPOINT_XFER_BULK,
69 static struct usb_endpoint_descriptor eem_fs_out_desc = {
70 .bLength = USB_DT_ENDPOINT_SIZE,
71 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = USB_DIR_OUT,
74 .bmAttributes = USB_ENDPOINT_XFER_BULK,
77 static struct usb_descriptor_header *eem_fs_function[] = {
78 /* CDC EEM control descriptors */
79 (struct usb_descriptor_header *) &eem_intf,
80 (struct usb_descriptor_header *) &eem_fs_in_desc,
81 (struct usb_descriptor_header *) &eem_fs_out_desc,
85 /* high speed support: */
87 static struct usb_endpoint_descriptor eem_hs_in_desc = {
88 .bLength = USB_DT_ENDPOINT_SIZE,
89 .bDescriptorType = USB_DT_ENDPOINT,
91 .bEndpointAddress = USB_DIR_IN,
92 .bmAttributes = USB_ENDPOINT_XFER_BULK,
93 .wMaxPacketSize = cpu_to_le16(512),
96 static struct usb_endpoint_descriptor eem_hs_out_desc = {
97 .bLength = USB_DT_ENDPOINT_SIZE,
98 .bDescriptorType = USB_DT_ENDPOINT,
100 .bEndpointAddress = USB_DIR_OUT,
101 .bmAttributes = USB_ENDPOINT_XFER_BULK,
102 .wMaxPacketSize = cpu_to_le16(512),
105 static struct usb_descriptor_header *eem_hs_function[] = {
106 /* CDC EEM control descriptors */
107 (struct usb_descriptor_header *) &eem_intf,
108 (struct usb_descriptor_header *) &eem_hs_in_desc,
109 (struct usb_descriptor_header *) &eem_hs_out_desc,
113 /* super speed support: */
115 static struct usb_endpoint_descriptor eem_ss_in_desc = {
116 .bLength = USB_DT_ENDPOINT_SIZE,
117 .bDescriptorType = USB_DT_ENDPOINT,
119 .bEndpointAddress = USB_DIR_IN,
120 .bmAttributes = USB_ENDPOINT_XFER_BULK,
121 .wMaxPacketSize = cpu_to_le16(1024),
124 static struct usb_endpoint_descriptor eem_ss_out_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
128 .bEndpointAddress = USB_DIR_OUT,
129 .bmAttributes = USB_ENDPOINT_XFER_BULK,
130 .wMaxPacketSize = cpu_to_le16(1024),
133 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = {
134 .bLength = sizeof eem_ss_bulk_comp_desc,
135 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
137 /* the following 2 values can be tweaked if necessary */
138 /* .bMaxBurst = 0, */
139 /* .bmAttributes = 0, */
142 static struct usb_descriptor_header *eem_ss_function[] = {
143 /* CDC EEM control descriptors */
144 (struct usb_descriptor_header *) &eem_intf,
145 (struct usb_descriptor_header *) &eem_ss_in_desc,
146 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
147 (struct usb_descriptor_header *) &eem_ss_out_desc,
148 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
152 /* string descriptors: */
154 static struct usb_string eem_string_defs[] = {
155 [0].s = "CDC Ethernet Emulation Model (EEM)",
156 { } /* end of list */
159 static struct usb_gadget_strings eem_string_table = {
160 .language = 0x0409, /* en-us */
161 .strings = eem_string_defs,
164 static struct usb_gadget_strings *eem_strings[] = {
169 /*-------------------------------------------------------------------------*/
171 static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
173 struct usb_composite_dev *cdev = f->config->cdev;
174 int value = -EOPNOTSUPP;
175 u16 w_index = le16_to_cpu(ctrl->wIndex);
176 u16 w_value = le16_to_cpu(ctrl->wValue);
177 u16 w_length = le16_to_cpu(ctrl->wLength);
179 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
180 ctrl->bRequestType, ctrl->bRequest,
181 w_value, w_index, w_length);
183 /* device either stalls (value < 0) or reports success */
188 static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
190 struct f_eem *eem = func_to_eem(f);
191 struct usb_composite_dev *cdev = f->config->cdev;
192 struct net_device *net;
194 /* we know alt == 0, so this is an activation or a reset */
198 if (intf == eem->ctrl_id) {
199 DBG(cdev, "reset eem\n");
200 gether_disconnect(&eem->port);
202 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
203 DBG(cdev, "init eem\n");
204 if (config_ep_by_speed(cdev->gadget, f,
206 config_ep_by_speed(cdev->gadget, f,
208 eem->port.in_ep->desc = NULL;
209 eem->port.out_ep->desc = NULL;
214 /* zlps should not occur because zero-length EEM packets
215 * will be inserted in those cases where they would occur
217 eem->port.is_zlp_ok = 1;
218 eem->port.cdc_filter = DEFAULT_FILTER;
219 DBG(cdev, "activate eem\n");
220 net = gether_connect(&eem->port);
231 static void eem_disable(struct usb_function *f)
233 struct f_eem *eem = func_to_eem(f);
234 struct usb_composite_dev *cdev = f->config->cdev;
236 DBG(cdev, "eem deactivated\n");
238 if (eem->port.in_ep->enabled)
239 gether_disconnect(&eem->port);
242 /*-------------------------------------------------------------------------*/
244 /* EEM function driver setup/binding */
246 static int eem_bind(struct usb_configuration *c, struct usb_function *f)
248 struct usb_composite_dev *cdev = c->cdev;
249 struct f_eem *eem = func_to_eem(f);
250 struct usb_string *us;
254 struct f_eem_opts *eem_opts;
256 eem_opts = container_of(f->fi, struct f_eem_opts, func_inst);
258 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
259 * configurations are bound in sequence with list_for_each_entry,
260 * in each configuration its functions are bound in sequence
261 * with list_for_each_entry, so we assume no race condition
262 * with regard to eem_opts->bound access
264 if (!eem_opts->bound) {
265 mutex_lock(&eem_opts->lock);
266 gether_set_gadget(eem_opts->net, cdev->gadget);
267 status = gether_register_netdev(eem_opts->net);
268 mutex_unlock(&eem_opts->lock);
271 eem_opts->bound = true;
274 us = usb_gstrings_attach(cdev, eem_strings,
275 ARRAY_SIZE(eem_string_defs));
278 eem_intf.iInterface = us[0].id;
280 /* allocate instance-specific interface IDs */
281 status = usb_interface_id(c, f);
284 eem->ctrl_id = status;
285 eem_intf.bInterfaceNumber = status;
289 /* allocate instance-specific endpoints */
290 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
293 eem->port.in_ep = ep;
295 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
298 eem->port.out_ep = ep;
302 /* support all relevant hardware speeds... we expect that when
303 * hardware is dual speed, all bulk-capable endpoints work at
306 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
307 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
309 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
310 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
312 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
313 eem_ss_function, eem_ss_function);
317 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
318 gadget_is_superspeed(c->cdev->gadget) ? "super" :
319 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
320 eem->port.in_ep->name, eem->port.out_ep->name);
324 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
329 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
331 struct in_context *ctx = req->context;
333 dev_kfree_skb_any(ctx->skb);
335 usb_ep_free_request(ctx->ep, req);
340 * Add the EEM header and ethernet checksum.
341 * We currently do not attempt to put multiple ethernet frames
342 * into a single USB transfer
344 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
346 struct sk_buff *skb2 = NULL;
347 struct usb_ep *in = port->in_ep;
348 int headroom, tailroom, padlen = 0;
355 headroom = skb_headroom(skb);
356 tailroom = skb_tailroom(skb);
358 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
359 * stick two bytes of zero-length EEM packet on the end.
361 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
364 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
365 (headroom >= EEM_HLEN) && !skb_cloned(skb))
368 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
369 dev_kfree_skb_any(skb);
375 /* use the "no CRC" option */
376 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
378 /* EEM packet header format:
379 * b0..13: length of ethernet frame
380 * b14: bmCRC (0 == sentinel CRC)
381 * b15: bmType (0 == data)
384 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
386 /* add a zero-length EEM packet, if needed */
388 put_unaligned_le16(0, skb_put(skb, 2));
394 * Remove the EEM header. Note that there can be many EEM packets in a single
395 * USB transfer, so we need to break them out and handle them independently.
397 static int eem_unwrap(struct gether *port,
399 struct sk_buff_head *list)
401 struct usb_composite_dev *cdev = port->func.config->cdev;
405 struct sk_buff *skb2;
409 if (skb->len < EEM_HLEN) {
411 DBG(cdev, "invalid EEM header\n");
415 /* remove the EEM header */
416 header = get_unaligned_le16(skb->data);
417 skb_pull(skb, EEM_HLEN);
419 /* EEM packet header format:
420 * b0..14: EEM type dependent (data or command)
421 * b15: bmType (0 == data, 1 == command)
423 if (header & BIT(15)) {
424 struct usb_request *req;
425 struct in_context *ctx;
429 /* EEM command packet format:
430 * b0..10: bmEEMCmdParam
432 * b14: reserved (must be zero)
433 * b15: bmType (1 == command)
435 if (header & BIT(14))
438 bmEEMCmd = (header >> 11) & 0x7;
441 len = header & 0x7FF;
442 if (skb->len < len) {
447 skb2 = skb_clone(skb, GFP_ATOMIC);
448 if (unlikely(!skb2)) {
449 DBG(cdev, "EEM echo response error\n");
453 put_unaligned_le16(BIT(15) | BIT(11) | len,
457 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
459 dev_kfree_skb_any(skb2);
463 req->buf = kmalloc(skb2->len, GFP_KERNEL);
465 usb_ep_free_request(ep, req);
466 dev_kfree_skb_any(skb2);
470 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
473 usb_ep_free_request(ep, req);
474 dev_kfree_skb_any(skb2);
480 skb_copy_bits(skb2, 0, req->buf, skb2->len);
481 req->length = skb2->len;
482 req->complete = eem_cmd_complete;
485 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
486 DBG(cdev, "echo response queue fail\n");
489 case 1: /* echo response */
490 case 2: /* suspend hint */
491 case 3: /* response hint */
492 case 4: /* response complete hint */
494 default: /* reserved */
499 struct sk_buff *skb3;
501 /* check for zero-length EEM packet */
505 /* EEM data packet format:
506 * b0..13: length of ethernet frame
507 * b14: bmCRC (0 == sentinel, 1 == calculated)
508 * b15: bmType (0 == data)
510 len = header & 0x3FFF;
512 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
518 if (header & BIT(14)) {
519 crc = get_unaligned_le32(skb->data + len
522 skb->data, len - ETH_FCS_LEN);
524 crc = get_unaligned_be32(skb->data + len
529 DBG(cdev, "invalid EEM CRC\n");
533 skb2 = skb_clone(skb, GFP_ATOMIC);
534 if (unlikely(!skb2)) {
535 DBG(cdev, "unable to unframe EEM packet\n");
538 skb_trim(skb2, len - ETH_FCS_LEN);
540 skb3 = skb_copy_expand(skb2,
544 if (unlikely(!skb3)) {
545 dev_kfree_skb_any(skb2);
548 dev_kfree_skb_any(skb2);
549 skb_queue_tail(list, skb3);
556 dev_kfree_skb_any(skb);
560 static inline struct f_eem_opts *to_f_eem_opts(struct config_item *item)
562 return container_of(to_config_group(item), struct f_eem_opts,
567 USB_ETHERNET_CONFIGFS_ITEM(eem);
569 /* f_eem_opts_dev_addr */
570 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem);
572 /* f_eem_opts_host_addr */
573 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem);
575 /* f_eem_opts_qmult */
576 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem);
578 /* f_eem_opts_ifname */
579 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem);
581 static struct configfs_attribute *eem_attrs[] = {
582 &eem_opts_attr_dev_addr,
583 &eem_opts_attr_host_addr,
584 &eem_opts_attr_qmult,
585 &eem_opts_attr_ifname,
589 static const struct config_item_type eem_func_type = {
590 .ct_item_ops = &eem_item_ops,
591 .ct_attrs = eem_attrs,
592 .ct_owner = THIS_MODULE,
595 static void eem_free_inst(struct usb_function_instance *f)
597 struct f_eem_opts *opts;
599 opts = container_of(f, struct f_eem_opts, func_inst);
601 gether_cleanup(netdev_priv(opts->net));
603 free_netdev(opts->net);
607 static struct usb_function_instance *eem_alloc_inst(void)
609 struct f_eem_opts *opts;
611 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
613 return ERR_PTR(-ENOMEM);
614 mutex_init(&opts->lock);
615 opts->func_inst.free_func_inst = eem_free_inst;
616 opts->net = gether_setup_default();
617 if (IS_ERR(opts->net)) {
618 struct net_device *net = opts->net;
620 return ERR_CAST(net);
623 config_group_init_type_name(&opts->func_inst.group, "", &eem_func_type);
625 return &opts->func_inst;
628 static void eem_free(struct usb_function *f)
631 struct f_eem_opts *opts;
633 eem = func_to_eem(f);
634 opts = container_of(f->fi, struct f_eem_opts, func_inst);
636 mutex_lock(&opts->lock);
638 mutex_unlock(&opts->lock);
641 static void eem_unbind(struct usb_configuration *c, struct usb_function *f)
643 DBG(c->cdev, "eem unbind\n");
645 usb_free_all_descriptors(f);
648 static struct usb_function *eem_alloc(struct usb_function_instance *fi)
651 struct f_eem_opts *opts;
653 /* allocate and initialize one new instance */
654 eem = kzalloc(sizeof(*eem), GFP_KERNEL);
656 return ERR_PTR(-ENOMEM);
658 opts = container_of(fi, struct f_eem_opts, func_inst);
659 mutex_lock(&opts->lock);
662 eem->port.ioport = netdev_priv(opts->net);
663 mutex_unlock(&opts->lock);
664 eem->port.cdc_filter = DEFAULT_FILTER;
666 eem->port.func.name = "cdc_eem";
667 /* descriptors are per-instance copies */
668 eem->port.func.bind = eem_bind;
669 eem->port.func.unbind = eem_unbind;
670 eem->port.func.set_alt = eem_set_alt;
671 eem->port.func.setup = eem_setup;
672 eem->port.func.disable = eem_disable;
673 eem->port.func.free_func = eem_free;
674 eem->port.wrap = eem_wrap;
675 eem->port.unwrap = eem_unwrap;
676 eem->port.header_len = EEM_HLEN;
678 return &eem->port.func;
681 DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc);
682 MODULE_LICENSE("GPL");
683 MODULE_AUTHOR("David Brownell");