lg-vl600.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Ethernet interface part of the LG VL600 LTE modem (4G dongle)
  3. *
  4. * Copyright (C) 2011 Intel Corporation
  5. * Author: Andrzej Zaborowski <balrogg@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/etherdevice.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/mii.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/cdc.h>
  26. #include <linux/usb/usbnet.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/inetdevice.h>
  30. /*
  31. * The device has a CDC ACM port for modem control (it claims to be
  32. * CDC ACM anyway) and a CDC Ethernet port for actual network data.
  33. * It will however ignore data on both ports that is not encapsulated
  34. * in a specific way, any data returned is also encapsulated the same
  35. * way. The headers don't seem to follow any popular standard.
  36. *
  37. * This driver adds and strips these headers from the ethernet frames
  38. * sent/received from the CDC Ethernet port. The proprietary header
  39. * replaces the standard ethernet header in a packet so only actual
  40. * ethernet frames are allowed. The headers allow some form of
  41. * multiplexing by using non standard values of the .h_proto field.
  42. * Windows/Mac drivers do send a couple of such frames to the device
  43. * during initialisation, with protocol set to 0x0906 or 0x0b06 and (what
  44. * seems to be) a flag in the .dummy_flags. This doesn't seem necessary
  45. * for modem operation but can possibly be used for GPS or other funcitons.
  46. */
  47. struct vl600_frame_hdr {
  48. __le32 len;
  49. __le32 serial;
  50. __le32 pkt_cnt;
  51. __le32 dummy_flags;
  52. __le32 dummy;
  53. __le32 magic;
  54. } __attribute__((packed));
  55. struct vl600_pkt_hdr {
  56. __le32 dummy[2];
  57. __le32 len;
  58. __be16 h_proto;
  59. } __attribute__((packed));
  60. struct vl600_state {
  61. struct sk_buff *current_rx_buf;
  62. };
  63. static int vl600_bind(struct usbnet *dev, struct usb_interface *intf)
  64. {
  65. int ret;
  66. struct vl600_state *s = kzalloc(sizeof(struct vl600_state), GFP_KERNEL);
  67. if (!s)
  68. return -ENOMEM;
  69. ret = usbnet_cdc_bind(dev, intf);
  70. if (ret) {
  71. kfree(s);
  72. return ret;
  73. }
  74. dev->driver_priv = s;
  75. /* ARP packets don't go through, but they're also of no use. The
  76. * subnet has only two hosts anyway: us and the gateway / DHCP
  77. * server (probably simulated by modem firmware or network operator)
  78. * whose address changes everytime we connect to the intarwebz and
  79. * who doesn't bother answering ARP requests either. So hardware
  80. * addresses have no meaning, the destination and the source of every
  81. * packet depend only on whether it is on the IN or OUT endpoint. */
  82. dev->net->flags |= IFF_NOARP;
  83. /* IPv6 NDP relies on multicast. Enable it by default. */
  84. dev->net->flags |= IFF_MULTICAST;
  85. return ret;
  86. }
  87. static void vl600_unbind(struct usbnet *dev, struct usb_interface *intf)
  88. {
  89. struct vl600_state *s = dev->driver_priv;
  90. if (s->current_rx_buf)
  91. dev_kfree_skb(s->current_rx_buf);
  92. kfree(s);
  93. return usbnet_cdc_unbind(dev, intf);
  94. }
  95. static int vl600_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  96. {
  97. struct vl600_frame_hdr *frame;
  98. struct vl600_pkt_hdr *packet;
  99. struct ethhdr *ethhdr;
  100. int packet_len, count;
  101. struct sk_buff *buf = skb;
  102. struct sk_buff *clone;
  103. struct vl600_state *s = dev->driver_priv;
  104. /* Frame lengths are generally 4B multiplies but every couple of
  105. * hours there's an odd number of bytes sized yet correct frame,
  106. * so don't require this. */
  107. /* Allow a packet (or multiple packets batched together) to be
  108. * split across many frames. We don't allow a new batch to
  109. * begin in the same frame another one is ending however, and no
  110. * leading or trailing pad bytes. */
  111. if (s->current_rx_buf) {
  112. frame = (struct vl600_frame_hdr *) s->current_rx_buf->data;
  113. if (skb->len + s->current_rx_buf->len >
  114. le32_to_cpup(&frame->len)) {
  115. netif_err(dev, ifup, dev->net, "Fragment too long\n");
  116. dev->net->stats.rx_length_errors++;
  117. goto error;
  118. }
  119. buf = s->current_rx_buf;
  120. memcpy(skb_put(buf, skb->len), skb->data, skb->len);
  121. } else if (skb->len < 4) {
  122. netif_err(dev, ifup, dev->net, "Frame too short\n");
  123. dev->net->stats.rx_length_errors++;
  124. goto error;
  125. }
  126. frame = (struct vl600_frame_hdr *) buf->data;
  127. /* NOTE: Should check that frame->magic == 0x53544448?
  128. * Otherwise if we receive garbage at the beginning of the frame
  129. * we may end up allocating a huge buffer and saving all the
  130. * future incoming data into it. */
  131. if (buf->len < sizeof(*frame) ||
  132. buf->len != le32_to_cpup(&frame->len)) {
  133. /* Save this fragment for later assembly */
  134. if (s->current_rx_buf)
  135. return 0;
  136. s->current_rx_buf = skb_copy_expand(skb, 0,
  137. le32_to_cpup(&frame->len), GFP_ATOMIC);
  138. if (!s->current_rx_buf) {
  139. netif_err(dev, ifup, dev->net, "Reserving %i bytes "
  140. "for packet assembly failed.\n",
  141. le32_to_cpup(&frame->len));
  142. dev->net->stats.rx_errors++;
  143. }
  144. return 0;
  145. }
  146. count = le32_to_cpup(&frame->pkt_cnt);
  147. skb_pull(buf, sizeof(*frame));
  148. while (count--) {
  149. if (buf->len < sizeof(*packet)) {
  150. netif_err(dev, ifup, dev->net, "Packet too short\n");
  151. goto error;
  152. }
  153. packet = (struct vl600_pkt_hdr *) buf->data;
  154. packet_len = sizeof(*packet) + le32_to_cpup(&packet->len);
  155. if (packet_len > buf->len) {
  156. netif_err(dev, ifup, dev->net,
  157. "Bad packet length stored in header\n");
  158. goto error;
  159. }
  160. /* Packet header is same size as the ethernet header
  161. * (sizeof(*packet) == sizeof(*ethhdr)), additionally
  162. * the h_proto field is in the same place so we just leave it
  163. * alone and fill in the remaining fields.
  164. */
  165. ethhdr = (struct ethhdr *) skb->data;
  166. if (be16_to_cpup(&ethhdr->h_proto) == ETH_P_ARP &&
  167. buf->len > 0x26) {
  168. /* Copy the addresses from packet contents */
  169. memcpy(ethhdr->h_source,
  170. &buf->data[sizeof(*ethhdr) + 0x8],
  171. ETH_ALEN);
  172. memcpy(ethhdr->h_dest,
  173. &buf->data[sizeof(*ethhdr) + 0x12],
  174. ETH_ALEN);
  175. } else {
  176. memset(ethhdr->h_source, 0, ETH_ALEN);
  177. memcpy(ethhdr->h_dest, dev->net->dev_addr, ETH_ALEN);
  178. /* Inbound IPv6 packets have an IPv4 ethertype (0x800)
  179. * for some reason. Peek at the L3 header to check
  180. * for IPv6 packets, and set the ethertype to IPv6
  181. * (0x86dd) so Linux can understand it.
  182. */
  183. if ((buf->data[sizeof(*ethhdr)] & 0xf0) == 0x60)
  184. ethhdr->h_proto = __constant_htons(ETH_P_IPV6);
  185. }
  186. if (count) {
  187. /* Not the last packet in this batch */
  188. clone = skb_clone(buf, GFP_ATOMIC);
  189. if (!clone)
  190. goto error;
  191. skb_trim(clone, packet_len);
  192. usbnet_skb_return(dev, clone);
  193. skb_pull(buf, (packet_len + 3) & ~3);
  194. } else {
  195. skb_trim(buf, packet_len);
  196. if (s->current_rx_buf) {
  197. usbnet_skb_return(dev, buf);
  198. s->current_rx_buf = NULL;
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. }
  204. error:
  205. if (s->current_rx_buf) {
  206. dev_kfree_skb_any(s->current_rx_buf);
  207. s->current_rx_buf = NULL;
  208. }
  209. dev->net->stats.rx_errors++;
  210. return 0;
  211. }
  212. static struct sk_buff *vl600_tx_fixup(struct usbnet *dev,
  213. struct sk_buff *skb, gfp_t flags)
  214. {
  215. struct sk_buff *ret;
  216. struct vl600_frame_hdr *frame;
  217. struct vl600_pkt_hdr *packet;
  218. static uint32_t serial = 1;
  219. int orig_len = skb->len - sizeof(struct ethhdr);
  220. int full_len = (skb->len + sizeof(struct vl600_frame_hdr) + 3) & ~3;
  221. frame = (struct vl600_frame_hdr *) skb->data;
  222. if (skb->len > sizeof(*frame) && skb->len == le32_to_cpup(&frame->len))
  223. return skb; /* Already encapsulated? */
  224. if (skb->len < sizeof(struct ethhdr))
  225. /* Drop, device can only deal with ethernet packets */
  226. return NULL;
  227. if (!skb_cloned(skb)) {
  228. int headroom = skb_headroom(skb);
  229. int tailroom = skb_tailroom(skb);
  230. if (tailroom >= full_len - skb->len - sizeof(*frame) &&
  231. headroom >= sizeof(*frame))
  232. /* There's enough head and tail room */
  233. goto encapsulate;
  234. if (headroom + tailroom + skb->len >= full_len) {
  235. /* There's enough total room, just readjust */
  236. skb->data = memmove(skb->head + sizeof(*frame),
  237. skb->data, skb->len);
  238. skb_set_tail_pointer(skb, skb->len);
  239. goto encapsulate;
  240. }
  241. }
  242. /* Alloc a new skb with the required size */
  243. ret = skb_copy_expand(skb, sizeof(struct vl600_frame_hdr), full_len -
  244. skb->len - sizeof(struct vl600_frame_hdr), flags);
  245. dev_kfree_skb_any(skb);
  246. if (!ret)
  247. return ret;
  248. skb = ret;
  249. encapsulate:
  250. /* Packet header is same size as ethernet packet header
  251. * (sizeof(*packet) == sizeof(struct ethhdr)), additionally the
  252. * h_proto field is in the same place so we just leave it alone and
  253. * overwrite the remaining fields.
  254. */
  255. packet = (struct vl600_pkt_hdr *) skb->data;
  256. memset(&packet->dummy, 0, sizeof(packet->dummy));
  257. packet->len = cpu_to_le32(orig_len);
  258. frame = (struct vl600_frame_hdr *) skb_push(skb, sizeof(*frame));
  259. memset(frame, 0, sizeof(*frame));
  260. frame->len = cpu_to_le32(full_len);
  261. frame->serial = cpu_to_le32(serial++);
  262. frame->pkt_cnt = cpu_to_le32(1);
  263. if (skb->len < full_len) /* Pad */
  264. skb_put(skb, full_len - skb->len);
  265. /* The VL600 wants IPv6 packets to have an IPv4 ethertype
  266. * Check if this is an IPv6 packet, and set the ethertype
  267. * to 0x800
  268. */
  269. if ((skb->data[sizeof(struct vl600_pkt_hdr *) + 0x22] & 0xf0) == 0x60) {
  270. skb->data[sizeof(struct vl600_pkt_hdr *) + 0x20] = 0x08;
  271. skb->data[sizeof(struct vl600_pkt_hdr *) + 0x21] = 0;
  272. }
  273. return skb;
  274. }
  275. static const struct driver_info vl600_info = {
  276. .description = "LG VL600 modem",
  277. .flags = FLAG_ETHER | FLAG_RX_ASSEMBLE,
  278. .bind = vl600_bind,
  279. .unbind = vl600_unbind,
  280. .status = usbnet_cdc_status,
  281. .rx_fixup = vl600_rx_fixup,
  282. .tx_fixup = vl600_tx_fixup,
  283. };
  284. static const struct usb_device_id products[] = {
  285. {
  286. USB_DEVICE_AND_INTERFACE_INFO(0x1004, 0x61aa, USB_CLASS_COMM,
  287. USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
  288. .driver_info = (unsigned long) &vl600_info,
  289. },
  290. {}, /* End */
  291. };
  292. MODULE_DEVICE_TABLE(usb, products);
  293. static struct usb_driver lg_vl600_driver = {
  294. .name = "lg-vl600",
  295. .id_table = products,
  296. .probe = usbnet_probe,
  297. .disconnect = usbnet_disconnect,
  298. .suspend = usbnet_suspend,
  299. .resume = usbnet_resume,
  300. };
  301. static int __init vl600_init(void)
  302. {
  303. return usb_register(&lg_vl600_driver);
  304. }
  305. module_init(vl600_init);
  306. static void __exit vl600_exit(void)
  307. {
  308. usb_deregister(&lg_vl600_driver);
  309. }
  310. module_exit(vl600_exit);
  311. MODULE_AUTHOR("Anrzej Zaborowski");
  312. MODULE_DESCRIPTION("LG-VL600 modem's ethernet link");
  313. MODULE_LICENSE("GPL");