cdc_eem.c 9.4 KB

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