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