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
;
294 /* support all relevant hardware speeds... we expect that when
295 * hardware is dual speed, all bulk-capable endpoints work at
298 eem_hs_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
299 eem_hs_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
301 eem_ss_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
302 eem_ss_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
304 status
= usb_assign_descriptors(f
, eem_fs_function
, eem_hs_function
,
305 eem_ss_function
, NULL
);
309 DBG(cdev
, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
310 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
311 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
312 eem
->port
.in_ep
->name
, eem
->port
.out_ep
->name
);
316 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
321 static void eem_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
323 struct sk_buff
*skb
= (struct sk_buff
*)req
->context
;
325 dev_kfree_skb_any(skb
);
329 * Add the EEM header and ethernet checksum.
330 * We currently do not attempt to put multiple ethernet frames
331 * into a single USB transfer
333 static struct sk_buff
*eem_wrap(struct gether
*port
, struct sk_buff
*skb
)
335 struct sk_buff
*skb2
= NULL
;
336 struct usb_ep
*in
= port
->in_ep
;
337 int headroom
, tailroom
, padlen
= 0;
344 headroom
= skb_headroom(skb
);
345 tailroom
= skb_tailroom(skb
);
347 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
348 * stick two bytes of zero-length EEM packet on the end.
350 if (((len
+ EEM_HLEN
+ ETH_FCS_LEN
) % in
->maxpacket
) == 0)
353 if ((tailroom
>= (ETH_FCS_LEN
+ padlen
)) &&
354 (headroom
>= EEM_HLEN
) && !skb_cloned(skb
))
357 skb2
= skb_copy_expand(skb
, EEM_HLEN
, ETH_FCS_LEN
+ padlen
, GFP_ATOMIC
);
358 dev_kfree_skb_any(skb
);
364 /* use the "no CRC" option */
365 put_unaligned_be32(0xdeadbeef, skb_put(skb
, 4));
367 /* EEM packet header format:
368 * b0..13: length of ethernet frame
369 * b14: bmCRC (0 == sentinel CRC)
370 * b15: bmType (0 == data)
373 put_unaligned_le16(len
& 0x3FFF, skb_push(skb
, 2));
375 /* add a zero-length EEM packet, if needed */
377 put_unaligned_le16(0, skb_put(skb
, 2));
383 * Remove the EEM header. Note that there can be many EEM packets in a single
384 * USB transfer, so we need to break them out and handle them independently.
386 static int eem_unwrap(struct gether
*port
,
388 struct sk_buff_head
*list
)
390 struct usb_composite_dev
*cdev
= port
->func
.config
->cdev
;
394 struct sk_buff
*skb2
;
398 if (skb
->len
< EEM_HLEN
) {
400 DBG(cdev
, "invalid EEM header\n");
404 /* remove the EEM header */
405 header
= get_unaligned_le16(skb
->data
);
406 skb_pull(skb
, EEM_HLEN
);
408 /* EEM packet header format:
409 * b0..14: EEM type dependent (data or command)
410 * b15: bmType (0 == data, 1 == command)
412 if (header
& BIT(15)) {
413 struct usb_request
*req
= cdev
->req
;
416 /* EEM command packet format:
417 * b0..10: bmEEMCmdParam
419 * b14: reserved (must be zero)
420 * b15: bmType (1 == command)
422 if (header
& BIT(14))
425 bmEEMCmd
= (header
>> 11) & 0x7;
428 len
= header
& 0x7FF;
429 if (skb
->len
< len
) {
434 skb2
= skb_clone(skb
, GFP_ATOMIC
);
435 if (unlikely(!skb2
)) {
436 DBG(cdev
, "EEM echo response error\n");
440 put_unaligned_le16(BIT(15) | BIT(11) | len
,
442 skb_copy_bits(skb2
, 0, req
->buf
, skb2
->len
);
443 req
->length
= skb2
->len
;
444 req
->complete
= eem_cmd_complete
;
447 if (usb_ep_queue(port
->in_ep
, req
, GFP_ATOMIC
))
448 DBG(cdev
, "echo response queue fail\n");
451 case 1: /* echo response */
452 case 2: /* suspend hint */
453 case 3: /* response hint */
454 case 4: /* response complete hint */
456 default: /* reserved */
461 struct sk_buff
*skb3
;
463 /* check for zero-length EEM packet */
467 /* EEM data packet format:
468 * b0..13: length of ethernet frame
469 * b14: bmCRC (0 == sentinel, 1 == calculated)
470 * b15: bmType (0 == data)
472 len
= header
& 0x3FFF;
474 || (len
< (ETH_HLEN
+ ETH_FCS_LEN
))) {
480 if (header
& BIT(14)) {
481 crc
= get_unaligned_le32(skb
->data
+ len
484 skb
->data
, len
- ETH_FCS_LEN
);
486 crc
= get_unaligned_be32(skb
->data
+ len
491 DBG(cdev
, "invalid EEM CRC\n");
495 skb2
= skb_clone(skb
, GFP_ATOMIC
);
496 if (unlikely(!skb2
)) {
497 DBG(cdev
, "unable to unframe EEM packet\n");
500 skb_trim(skb2
, len
- ETH_FCS_LEN
);
502 skb3
= skb_copy_expand(skb2
,
506 if (unlikely(!skb3
)) {
507 dev_kfree_skb_any(skb2
);
510 dev_kfree_skb_any(skb2
);
511 skb_queue_tail(list
, skb3
);
518 dev_kfree_skb_any(skb
);
522 static inline struct f_eem_opts
*to_f_eem_opts(struct config_item
*item
)
524 return container_of(to_config_group(item
), struct f_eem_opts
,
529 USB_ETHERNET_CONFIGFS_ITEM(eem
);
531 /* f_eem_opts_dev_addr */
532 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem
);
534 /* f_eem_opts_host_addr */
535 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem
);
537 /* f_eem_opts_qmult */
538 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem
);
540 /* f_eem_opts_ifname */
541 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem
);
543 static struct configfs_attribute
*eem_attrs
[] = {
544 &eem_opts_attr_dev_addr
,
545 &eem_opts_attr_host_addr
,
546 &eem_opts_attr_qmult
,
547 &eem_opts_attr_ifname
,
551 static const struct config_item_type eem_func_type
= {
552 .ct_item_ops
= &eem_item_ops
,
553 .ct_attrs
= eem_attrs
,
554 .ct_owner
= THIS_MODULE
,
557 static void eem_free_inst(struct usb_function_instance
*f
)
559 struct f_eem_opts
*opts
;
561 opts
= container_of(f
, struct f_eem_opts
, func_inst
);
563 gether_cleanup(netdev_priv(opts
->net
));
565 free_netdev(opts
->net
);
569 static struct usb_function_instance
*eem_alloc_inst(void)
571 struct f_eem_opts
*opts
;
573 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
575 return ERR_PTR(-ENOMEM
);
576 mutex_init(&opts
->lock
);
577 opts
->func_inst
.free_func_inst
= eem_free_inst
;
578 opts
->net
= gether_setup_default();
579 if (IS_ERR(opts
->net
)) {
580 struct net_device
*net
= opts
->net
;
582 return ERR_CAST(net
);
585 config_group_init_type_name(&opts
->func_inst
.group
, "", &eem_func_type
);
587 return &opts
->func_inst
;
590 static void eem_free(struct usb_function
*f
)
593 struct f_eem_opts
*opts
;
595 eem
= func_to_eem(f
);
596 opts
= container_of(f
->fi
, struct f_eem_opts
, func_inst
);
598 mutex_lock(&opts
->lock
);
600 mutex_unlock(&opts
->lock
);
603 static void eem_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
605 DBG(c
->cdev
, "eem unbind\n");
607 usb_free_all_descriptors(f
);
610 static struct usb_function
*eem_alloc(struct usb_function_instance
*fi
)
613 struct f_eem_opts
*opts
;
615 /* allocate and initialize one new instance */
616 eem
= kzalloc(sizeof(*eem
), GFP_KERNEL
);
618 return ERR_PTR(-ENOMEM
);
620 opts
= container_of(fi
, struct f_eem_opts
, func_inst
);
621 mutex_lock(&opts
->lock
);
624 eem
->port
.ioport
= netdev_priv(opts
->net
);
625 mutex_unlock(&opts
->lock
);
626 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
628 eem
->port
.func
.name
= "cdc_eem";
629 /* descriptors are per-instance copies */
630 eem
->port
.func
.bind
= eem_bind
;
631 eem
->port
.func
.unbind
= eem_unbind
;
632 eem
->port
.func
.set_alt
= eem_set_alt
;
633 eem
->port
.func
.setup
= eem_setup
;
634 eem
->port
.func
.disable
= eem_disable
;
635 eem
->port
.func
.free_func
= eem_free
;
636 eem
->port
.wrap
= eem_wrap
;
637 eem
->port
.unwrap
= eem_unwrap
;
638 eem
->port
.header_len
= EEM_HLEN
;
640 return &eem
->port
.func
;
643 DECLARE_USB_FUNCTION_INIT(eem
, eem_alloc_inst
, eem_alloc
);
644 MODULE_LICENSE("GPL");
645 MODULE_AUTHOR("David Brownell");