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