2 * f_eem.c -- USB CDC Ethernet (EEM) link function driver
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 EF Johnson Technologies
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.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/etherdevice.h>
18 #include <linux/crc32.h>
19 #include <linux/slab.h>
22 #include "u_ether_configfs.h"
28 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
42 static inline struct f_eem *func_to_eem(struct usb_function *f)
44 return container_of(f, struct f_eem, port.func);
47 /*-------------------------------------------------------------------------*/
49 /* interface descriptor: */
51 static struct usb_interface_descriptor eem_intf = {
52 .bLength = sizeof eem_intf,
53 .bDescriptorType = USB_DT_INTERFACE,
55 /* .bInterfaceNumber = DYNAMIC */
57 .bInterfaceClass = USB_CLASS_COMM,
58 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
59 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
60 /* .iInterface = DYNAMIC */
63 /* full speed support: */
65 static struct usb_endpoint_descriptor eem_fs_in_desc = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = USB_DIR_IN,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
73 static struct usb_endpoint_descriptor eem_fs_out_desc = {
74 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
77 .bEndpointAddress = USB_DIR_OUT,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
81 static struct usb_descriptor_header *eem_fs_function[] = {
82 /* CDC EEM control descriptors */
83 (struct usb_descriptor_header *) &eem_intf,
84 (struct usb_descriptor_header *) &eem_fs_in_desc,
85 (struct usb_descriptor_header *) &eem_fs_out_desc,
89 /* high speed support: */
91 static struct usb_endpoint_descriptor eem_hs_in_desc = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
95 .bEndpointAddress = USB_DIR_IN,
96 .bmAttributes = USB_ENDPOINT_XFER_BULK,
97 .wMaxPacketSize = cpu_to_le16(512),
100 static struct usb_endpoint_descriptor eem_hs_out_desc = {
101 .bLength = USB_DT_ENDPOINT_SIZE,
102 .bDescriptorType = USB_DT_ENDPOINT,
104 .bEndpointAddress = USB_DIR_OUT,
105 .bmAttributes = USB_ENDPOINT_XFER_BULK,
106 .wMaxPacketSize = cpu_to_le16(512),
109 static struct usb_descriptor_header *eem_hs_function[] = {
110 /* CDC EEM control descriptors */
111 (struct usb_descriptor_header *) &eem_intf,
112 (struct usb_descriptor_header *) &eem_hs_in_desc,
113 (struct usb_descriptor_header *) &eem_hs_out_desc,
117 /* super speed support: */
119 static struct usb_endpoint_descriptor eem_ss_in_desc = {
120 .bLength = USB_DT_ENDPOINT_SIZE,
121 .bDescriptorType = USB_DT_ENDPOINT,
123 .bEndpointAddress = USB_DIR_IN,
124 .bmAttributes = USB_ENDPOINT_XFER_BULK,
125 .wMaxPacketSize = cpu_to_le16(1024),
128 static struct usb_endpoint_descriptor eem_ss_out_desc = {
129 .bLength = USB_DT_ENDPOINT_SIZE,
130 .bDescriptorType = USB_DT_ENDPOINT,
132 .bEndpointAddress = USB_DIR_OUT,
133 .bmAttributes = USB_ENDPOINT_XFER_BULK,
134 .wMaxPacketSize = cpu_to_le16(1024),
137 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = {
138 .bLength = sizeof eem_ss_bulk_comp_desc,
139 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
141 /* the following 2 values can be tweaked if necessary */
142 /* .bMaxBurst = 0, */
143 /* .bmAttributes = 0, */
146 static struct usb_descriptor_header *eem_ss_function[] = {
147 /* CDC EEM control descriptors */
148 (struct usb_descriptor_header *) &eem_intf,
149 (struct usb_descriptor_header *) &eem_ss_in_desc,
150 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
151 (struct usb_descriptor_header *) &eem_ss_out_desc,
152 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
156 /* string descriptors: */
158 static struct usb_string eem_string_defs[] = {
159 [0].s = "CDC Ethernet Emulation Model (EEM)",
160 { } /* end of list */
163 static struct usb_gadget_strings eem_string_table = {
164 .language = 0x0409, /* en-us */
165 .strings = eem_string_defs,
168 static struct usb_gadget_strings *eem_strings[] = {
173 /*-------------------------------------------------------------------------*/
175 static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
177 struct usb_composite_dev *cdev = f->config->cdev;
178 int value = -EOPNOTSUPP;
179 u16 w_index = le16_to_cpu(ctrl->wIndex);
180 u16 w_value = le16_to_cpu(ctrl->wValue);
181 u16 w_length = le16_to_cpu(ctrl->wLength);
183 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
184 ctrl->bRequestType, ctrl->bRequest,
185 w_value, w_index, w_length);
187 /* device either stalls (value < 0) or reports success */
192 static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
194 struct f_eem *eem = func_to_eem(f);
195 struct usb_composite_dev *cdev = f->config->cdev;
196 struct net_device *net;
198 /* we know alt == 0, so this is an activation or a reset */
202 if (intf == eem->ctrl_id) {
203 DBG(cdev, "reset eem\n");
204 gether_disconnect(&eem->port);
206 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
207 DBG(cdev, "init eem\n");
208 if (config_ep_by_speed(cdev->gadget, f,
210 config_ep_by_speed(cdev->gadget, f,
212 eem->port.in_ep->desc = NULL;
213 eem->port.out_ep->desc = NULL;
218 /* zlps should not occur because zero-length EEM packets
219 * will be inserted in those cases where they would occur
221 eem->port.is_zlp_ok = 1;
222 eem->port.cdc_filter = DEFAULT_FILTER;
223 DBG(cdev, "activate eem\n");
224 net = gether_connect(&eem->port);
235 static void eem_disable(struct usb_function *f)
237 struct f_eem *eem = func_to_eem(f);
238 struct usb_composite_dev *cdev = f->config->cdev;
240 DBG(cdev, "eem deactivated\n");
242 if (eem->port.in_ep->enabled)
243 gether_disconnect(&eem->port);
246 /*-------------------------------------------------------------------------*/
248 /* EEM function driver setup/binding */
250 static int eem_bind(struct usb_configuration *c, struct usb_function *f)
252 struct usb_composite_dev *cdev = c->cdev;
253 struct f_eem *eem = func_to_eem(f);
254 struct usb_string *us;
258 struct f_eem_opts *eem_opts;
260 eem_opts = container_of(f->fi, struct f_eem_opts, func_inst);
262 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
263 * configurations are bound in sequence with list_for_each_entry,
264 * in each configuration its functions are bound in sequence
265 * with list_for_each_entry, so we assume no race condition
266 * with regard to eem_opts->bound access
268 if (!eem_opts->bound) {
269 mutex_lock(&eem_opts->lock);
270 gether_set_gadget(eem_opts->net, cdev->gadget);
271 status = gether_register_netdev(eem_opts->net);
272 mutex_unlock(&eem_opts->lock);
275 eem_opts->bound = true;
278 us = usb_gstrings_attach(cdev, eem_strings,
279 ARRAY_SIZE(eem_string_defs));
282 eem_intf.iInterface = us[0].id;
284 /* allocate instance-specific interface IDs */
285 status = usb_interface_id(c, f);
288 eem->ctrl_id = status;
289 eem_intf.bInterfaceNumber = status;
293 /* allocate instance-specific endpoints */
294 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
297 eem->port.in_ep = ep;
299 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
302 eem->port.out_ep = ep;
306 /* support all relevant hardware speeds... we expect that when
307 * hardware is dual speed, all bulk-capable endpoints work at
310 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
311 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
313 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
314 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
316 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
321 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
322 gadget_is_superspeed(c->cdev->gadget) ? "super" :
323 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
324 eem->port.in_ep->name, eem->port.out_ep->name);
328 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
333 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
335 struct in_context *ctx = req->context;
337 dev_kfree_skb_any(ctx->skb);
339 usb_ep_free_request(ctx->ep, req);
344 * Add the EEM header and ethernet checksum.
345 * We currently do not attempt to put multiple ethernet frames
346 * into a single USB transfer
348 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
350 struct sk_buff *skb2 = NULL;
351 struct usb_ep *in = port->in_ep;
355 int headroom = skb_headroom(skb);
356 int 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 DBG(cdev, "unable to realign EEM packet\n");
546 dev_kfree_skb_any(skb2);
549 dev_kfree_skb_any(skb2);
550 skb_queue_tail(list, skb3);
557 dev_kfree_skb_any(skb);
561 static inline struct f_eem_opts *to_f_eem_opts(struct config_item *item)
563 return container_of(to_config_group(item), struct f_eem_opts,
568 USB_ETHERNET_CONFIGFS_ITEM(eem);
570 /* f_eem_opts_dev_addr */
571 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem);
573 /* f_eem_opts_host_addr */
574 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem);
576 /* f_eem_opts_qmult */
577 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem);
579 /* f_eem_opts_ifname */
580 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem);
582 static struct configfs_attribute *eem_attrs[] = {
583 &eem_opts_attr_dev_addr,
584 &eem_opts_attr_host_addr,
585 &eem_opts_attr_qmult,
586 &eem_opts_attr_ifname,
590 static struct config_item_type eem_func_type = {
591 .ct_item_ops = &eem_item_ops,
592 .ct_attrs = eem_attrs,
593 .ct_owner = THIS_MODULE,
596 static void eem_free_inst(struct usb_function_instance *f)
598 struct f_eem_opts *opts;
600 opts = container_of(f, struct f_eem_opts, func_inst);
602 gether_cleanup(netdev_priv(opts->net));
604 free_netdev(opts->net);
608 static struct usb_function_instance *eem_alloc_inst(void)
610 struct f_eem_opts *opts;
612 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
614 return ERR_PTR(-ENOMEM);
615 mutex_init(&opts->lock);
616 opts->func_inst.free_func_inst = eem_free_inst;
617 opts->net = gether_setup_default();
618 if (IS_ERR(opts->net)) {
619 struct net_device *net = opts->net;
621 return ERR_CAST(net);
624 config_group_init_type_name(&opts->func_inst.group, "", &eem_func_type);
626 return &opts->func_inst;
629 static void eem_free(struct usb_function *f)
632 struct f_eem_opts *opts;
634 eem = func_to_eem(f);
635 opts = container_of(f->fi, struct f_eem_opts, func_inst);
637 mutex_lock(&opts->lock);
639 mutex_unlock(&opts->lock);
642 static void eem_unbind(struct usb_configuration *c, struct usb_function *f)
644 DBG(c->cdev, "eem unbind\n");
646 usb_free_all_descriptors(f);
649 static struct usb_function *eem_alloc(struct usb_function_instance *fi)
652 struct f_eem_opts *opts;
654 /* allocate and initialize one new instance */
655 eem = kzalloc(sizeof(*eem), GFP_KERNEL);
657 return ERR_PTR(-ENOMEM);
659 opts = container_of(fi, struct f_eem_opts, func_inst);
660 mutex_lock(&opts->lock);
663 eem->port.ioport = netdev_priv(opts->net);
664 mutex_unlock(&opts->lock);
665 eem->port.cdc_filter = DEFAULT_FILTER;
667 eem->port.func.name = "cdc_eem";
668 /* descriptors are per-instance copies */
669 eem->port.func.bind = eem_bind;
670 eem->port.func.unbind = eem_unbind;
671 eem->port.func.set_alt = eem_set_alt;
672 eem->port.func.setup = eem_setup;
673 eem->port.func.disable = eem_disable;
674 eem->port.func.free_func = eem_free;
675 eem->port.wrap = eem_wrap;
676 eem->port.unwrap = eem_unwrap;
677 eem->port.header_len = EEM_HLEN;
679 return &eem->port.func;
682 DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc);
683 MODULE_LICENSE("GPL");
684 MODULE_AUTHOR("David Brownell");