cdc_eem.c 8.7 KB

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