19f722c09SOmar Laazimani /* 29f722c09SOmar Laazimani * USB CDC EEM network interface driver 39f722c09SOmar Laazimani * Copyright (C) 2009 Oberthur Technologies 49f722c09SOmar Laazimani * by Omar Laazimani, Olivier Condemine 59f722c09SOmar Laazimani * 69f722c09SOmar Laazimani * This program is free software; you can redistribute it and/or modify 79f722c09SOmar Laazimani * it under the terms of the GNU General Public License as published by 89f722c09SOmar Laazimani * the Free Software Foundation; either version 2 of the License, or 99f722c09SOmar Laazimani * (at your option) any later version. 109f722c09SOmar Laazimani * 119f722c09SOmar Laazimani * This program is distributed in the hope that it will be useful, 129f722c09SOmar Laazimani * but WITHOUT ANY WARRANTY; without even the implied warranty of 139f722c09SOmar Laazimani * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 149f722c09SOmar Laazimani * GNU General Public License for more details. 159f722c09SOmar Laazimani * 169f722c09SOmar Laazimani * You should have received a copy of the GNU General Public License 179f722c09SOmar Laazimani * along with this program; if not, write to the Free Software 189f722c09SOmar Laazimani * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 199f722c09SOmar Laazimani */ 209f722c09SOmar Laazimani 219f722c09SOmar Laazimani #include <linux/module.h> 229f722c09SOmar Laazimani #include <linux/init.h> 239f722c09SOmar Laazimani #include <linux/netdevice.h> 249f722c09SOmar Laazimani #include <linux/etherdevice.h> 259f722c09SOmar Laazimani #include <linux/ctype.h> 269f722c09SOmar Laazimani #include <linux/ethtool.h> 279f722c09SOmar Laazimani #include <linux/workqueue.h> 289f722c09SOmar Laazimani #include <linux/mii.h> 299f722c09SOmar Laazimani #include <linux/usb.h> 309f722c09SOmar Laazimani #include <linux/crc32.h> 319f722c09SOmar Laazimani #include <linux/usb/cdc.h> 329f722c09SOmar Laazimani #include <linux/usb/usbnet.h> 339f722c09SOmar Laazimani 349f722c09SOmar Laazimani 359f722c09SOmar Laazimani /* 369f722c09SOmar Laazimani * This driver is an implementation of the CDC "Ethernet Emulation 379f722c09SOmar Laazimani * Model" (EEM) specification, which encapsulates Ethernet frames 389f722c09SOmar Laazimani * for transport over USB using a simpler USB device model than the 399f722c09SOmar Laazimani * previous CDC "Ethernet Control Model" (ECM, or "CDC Ethernet"). 409f722c09SOmar Laazimani * 419f722c09SOmar Laazimani * For details, see www.usb.org/developers/devclass_docs/CDC_EEM10.pdf 429f722c09SOmar Laazimani * 439f722c09SOmar Laazimani * This version has been tested with GIGAntIC WuaoW SIM Smart Card on 2.6.24, 449f722c09SOmar Laazimani * 2.6.27 and 2.6.30rc2 kernel. 459f722c09SOmar Laazimani * It has also been validated on Openmoko Om 2008.12 (based on 2.6.24 kernel). 469f722c09SOmar Laazimani * build on 23-April-2009 479f722c09SOmar Laazimani */ 489f722c09SOmar Laazimani 499f722c09SOmar Laazimani #define EEM_HEAD 2 /* 2 byte header */ 509f722c09SOmar Laazimani 519f722c09SOmar Laazimani /*-------------------------------------------------------------------------*/ 529f722c09SOmar Laazimani 539f722c09SOmar Laazimani static void eem_linkcmd_complete(struct urb *urb) 549f722c09SOmar Laazimani { 559f722c09SOmar Laazimani dev_kfree_skb(urb->context); 569f722c09SOmar Laazimani usb_free_urb(urb); 579f722c09SOmar Laazimani } 589f722c09SOmar Laazimani 599f722c09SOmar Laazimani static void eem_linkcmd(struct usbnet *dev, struct sk_buff *skb) 609f722c09SOmar Laazimani { 619f722c09SOmar Laazimani struct urb *urb; 629f722c09SOmar Laazimani int status; 639f722c09SOmar Laazimani 649f722c09SOmar Laazimani urb = usb_alloc_urb(0, GFP_ATOMIC); 659f722c09SOmar Laazimani if (!urb) 669f722c09SOmar Laazimani goto fail; 679f722c09SOmar Laazimani 689f722c09SOmar Laazimani usb_fill_bulk_urb(urb, dev->udev, dev->out, 699f722c09SOmar Laazimani skb->data, skb->len, eem_linkcmd_complete, skb); 709f722c09SOmar Laazimani 719f722c09SOmar Laazimani status = usb_submit_urb(urb, GFP_ATOMIC); 729f722c09SOmar Laazimani if (status) { 739f722c09SOmar Laazimani usb_free_urb(urb); 749f722c09SOmar Laazimani fail: 759f722c09SOmar Laazimani dev_kfree_skb(skb); 769f722c09SOmar Laazimani devwarn(dev, "link cmd failure\n"); 779f722c09SOmar Laazimani return; 789f722c09SOmar Laazimani } 799f722c09SOmar Laazimani } 809f722c09SOmar Laazimani 819f722c09SOmar Laazimani static int eem_bind(struct usbnet *dev, struct usb_interface *intf) 829f722c09SOmar Laazimani { 839f722c09SOmar Laazimani int status = 0; 849f722c09SOmar Laazimani 859f722c09SOmar Laazimani status = usbnet_get_endpoints(dev, intf); 869f722c09SOmar Laazimani if (status < 0) { 879f722c09SOmar Laazimani usb_set_intfdata(intf, NULL); 889f722c09SOmar Laazimani usb_driver_release_interface(driver_of(intf), intf); 899f722c09SOmar Laazimani return status; 909f722c09SOmar Laazimani } 919f722c09SOmar Laazimani 929f722c09SOmar Laazimani /* no jumbogram (16K) support for now */ 939f722c09SOmar Laazimani 949f722c09SOmar Laazimani dev->net->hard_header_len += EEM_HEAD + ETH_FCS_LEN; 959f722c09SOmar Laazimani 969f722c09SOmar Laazimani return 0; 979f722c09SOmar Laazimani } 989f722c09SOmar Laazimani 999f722c09SOmar Laazimani /* 1009f722c09SOmar Laazimani * EEM permits packing multiple Ethernet frames into USB transfers 1019f722c09SOmar Laazimani * (a "bundle"), but for TX we don't try to do that. 1029f722c09SOmar Laazimani */ 1039f722c09SOmar Laazimani static struct sk_buff *eem_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 1049f722c09SOmar Laazimani gfp_t flags) 1059f722c09SOmar Laazimani { 1069f722c09SOmar Laazimani struct sk_buff *skb2 = NULL; 1079f722c09SOmar Laazimani u16 len = skb->len; 1089f722c09SOmar Laazimani u32 crc = 0; 1099f722c09SOmar Laazimani int padlen = 0; 1109f722c09SOmar Laazimani 1119f722c09SOmar Laazimani /* When ((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket) is 1129f722c09SOmar Laazimani * zero, stick two bytes of zero length EEM packet on the end. 1139f722c09SOmar Laazimani * Else the framework would add invalid single byte padding, 1149f722c09SOmar Laazimani * since it can't know whether ZLPs will be handled right by 1159f722c09SOmar Laazimani * all the relevant hardware and software. 1169f722c09SOmar Laazimani */ 1179f722c09SOmar Laazimani if (!((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket)) 1189f722c09SOmar Laazimani padlen += 2; 1199f722c09SOmar Laazimani 1209f722c09SOmar Laazimani if (!skb_cloned(skb)) { 1219f722c09SOmar Laazimani int headroom = skb_headroom(skb); 1229f722c09SOmar Laazimani int tailroom = skb_tailroom(skb); 1239f722c09SOmar Laazimani 1249f722c09SOmar Laazimani if ((tailroom >= ETH_FCS_LEN + padlen) 1259f722c09SOmar Laazimani && (headroom >= EEM_HEAD)) 1269f722c09SOmar Laazimani goto done; 1279f722c09SOmar Laazimani 1289f722c09SOmar Laazimani if ((headroom + tailroom) 1299f722c09SOmar Laazimani > (EEM_HEAD + ETH_FCS_LEN + padlen)) { 1309f722c09SOmar Laazimani skb->data = memmove(skb->head + 1319f722c09SOmar Laazimani EEM_HEAD, 1329f722c09SOmar Laazimani skb->data, 1339f722c09SOmar Laazimani skb->len); 1349f722c09SOmar Laazimani skb_set_tail_pointer(skb, len); 1359f722c09SOmar Laazimani goto done; 1369f722c09SOmar Laazimani } 1379f722c09SOmar Laazimani } 1389f722c09SOmar Laazimani 1399f722c09SOmar Laazimani skb2 = skb_copy_expand(skb, EEM_HEAD, ETH_FCS_LEN + padlen, flags); 1409f722c09SOmar Laazimani if (!skb2) 1419f722c09SOmar Laazimani return NULL; 1429f722c09SOmar Laazimani 1439f722c09SOmar Laazimani dev_kfree_skb_any(skb); 1449f722c09SOmar Laazimani skb = skb2; 1459f722c09SOmar Laazimani 1469f722c09SOmar Laazimani done: 1479f722c09SOmar Laazimani /* we don't use the "no Ethernet CRC" option */ 1489f722c09SOmar Laazimani crc = crc32_le(~0, skb->data, skb->len); 1499f722c09SOmar Laazimani crc = ~crc; 1509f722c09SOmar Laazimani 1519f722c09SOmar Laazimani put_unaligned_le32(crc, skb_put(skb, 4)); 1529f722c09SOmar Laazimani 1539f722c09SOmar Laazimani /* EEM packet header format: 1549f722c09SOmar Laazimani * b0..13: length of ethernet frame 1559f722c09SOmar Laazimani * b14: bmCRC (1 == valid Ethernet CRC) 1569f722c09SOmar Laazimani * b15: bmType (0 == data) 1579f722c09SOmar Laazimani */ 1589f722c09SOmar Laazimani len = skb->len; 1599f722c09SOmar Laazimani put_unaligned_le16(BIT(14) | len, skb_push(skb, 2)); 1609f722c09SOmar Laazimani 1619f722c09SOmar Laazimani /* Bundle a zero length EEM packet if needed */ 1629f722c09SOmar Laazimani if (padlen) 1639f722c09SOmar Laazimani put_unaligned_le16(0, skb_put(skb, 2)); 1649f722c09SOmar Laazimani 1659f722c09SOmar Laazimani return skb; 1669f722c09SOmar Laazimani } 1679f722c09SOmar Laazimani 1689f722c09SOmar Laazimani static int eem_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 1699f722c09SOmar Laazimani { 1709f722c09SOmar Laazimani /* 1719f722c09SOmar Laazimani * Our task here is to strip off framing, leaving skb with one 1729f722c09SOmar Laazimani * data frame for the usbnet framework code to process. But we 1739f722c09SOmar Laazimani * may have received multiple EEM payloads, or command payloads. 1749f722c09SOmar Laazimani * So we must process _everything_ as if it's a header, except 1759f722c09SOmar Laazimani * maybe the last data payload 1769f722c09SOmar Laazimani * 1779f722c09SOmar Laazimani * REVISIT the framework needs updating so that when we consume 1789f722c09SOmar Laazimani * all payloads (the last or only message was a command, or a 1799f722c09SOmar Laazimani * zero length EEM packet) that is not accounted as an rx_error. 1809f722c09SOmar Laazimani */ 1819f722c09SOmar Laazimani do { 1829f722c09SOmar Laazimani struct sk_buff *skb2 = NULL; 1839f722c09SOmar Laazimani u16 header; 1849f722c09SOmar Laazimani u16 len = 0; 1859f722c09SOmar Laazimani 1869f722c09SOmar Laazimani /* incomplete EEM header? */ 1879f722c09SOmar Laazimani if (skb->len < EEM_HEAD) 1889f722c09SOmar Laazimani return 0; 1899f722c09SOmar Laazimani 1909f722c09SOmar Laazimani /* 1919f722c09SOmar Laazimani * EEM packet header format: 1929f722c09SOmar Laazimani * b0..14: EEM type dependant (Data or Command) 1939f722c09SOmar Laazimani * b15: bmType 1949f722c09SOmar Laazimani */ 1959f722c09SOmar Laazimani header = get_unaligned_le16(skb->data); 1969f722c09SOmar Laazimani skb_pull(skb, EEM_HEAD); 1979f722c09SOmar Laazimani 1989f722c09SOmar Laazimani /* 1999f722c09SOmar Laazimani * The bmType bit helps to denote when EEM 2009f722c09SOmar Laazimani * packet is data or command : 2019f722c09SOmar Laazimani * bmType = 0 : EEM data payload 2029f722c09SOmar Laazimani * bmType = 1 : EEM (link) command 2039f722c09SOmar Laazimani */ 2049f722c09SOmar Laazimani if (header & BIT(15)) { 2059f722c09SOmar Laazimani u16 bmEEMCmd; 2069f722c09SOmar Laazimani 2079f722c09SOmar Laazimani /* 2089f722c09SOmar Laazimani * EEM (link) command packet: 2099f722c09SOmar Laazimani * b0..10: bmEEMCmdParam 2109f722c09SOmar Laazimani * b11..13: bmEEMCmd 2119f722c09SOmar Laazimani * b14: bmReserved (must be 0) 2129f722c09SOmar Laazimani * b15: 1 (EEM command) 2139f722c09SOmar Laazimani */ 2149f722c09SOmar Laazimani if (header & BIT(14)) { 2159f722c09SOmar Laazimani devdbg(dev, "reserved command %04x\n", header); 2169f722c09SOmar Laazimani continue; 2179f722c09SOmar Laazimani } 2189f722c09SOmar Laazimani 2199f722c09SOmar Laazimani bmEEMCmd = (header >> 11) & 0x7; 2209f722c09SOmar Laazimani switch (bmEEMCmd) { 2219f722c09SOmar Laazimani 2229f722c09SOmar Laazimani /* Responding to echo requests is mandatory. */ 2239f722c09SOmar Laazimani case 0: /* Echo command */ 2249f722c09SOmar Laazimani len = header & 0x7FF; 2259f722c09SOmar Laazimani 2269f722c09SOmar Laazimani /* bogus command? */ 2279f722c09SOmar Laazimani if (skb->len < len) 2289f722c09SOmar Laazimani return 0; 2299f722c09SOmar Laazimani 2309f722c09SOmar Laazimani skb2 = skb_clone(skb, GFP_ATOMIC); 2319f722c09SOmar Laazimani if (unlikely(!skb2)) 2329f722c09SOmar Laazimani goto next; 2339f722c09SOmar Laazimani skb_trim(skb2, len); 2349f722c09SOmar Laazimani put_unaligned_le16(BIT(15) | (1 << 11) | len, 2359f722c09SOmar Laazimani skb_push(skb2, 2)); 2369f722c09SOmar Laazimani eem_linkcmd(dev, skb2); 2379f722c09SOmar Laazimani break; 2389f722c09SOmar Laazimani 2399f722c09SOmar Laazimani /* 2409f722c09SOmar Laazimani * Host may choose to ignore hints. 2419f722c09SOmar Laazimani * - suspend: peripheral ready to suspend 2429f722c09SOmar Laazimani * - response: suggest N millisec polling 2439f722c09SOmar Laazimani * - response complete: suggest N sec polling 2449f722c09SOmar Laazimani */ 2459f722c09SOmar Laazimani case 2: /* Suspend hint */ 2469f722c09SOmar Laazimani case 3: /* Response hint */ 2479f722c09SOmar Laazimani case 4: /* Response complete hint */ 2489f722c09SOmar Laazimani continue; 2499f722c09SOmar Laazimani 2509f722c09SOmar Laazimani /* 2519f722c09SOmar Laazimani * Hosts should never receive host-to-peripheral 2529f722c09SOmar Laazimani * or reserved command codes; or responses to an 2539f722c09SOmar Laazimani * echo command we didn't send. 2549f722c09SOmar Laazimani */ 2559f722c09SOmar Laazimani case 1: /* Echo response */ 2569f722c09SOmar Laazimani case 5: /* Tickle */ 2579f722c09SOmar Laazimani default: /* reserved */ 2589f722c09SOmar Laazimani devwarn(dev, "unexpected link command %d\n", 2599f722c09SOmar Laazimani bmEEMCmd); 2609f722c09SOmar Laazimani continue; 2619f722c09SOmar Laazimani } 2629f722c09SOmar Laazimani 2639f722c09SOmar Laazimani } else { 2649f722c09SOmar Laazimani u32 crc, crc2; 2659f722c09SOmar Laazimani int is_last; 2669f722c09SOmar Laazimani 2679f722c09SOmar Laazimani /* zero length EEM packet? */ 2689f722c09SOmar Laazimani if (header == 0) 2699f722c09SOmar Laazimani continue; 2709f722c09SOmar Laazimani 2719f722c09SOmar Laazimani /* 2729f722c09SOmar Laazimani * EEM data packet header : 2739f722c09SOmar Laazimani * b0..13: length of ethernet frame 2749f722c09SOmar Laazimani * b14: bmCRC 2759f722c09SOmar Laazimani * b15: 0 (EEM data) 2769f722c09SOmar Laazimani */ 2779f722c09SOmar Laazimani len = header & 0x3FFF; 2789f722c09SOmar Laazimani 2799f722c09SOmar Laazimani /* bogus EEM payload? */ 2809f722c09SOmar Laazimani if (skb->len < len) 2819f722c09SOmar Laazimani return 0; 2829f722c09SOmar Laazimani 2839f722c09SOmar Laazimani /* bogus ethernet frame? */ 2849f722c09SOmar Laazimani if (len < (ETH_HLEN + ETH_FCS_LEN)) 2859f722c09SOmar Laazimani goto next; 2869f722c09SOmar Laazimani 2879f722c09SOmar Laazimani /* 2889f722c09SOmar Laazimani * Treat the last payload differently: framework 2899f722c09SOmar Laazimani * code expects our "fixup" to have stripped off 2909f722c09SOmar Laazimani * headers, so "skb" is a data packet (or error). 2919f722c09SOmar Laazimani * Else if it's not the last payload, keep "skb" 2929f722c09SOmar Laazimani * for further processing. 2939f722c09SOmar Laazimani */ 2949f722c09SOmar Laazimani is_last = (len == skb->len); 2959f722c09SOmar Laazimani if (is_last) 2969f722c09SOmar Laazimani skb2 = skb; 2979f722c09SOmar Laazimani else { 2989f722c09SOmar Laazimani skb2 = skb_clone(skb, GFP_ATOMIC); 2999f722c09SOmar Laazimani if (unlikely(!skb2)) 3009f722c09SOmar Laazimani return 0; 3019f722c09SOmar Laazimani } 3029f722c09SOmar Laazimani 3039f722c09SOmar Laazimani crc = get_unaligned_le32(skb2->data 3049f722c09SOmar Laazimani + len - ETH_FCS_LEN); 3059f722c09SOmar Laazimani skb_trim(skb2, len - ETH_FCS_LEN); 3069f722c09SOmar Laazimani 3079f722c09SOmar Laazimani /* 3089f722c09SOmar Laazimani * The bmCRC helps to denote when the CRC field in 3099f722c09SOmar Laazimani * the Ethernet frame contains a calculated CRC: 3109f722c09SOmar Laazimani * bmCRC = 1 : CRC is calculated 3119f722c09SOmar Laazimani * bmCRC = 0 : CRC = 0xDEADBEEF 3129f722c09SOmar Laazimani */ 3139f722c09SOmar Laazimani if (header & BIT(14)) 3149f722c09SOmar Laazimani crc2 = ~crc32_le(~0, skb2->data, len); 3159f722c09SOmar Laazimani else 3169f722c09SOmar Laazimani crc2 = 0xdeadbeef; 3179f722c09SOmar Laazimani 3189f722c09SOmar Laazimani if (is_last) 3199f722c09SOmar Laazimani return crc == crc2; 3209f722c09SOmar Laazimani 3219f722c09SOmar Laazimani if (unlikely(crc != crc2)) { 3229f722c09SOmar Laazimani dev->stats.rx_errors++; 3239f722c09SOmar Laazimani dev_kfree_skb_any(skb2); 3249f722c09SOmar Laazimani } else 3259f722c09SOmar Laazimani usbnet_skb_return(dev, skb2); 3269f722c09SOmar Laazimani } 3279f722c09SOmar Laazimani 3289f722c09SOmar Laazimani next: 3299f722c09SOmar Laazimani skb_pull(skb, len); 3309f722c09SOmar Laazimani } while (skb->len); 3319f722c09SOmar Laazimani 3329f722c09SOmar Laazimani return 1; 3339f722c09SOmar Laazimani } 3349f722c09SOmar Laazimani 3359f722c09SOmar Laazimani static const struct driver_info eem_info = { 3369f722c09SOmar Laazimani .description = "CDC EEM Device", 3379f722c09SOmar Laazimani .flags = FLAG_ETHER, 3389f722c09SOmar Laazimani .bind = eem_bind, 3399f722c09SOmar Laazimani .rx_fixup = eem_rx_fixup, 3409f722c09SOmar Laazimani .tx_fixup = eem_tx_fixup, 3419f722c09SOmar Laazimani }; 3429f722c09SOmar Laazimani 3439f722c09SOmar Laazimani /*-------------------------------------------------------------------------*/ 3449f722c09SOmar Laazimani 3459f722c09SOmar Laazimani static const struct usb_device_id products[] = { 3469f722c09SOmar Laazimani { 3479f722c09SOmar Laazimani USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_EEM, 3489f722c09SOmar Laazimani USB_CDC_PROTO_EEM), 3499f722c09SOmar Laazimani .driver_info = (unsigned long) &eem_info, 3509f722c09SOmar Laazimani }, 3519f722c09SOmar Laazimani { 3529f722c09SOmar Laazimani /* EMPTY == end of list */ 3539f722c09SOmar Laazimani }, 3549f722c09SOmar Laazimani }; 3559f722c09SOmar Laazimani MODULE_DEVICE_TABLE(usb, products); 3569f722c09SOmar Laazimani 3579f722c09SOmar Laazimani static struct usb_driver eem_driver = { 3589f722c09SOmar Laazimani .name = "cdc_eem", 3599f722c09SOmar Laazimani .id_table = products, 3609f722c09SOmar Laazimani .probe = usbnet_probe, 3619f722c09SOmar Laazimani .disconnect = usbnet_disconnect, 3629f722c09SOmar Laazimani .suspend = usbnet_suspend, 3639f722c09SOmar Laazimani .resume = usbnet_resume, 3649f722c09SOmar Laazimani }; 3659f722c09SOmar Laazimani 3669f722c09SOmar Laazimani 3679f722c09SOmar Laazimani static int __init eem_init(void) 3689f722c09SOmar Laazimani { 3699f722c09SOmar Laazimani return usb_register(&eem_driver); 3709f722c09SOmar Laazimani } 3719f722c09SOmar Laazimani module_init(eem_init); 3729f722c09SOmar Laazimani 3739f722c09SOmar Laazimani static void __exit eem_exit(void) 3749f722c09SOmar Laazimani { 3759f722c09SOmar Laazimani usb_deregister(&eem_driver); 3769f722c09SOmar Laazimani } 3779f722c09SOmar Laazimani module_exit(eem_exit); 3789f722c09SOmar Laazimani 3799f722c09SOmar Laazimani MODULE_AUTHOR("Omar Laazimani <omar.oberthur@gmail.com>"); 3809f722c09SOmar Laazimani MODULE_DESCRIPTION("USB CDC EEM"); 3819f722c09SOmar Laazimani MODULE_LICENSE("GPL"); 382