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)
37 static inline struct f_eem
*func_to_eem(struct usb_function
*f
)
39 return container_of(f
, struct f_eem
, port
.func
);
42 /*-------------------------------------------------------------------------*/
44 /* interface descriptor: */
46 static struct usb_interface_descriptor eem_intf
= {
47 .bLength
= sizeof eem_intf
,
48 .bDescriptorType
= USB_DT_INTERFACE
,
50 /* .bInterfaceNumber = DYNAMIC */
52 .bInterfaceClass
= USB_CLASS_COMM
,
53 .bInterfaceSubClass
= USB_CDC_SUBCLASS_EEM
,
54 .bInterfaceProtocol
= USB_CDC_PROTO_EEM
,
55 /* .iInterface = DYNAMIC */
58 /* full speed support: */
60 static struct usb_endpoint_descriptor eem_fs_in_desc
= {
61 .bLength
= USB_DT_ENDPOINT_SIZE
,
62 .bDescriptorType
= USB_DT_ENDPOINT
,
64 .bEndpointAddress
= USB_DIR_IN
,
65 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
68 static struct usb_endpoint_descriptor eem_fs_out_desc
= {
69 .bLength
= USB_DT_ENDPOINT_SIZE
,
70 .bDescriptorType
= USB_DT_ENDPOINT
,
72 .bEndpointAddress
= USB_DIR_OUT
,
73 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
76 static struct usb_descriptor_header
*eem_fs_function
[] = {
77 /* CDC EEM control descriptors */
78 (struct usb_descriptor_header
*) &eem_intf
,
79 (struct usb_descriptor_header
*) &eem_fs_in_desc
,
80 (struct usb_descriptor_header
*) &eem_fs_out_desc
,
84 /* high speed support: */
86 static struct usb_endpoint_descriptor eem_hs_in_desc
= {
87 .bLength
= USB_DT_ENDPOINT_SIZE
,
88 .bDescriptorType
= USB_DT_ENDPOINT
,
90 .bEndpointAddress
= USB_DIR_IN
,
91 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
92 .wMaxPacketSize
= cpu_to_le16(512),
95 static struct usb_endpoint_descriptor eem_hs_out_desc
= {
96 .bLength
= USB_DT_ENDPOINT_SIZE
,
97 .bDescriptorType
= USB_DT_ENDPOINT
,
99 .bEndpointAddress
= USB_DIR_OUT
,
100 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
101 .wMaxPacketSize
= cpu_to_le16(512),
104 static struct usb_descriptor_header
*eem_hs_function
[] = {
105 /* CDC EEM control descriptors */
106 (struct usb_descriptor_header
*) &eem_intf
,
107 (struct usb_descriptor_header
*) &eem_hs_in_desc
,
108 (struct usb_descriptor_header
*) &eem_hs_out_desc
,
112 /* super speed support: */
114 static struct usb_endpoint_descriptor eem_ss_in_desc
= {
115 .bLength
= USB_DT_ENDPOINT_SIZE
,
116 .bDescriptorType
= USB_DT_ENDPOINT
,
118 .bEndpointAddress
= USB_DIR_IN
,
119 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
120 .wMaxPacketSize
= cpu_to_le16(1024),
123 static struct usb_endpoint_descriptor eem_ss_out_desc
= {
124 .bLength
= USB_DT_ENDPOINT_SIZE
,
125 .bDescriptorType
= USB_DT_ENDPOINT
,
127 .bEndpointAddress
= USB_DIR_OUT
,
128 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
129 .wMaxPacketSize
= cpu_to_le16(1024),
132 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc
= {
133 .bLength
= sizeof eem_ss_bulk_comp_desc
,
134 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
136 /* the following 2 values can be tweaked if necessary */
137 /* .bMaxBurst = 0, */
138 /* .bmAttributes = 0, */
141 static struct usb_descriptor_header
*eem_ss_function
[] = {
142 /* CDC EEM control descriptors */
143 (struct usb_descriptor_header
*) &eem_intf
,
144 (struct usb_descriptor_header
*) &eem_ss_in_desc
,
145 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
146 (struct usb_descriptor_header
*) &eem_ss_out_desc
,
147 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
151 /* string descriptors: */
153 static struct usb_string eem_string_defs
[] = {
154 [0].s
= "CDC Ethernet Emulation Model (EEM)",
155 { } /* end of list */
158 static struct usb_gadget_strings eem_string_table
= {
159 .language
= 0x0409, /* en-us */
160 .strings
= eem_string_defs
,
163 static struct usb_gadget_strings
*eem_strings
[] = {
168 /*-------------------------------------------------------------------------*/
170 static int eem_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
172 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
173 int value
= -EOPNOTSUPP
;
174 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
175 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
176 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
178 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
179 ctrl
->bRequestType
, ctrl
->bRequest
,
180 w_value
, w_index
, w_length
);
182 /* device either stalls (value < 0) or reports success */
187 static int eem_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
189 struct f_eem
*eem
= func_to_eem(f
);
190 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
191 struct net_device
*net
;
193 /* we know alt == 0, so this is an activation or a reset */
197 if (intf
== eem
->ctrl_id
) {
198 DBG(cdev
, "reset eem\n");
199 gether_disconnect(&eem
->port
);
201 if (!eem
->port
.in_ep
->desc
|| !eem
->port
.out_ep
->desc
) {
202 DBG(cdev
, "init eem\n");
203 if (config_ep_by_speed(cdev
->gadget
, f
,
205 config_ep_by_speed(cdev
->gadget
, f
,
207 eem
->port
.in_ep
->desc
= NULL
;
208 eem
->port
.out_ep
->desc
= NULL
;
213 /* zlps should not occur because zero-length EEM packets
214 * will be inserted in those cases where they would occur
216 eem
->port
.is_zlp_ok
= 1;
217 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
218 DBG(cdev
, "activate eem\n");
219 net
= gether_connect(&eem
->port
);
230 static void eem_disable(struct usb_function
*f
)
232 struct f_eem
*eem
= func_to_eem(f
);
233 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
235 DBG(cdev
, "eem deactivated\n");
237 if (eem
->port
.in_ep
->enabled
)
238 gether_disconnect(&eem
->port
);
241 /*-------------------------------------------------------------------------*/
243 /* EEM function driver setup/binding */
245 static int eem_bind(struct usb_configuration
*c
, struct usb_function
*f
)
247 struct usb_composite_dev
*cdev
= c
->cdev
;
248 struct f_eem
*eem
= func_to_eem(f
);
249 struct usb_string
*us
;
253 struct f_eem_opts
*eem_opts
;
255 eem_opts
= container_of(f
->fi
, struct f_eem_opts
, func_inst
);
257 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
258 * configurations are bound in sequence with list_for_each_entry,
259 * in each configuration its functions are bound in sequence
260 * with list_for_each_entry, so we assume no race condition
261 * with regard to eem_opts->bound access
263 if (!eem_opts
->bound
) {
264 mutex_lock(&eem_opts
->lock
);
265 gether_set_gadget(eem_opts
->net
, cdev
->gadget
);
266 status
= gether_register_netdev(eem_opts
->net
);
267 mutex_unlock(&eem_opts
->lock
);
270 eem_opts
->bound
= true;
273 us
= usb_gstrings_attach(cdev
, eem_strings
,
274 ARRAY_SIZE(eem_string_defs
));
277 eem_intf
.iInterface
= us
[0].id
;
279 /* allocate instance-specific interface IDs */
280 status
= usb_interface_id(c
, f
);
283 eem
->ctrl_id
= status
;
284 eem_intf
.bInterfaceNumber
= status
;
288 /* allocate instance-specific endpoints */
289 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_in_desc
);
292 eem
->port
.in_ep
= ep
;
294 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_out_desc
);
297 eem
->port
.out_ep
= ep
;
301 /* support all relevant hardware speeds... we expect that when
302 * hardware is dual speed, all bulk-capable endpoints work at
305 eem_hs_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
306 eem_hs_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
308 eem_ss_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
309 eem_ss_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
311 status
= usb_assign_descriptors(f
, eem_fs_function
, eem_hs_function
,
312 eem_ss_function
, NULL
);
316 DBG(cdev
, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
317 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
318 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
319 eem
->port
.in_ep
->name
, eem
->port
.out_ep
->name
);
323 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
328 static void eem_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
330 struct sk_buff
*skb
= (struct sk_buff
*)req
->context
;
332 dev_kfree_skb_any(skb
);
336 * Add the EEM header and ethernet checksum.
337 * We currently do not attempt to put multiple ethernet frames
338 * into a single USB transfer
340 static struct sk_buff
*eem_wrap(struct gether
*port
, struct sk_buff
*skb
)
342 struct sk_buff
*skb2
= NULL
;
343 struct usb_ep
*in
= port
->in_ep
;
347 int headroom
= skb_headroom(skb
);
348 int tailroom
= skb_tailroom(skb
);
350 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
351 * stick two bytes of zero-length EEM packet on the end.
353 if (((len
+ EEM_HLEN
+ ETH_FCS_LEN
) % in
->maxpacket
) == 0)
356 if ((tailroom
>= (ETH_FCS_LEN
+ padlen
)) &&
357 (headroom
>= EEM_HLEN
) && !skb_cloned(skb
))
360 skb2
= skb_copy_expand(skb
, EEM_HLEN
, ETH_FCS_LEN
+ padlen
, GFP_ATOMIC
);
361 dev_kfree_skb_any(skb
);
367 /* use the "no CRC" option */
368 put_unaligned_be32(0xdeadbeef, skb_put(skb
, 4));
370 /* EEM packet header format:
371 * b0..13: length of ethernet frame
372 * b14: bmCRC (0 == sentinel CRC)
373 * b15: bmType (0 == data)
376 put_unaligned_le16(len
& 0x3FFF, skb_push(skb
, 2));
378 /* add a zero-length EEM packet, if needed */
380 put_unaligned_le16(0, skb_put(skb
, 2));
386 * Remove the EEM header. Note that there can be many EEM packets in a single
387 * USB transfer, so we need to break them out and handle them independently.
389 static int eem_unwrap(struct gether
*port
,
391 struct sk_buff_head
*list
)
393 struct usb_composite_dev
*cdev
= port
->func
.config
->cdev
;
397 struct sk_buff
*skb2
;
401 if (skb
->len
< EEM_HLEN
) {
403 DBG(cdev
, "invalid EEM header\n");
407 /* remove the EEM header */
408 header
= get_unaligned_le16(skb
->data
);
409 skb_pull(skb
, EEM_HLEN
);
411 /* EEM packet header format:
412 * b0..14: EEM type dependent (data or command)
413 * b15: bmType (0 == data, 1 == command)
415 if (header
& BIT(15)) {
416 struct usb_request
*req
= cdev
->req
;
419 /* EEM command packet format:
420 * b0..10: bmEEMCmdParam
422 * b14: reserved (must be zero)
423 * b15: bmType (1 == command)
425 if (header
& BIT(14))
428 bmEEMCmd
= (header
>> 11) & 0x7;
431 len
= header
& 0x7FF;
432 if (skb
->len
< len
) {
437 skb2
= skb_clone(skb
, GFP_ATOMIC
);
438 if (unlikely(!skb2
)) {
439 DBG(cdev
, "EEM echo response error\n");
443 put_unaligned_le16(BIT(15) | BIT(11) | len
,
445 skb_copy_bits(skb2
, 0, req
->buf
, skb2
->len
);
446 req
->length
= skb2
->len
;
447 req
->complete
= eem_cmd_complete
;
450 if (usb_ep_queue(port
->in_ep
, req
, GFP_ATOMIC
))
451 DBG(cdev
, "echo response queue fail\n");
454 case 1: /* echo response */
455 case 2: /* suspend hint */
456 case 3: /* response hint */
457 case 4: /* response complete hint */
459 default: /* reserved */
464 struct sk_buff
*skb3
;
466 /* check for zero-length EEM packet */
470 /* EEM data packet format:
471 * b0..13: length of ethernet frame
472 * b14: bmCRC (0 == sentinel, 1 == calculated)
473 * b15: bmType (0 == data)
475 len
= header
& 0x3FFF;
477 || (len
< (ETH_HLEN
+ ETH_FCS_LEN
))) {
483 if (header
& BIT(14)) {
484 crc
= get_unaligned_le32(skb
->data
+ len
487 skb
->data
, len
- ETH_FCS_LEN
);
489 crc
= get_unaligned_be32(skb
->data
+ len
494 DBG(cdev
, "invalid EEM CRC\n");
498 skb2
= skb_clone(skb
, GFP_ATOMIC
);
499 if (unlikely(!skb2
)) {
500 DBG(cdev
, "unable to unframe EEM packet\n");
503 skb_trim(skb2
, len
- ETH_FCS_LEN
);
505 skb3
= skb_copy_expand(skb2
,
509 if (unlikely(!skb3
)) {
510 DBG(cdev
, "unable to realign EEM packet\n");
511 dev_kfree_skb_any(skb2
);
514 dev_kfree_skb_any(skb2
);
515 skb_queue_tail(list
, skb3
);
522 dev_kfree_skb_any(skb
);
526 static inline struct f_eem_opts
*to_f_eem_opts(struct config_item
*item
)
528 return container_of(to_config_group(item
), struct f_eem_opts
,
533 USB_ETHERNET_CONFIGFS_ITEM(eem
);
535 /* f_eem_opts_dev_addr */
536 USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(eem
);
538 /* f_eem_opts_host_addr */
539 USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(eem
);
541 /* f_eem_opts_qmult */
542 USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(eem
);
544 /* f_eem_opts_ifname */
545 USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(eem
);
547 static struct configfs_attribute
*eem_attrs
[] = {
548 &eem_opts_attr_dev_addr
,
549 &eem_opts_attr_host_addr
,
550 &eem_opts_attr_qmult
,
551 &eem_opts_attr_ifname
,
555 static struct config_item_type eem_func_type
= {
556 .ct_item_ops
= &eem_item_ops
,
557 .ct_attrs
= eem_attrs
,
558 .ct_owner
= THIS_MODULE
,
561 static void eem_free_inst(struct usb_function_instance
*f
)
563 struct f_eem_opts
*opts
;
565 opts
= container_of(f
, struct f_eem_opts
, func_inst
);
567 gether_cleanup(netdev_priv(opts
->net
));
569 free_netdev(opts
->net
);
573 static struct usb_function_instance
*eem_alloc_inst(void)
575 struct f_eem_opts
*opts
;
577 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
579 return ERR_PTR(-ENOMEM
);
580 mutex_init(&opts
->lock
);
581 opts
->func_inst
.free_func_inst
= eem_free_inst
;
582 opts
->net
= gether_setup_default();
583 if (IS_ERR(opts
->net
)) {
584 struct net_device
*net
= opts
->net
;
586 return ERR_CAST(net
);
589 config_group_init_type_name(&opts
->func_inst
.group
, "", &eem_func_type
);
591 return &opts
->func_inst
;
594 static void eem_free(struct usb_function
*f
)
597 struct f_eem_opts
*opts
;
599 eem
= func_to_eem(f
);
600 opts
= container_of(f
->fi
, struct f_eem_opts
, func_inst
);
602 mutex_lock(&opts
->lock
);
604 mutex_unlock(&opts
->lock
);
607 static void eem_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
609 DBG(c
->cdev
, "eem unbind\n");
611 usb_free_all_descriptors(f
);
614 static struct usb_function
*eem_alloc(struct usb_function_instance
*fi
)
617 struct f_eem_opts
*opts
;
619 /* allocate and initialize one new instance */
620 eem
= kzalloc(sizeof(*eem
), GFP_KERNEL
);
622 return ERR_PTR(-ENOMEM
);
624 opts
= container_of(fi
, struct f_eem_opts
, func_inst
);
625 mutex_lock(&opts
->lock
);
628 eem
->port
.ioport
= netdev_priv(opts
->net
);
629 mutex_unlock(&opts
->lock
);
630 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
632 eem
->port
.func
.name
= "cdc_eem";
633 /* descriptors are per-instance copies */
634 eem
->port
.func
.bind
= eem_bind
;
635 eem
->port
.func
.unbind
= eem_unbind
;
636 eem
->port
.func
.set_alt
= eem_set_alt
;
637 eem
->port
.func
.setup
= eem_setup
;
638 eem
->port
.func
.disable
= eem_disable
;
639 eem
->port
.func
.free_func
= eem_free
;
640 eem
->port
.wrap
= eem_wrap
;
641 eem
->port
.unwrap
= eem_unwrap
;
642 eem
->port
.header_len
= EEM_HLEN
;
644 return &eem
->port
.func
;
647 DECLARE_USB_FUNCTION_INIT(eem
, eem_alloc_inst
, eem_alloc
);
648 MODULE_LICENSE("GPL");
649 MODULE_AUTHOR("David Brownell");