lg-vl600.c 10 KB

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