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>
27 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
36 static inline struct f_eem
*func_to_eem(struct usb_function
*f
)
38 return container_of(f
, struct f_eem
, port
.func
);
41 /*-------------------------------------------------------------------------*/
43 /* interface descriptor: */
45 static struct usb_interface_descriptor eem_intf
= {
46 .bLength
= sizeof eem_intf
,
47 .bDescriptorType
= USB_DT_INTERFACE
,
49 /* .bInterfaceNumber = DYNAMIC */
51 .bInterfaceClass
= USB_CLASS_COMM
,
52 .bInterfaceSubClass
= USB_CDC_SUBCLASS_EEM
,
53 .bInterfaceProtocol
= USB_CDC_PROTO_EEM
,
54 /* .iInterface = DYNAMIC */
57 /* full speed support: */
59 static struct usb_endpoint_descriptor eem_fs_in_desc
= {
60 .bLength
= USB_DT_ENDPOINT_SIZE
,
61 .bDescriptorType
= USB_DT_ENDPOINT
,
63 .bEndpointAddress
= USB_DIR_IN
,
64 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
67 static struct usb_endpoint_descriptor eem_fs_out_desc
= {
68 .bLength
= USB_DT_ENDPOINT_SIZE
,
69 .bDescriptorType
= USB_DT_ENDPOINT
,
71 .bEndpointAddress
= USB_DIR_OUT
,
72 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
75 static struct usb_descriptor_header
*eem_fs_function
[] = {
76 /* CDC EEM control descriptors */
77 (struct usb_descriptor_header
*) &eem_intf
,
78 (struct usb_descriptor_header
*) &eem_fs_in_desc
,
79 (struct usb_descriptor_header
*) &eem_fs_out_desc
,
83 /* high speed support: */
85 static struct usb_endpoint_descriptor eem_hs_in_desc
= {
86 .bLength
= USB_DT_ENDPOINT_SIZE
,
87 .bDescriptorType
= USB_DT_ENDPOINT
,
89 .bEndpointAddress
= USB_DIR_IN
,
90 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
91 .wMaxPacketSize
= cpu_to_le16(512),
94 static struct usb_endpoint_descriptor eem_hs_out_desc
= {
95 .bLength
= USB_DT_ENDPOINT_SIZE
,
96 .bDescriptorType
= USB_DT_ENDPOINT
,
98 .bEndpointAddress
= USB_DIR_OUT
,
99 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
100 .wMaxPacketSize
= cpu_to_le16(512),
103 static struct usb_descriptor_header
*eem_hs_function
[] = {
104 /* CDC EEM control descriptors */
105 (struct usb_descriptor_header
*) &eem_intf
,
106 (struct usb_descriptor_header
*) &eem_hs_in_desc
,
107 (struct usb_descriptor_header
*) &eem_hs_out_desc
,
111 /* super speed support: */
113 static struct usb_endpoint_descriptor eem_ss_in_desc
= {
114 .bLength
= USB_DT_ENDPOINT_SIZE
,
115 .bDescriptorType
= USB_DT_ENDPOINT
,
117 .bEndpointAddress
= USB_DIR_IN
,
118 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
119 .wMaxPacketSize
= cpu_to_le16(1024),
122 static struct usb_endpoint_descriptor eem_ss_out_desc
= {
123 .bLength
= USB_DT_ENDPOINT_SIZE
,
124 .bDescriptorType
= USB_DT_ENDPOINT
,
126 .bEndpointAddress
= USB_DIR_OUT
,
127 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
128 .wMaxPacketSize
= cpu_to_le16(1024),
131 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc
= {
132 .bLength
= sizeof eem_ss_bulk_comp_desc
,
133 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
135 /* the following 2 values can be tweaked if necessary */
136 /* .bMaxBurst = 0, */
137 /* .bmAttributes = 0, */
140 static struct usb_descriptor_header
*eem_ss_function
[] = {
141 /* CDC EEM control descriptors */
142 (struct usb_descriptor_header
*) &eem_intf
,
143 (struct usb_descriptor_header
*) &eem_ss_in_desc
,
144 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
145 (struct usb_descriptor_header
*) &eem_ss_out_desc
,
146 (struct usb_descriptor_header
*) &eem_ss_bulk_comp_desc
,
150 /* string descriptors: */
152 static struct usb_string eem_string_defs
[] = {
153 [0].s
= "CDC Ethernet Emulation Model (EEM)",
154 { } /* end of list */
157 static struct usb_gadget_strings eem_string_table
= {
158 .language
= 0x0409, /* en-us */
159 .strings
= eem_string_defs
,
162 static struct usb_gadget_strings
*eem_strings
[] = {
167 /*-------------------------------------------------------------------------*/
169 static int eem_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
171 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
172 int value
= -EOPNOTSUPP
;
173 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
174 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
175 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
177 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
178 ctrl
->bRequestType
, ctrl
->bRequest
,
179 w_value
, w_index
, w_length
);
181 /* device either stalls (value < 0) or reports success */
186 static int eem_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
188 struct f_eem
*eem
= func_to_eem(f
);
189 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
190 struct net_device
*net
;
192 /* we know alt == 0, so this is an activation or a reset */
196 if (intf
== eem
->ctrl_id
) {
198 if (eem
->port
.in_ep
->driver_data
) {
199 DBG(cdev
, "reset eem\n");
200 gether_disconnect(&eem
->port
);
203 if (!eem
->port
.in_ep
->desc
|| !eem
->port
.out_ep
->desc
) {
204 DBG(cdev
, "init eem\n");
205 if (config_ep_by_speed(cdev
->gadget
, f
,
207 config_ep_by_speed(cdev
->gadget
, f
,
209 eem
->port
.in_ep
->desc
= NULL
;
210 eem
->port
.out_ep
->desc
= NULL
;
215 /* zlps should not occur because zero-length EEM packets
216 * will be inserted in those cases where they would occur
218 eem
->port
.is_zlp_ok
= 1;
219 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
220 DBG(cdev
, "activate eem\n");
221 net
= gether_connect(&eem
->port
);
232 static void eem_disable(struct usb_function
*f
)
234 struct f_eem
*eem
= func_to_eem(f
);
235 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
237 DBG(cdev
, "eem deactivated\n");
239 if (eem
->port
.in_ep
->driver_data
)
240 gether_disconnect(&eem
->port
);
243 /*-------------------------------------------------------------------------*/
245 /* EEM function driver setup/binding */
247 static int eem_bind(struct usb_configuration
*c
, struct usb_function
*f
)
249 struct usb_composite_dev
*cdev
= c
->cdev
;
250 struct f_eem
*eem
= func_to_eem(f
);
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 gether_set_gadget(eem_opts
->net
, cdev
->gadget
);
266 status
= gether_register_netdev(eem_opts
->net
);
269 eem_opts
->bound
= true;
272 /* maybe allocate device-global string IDs */
273 if (eem_string_defs
[0].id
== 0) {
275 /* control interface label */
276 status
= usb_string_id(c
->cdev
);
279 eem_string_defs
[0].id
= status
;
280 eem_intf
.iInterface
= status
;
283 /* allocate instance-specific interface IDs */
284 status
= usb_interface_id(c
, f
);
287 eem
->ctrl_id
= status
;
288 eem_intf
.bInterfaceNumber
= status
;
292 /* allocate instance-specific endpoints */
293 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_in_desc
);
296 eem
->port
.in_ep
= ep
;
297 ep
->driver_data
= cdev
; /* claim */
299 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_out_desc
);
302 eem
->port
.out_ep
= ep
;
303 ep
->driver_data
= cdev
; /* claim */
307 /* support all relevant hardware speeds... we expect that when
308 * hardware is dual speed, all bulk-capable endpoints work at
311 eem_hs_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
312 eem_hs_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
314 eem_ss_in_desc
.bEndpointAddress
= eem_fs_in_desc
.bEndpointAddress
;
315 eem_ss_out_desc
.bEndpointAddress
= eem_fs_out_desc
.bEndpointAddress
;
317 status
= usb_assign_descriptors(f
, eem_fs_function
, eem_hs_function
,
322 DBG(cdev
, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
323 gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
324 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
325 eem
->port
.in_ep
->name
, eem
->port
.out_ep
->name
);
329 usb_free_all_descriptors(f
);
330 if (eem
->port
.out_ep
)
331 eem
->port
.out_ep
->driver_data
= NULL
;
333 eem
->port
.in_ep
->driver_data
= NULL
;
335 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
340 static void eem_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
342 struct sk_buff
*skb
= (struct sk_buff
*)req
->context
;
344 dev_kfree_skb_any(skb
);
348 * Add the EEM header and ethernet checksum.
349 * We currently do not attempt to put multiple ethernet frames
350 * into a single USB transfer
352 static struct sk_buff
*eem_wrap(struct gether
*port
, struct sk_buff
*skb
)
354 struct sk_buff
*skb2
= NULL
;
355 struct usb_ep
*in
= port
->in_ep
;
359 if (!skb_cloned(skb
)) {
360 int headroom
= skb_headroom(skb
);
361 int tailroom
= skb_tailroom(skb
);
363 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
364 * stick two bytes of zero-length EEM packet on the end.
366 if (((len
+ EEM_HLEN
+ ETH_FCS_LEN
) % in
->maxpacket
) == 0)
369 if ((tailroom
>= (ETH_FCS_LEN
+ padlen
)) &&
370 (headroom
>= EEM_HLEN
))
374 skb2
= skb_copy_expand(skb
, EEM_HLEN
, ETH_FCS_LEN
+ padlen
, GFP_ATOMIC
);
375 dev_kfree_skb_any(skb
);
381 /* use the "no CRC" option */
382 put_unaligned_be32(0xdeadbeef, skb_put(skb
, 4));
384 /* EEM packet header format:
385 * b0..13: length of ethernet frame
386 * b14: bmCRC (0 == sentinel CRC)
387 * b15: bmType (0 == data)
390 put_unaligned_le16(len
& 0x3FFF, skb_push(skb
, 2));
392 /* add a zero-length EEM packet, if needed */
394 put_unaligned_le16(0, skb_put(skb
, 2));
400 * Remove the EEM header. Note that there can be many EEM packets in a single
401 * USB transfer, so we need to break them out and handle them independently.
403 static int eem_unwrap(struct gether
*port
,
405 struct sk_buff_head
*list
)
407 struct usb_composite_dev
*cdev
= port
->func
.config
->cdev
;
411 struct sk_buff
*skb2
;
415 if (skb
->len
< EEM_HLEN
) {
417 DBG(cdev
, "invalid EEM header\n");
421 /* remove the EEM header */
422 header
= get_unaligned_le16(skb
->data
);
423 skb_pull(skb
, EEM_HLEN
);
425 /* EEM packet header format:
426 * b0..14: EEM type dependent (data or command)
427 * b15: bmType (0 == data, 1 == command)
429 if (header
& BIT(15)) {
430 struct usb_request
*req
= cdev
->req
;
433 /* EEM command packet format:
434 * b0..10: bmEEMCmdParam
436 * b14: reserved (must be zero)
437 * b15: bmType (1 == command)
439 if (header
& BIT(14))
442 bmEEMCmd
= (header
>> 11) & 0x7;
445 len
= header
& 0x7FF;
446 if (skb
->len
< len
) {
451 skb2
= skb_clone(skb
, GFP_ATOMIC
);
452 if (unlikely(!skb2
)) {
453 DBG(cdev
, "EEM echo response error\n");
457 put_unaligned_le16(BIT(15) | BIT(11) | len
,
459 skb_copy_bits(skb2
, 0, req
->buf
, skb2
->len
);
460 req
->length
= skb2
->len
;
461 req
->complete
= eem_cmd_complete
;
464 if (usb_ep_queue(port
->in_ep
, req
, GFP_ATOMIC
))
465 DBG(cdev
, "echo response queue fail\n");
468 case 1: /* echo response */
469 case 2: /* suspend hint */
470 case 3: /* response hint */
471 case 4: /* response complete hint */
473 default: /* reserved */
478 struct sk_buff
*skb3
;
480 /* check for zero-length EEM packet */
484 /* EEM data packet format:
485 * b0..13: length of ethernet frame
486 * b14: bmCRC (0 == sentinel, 1 == calculated)
487 * b15: bmType (0 == data)
489 len
= header
& 0x3FFF;
491 || (len
< (ETH_HLEN
+ ETH_FCS_LEN
))) {
497 if (header
& BIT(14)) {
498 crc
= get_unaligned_le32(skb
->data
+ len
501 skb
->data
, len
- ETH_FCS_LEN
);
503 crc
= get_unaligned_be32(skb
->data
+ len
508 DBG(cdev
, "invalid EEM CRC\n");
512 skb2
= skb_clone(skb
, GFP_ATOMIC
);
513 if (unlikely(!skb2
)) {
514 DBG(cdev
, "unable to unframe EEM packet\n");
517 skb_trim(skb2
, len
- ETH_FCS_LEN
);
519 skb3
= skb_copy_expand(skb2
,
523 if (unlikely(!skb3
)) {
524 DBG(cdev
, "unable to realign EEM packet\n");
525 dev_kfree_skb_any(skb2
);
528 dev_kfree_skb_any(skb2
);
529 skb_queue_tail(list
, skb3
);
536 dev_kfree_skb_any(skb
);
540 static void eem_free_inst(struct usb_function_instance
*f
)
542 struct f_eem_opts
*opts
;
544 opts
= container_of(f
, struct f_eem_opts
, func_inst
);
546 gether_cleanup(netdev_priv(opts
->net
));
548 free_netdev(opts
->net
);
552 static struct usb_function_instance
*eem_alloc_inst(void)
554 struct f_eem_opts
*opts
;
556 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
558 return ERR_PTR(-ENOMEM
);
559 opts
->func_inst
.free_func_inst
= eem_free_inst
;
560 opts
->net
= gether_setup_default();
561 if (IS_ERR(opts
->net
))
562 return ERR_CAST(opts
->net
);
564 return &opts
->func_inst
;
567 static void eem_free(struct usb_function
*f
)
571 eem
= func_to_eem(f
);
575 static void eem_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
577 DBG(c
->cdev
, "eem unbind\n");
579 usb_free_all_descriptors(f
);
582 struct usb_function
*eem_alloc(struct usb_function_instance
*fi
)
585 struct f_eem_opts
*opts
;
587 /* allocate and initialize one new instance */
588 eem
= kzalloc(sizeof(*eem
), GFP_KERNEL
);
590 return ERR_PTR(-ENOMEM
);
592 opts
= container_of(fi
, struct f_eem_opts
, func_inst
);
594 eem
->port
.ioport
= netdev_priv(opts
->net
);
595 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
597 eem
->port
.func
.name
= "cdc_eem";
598 eem
->port
.func
.strings
= eem_strings
;
599 /* descriptors are per-instance copies */
600 eem
->port
.func
.bind
= eem_bind
;
601 eem
->port
.func
.unbind
= eem_unbind
;
602 eem
->port
.func
.set_alt
= eem_set_alt
;
603 eem
->port
.func
.setup
= eem_setup
;
604 eem
->port
.func
.disable
= eem_disable
;
605 eem
->port
.func
.free_func
= eem_free
;
606 eem
->port
.wrap
= eem_wrap
;
607 eem
->port
.unwrap
= eem_unwrap
;
608 eem
->port
.header_len
= EEM_HLEN
;
610 return &eem
->port
.func
;
613 DECLARE_USB_FUNCTION_INIT(eem
, eem_alloc_inst
, eem_alloc
);
614 MODULE_LICENSE("GPL");
615 MODULE_AUTHOR("David Brownell");