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>
36 * This driver is an implementation of the CDC "Ethernet Emulation
37 * Model" (EEM) specification, which encapsulates Ethernet frames
38 * for transport over USB using a simpler USB device model than the
39 * previous CDC "Ethernet Control Model" (ECM, or "CDC Ethernet").
41 * For details, see www.usb.org/developers/devclass_docs/CDC_EEM10.pdf
43 * This version has been tested with GIGAntIC WuaoW SIM Smart Card on 2.6.24,
44 * 2.6.27 and 2.6.30rc2 kernel.
45 * It has also been validated on Openmoko Om 2008.12 (based on 2.6.24 kernel).
46 * build on 23-April-2009
49 #define EEM_HEAD 2 /* 2 byte header */
51 /*-------------------------------------------------------------------------*/
53 static void eem_linkcmd_complete(struct urb
*urb
)
55 dev_kfree_skb(urb
->context
);
59 static void eem_linkcmd(struct usbnet
*dev
, struct sk_buff
*skb
)
64 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
68 usb_fill_bulk_urb(urb
, dev
->udev
, dev
->out
,
69 skb
->data
, skb
->len
, eem_linkcmd_complete
, skb
);
71 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
76 devwarn(dev
, "link cmd failure\n");
81 static int eem_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
85 status
= usbnet_get_endpoints(dev
, intf
);
87 usb_set_intfdata(intf
, NULL
);
88 usb_driver_release_interface(driver_of(intf
), intf
);
92 /* no jumbogram (16K) support for now */
94 dev
->net
->hard_header_len
+= EEM_HEAD
+ ETH_FCS_LEN
;
100 * EEM permits packing multiple Ethernet frames into USB transfers
101 * (a "bundle"), but for TX we don't try to do that.
103 static struct sk_buff
*eem_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
106 struct sk_buff
*skb2
= NULL
;
111 /* When ((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket) is
112 * zero, stick two bytes of zero length EEM packet on the end.
113 * Else the framework would add invalid single byte padding,
114 * since it can't know whether ZLPs will be handled right by
115 * all the relevant hardware and software.
117 if (!((len
+ EEM_HEAD
+ ETH_FCS_LEN
) % dev
->maxpacket
))
120 if (!skb_cloned(skb
)) {
121 int headroom
= skb_headroom(skb
);
122 int tailroom
= skb_tailroom(skb
);
124 if ((tailroom
>= ETH_FCS_LEN
+ padlen
)
125 && (headroom
>= EEM_HEAD
))
128 if ((headroom
+ tailroom
)
129 > (EEM_HEAD
+ ETH_FCS_LEN
+ padlen
)) {
130 skb
->data
= memmove(skb
->head
+
134 skb_set_tail_pointer(skb
, len
);
139 skb2
= skb_copy_expand(skb
, EEM_HEAD
, ETH_FCS_LEN
+ padlen
, flags
);
143 dev_kfree_skb_any(skb
);
147 /* we don't use the "no Ethernet CRC" option */
148 crc
= crc32_le(~0, skb
->data
, skb
->len
);
151 put_unaligned_le32(crc
, skb_put(skb
, 4));
153 /* EEM packet header format:
154 * b0..13: length of ethernet frame
155 * b14: bmCRC (1 == valid Ethernet CRC)
156 * b15: bmType (0 == data)
159 put_unaligned_le16(BIT(14) | len
, skb_push(skb
, 2));
161 /* Bundle a zero length EEM packet if needed */
163 put_unaligned_le16(0, skb_put(skb
, 2));
168 static int eem_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
171 * Our task here is to strip off framing, leaving skb with one
172 * data frame for the usbnet framework code to process. But we
173 * may have received multiple EEM payloads, or command payloads.
174 * So we must process _everything_ as if it's a header, except
175 * maybe the last data payload
177 * REVISIT the framework needs updating so that when we consume
178 * all payloads (the last or only message was a command, or a
179 * zero length EEM packet) that is not accounted as an rx_error.
182 struct sk_buff
*skb2
= NULL
;
186 /* incomplete EEM header? */
187 if (skb
->len
< EEM_HEAD
)
191 * EEM packet header format:
192 * b0..14: EEM type dependant (Data or Command)
195 header
= get_unaligned_le16(skb
->data
);
196 skb_pull(skb
, EEM_HEAD
);
199 * The bmType bit helps to denote when EEM
200 * packet is data or command :
201 * bmType = 0 : EEM data payload
202 * bmType = 1 : EEM (link) command
204 if (header
& BIT(15)) {
208 * EEM (link) command packet:
209 * b0..10: bmEEMCmdParam
211 * b14: bmReserved (must be 0)
212 * b15: 1 (EEM command)
214 if (header
& BIT(14)) {
215 devdbg(dev
, "reserved command %04x\n", header
);
219 bmEEMCmd
= (header
>> 11) & 0x7;
222 /* Responding to echo requests is mandatory. */
223 case 0: /* Echo command */
224 len
= header
& 0x7FF;
230 skb2
= skb_clone(skb
, GFP_ATOMIC
);
234 put_unaligned_le16(BIT(15) | (1 << 11) | len
,
236 eem_linkcmd(dev
, skb2
);
240 * Host may choose to ignore hints.
241 * - suspend: peripheral ready to suspend
242 * - response: suggest N millisec polling
243 * - response complete: suggest N sec polling
245 case 2: /* Suspend hint */
246 case 3: /* Response hint */
247 case 4: /* Response complete hint */
251 * Hosts should never receive host-to-peripheral
252 * or reserved command codes; or responses to an
253 * echo command we didn't send.
255 case 1: /* Echo response */
257 default: /* reserved */
258 devwarn(dev
, "unexpected link command %d\n",
267 /* zero length EEM packet? */
272 * EEM data packet header :
273 * b0..13: length of ethernet frame
277 len
= header
& 0x3FFF;
279 /* bogus EEM payload? */
283 /* bogus ethernet frame? */
284 if (len
< (ETH_HLEN
+ ETH_FCS_LEN
))
288 * Treat the last payload differently: framework
289 * code expects our "fixup" to have stripped off
290 * headers, so "skb" is a data packet (or error).
291 * Else if it's not the last payload, keep "skb"
292 * for further processing.
294 is_last
= (len
== skb
->len
);
298 skb2
= skb_clone(skb
, GFP_ATOMIC
);
304 * The bmCRC helps to denote when the CRC field in
305 * the Ethernet frame contains a calculated CRC:
306 * bmCRC = 1 : CRC is calculated
307 * bmCRC = 0 : CRC = 0xDEADBEEF
309 if (header
& BIT(14)) {
310 crc
= get_unaligned_le32(skb2
->data
311 + len
- ETH_FCS_LEN
);
312 crc2
= ~crc32_le(~0, skb2
->data
, skb2
->len
315 crc
= get_unaligned_be32(skb2
->data
316 + len
- ETH_FCS_LEN
);
319 skb_trim(skb2
, len
- ETH_FCS_LEN
);
324 if (unlikely(crc
!= crc2
)) {
325 dev
->net
->stats
.rx_errors
++;
326 dev_kfree_skb_any(skb2
);
328 usbnet_skb_return(dev
, skb2
);
338 static const struct driver_info eem_info
= {
339 .description
= "CDC EEM Device",
342 .rx_fixup
= eem_rx_fixup
,
343 .tx_fixup
= eem_tx_fixup
,
346 /*-------------------------------------------------------------------------*/
348 static const struct usb_device_id products
[] = {
350 USB_INTERFACE_INFO(USB_CLASS_COMM
, USB_CDC_SUBCLASS_EEM
,
352 .driver_info
= (unsigned long) &eem_info
,
355 /* EMPTY == end of list */
358 MODULE_DEVICE_TABLE(usb
, products
);
360 static struct usb_driver eem_driver
= {
362 .id_table
= products
,
363 .probe
= usbnet_probe
,
364 .disconnect
= usbnet_disconnect
,
365 .suspend
= usbnet_suspend
,
366 .resume
= usbnet_resume
,
370 static int __init
eem_init(void)
372 return usb_register(&eem_driver
);
374 module_init(eem_init
);
376 static void __exit
eem_exit(void)
378 usb_deregister(&eem_driver
);
380 module_exit(eem_exit
);
382 MODULE_AUTHOR("Omar Laazimani <omar.oberthur@gmail.com>");
383 MODULE_DESCRIPTION("USB CDC EEM");
384 MODULE_LICENSE("GPL");