cdc_eem.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * USB CDC EEM network interface driver
  3. * Copyright (C) 2009 Oberthur Technologies
  4. * by Omar Laazimani, Olivier Condemine
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ctype.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mii.h>
  28. #include <linux/usb.h>
  29. #include <linux/crc32.h>
  30. #include <linux/usb/cdc.h>
  31. #include <linux/usb/usbnet.h>
  32. #include <linux/gfp.h>
  33. /*
  34. * This driver is an implementation of the CDC "Ethernet Emulation
  35. * Model" (EEM) specification, which encapsulates Ethernet frames
  36. * for transport over USB using a simpler USB device model than the
  37. * previous CDC "Ethernet Control Model" (ECM, or "CDC Ethernet").
  38. *
  39. * For details, see www.usb.org/developers/devclass_docs/CDC_EEM10.pdf
  40. *
  41. * This version has been tested with GIGAntIC WuaoW SIM Smart Card on 2.6.24,
  42. * 2.6.27 and 2.6.30rc2 kernel.
  43. * It has also been validated on Openmoko Om 2008.12 (based on 2.6.24 kernel).
  44. * build on 23-April-2009
  45. */
  46. #define EEM_HEAD 2 /* 2 byte header */
  47. /*-------------------------------------------------------------------------*/
  48. static void eem_linkcmd_complete(struct urb *urb)
  49. {
  50. dev_kfree_skb(urb->context);
  51. usb_free_urb(urb);
  52. }
  53. static void eem_linkcmd(struct usbnet *dev, struct sk_buff *skb)
  54. {
  55. struct urb *urb;
  56. int status;
  57. urb = usb_alloc_urb(0, GFP_ATOMIC);
  58. if (!urb)
  59. goto fail;
  60. usb_fill_bulk_urb(urb, dev->udev, dev->out,
  61. skb->data, skb->len, eem_linkcmd_complete, skb);
  62. status = usb_submit_urb(urb, GFP_ATOMIC);
  63. if (status) {
  64. usb_free_urb(urb);
  65. fail:
  66. dev_kfree_skb(skb);
  67. netdev_warn(dev->net, "link cmd failure\n");
  68. return;
  69. }
  70. }
  71. static int eem_bind(struct usbnet *dev, struct usb_interface *intf)
  72. {
  73. int status = 0;
  74. status = usbnet_get_endpoints(dev, intf);
  75. if (status < 0) {
  76. usb_set_intfdata(intf, NULL);
  77. usb_driver_release_interface(driver_of(intf), intf);
  78. return status;
  79. }
  80. /* no jumbogram (16K) support for now */
  81. dev->net->hard_header_len += EEM_HEAD + ETH_FCS_LEN;
  82. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  83. return 0;
  84. }
  85. /*
  86. * EEM permits packing multiple Ethernet frames into USB transfers
  87. * (a "bundle"), but for TX we don't try to do that.
  88. */
  89. static struct sk_buff *eem_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  90. gfp_t flags)
  91. {
  92. struct sk_buff *skb2 = NULL;
  93. u16 len = skb->len;
  94. u32 crc = 0;
  95. int padlen = 0;
  96. /* When ((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket) is
  97. * zero, stick two bytes of zero length EEM packet on the end.
  98. * Else the framework would add invalid single byte padding,
  99. * since it can't know whether ZLPs will be handled right by
  100. * all the relevant hardware and software.
  101. */
  102. if (!((len + EEM_HEAD + ETH_FCS_LEN) % dev->maxpacket))
  103. padlen += 2;
  104. if (!skb_cloned(skb)) {
  105. int headroom = skb_headroom(skb);
  106. int tailroom = skb_tailroom(skb);
  107. if ((tailroom >= ETH_FCS_LEN + padlen) &&
  108. (headroom >= EEM_HEAD))
  109. goto done;
  110. if ((headroom + tailroom)
  111. > (EEM_HEAD + ETH_FCS_LEN + padlen)) {
  112. skb->data = memmove(skb->head +
  113. EEM_HEAD,
  114. skb->data,
  115. skb->len);
  116. skb_set_tail_pointer(skb, len);
  117. goto done;
  118. }
  119. }
  120. skb2 = skb_copy_expand(skb, EEM_HEAD, ETH_FCS_LEN + padlen, flags);
  121. if (!skb2)
  122. return NULL;
  123. dev_kfree_skb_any(skb);
  124. skb = skb2;
  125. done:
  126. /* we don't use the "no Ethernet CRC" option */
  127. crc = crc32_le(~0, skb->data, skb->len);
  128. crc = ~crc;
  129. put_unaligned_le32(crc, skb_put(skb, 4));
  130. /* EEM packet header format:
  131. * b0..13: length of ethernet frame
  132. * b14: bmCRC (1 == valid Ethernet CRC)
  133. * b15: bmType (0 == data)
  134. */
  135. len = skb->len;
  136. put_unaligned_le16(BIT(14) | len, skb_push(skb, 2));
  137. /* Bundle a zero length EEM packet if needed */
  138. if (padlen)
  139. put_unaligned_le16(0, skb_put(skb, 2));
  140. return skb;
  141. }
  142. static int eem_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  143. {
  144. /*
  145. * Our task here is to strip off framing, leaving skb with one
  146. * data frame for the usbnet framework code to process. But we
  147. * may have received multiple EEM payloads, or command payloads.
  148. * So we must process _everything_ as if it's a header, except
  149. * maybe the last data payload
  150. *
  151. * REVISIT the framework needs updating so that when we consume
  152. * all payloads (the last or only message was a command, or a
  153. * zero length EEM packet) that is not accounted as an rx_error.
  154. */
  155. do {
  156. struct sk_buff *skb2 = NULL;
  157. u16 header;
  158. u16 len = 0;
  159. /* incomplete EEM header? */
  160. if (skb->len < EEM_HEAD)
  161. return 0;
  162. /*
  163. * EEM packet header format:
  164. * b0..14: EEM type dependent (Data or Command)
  165. * b15: bmType
  166. */
  167. header = get_unaligned_le16(skb->data);
  168. skb_pull(skb, EEM_HEAD);
  169. /*
  170. * The bmType bit helps to denote when EEM
  171. * packet is data or command :
  172. * bmType = 0 : EEM data payload
  173. * bmType = 1 : EEM (link) command
  174. */
  175. if (header & BIT(15)) {
  176. u16 bmEEMCmd;
  177. /*
  178. * EEM (link) command packet:
  179. * b0..10: bmEEMCmdParam
  180. * b11..13: bmEEMCmd
  181. * b14: bmReserved (must be 0)
  182. * b15: 1 (EEM command)
  183. */
  184. if (header & BIT(14)) {
  185. netdev_dbg(dev->net, "reserved command %04x\n",
  186. header);
  187. continue;
  188. }
  189. bmEEMCmd = (header >> 11) & 0x7;
  190. switch (bmEEMCmd) {
  191. /* Responding to echo requests is mandatory. */
  192. case 0: /* Echo command */
  193. len = header & 0x7FF;
  194. /* bogus command? */
  195. if (skb->len < len)
  196. return 0;
  197. skb2 = skb_clone(skb, GFP_ATOMIC);
  198. if (unlikely(!skb2))
  199. goto next;
  200. skb_trim(skb2, len);
  201. put_unaligned_le16(BIT(15) | (1 << 11) | len,
  202. skb_push(skb2, 2));
  203. eem_linkcmd(dev, skb2);
  204. break;
  205. /*
  206. * Host may choose to ignore hints.
  207. * - suspend: peripheral ready to suspend
  208. * - response: suggest N millisec polling
  209. * - response complete: suggest N sec polling
  210. */
  211. case 2: /* Suspend hint */
  212. case 3: /* Response hint */
  213. case 4: /* Response complete hint */
  214. continue;
  215. /*
  216. * Hosts should never receive host-to-peripheral
  217. * or reserved command codes; or responses to an
  218. * echo command we didn't send.
  219. */
  220. case 1: /* Echo response */
  221. case 5: /* Tickle */
  222. default: /* reserved */
  223. netdev_warn(dev->net,
  224. "unexpected link command %d\n",
  225. bmEEMCmd);
  226. continue;
  227. }
  228. } else {
  229. u32 crc, crc2;
  230. int is_last;
  231. /* zero length EEM packet? */
  232. if (header == 0)
  233. continue;
  234. /*
  235. * EEM data packet header :
  236. * b0..13: length of ethernet frame
  237. * b14: bmCRC
  238. * b15: 0 (EEM data)
  239. */
  240. len = header & 0x3FFF;
  241. /* bogus EEM payload? */
  242. if (skb->len < len)
  243. return 0;
  244. /* bogus ethernet frame? */
  245. if (len < (ETH_HLEN + ETH_FCS_LEN))
  246. goto next;
  247. /*
  248. * Treat the last payload differently: framework
  249. * code expects our "fixup" to have stripped off
  250. * headers, so "skb" is a data packet (or error).
  251. * Else if it's not the last payload, keep "skb"
  252. * for further processing.
  253. */
  254. is_last = (len == skb->len);
  255. if (is_last)
  256. skb2 = skb;
  257. else {
  258. skb2 = skb_clone(skb, GFP_ATOMIC);
  259. if (unlikely(!skb2))
  260. return 0;
  261. }
  262. /*
  263. * The bmCRC helps to denote when the CRC field in
  264. * the Ethernet frame contains a calculated CRC:
  265. * bmCRC = 1 : CRC is calculated
  266. * bmCRC = 0 : CRC = 0xDEADBEEF
  267. */
  268. if (header & BIT(14)) {
  269. crc = get_unaligned_le32(skb2->data
  270. + len - ETH_FCS_LEN);
  271. crc2 = ~crc32_le(~0, skb2->data, skb2->len
  272. - ETH_FCS_LEN);
  273. } else {
  274. crc = get_unaligned_be32(skb2->data
  275. + len - ETH_FCS_LEN);
  276. crc2 = 0xdeadbeef;
  277. }
  278. skb_trim(skb2, len - ETH_FCS_LEN);
  279. if (is_last)
  280. return crc == crc2;
  281. if (unlikely(crc != crc2)) {
  282. dev->net->stats.rx_errors++;
  283. dev_kfree_skb_any(skb2);
  284. } else
  285. usbnet_skb_return(dev, skb2);
  286. }
  287. next:
  288. skb_pull(skb, len);
  289. } while (skb->len);
  290. return 1;
  291. }
  292. static const struct driver_info eem_info = {
  293. .description = "CDC EEM Device",
  294. .flags = FLAG_ETHER | FLAG_POINTTOPOINT,
  295. .bind = eem_bind,
  296. .rx_fixup = eem_rx_fixup,
  297. .tx_fixup = eem_tx_fixup,
  298. };
  299. /*-------------------------------------------------------------------------*/
  300. static const struct usb_device_id products[] = {
  301. {
  302. USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_EEM,
  303. USB_CDC_PROTO_EEM),
  304. .driver_info = (unsigned long) &eem_info,
  305. },
  306. {
  307. /* EMPTY == end of list */
  308. },
  309. };
  310. MODULE_DEVICE_TABLE(usb, products);
  311. static struct usb_driver eem_driver = {
  312. .name = "cdc_eem",
  313. .id_table = products,
  314. .probe = usbnet_probe,
  315. .disconnect = usbnet_disconnect,
  316. .suspend = usbnet_suspend,
  317. .resume = usbnet_resume,
  318. };
  319. static int __init eem_init(void)
  320. {
  321. return usb_register(&eem_driver);
  322. }
  323. module_init(eem_init);
  324. static void __exit eem_exit(void)
  325. {
  326. usb_deregister(&eem_driver);
  327. }
  328. module_exit(eem_exit);
  329. MODULE_AUTHOR("Omar Laazimani <omar.oberthur@gmail.com>");
  330. MODULE_DESCRIPTION("USB CDC EEM");
  331. MODULE_LICENSE("GPL");