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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/kernel.h>
24 #include <linux/device.h>
25 #include <linux/etherdevice.h>
26 #include <linux/crc32.h>
33 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
38 struct usb_endpoint_descriptor
*in
;
39 struct usb_endpoint_descriptor
*out
;
46 struct eem_ep_descs fs
;
47 struct eem_ep_descs hs
;
50 static inline struct f_eem
*func_to_eem(struct usb_function
*f
)
52 return container_of(f
, struct f_eem
, port
.func
);
55 /*-------------------------------------------------------------------------*/
57 /* interface descriptor: */
59 static struct usb_interface_descriptor eem_intf __initdata
= {
60 .bLength
= sizeof eem_intf
,
61 .bDescriptorType
= USB_DT_INTERFACE
,
63 /* .bInterfaceNumber = DYNAMIC */
65 .bInterfaceClass
= USB_CLASS_COMM
,
66 .bInterfaceSubClass
= USB_CDC_SUBCLASS_EEM
,
67 .bInterfaceProtocol
= USB_CDC_PROTO_EEM
,
68 /* .iInterface = DYNAMIC */
71 /* full speed support: */
73 static struct usb_endpoint_descriptor eem_fs_in_desc __initdata
= {
74 .bLength
= USB_DT_ENDPOINT_SIZE
,
75 .bDescriptorType
= USB_DT_ENDPOINT
,
77 .bEndpointAddress
= USB_DIR_IN
,
78 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
81 static struct usb_endpoint_descriptor eem_fs_out_desc __initdata
= {
82 .bLength
= USB_DT_ENDPOINT_SIZE
,
83 .bDescriptorType
= USB_DT_ENDPOINT
,
85 .bEndpointAddress
= USB_DIR_OUT
,
86 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
89 static struct usb_descriptor_header
*eem_fs_function
[] __initdata
= {
90 /* CDC EEM control descriptors */
91 (struct usb_descriptor_header
*) &eem_intf
,
92 (struct usb_descriptor_header
*) &eem_fs_in_desc
,
93 (struct usb_descriptor_header
*) &eem_fs_out_desc
,
97 /* high speed support: */
99 static struct usb_endpoint_descriptor eem_hs_in_desc __initdata
= {
100 .bLength
= USB_DT_ENDPOINT_SIZE
,
101 .bDescriptorType
= USB_DT_ENDPOINT
,
103 .bEndpointAddress
= USB_DIR_IN
,
104 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
105 .wMaxPacketSize
= cpu_to_le16(512),
108 static struct usb_endpoint_descriptor eem_hs_out_desc __initdata
= {
109 .bLength
= USB_DT_ENDPOINT_SIZE
,
110 .bDescriptorType
= USB_DT_ENDPOINT
,
112 .bEndpointAddress
= USB_DIR_OUT
,
113 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
114 .wMaxPacketSize
= cpu_to_le16(512),
117 static struct usb_descriptor_header
*eem_hs_function
[] __initdata
= {
118 /* CDC EEM control descriptors */
119 (struct usb_descriptor_header
*) &eem_intf
,
120 (struct usb_descriptor_header
*) &eem_hs_in_desc
,
121 (struct usb_descriptor_header
*) &eem_hs_out_desc
,
125 /* string descriptors: */
127 static struct usb_string eem_string_defs
[] = {
128 [0].s
= "CDC Ethernet Emulation Model (EEM)",
129 { } /* end of list */
132 static struct usb_gadget_strings eem_string_table
= {
133 .language
= 0x0409, /* en-us */
134 .strings
= eem_string_defs
,
137 static struct usb_gadget_strings
*eem_strings
[] = {
142 /*-------------------------------------------------------------------------*/
144 static int eem_setup(struct usb_function
*f
, const struct usb_ctrlrequest
*ctrl
)
146 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
147 int value
= -EOPNOTSUPP
;
148 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
149 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
150 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
152 DBG(cdev
, "invalid control req%02x.%02x v%04x i%04x l%d\n",
153 ctrl
->bRequestType
, ctrl
->bRequest
,
154 w_value
, w_index
, w_length
);
156 /* device either stalls (value < 0) or reports success */
161 static int eem_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
163 struct f_eem
*eem
= func_to_eem(f
);
164 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
165 struct net_device
*net
;
167 /* we know alt == 0, so this is an activation or a reset */
171 if (intf
== eem
->ctrl_id
) {
173 if (eem
->port
.in_ep
->driver_data
) {
174 DBG(cdev
, "reset eem\n");
175 gether_disconnect(&eem
->port
);
179 DBG(cdev
, "init eem\n");
180 eem
->port
.in
= ep_choose(cdev
->gadget
,
181 eem
->hs
.in
, eem
->fs
.in
);
182 eem
->port
.out
= ep_choose(cdev
->gadget
,
183 eem
->hs
.out
, eem
->fs
.out
);
186 /* zlps should not occur because zero-length EEM packets
187 * will be inserted in those cases where they would occur
189 eem
->port
.is_zlp_ok
= 1;
190 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
191 DBG(cdev
, "activate eem\n");
192 net
= gether_connect(&eem
->port
);
203 static void eem_disable(struct usb_function
*f
)
205 struct f_eem
*eem
= func_to_eem(f
);
206 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
208 DBG(cdev
, "eem deactivated\n");
210 if (eem
->port
.in_ep
->driver_data
)
211 gether_disconnect(&eem
->port
);
214 /*-------------------------------------------------------------------------*/
216 /* EEM function driver setup/binding */
219 eem_bind(struct usb_configuration
*c
, struct usb_function
*f
)
221 struct usb_composite_dev
*cdev
= c
->cdev
;
222 struct f_eem
*eem
= func_to_eem(f
);
226 /* allocate instance-specific interface IDs */
227 status
= usb_interface_id(c
, f
);
230 eem
->ctrl_id
= status
;
231 eem_intf
.bInterfaceNumber
= status
;
235 /* allocate instance-specific endpoints */
236 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_in_desc
);
239 eem
->port
.in_ep
= ep
;
240 ep
->driver_data
= cdev
; /* claim */
242 ep
= usb_ep_autoconfig(cdev
->gadget
, &eem_fs_out_desc
);
245 eem
->port
.out_ep
= ep
;
246 ep
->driver_data
= cdev
; /* claim */
250 /* copy descriptors, and track endpoint copies */
251 f
->descriptors
= usb_copy_descriptors(eem_fs_function
);
255 eem
->fs
.in
= usb_find_endpoint(eem_fs_function
,
256 f
->descriptors
, &eem_fs_in_desc
);
257 eem
->fs
.out
= usb_find_endpoint(eem_fs_function
,
258 f
->descriptors
, &eem_fs_out_desc
);
260 /* support all relevant hardware speeds... we expect that when
261 * hardware is dual speed, all bulk-capable endpoints work at
264 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
265 eem_hs_in_desc
.bEndpointAddress
=
266 eem_fs_in_desc
.bEndpointAddress
;
267 eem_hs_out_desc
.bEndpointAddress
=
268 eem_fs_out_desc
.bEndpointAddress
;
270 /* copy descriptors, and track endpoint copies */
271 f
->hs_descriptors
= usb_copy_descriptors(eem_hs_function
);
272 if (!f
->hs_descriptors
)
275 eem
->hs
.in
= usb_find_endpoint(eem_hs_function
,
276 f
->hs_descriptors
, &eem_hs_in_desc
);
277 eem
->hs
.out
= usb_find_endpoint(eem_hs_function
,
278 f
->hs_descriptors
, &eem_hs_out_desc
);
281 DBG(cdev
, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
282 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
283 eem
->port
.in_ep
->name
, eem
->port
.out_ep
->name
);
288 usb_free_descriptors(f
->descriptors
);
290 /* we might as well release our claims on endpoints */
292 eem
->port
.out_ep
->driver_data
= NULL
;
294 eem
->port
.in_ep
->driver_data
= NULL
;
296 ERROR(cdev
, "%s: can't bind, err %d\n", f
->name
, status
);
302 eem_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
304 struct f_eem
*eem
= func_to_eem(f
);
306 DBG(c
->cdev
, "eem unbind\n");
308 if (gadget_is_dualspeed(c
->cdev
->gadget
))
309 usb_free_descriptors(f
->hs_descriptors
);
310 usb_free_descriptors(f
->descriptors
);
314 static void eem_cmd_complete(struct usb_ep
*ep
, struct usb_request
*req
)
319 * Add the EEM header and ethernet checksum.
320 * We currently do not attempt to put multiple ethernet frames
321 * into a single USB transfer
323 static struct sk_buff
*eem_wrap(struct gether
*port
, struct sk_buff
*skb
)
325 struct sk_buff
*skb2
= NULL
;
326 struct usb_ep
*in
= port
->in_ep
;
330 if (!skb_cloned(skb
)) {
331 int headroom
= skb_headroom(skb
);
332 int tailroom
= skb_tailroom(skb
);
334 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
335 * stick two bytes of zero-length EEM packet on the end.
337 if (((len
+ EEM_HLEN
+ ETH_FCS_LEN
) % in
->maxpacket
) == 0)
340 if ((tailroom
>= (ETH_FCS_LEN
+ padlen
)) &&
341 (headroom
>= EEM_HLEN
))
345 skb2
= skb_copy_expand(skb
, EEM_HLEN
, ETH_FCS_LEN
+ padlen
, GFP_ATOMIC
);
346 dev_kfree_skb_any(skb
);
352 /* use the "no CRC" option */
353 put_unaligned_be32(0xdeadbeef, skb_put(skb
, 4));
355 /* EEM packet header format:
356 * b0..13: length of ethernet frame
357 * b14: bmCRC (0 == sentinel CRC)
358 * b15: bmType (0 == data)
361 put_unaligned_le16((len
& 0x3FFF) | BIT(14), skb_push(skb
, 2));
363 /* add a zero-length EEM packet, if needed */
365 put_unaligned_le16(0, skb_put(skb
, 2));
371 * Remove the EEM header. Note that there can be many EEM packets in a single
372 * USB transfer, so we need to break them out and handle them independently.
374 static int eem_unwrap(struct gether
*port
,
376 struct sk_buff_head
*list
)
378 struct usb_composite_dev
*cdev
= port
->func
.config
->cdev
;
382 struct sk_buff
*skb2
;
386 if (skb
->len
< EEM_HLEN
) {
388 DBG(cdev
, "invalid EEM header\n");
392 /* remove the EEM header */
393 header
= get_unaligned_le16(skb
->data
);
394 skb_pull(skb
, EEM_HLEN
);
396 /* EEM packet header format:
397 * b0..14: EEM type dependent (data or command)
398 * b15: bmType (0 == data, 1 == command)
400 if (header
& BIT(15)) {
401 struct usb_request
*req
= cdev
->req
;
404 /* EEM command packet format:
405 * b0..10: bmEEMCmdParam
407 * b14: reserved (must be zero)
408 * b15: bmType (1 == command)
410 if (header
& BIT(14))
413 bmEEMCmd
= (header
>> 11) & 0x7;
416 len
= header
& 0x7FF;
417 if (skb
->len
< len
) {
422 skb2
= skb_clone(skb
, GFP_ATOMIC
);
423 if (unlikely(!skb2
)) {
424 DBG(cdev
, "EEM echo response error\n");
428 put_unaligned_le16(BIT(15) | BIT(11) | len
,
430 skb_copy_bits(skb
, 0, req
->buf
, skb
->len
);
431 req
->length
= skb
->len
;
432 req
->complete
= eem_cmd_complete
;
434 if (usb_ep_queue(port
->in_ep
, req
, GFP_ATOMIC
))
435 DBG(cdev
, "echo response queue fail\n");
438 case 1: /* echo response */
439 case 2: /* suspend hint */
440 case 3: /* response hint */
441 case 4: /* response complete hint */
443 default: /* reserved */
448 struct sk_buff
*skb3
;
450 /* check for zero-length EEM packet */
454 /* EEM data packet format:
455 * b0..13: length of ethernet frame
456 * b14: bmCRC (0 == sentinel, 1 == calculated)
457 * b15: bmType (0 == data)
459 len
= header
& 0x3FFF;
461 || (len
< (ETH_HLEN
+ ETH_FCS_LEN
))) {
467 crc
= get_unaligned_le32(skb
->data
+ len
- ETH_FCS_LEN
);
468 if (header
& BIT(14)) {
469 crc
= get_unaligned_le32(skb
->data
+ len
473 skb
->len
- ETH_FCS_LEN
);
475 crc
= get_unaligned_be32(skb
->data
+ len
480 DBG(cdev
, "invalid EEM CRC\n");
484 skb2
= skb_clone(skb
, GFP_ATOMIC
);
485 if (unlikely(!skb2
)) {
486 DBG(cdev
, "unable to unframe EEM packet\n");
489 skb_trim(skb2
, len
- ETH_FCS_LEN
);
491 skb3
= skb_copy_expand(skb2
,
495 if (unlikely(!skb3
)) {
496 DBG(cdev
, "unable to realign EEM packet\n");
497 dev_kfree_skb_any(skb2
);
500 dev_kfree_skb_any(skb2
);
501 skb_queue_tail(list
, skb3
);
508 dev_kfree_skb_any(skb
);
513 * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
514 * @c: the configuration to support the network link
515 * Context: single threaded during gadget setup
517 * Returns zero on success, else negative errno.
519 * Caller must have called @gether_setup(). Caller is also responsible
520 * for calling @gether_cleanup() before module unload.
522 int __init
eem_bind_config(struct usb_configuration
*c
)
527 /* maybe allocate device-global string IDs */
528 if (eem_string_defs
[0].id
== 0) {
530 /* control interface label */
531 status
= usb_string_id(c
->cdev
);
534 eem_string_defs
[0].id
= status
;
535 eem_intf
.iInterface
= status
;
538 /* allocate and initialize one new instance */
539 eem
= kzalloc(sizeof *eem
, GFP_KERNEL
);
543 eem
->port
.cdc_filter
= DEFAULT_FILTER
;
545 eem
->port
.func
.name
= "cdc_eem";
546 eem
->port
.func
.strings
= eem_strings
;
547 /* descriptors are per-instance copies */
548 eem
->port
.func
.bind
= eem_bind
;
549 eem
->port
.func
.unbind
= eem_unbind
;
550 eem
->port
.func
.set_alt
= eem_set_alt
;
551 eem
->port
.func
.setup
= eem_setup
;
552 eem
->port
.func
.disable
= eem_disable
;
553 eem
->port
.wrap
= eem_wrap
;
554 eem
->port
.unwrap
= eem_unwrap
;
555 eem
->port
.header_len
= EEM_HLEN
;
557 status
= usb_add_function(c
, &eem
->port
.func
);