1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB CDC EEM network interface driver
4 * Copyright (C) 2009 Oberthur Technologies
5 * by Omar Laazimani, Olivier Condemine
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/ctype.h>
12 #include <linux/ethtool.h>
13 #include <linux/workqueue.h>
14 #include <linux/mii.h>
15 #include <linux/usb.h>
16 #include <linux/crc32.h>
17 #include <linux/usb/cdc.h>
18 #include <linux/usb/usbnet.h>
19 #include <linux/gfp.h>
20 #include <linux/if_vlan.h>
24 * This driver is an implementation of the CDC "Ethernet Emulation
25 * Model" (EEM) specification, which encapsulates Ethernet frames
26 * for transport over USB using a simpler USB device model than the
27 * previous CDC "Ethernet Control Model" (ECM, or "CDC Ethernet").
29 * For details, see https://usb.org/sites/default/files/CDC_EEM10.pdf
31 * This version has been tested with GIGAntIC WuaoW SIM Smart Card on 2.6.24,
32 * 2.6.27 and 2.6.30rc2 kernel.
33 * It has also been validated on Openmoko Om 2008.12 (based on 2.6.24 kernel).
34 * build on 23-April-2009
37 #define EEM_HEAD 2 /* 2 byte header */
39 /*-------------------------------------------------------------------------*/
41 static void eem_linkcmd_complete(struct urb
*urb
)
43 dev_kfree_skb(urb
->context
);
47 static void eem_linkcmd(struct usbnet
*dev
, struct sk_buff
*skb
)
52 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
56 usb_fill_bulk_urb(urb
, dev
->udev
, dev
->out
,
57 skb
->data
, skb
->len
, eem_linkcmd_complete
, skb
);
59 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
64 netdev_warn(dev
->net
, "link cmd failure\n");
69 static int eem_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
73 status
= usbnet_get_endpoints(dev
, intf
);
77 /* no jumbogram (16K) support for now */
79 dev
->net
->hard_header_len
+= EEM_HEAD
+ ETH_FCS_LEN
+ VLAN_HLEN
;
80 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
86 * EEM permits packing multiple Ethernet frames into USB transfers
87 * (a "bundle"), but for TX we don't try to do that.
89 static struct sk_buff
*eem_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
92 struct sk_buff
*skb2
= NULL
;
97 /* When ((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket) is
98 * zero, stick two bytes of zero length EEM packet on the end.
99 * Else the framework would add invalid single byte padding,
100 * since it can't know whether ZLPs will be handled right by
101 * all the relevant hardware and software.
103 if (!((len
+ EEM_HEAD
+ ETH_FCS_LEN
) % dev
->maxpacket
))
106 if (!skb_cloned(skb
)) {
107 int headroom
= skb_headroom(skb
);
108 int tailroom
= skb_tailroom(skb
);
110 if ((tailroom
>= ETH_FCS_LEN
+ padlen
) &&
111 (headroom
>= EEM_HEAD
))
114 if ((headroom
+ tailroom
)
115 > (EEM_HEAD
+ ETH_FCS_LEN
+ padlen
)) {
116 skb
->data
= memmove(skb
->head
+
120 skb_set_tail_pointer(skb
, len
);
125 skb2
= skb_copy_expand(skb
, EEM_HEAD
, ETH_FCS_LEN
+ padlen
, flags
);
126 dev_kfree_skb_any(skb
);
133 /* we don't use the "no Ethernet CRC" option */
134 crc
= crc32_le(~0, skb
->data
, skb
->len
);
137 put_unaligned_le32(crc
, skb_put(skb
, 4));
139 /* EEM packet header format:
140 * b0..13: length of ethernet frame
141 * b14: bmCRC (1 == valid Ethernet CRC)
142 * b15: bmType (0 == data)
145 put_unaligned_le16(BIT(14) | len
, skb_push(skb
, 2));
147 /* Bundle a zero length EEM packet if needed */
149 put_unaligned_le16(0, skb_put(skb
, 2));
154 static int eem_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
157 * Our task here is to strip off framing, leaving skb with one
158 * data frame for the usbnet framework code to process. But we
159 * may have received multiple EEM payloads, or command payloads.
160 * So we must process _everything_ as if it's a header, except
161 * maybe the last data payload
163 * REVISIT the framework needs updating so that when we consume
164 * all payloads (the last or only message was a command, or a
165 * zero length EEM packet) that is not accounted as an rx_error.
168 struct sk_buff
*skb2
= NULL
;
172 /* incomplete EEM header? */
173 if (skb
->len
< EEM_HEAD
)
177 * EEM packet header format:
178 * b0..14: EEM type dependent (Data or Command)
181 header
= get_unaligned_le16(skb
->data
);
182 skb_pull(skb
, EEM_HEAD
);
185 * The bmType bit helps to denote when EEM
186 * packet is data or command :
187 * bmType = 0 : EEM data payload
188 * bmType = 1 : EEM (link) command
190 if (header
& BIT(15)) {
194 * EEM (link) command packet:
195 * b0..10: bmEEMCmdParam
197 * b14: bmReserved (must be 0)
198 * b15: 1 (EEM command)
200 if (header
& BIT(14)) {
201 netdev_dbg(dev
->net
, "reserved command %04x\n",
206 bmEEMCmd
= (header
>> 11) & 0x7;
209 /* Responding to echo requests is mandatory. */
210 case 0: /* Echo command */
211 len
= header
& 0x7FF;
217 skb2
= skb_clone(skb
, GFP_ATOMIC
);
221 put_unaligned_le16(BIT(15) | BIT(11) | len
,
223 eem_linkcmd(dev
, skb2
);
227 * Host may choose to ignore hints.
228 * - suspend: peripheral ready to suspend
229 * - response: suggest N millisec polling
230 * - response complete: suggest N sec polling
232 * Suspend is reported and maybe heeded.
234 case 2: /* Suspend hint */
235 usbnet_device_suggests_idle(dev
);
237 case 3: /* Response hint */
238 case 4: /* Response complete hint */
242 * Hosts should never receive host-to-peripheral
243 * or reserved command codes; or responses to an
244 * echo command we didn't send.
246 case 1: /* Echo response */
248 default: /* reserved */
249 netdev_warn(dev
->net
,
250 "unexpected link command %d\n",
259 /* zero length EEM packet? */
264 * EEM data packet header :
265 * b0..13: length of ethernet frame
269 len
= header
& 0x3FFF;
271 /* bogus EEM payload? */
275 /* bogus ethernet frame? */
276 if (len
< (ETH_HLEN
+ ETH_FCS_LEN
))
280 * Treat the last payload differently: framework
281 * code expects our "fixup" to have stripped off
282 * headers, so "skb" is a data packet (or error).
283 * Else if it's not the last payload, keep "skb"
284 * for further processing.
286 is_last
= (len
== skb
->len
);
290 skb2
= skb_clone(skb
, GFP_ATOMIC
);
296 * The bmCRC helps to denote when the CRC field in
297 * the Ethernet frame contains a calculated CRC:
298 * bmCRC = 1 : CRC is calculated
299 * bmCRC = 0 : CRC = 0xDEADBEEF
301 if (header
& BIT(14)) {
302 crc
= get_unaligned_le32(skb2
->data
303 + len
- ETH_FCS_LEN
);
304 crc2
= ~crc32_le(~0, skb2
->data
, skb2
->len
307 crc
= get_unaligned_be32(skb2
->data
308 + len
- ETH_FCS_LEN
);
311 skb_trim(skb2
, len
- ETH_FCS_LEN
);
316 if (unlikely(crc
!= crc2
)) {
317 dev
->net
->stats
.rx_errors
++;
318 dev_kfree_skb_any(skb2
);
320 usbnet_skb_return(dev
, skb2
);
330 static const struct driver_info eem_info
= {
331 .description
= "CDC EEM Device",
332 .flags
= FLAG_ETHER
| FLAG_POINTTOPOINT
,
334 .rx_fixup
= eem_rx_fixup
,
335 .tx_fixup
= eem_tx_fixup
,
338 /*-------------------------------------------------------------------------*/
340 static const struct usb_device_id products
[] = {
342 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_EEM
,
344 .driver_info
= (unsigned long) &eem_info
,
347 /* EMPTY == end of list */
350 MODULE_DEVICE_TABLE(usb
, products
);
352 static struct usb_driver eem_driver
= {
354 .id_table
= products
,
355 .probe
= usbnet_probe
,
356 .disconnect
= usbnet_disconnect
,
357 .suspend
= usbnet_suspend
,
358 .resume
= usbnet_resume
,
359 .disable_hub_initiated_lpm
= 1,
362 module_usb_driver(eem_driver
);
364 MODULE_AUTHOR("Omar Laazimani <omar.oberthur@gmail.com>");
365 MODULE_DESCRIPTION("USB CDC EEM");
366 MODULE_LICENSE("GPL");