2 * USB CDC EEM network interface driver
3 * Copyright (C) 2009 Oberthur Technologies
4 * by Omar Laazimani, Olivier Condemine
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ctype.h>
26 #include <linux/ethtool.h>
27 #include <linux/workqueue.h>
28 #include <linux/mii.h>
29 #include <linux/usb.h>
30 #include <linux/crc32.h>
31 #include <linux/usb/cdc.h>
32 #include <linux/usb/usbnet.h>
33 #include <linux/gfp.h>
34 #include <linux/if_vlan.h>
38 * This driver is an implementation of the CDC "Ethernet Emulation
39 * Model" (EEM) specification, which encapsulates Ethernet frames
40 * for transport over USB using a simpler USB device model than the
41 * previous CDC "Ethernet Control Model" (ECM, or "CDC Ethernet").
43 * For details, see www.usb.org/developers/devclass_docs/CDC_EEM10.pdf
45 * This version has been tested with GIGAntIC WuaoW SIM Smart Card on 2.6.24,
46 * 2.6.27 and 2.6.30rc2 kernel.
47 * It has also been validated on Openmoko Om 2008.12 (based on 2.6.24 kernel).
48 * build on 23-April-2009
51 #define EEM_HEAD 2 /* 2 byte header */
53 /*-------------------------------------------------------------------------*/
55 static void eem_linkcmd_complete(struct urb
*urb
)
57 dev_kfree_skb(urb
->context
);
61 static void eem_linkcmd(struct usbnet
*dev
, struct sk_buff
*skb
)
66 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
70 usb_fill_bulk_urb(urb
, dev
->udev
, dev
->out
,
71 skb
->data
, skb
->len
, eem_linkcmd_complete
, skb
);
73 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
78 netdev_warn(dev
->net
, "link cmd failure\n");
83 static int eem_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
87 status
= usbnet_get_endpoints(dev
, intf
);
89 usb_set_intfdata(intf
, NULL
);
90 usb_driver_release_interface(driver_of(intf
), intf
);
94 /* no jumbogram (16K) support for now */
96 dev
->net
->hard_header_len
+= EEM_HEAD
+ ETH_FCS_LEN
+ VLAN_HLEN
;
97 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
103 * EEM permits packing multiple Ethernet frames into USB transfers
104 * (a "bundle"), but for TX we don't try to do that.
106 static struct sk_buff
*eem_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
109 struct sk_buff
*skb2
= NULL
;
114 /* When ((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket) is
115 * zero, stick two bytes of zero length EEM packet on the end.
116 * Else the framework would add invalid single byte padding,
117 * since it can't know whether ZLPs will be handled right by
118 * all the relevant hardware and software.
120 if (!((len
+ EEM_HEAD
+ ETH_FCS_LEN
) % dev
->maxpacket
))
123 if (!skb_cloned(skb
)) {
124 int headroom
= skb_headroom(skb
);
125 int tailroom
= skb_tailroom(skb
);
127 if ((tailroom
>= ETH_FCS_LEN
+ padlen
) &&
128 (headroom
>= EEM_HEAD
))
131 if ((headroom
+ tailroom
)
132 > (EEM_HEAD
+ ETH_FCS_LEN
+ padlen
)) {
133 skb
->data
= memmove(skb
->head
+
137 skb_set_tail_pointer(skb
, len
);
142 skb2
= skb_copy_expand(skb
, EEM_HEAD
, ETH_FCS_LEN
+ padlen
, flags
);
146 dev_kfree_skb_any(skb
);
150 /* we don't use the "no Ethernet CRC" option */
151 crc
= crc32_le(~0, skb
->data
, skb
->len
);
154 put_unaligned_le32(crc
, skb_put(skb
, 4));
156 /* EEM packet header format:
157 * b0..13: length of ethernet frame
158 * b14: bmCRC (1 == valid Ethernet CRC)
159 * b15: bmType (0 == data)
162 put_unaligned_le16(BIT(14) | len
, skb_push(skb
, 2));
164 /* Bundle a zero length EEM packet if needed */
166 put_unaligned_le16(0, skb_put(skb
, 2));
171 static int eem_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
174 * Our task here is to strip off framing, leaving skb with one
175 * data frame for the usbnet framework code to process. But we
176 * may have received multiple EEM payloads, or command payloads.
177 * So we must process _everything_ as if it's a header, except
178 * maybe the last data payload
180 * REVISIT the framework needs updating so that when we consume
181 * all payloads (the last or only message was a command, or a
182 * zero length EEM packet) that is not accounted as an rx_error.
185 struct sk_buff
*skb2
= NULL
;
189 /* incomplete EEM header? */
190 if (skb
->len
< EEM_HEAD
)
194 * EEM packet header format:
195 * b0..14: EEM type dependent (Data or Command)
198 header
= get_unaligned_le16(skb
->data
);
199 skb_pull(skb
, EEM_HEAD
);
202 * The bmType bit helps to denote when EEM
203 * packet is data or command :
204 * bmType = 0 : EEM data payload
205 * bmType = 1 : EEM (link) command
207 if (header
& BIT(15)) {
211 * EEM (link) command packet:
212 * b0..10: bmEEMCmdParam
214 * b14: bmReserved (must be 0)
215 * b15: 1 (EEM command)
217 if (header
& BIT(14)) {
218 netdev_dbg(dev
->net
, "reserved command %04x\n",
223 bmEEMCmd
= (header
>> 11) & 0x7;
226 /* Responding to echo requests is mandatory. */
227 case 0: /* Echo command */
228 len
= header
& 0x7FF;
234 skb2
= skb_clone(skb
, GFP_ATOMIC
);
238 put_unaligned_le16(BIT(15) | (1 << 11) | len
,
240 eem_linkcmd(dev
, skb2
);
244 * Host may choose to ignore hints.
245 * - suspend: peripheral ready to suspend
246 * - response: suggest N millisec polling
247 * - response complete: suggest N sec polling
249 * Suspend is reported and maybe heeded.
251 case 2: /* Suspend hint */
252 usbnet_device_suggests_idle(dev
);
254 case 3: /* Response hint */
255 case 4: /* Response complete hint */
259 * Hosts should never receive host-to-peripheral
260 * or reserved command codes; or responses to an
261 * echo command we didn't send.
263 case 1: /* Echo response */
265 default: /* reserved */
266 netdev_warn(dev
->net
,
267 "unexpected link command %d\n",
276 /* zero length EEM packet? */
281 * EEM data packet header :
282 * b0..13: length of ethernet frame
286 len
= header
& 0x3FFF;
288 /* bogus EEM payload? */
292 /* bogus ethernet frame? */
293 if (len
< (ETH_HLEN
+ ETH_FCS_LEN
))
297 * Treat the last payload differently: framework
298 * code expects our "fixup" to have stripped off
299 * headers, so "skb" is a data packet (or error).
300 * Else if it's not the last payload, keep "skb"
301 * for further processing.
303 is_last
= (len
== skb
->len
);
307 skb2
= skb_clone(skb
, GFP_ATOMIC
);
313 * The bmCRC helps to denote when the CRC field in
314 * the Ethernet frame contains a calculated CRC:
315 * bmCRC = 1 : CRC is calculated
316 * bmCRC = 0 : CRC = 0xDEADBEEF
318 if (header
& BIT(14)) {
319 crc
= get_unaligned_le32(skb2
->data
320 + len
- ETH_FCS_LEN
);
321 crc2
= ~crc32_le(~0, skb2
->data
, skb2
->len
324 crc
= get_unaligned_be32(skb2
->data
325 + len
- ETH_FCS_LEN
);
328 skb_trim(skb2
, len
- ETH_FCS_LEN
);
333 if (unlikely(crc
!= crc2
)) {
334 dev
->net
->stats
.rx_errors
++;
335 dev_kfree_skb_any(skb2
);
337 usbnet_skb_return(dev
, skb2
);
347 static const struct driver_info eem_info
= {
348 .description
= "CDC EEM Device",
349 .flags
= FLAG_ETHER
| FLAG_POINTTOPOINT
,
351 .rx_fixup
= eem_rx_fixup
,
352 .tx_fixup
= eem_tx_fixup
,
355 /*-------------------------------------------------------------------------*/
357 static const struct usb_device_id products
[] = {
359 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_EEM
,
361 .driver_info
= (unsigned long) &eem_info
,
364 /* EMPTY == end of list */
367 MODULE_DEVICE_TABLE(usb
, products
);
369 static struct usb_driver eem_driver
= {
371 .id_table
= products
,
372 .probe
= usbnet_probe
,
373 .disconnect
= usbnet_disconnect
,
374 .suspend
= usbnet_suspend
,
375 .resume
= usbnet_resume
,
376 .disable_hub_initiated_lpm
= 1,
379 module_usb_driver(eem_driver
);
381 MODULE_AUTHOR("Omar Laazimani <omar.oberthur@gmail.com>");
382 MODULE_DESCRIPTION("USB CDC EEM");
383 MODULE_LICENSE("GPL");