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)
33 static inline struct f_eem
*func_to_eem(struct usb_function
*f
)
35 return container_of(f
, struct f_eem
, port
.func
);
38 /*-------------------------------------------------------------------------*/
40 /* interface descriptor: */
42 static struct usb_interface_descriptor eem_intf
= {
43 .bLength
= sizeof eem_intf
,
44 .bDescriptorType
= USB_DT_INTERFACE
,
46 /* .bInterfaceNumber = DYNAMIC */
48 .bInterfaceClass
= USB_CLASS_COMM
,
49 .bInterfaceSubClass
= USB_CDC_SUBCLASS_EEM
,
50 .bInterfaceProtocol
= USB_CDC_PROTO_EEM
,
51 /* .iInterface = DYNAMIC */
54 /* full speed support: */
56 static struct usb_endpoint_descriptor eem_fs_in_desc
= {
57 .bLength
= USB_DT_ENDPOINT_SIZE
,
58 .bDescriptorType
= USB_DT_ENDPOINT
,
60 .bEndpointAddress
= USB_DIR_IN
,
61 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
64 static struct usb_endpoint_descriptor eem_fs_out_desc
= {
65 .bLength
= USB_DT_ENDPOINT_SIZE
,
66 .bDescriptorType
= USB_DT_ENDPOINT
,
68 .bEndpointAddress
= USB_DIR_OUT
,
69 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
72 static struct usb_descriptor_header
*eem_fs_function
[] = {
73 /* CDC EEM control descriptors */
74 (struct usb_descriptor_header
*) &eem_intf
,
75 (struct usb_descriptor_header
*) &eem_fs_in_desc
,
76 (struct usb_descriptor_header
*) &eem_fs_out_desc
,
80 /* high speed support: */
82 static struct usb_endpoint_descriptor eem_hs_in_desc
= {
83 .bLength
= USB_DT_ENDPOINT_SIZE
,
84 .bDescriptorType
= USB_DT_ENDPOINT
,
86 .bEndpointAddress
= USB_DIR_IN
,
87 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
88 .wMaxPacketSize
= cpu_to_le16(512),
91 static struct usb_endpoint_descriptor eem_hs_out_desc
= {
92 .bLength
= USB_DT_ENDPOINT_SIZE
,
93 .bDescriptorType
= USB_DT_ENDPOINT
,
95 .bEndpointAddress
= USB_DIR_OUT
,
96 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
97 .wMaxPacketSize
= cpu_to_le16(512),
100 static struct usb_descriptor_header
*eem_hs_function
[] = {
101 /* CDC EEM control descriptors */
102 (struct usb_descriptor_header
*) &eem_intf
,
103 (struct usb_descriptor_header
*) &eem_hs_in_desc
,
104 (struct usb_descriptor_header
*) &eem_hs_out_desc
,
108 /* super speed support: */
110 static struct usb_endpoint_descriptor eem_ss_in_desc
= {
111 .bLength
= USB_DT_ENDPOINT_SIZE
,
112 .bDescriptorType
= USB_DT_ENDPOINT
,
114 .bEndpointAddress
= USB_DIR_IN
,
115 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
116 .wMaxPacketSize
= cpu_to_le16(1024),
119 static struct usb_endpoint_descriptor eem_ss_out_desc
= {
120 .bLength
= USB_DT_ENDPOINT_SIZE
,
121 .bDescriptorType
= USB_DT_ENDPOINT
,
123 .bEndpointAddress
= USB_DIR_OUT
,
124 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
125 .wMaxPacketSize
= cpu_to_le16(1024),
128 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc
= {
129 .bLength
= sizeof eem_ss_bulk_comp_desc
,
130 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
132 /* the following 2 values can be tweaked if necessary */
133 /* .bMaxBurst = 0, */
134 /* .bmAttributes = 0, */
137 static struct usb_descriptor_header
*eem_ss_function
[] = {
138 /* CDC EEM control descriptors */
139 (struct usb_descriptor_header
*) &eem_intf
,
140 (struct usb_descriptor_header
*) &eem_ss_in_desc
,
141 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
142 (struct usb_descriptor_header
*) &eem_ss_out_desc
,
143 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
147 /* string descriptors: */
149 static struct usb_string eem_string_defs
[] = {
150 [0].s
= "CDC Ethernet Emulation Model (EEM)",
151 { } /* end of list */
154 static struct usb_gadget_strings eem_string_table
= {
155 .language
= 0x0409, /* en-us */
156 .strings
= eem_string_defs
,
159 static struct usb_gadget_strings
*eem_strings
[] = {
164 /*-------------------------------------------------------------------------*/
166 static int eem_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
168 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
169 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
170 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
171 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
173 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
174 ctrl
->bRequestType
, ctrl
->bRequest
,
175 w_value
, w_index
, w_length
);
177 /* device either stalls (value < 0) or reports success */
182 static int eem_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
184 struct f_eem
*eem
= func_to_eem(f
);
185 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
186 struct net_device
*net
;
188 /* we know alt == 0, so this is an activation or a reset */
192 if (intf
== eem
->ctrl_id
) {
193 DBG(cdev
, "reset eem\n");
194 gether_disconnect(&eem
->port
);
196 if (!eem
->port
.in_ep
->desc
|| !eem
->port
.out_ep
->desc
) {
197 DBG(cdev
, "init eem\n");
198 if (config_ep_by_speed(cdev
->gadget
, f
,
200 config_ep_by_speed(cdev
->gadget
, f
,
202 eem
->port
.in_ep
->desc
= NULL
;
203 eem
->port
.out_ep
->desc
= NULL
;
208 /* zlps should not occur because zero-length EEM packets
209 * will be inserted in those cases where they would occur
211 eem
->port
.is_zlp_ok
= 1;
212 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
213 DBG(cdev
, "activate eem\n");
214 net
= gether_connect(&eem
->port
);
225 static void eem_disable(struct usb_function
*f
)
227 struct f_eem
*eem
= func_to_eem(f
);
228 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
230 DBG(cdev
, "eem deactivated\n");
232 if (eem
->port
.in_ep
->enabled
)
233 gether_disconnect(&eem
->port
);
236 /*-------------------------------------------------------------------------*/
238 /* EEM function driver setup/binding */
240 static int eem_bind(struct usb_configuration
*c
, struct usb_function
*f
)
242 struct usb_composite_dev
*cdev
= c
->cdev
;
243 struct f_eem
*eem
= func_to_eem(f
);
244 struct usb_string
*us
;
248 struct f_eem_opts
*eem_opts
;
250 eem_opts
= container_of(f
->fi
, struct f_eem_opts
, func_inst
);
252 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
253 * configurations are bound in sequence with list_for_each_entry,
254 * in each configuration its functions are bound in sequence
255 * with list_for_each_entry, so we assume no race condition
256 * with regard to eem_opts->bound access
258 if (!eem_opts
->bound
) {
259 mutex_lock(&eem_opts
->lock
);
260 gether_set_gadget(eem_opts
->net
, cdev
->gadget
);
261 status
= gether_register_netdev(eem_opts
->net
);
262 mutex_unlock(&eem_opts
->lock
);
265 eem_opts
->bound
= true;
268 us
= usb_gstrings_attach(cdev
, eem_strings
,
269 ARRAY_SIZE(eem_string_defs
));
272 eem_intf
.iInterface
= us
[0].id
;
274 /* allocate instance-specific interface IDs */
275 status
= usb_interface_id(c
, f
);
278 eem
->ctrl_id
= status
;
279 eem_intf
.bInterfaceNumber
= status
;
283 /* allocate instance-specific endpoints */
284 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_in_desc
);
287 eem
->port
.in_ep
= ep
;
289 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_out_desc
);
292 eem
->port
.out_ep
= ep
;
296 /* support all relevant hardware speeds... we expect that when
297 * hardware is dual speed, all bulk-capable endpoints work at
300 eem_hs_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
301 eem_hs_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
303 eem_ss_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
304 eem_ss_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
306 status
= usb_assign_descriptors(f
, eem_fs_function
, eem_hs_function
,
307 eem_ss_function
, NULL
);
311 DBG(cdev
, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
312 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
313 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
314 eem
->port
.in_ep
->name
, eem
->port
.out_ep
->name
);
318 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
323 static void eem_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
325 struct sk_buff
*skb
= (struct sk_buff
*)req
->context
;
327 dev_kfree_skb_any(skb
);
331 * Add the EEM header and ethernet checksum.
332 * We currently do not attempt to put multiple ethernet frames
333 * into a single USB transfer
335 static struct sk_buff
*eem_wrap(struct gether
*port
, struct sk_buff
*skb
)
337 struct sk_buff
*skb2
= NULL
;
338 struct usb_ep
*in
= port
->in_ep
;
339 int headroom
, tailroom
, padlen
= 0;
346 headroom
= skb_headroom(skb
);
347 tailroom
= skb_tailroom(skb
);
349 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
350 * stick two bytes of zero-length EEM packet on the end.
352 if (((len
+ EEM_HLEN
+ ETH_FCS_LEN
) % in
->maxpacket
) == 0)
355 if ((tailroom
>= (ETH_FCS_LEN
+ padlen
)) &&
356 (headroom
>= EEM_HLEN
) && !skb_cloned(skb
))
359 skb2
= skb_copy_expand(skb
, EEM_HLEN
, ETH_FCS_LEN
+ padlen
, GFP_ATOMIC
);
360 dev_kfree_skb_any(skb
);
366 /* use the "no CRC" option */
367 put_unaligned_be32(0xdeadbeef, skb_put(skb
, 4));
369 /* EEM packet header format:
370 * b0..13: length of ethernet frame
371 * b14: bmCRC (0 == sentinel CRC)
372 * b15: bmType (0 == data)
375 put_unaligned_le16(len
& 0x3FFF, skb_push(skb
, 2));
377 /* add a zero-length EEM packet, if needed */
379 put_unaligned_le16(0, skb_put(skb
, 2));
385 * Remove the EEM header. Note that there can be many EEM packets in a single
386 * USB transfer, so we need to break them out and handle them independently.
388 static int eem_unwrap(struct gether
*port
,
390 struct sk_buff_head
*list
)
392 struct usb_composite_dev
*cdev
= port
->func
.config
->cdev
;
396 struct sk_buff
*skb2
;
400 if (skb
->len
< EEM_HLEN
) {
402 DBG(cdev
, "invalid EEM header\n");
406 /* remove the EEM header */
407 header
= get_unaligned_le16(skb
->data
);
408 skb_pull(skb
, EEM_HLEN
);
410 /* EEM packet header format:
411 * b0..14: EEM type dependent (data or command)
412 * b15: bmType (0 == data, 1 == command)
414 if (header
& BIT(15)) {
415 struct usb_request
*req
= cdev
->req
;
418 /* EEM command packet format:
419 * b0..10: bmEEMCmdParam
421 * b14: reserved (must be zero)
422 * b15: bmType (1 == command)
424 if (header
& BIT(14))
427 bmEEMCmd
= (header
>> 11) & 0x7;
430 len
= header
& 0x7FF;
431 if (skb
->len
< len
) {
436 skb2
= skb_clone(skb
, GFP_ATOMIC
);
437 if (unlikely(!skb2
)) {
438 DBG(cdev
, "EEM echo response error\n");
442 put_unaligned_le16(BIT(15) | BIT(11) | len
,
444 skb_copy_bits(skb2
, 0, req
->buf
, skb2
->len
);
445 req
->length
= skb2
->len
;
446 req
->complete
= eem_cmd_complete
;
449 if (usb_ep_queue(port
->in_ep
, req
, GFP_ATOMIC
))
450 DBG(cdev
, "echo response queue fail\n");
453 case 1: /* echo response */
454 case 2: /* suspend hint */
455 case 3: /* response hint */
456 case 4: /* response complete hint */
458 default: /* reserved */
463 struct sk_buff
*skb3
;
465 /* check for zero-length EEM packet */
469 /* EEM data packet format:
470 * b0..13: length of ethernet frame
471 * b14: bmCRC (0 == sentinel, 1 == calculated)
472 * b15: bmType (0 == data)
474 len
= header
& 0x3FFF;
476 || (len
< (ETH_HLEN
+ ETH_FCS_LEN
))) {
482 if (header
& BIT(14)) {
483 crc
= get_unaligned_le32(skb
->data
+ len
486 skb
->data
, len
- ETH_FCS_LEN
);
488 crc
= get_unaligned_be32(skb
->data
+ len
493 DBG(cdev
, "invalid EEM CRC\n");
497 skb2
= skb_clone(skb
, GFP_ATOMIC
);
498 if (unlikely(!skb2
)) {
499 DBG(cdev
, "unable to unframe EEM packet\n");
502 skb_trim(skb2
, len
- ETH_FCS_LEN
);
504 skb3
= skb_copy_expand(skb2
,
508 if (unlikely(!skb3
)) {
509 dev_kfree_skb_any(skb2
);
512 dev_kfree_skb_any(skb2
);
513 skb_queue_tail(list
, skb3
);
520 dev_kfree_skb_any(skb
);
524 static inline struct f_eem_opts
*to_f_eem_opts(struct config_item
*item
)
526 return container_of(to_config_group(item
), struct f_eem_opts
,
531 USB_ETHERNET_CONFIGFS_ITEM(eem
);
533 /* f_eem_opts_dev_addr */
534 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem
);
536 /* f_eem_opts_host_addr */
537 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem
);
539 /* f_eem_opts_qmult */
540 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem
);
542 /* f_eem_opts_ifname */
543 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem
);
545 static struct configfs_attribute
*eem_attrs
[] = {
546 &eem_opts_attr_dev_addr
,
547 &eem_opts_attr_host_addr
,
548 &eem_opts_attr_qmult
,
549 &eem_opts_attr_ifname
,
553 static const struct config_item_type eem_func_type
= {
554 .ct_item_ops
= &eem_item_ops
,
555 .ct_attrs
= eem_attrs
,
556 .ct_owner
= THIS_MODULE
,
559 static void eem_free_inst(struct usb_function_instance
*f
)
561 struct f_eem_opts
*opts
;
563 opts
= container_of(f
, struct f_eem_opts
, func_inst
);
565 gether_cleanup(netdev_priv(opts
->net
));
567 free_netdev(opts
->net
);
571 static struct usb_function_instance
*eem_alloc_inst(void)
573 struct f_eem_opts
*opts
;
575 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
577 return ERR_PTR(-ENOMEM
);
578 mutex_init(&opts
->lock
);
579 opts
->func_inst
.free_func_inst
= eem_free_inst
;
580 opts
->net
= gether_setup_default();
581 if (IS_ERR(opts
->net
)) {
582 struct net_device
*net
= opts
->net
;
584 return ERR_CAST(net
);
587 config_group_init_type_name(&opts
->func_inst
.group
, "", &eem_func_type
);
589 return &opts
->func_inst
;
592 static void eem_free(struct usb_function
*f
)
595 struct f_eem_opts
*opts
;
597 eem
= func_to_eem(f
);
598 opts
= container_of(f
->fi
, struct f_eem_opts
, func_inst
);
600 mutex_lock(&opts
->lock
);
602 mutex_unlock(&opts
->lock
);
605 static void eem_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
607 DBG(c
->cdev
, "eem unbind\n");
609 usb_free_all_descriptors(f
);
612 static struct usb_function
*eem_alloc(struct usb_function_instance
*fi
)
615 struct f_eem_opts
*opts
;
617 /* allocate and initialize one new instance */
618 eem
= kzalloc(sizeof(*eem
), GFP_KERNEL
);
620 return ERR_PTR(-ENOMEM
);
622 opts
= container_of(fi
, struct f_eem_opts
, func_inst
);
623 mutex_lock(&opts
->lock
);
626 eem
->port
.ioport
= netdev_priv(opts
->net
);
627 mutex_unlock(&opts
->lock
);
628 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
630 eem
->port
.func
.name
= "cdc_eem";
631 /* descriptors are per-instance copies */
632 eem
->port
.func
.bind
= eem_bind
;
633 eem
->port
.func
.unbind
= eem_unbind
;
634 eem
->port
.func
.set_alt
= eem_set_alt
;
635 eem
->port
.func
.setup
= eem_setup
;
636 eem
->port
.func
.disable
= eem_disable
;
637 eem
->port
.func
.free_func
= eem_free
;
638 eem
->port
.wrap
= eem_wrap
;
639 eem
->port
.unwrap
= eem_unwrap
;
640 eem
->port
.header_len
= EEM_HLEN
;
642 return &eem
->port
.func
;
645 DECLARE_USB_FUNCTION_INIT(eem
, eem_alloc_inst
, eem_alloc
);
646 MODULE_LICENSE("GPL");
647 MODULE_AUTHOR("David Brownell");