Commit | Line | Data |
---|---|---|
7a635ea9 AZ |
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 | /* | |
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 | ||
49 | struct vl600_frame_hdr { | |
50 | __le32 len; | |
51 | __le32 serial; | |
52 | __le32 pkt_cnt; | |
53 | __le32 dummy_flags; | |
54 | __le32 dummy; | |
55 | __le32 magic; | |
56 | } __attribute__((packed)); | |
57 | ||
58 | struct vl600_pkt_hdr { | |
59 | __le32 dummy[2]; | |
60 | __le32 len; | |
61 | __be16 h_proto; | |
62 | } __attribute__((packed)); | |
63 | ||
64 | struct vl600_state { | |
65 | struct sk_buff *current_rx_buf; | |
66 | }; | |
67 | ||
68 | static int vl600_bind(struct usbnet *dev, struct usb_interface *intf) | |
69 | { | |
70 | int ret; | |
71 | struct vl600_state *s = kzalloc(sizeof(struct vl600_state), GFP_KERNEL); | |
72 | ||
73 | if (!s) | |
74 | return -ENOMEM; | |
75 | ||
76 | ret = usbnet_cdc_bind(dev, intf); | |
77 | if (ret) { | |
78 | kfree(s); | |
79 | return ret; | |
80 | } | |
81 | ||
82 | dev->driver_priv = s; | |
83 | ||
84 | /* ARP packets don't go through, but they're also of no use. The | |
85 | * subnet has only two hosts anyway: us and the gateway / DHCP | |
86 | * server (probably simulated by modem firmware or network operator) | |
87 | * whose address changes everytime we connect to the intarwebz and | |
88 | * who doesn't bother answering ARP requests either. So hardware | |
89 | * addresses have no meaning, the destination and the source of every | |
90 | * packet depend only on whether it is on the IN or OUT endpoint. */ | |
91 | dev->net->flags |= IFF_NOARP; | |
2ae40ee9 MK |
92 | /* IPv6 NDP relies on multicast. Enable it by default. */ |
93 | dev->net->flags |= IFF_MULTICAST; | |
7a635ea9 AZ |
94 | |
95 | return ret; | |
96 | } | |
97 | ||
98 | static void vl600_unbind(struct usbnet *dev, struct usb_interface *intf) | |
99 | { | |
100 | struct vl600_state *s = dev->driver_priv; | |
101 | ||
102 | if (s->current_rx_buf) | |
103 | dev_kfree_skb(s->current_rx_buf); | |
104 | ||
105 | kfree(s); | |
106 | ||
107 | return usbnet_cdc_unbind(dev, intf); | |
108 | } | |
109 | ||
110 | static int vl600_rx_fixup(struct usbnet *dev, struct sk_buff *skb) | |
111 | { | |
112 | struct vl600_frame_hdr *frame; | |
113 | struct vl600_pkt_hdr *packet; | |
114 | struct ethhdr *ethhdr; | |
115 | int packet_len, count; | |
116 | struct sk_buff *buf = skb; | |
117 | struct sk_buff *clone; | |
118 | struct vl600_state *s = dev->driver_priv; | |
119 | ||
120 | /* Frame lengths are generally 4B multiplies but every couple of | |
121 | * hours there's an odd number of bytes sized yet correct frame, | |
122 | * so don't require this. */ | |
123 | ||
124 | /* Allow a packet (or multiple packets batched together) to be | |
125 | * split across many frames. We don't allow a new batch to | |
126 | * begin in the same frame another one is ending however, and no | |
127 | * leading or trailing pad bytes. */ | |
128 | if (s->current_rx_buf) { | |
129 | frame = (struct vl600_frame_hdr *) s->current_rx_buf->data; | |
130 | if (skb->len + s->current_rx_buf->len > | |
131 | le32_to_cpup(&frame->len)) { | |
132 | netif_err(dev, ifup, dev->net, "Fragment too long\n"); | |
133 | dev->net->stats.rx_length_errors++; | |
134 | goto error; | |
135 | } | |
136 | ||
137 | buf = s->current_rx_buf; | |
138 | memcpy(skb_put(buf, skb->len), skb->data, skb->len); | |
139 | } else if (skb->len < 4) { | |
140 | netif_err(dev, ifup, dev->net, "Frame too short\n"); | |
141 | dev->net->stats.rx_length_errors++; | |
142 | goto error; | |
143 | } | |
144 | ||
145 | frame = (struct vl600_frame_hdr *) buf->data; | |
146 | /* NOTE: Should check that frame->magic == 0x53544448? | |
147 | * Otherwise if we receive garbage at the beginning of the frame | |
148 | * we may end up allocating a huge buffer and saving all the | |
149 | * future incoming data into it. */ | |
150 | ||
151 | if (buf->len < sizeof(*frame) || | |
152 | buf->len != le32_to_cpup(&frame->len)) { | |
153 | /* Save this fragment for later assembly */ | |
154 | if (s->current_rx_buf) | |
155 | return 0; | |
156 | ||
157 | s->current_rx_buf = skb_copy_expand(skb, 0, | |
158 | le32_to_cpup(&frame->len), GFP_ATOMIC); | |
159 | if (!s->current_rx_buf) { | |
160 | netif_err(dev, ifup, dev->net, "Reserving %i bytes " | |
161 | "for packet assembly failed.\n", | |
162 | le32_to_cpup(&frame->len)); | |
163 | dev->net->stats.rx_errors++; | |
164 | } | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
169 | count = le32_to_cpup(&frame->pkt_cnt); | |
170 | ||
171 | skb_pull(buf, sizeof(*frame)); | |
172 | ||
173 | while (count--) { | |
174 | if (buf->len < sizeof(*packet)) { | |
175 | netif_err(dev, ifup, dev->net, "Packet too short\n"); | |
176 | goto error; | |
177 | } | |
178 | ||
179 | packet = (struct vl600_pkt_hdr *) buf->data; | |
180 | packet_len = sizeof(*packet) + le32_to_cpup(&packet->len); | |
181 | if (packet_len > buf->len) { | |
182 | netif_err(dev, ifup, dev->net, | |
183 | "Bad packet length stored in header\n"); | |
184 | goto error; | |
185 | } | |
186 | ||
187 | /* Packet header is same size as the ethernet header | |
188 | * (sizeof(*packet) == sizeof(*ethhdr)), additionally | |
189 | * the h_proto field is in the same place so we just leave it | |
190 | * alone and fill in the remaining fields. | |
191 | */ | |
192 | ethhdr = (struct ethhdr *) skb->data; | |
193 | if (be16_to_cpup(ðhdr->h_proto) == ETH_P_ARP && | |
194 | buf->len > 0x26) { | |
195 | /* Copy the addresses from packet contents */ | |
196 | memcpy(ethhdr->h_source, | |
197 | &buf->data[sizeof(*ethhdr) + 0x8], | |
198 | ETH_ALEN); | |
199 | memcpy(ethhdr->h_dest, | |
200 | &buf->data[sizeof(*ethhdr) + 0x12], | |
201 | ETH_ALEN); | |
202 | } else { | |
203 | memset(ethhdr->h_source, 0, ETH_ALEN); | |
204 | memcpy(ethhdr->h_dest, dev->net->dev_addr, ETH_ALEN); | |
2ae40ee9 MK |
205 | |
206 | /* Inbound IPv6 packets have an IPv4 ethertype (0x800) | |
207 | * for some reason. Peek at the L3 header to check | |
208 | * for IPv6 packets, and set the ethertype to IPv6 | |
209 | * (0x86dd) so Linux can understand it. | |
210 | */ | |
211 | if ((buf->data[sizeof(*ethhdr)] & 0xf0) == 0x60) | |
212 | ethhdr->h_proto = __constant_htons(ETH_P_IPV6); | |
7a635ea9 AZ |
213 | } |
214 | ||
215 | if (count) { | |
216 | /* Not the last packet in this batch */ | |
217 | clone = skb_clone(buf, GFP_ATOMIC); | |
218 | if (!clone) | |
219 | goto error; | |
220 | ||
221 | skb_trim(clone, packet_len); | |
222 | usbnet_skb_return(dev, clone); | |
223 | ||
224 | skb_pull(buf, (packet_len + 3) & ~3); | |
225 | } else { | |
226 | skb_trim(buf, packet_len); | |
227 | ||
228 | if (s->current_rx_buf) { | |
229 | usbnet_skb_return(dev, buf); | |
230 | s->current_rx_buf = NULL; | |
231 | return 0; | |
232 | } | |
233 | ||
234 | return 1; | |
235 | } | |
236 | } | |
237 | ||
238 | error: | |
239 | if (s->current_rx_buf) { | |
240 | dev_kfree_skb_any(s->current_rx_buf); | |
241 | s->current_rx_buf = NULL; | |
242 | } | |
243 | dev->net->stats.rx_errors++; | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static struct sk_buff *vl600_tx_fixup(struct usbnet *dev, | |
248 | struct sk_buff *skb, gfp_t flags) | |
249 | { | |
250 | struct sk_buff *ret; | |
251 | struct vl600_frame_hdr *frame; | |
252 | struct vl600_pkt_hdr *packet; | |
253 | static uint32_t serial = 1; | |
254 | int orig_len = skb->len - sizeof(struct ethhdr); | |
255 | int full_len = (skb->len + sizeof(struct vl600_frame_hdr) + 3) & ~3; | |
256 | ||
257 | frame = (struct vl600_frame_hdr *) skb->data; | |
258 | if (skb->len > sizeof(*frame) && skb->len == le32_to_cpup(&frame->len)) | |
259 | return skb; /* Already encapsulated? */ | |
260 | ||
261 | if (skb->len < sizeof(struct ethhdr)) | |
262 | /* Drop, device can only deal with ethernet packets */ | |
263 | return NULL; | |
264 | ||
265 | if (!skb_cloned(skb)) { | |
266 | int headroom = skb_headroom(skb); | |
267 | int tailroom = skb_tailroom(skb); | |
268 | ||
269 | if (tailroom >= full_len - skb->len - sizeof(*frame) && | |
270 | headroom >= sizeof(*frame)) | |
271 | /* There's enough head and tail room */ | |
272 | goto encapsulate; | |
273 | ||
274 | if (headroom + tailroom + skb->len >= full_len) { | |
275 | /* There's enough total room, just readjust */ | |
276 | skb->data = memmove(skb->head + sizeof(*frame), | |
277 | skb->data, skb->len); | |
278 | skb_set_tail_pointer(skb, skb->len); | |
279 | goto encapsulate; | |
280 | } | |
281 | } | |
282 | ||
283 | /* Alloc a new skb with the required size */ | |
284 | ret = skb_copy_expand(skb, sizeof(struct vl600_frame_hdr), full_len - | |
285 | skb->len - sizeof(struct vl600_frame_hdr), flags); | |
286 | dev_kfree_skb_any(skb); | |
287 | if (!ret) | |
288 | return ret; | |
289 | skb = ret; | |
290 | ||
291 | encapsulate: | |
292 | /* Packet header is same size as ethernet packet header | |
293 | * (sizeof(*packet) == sizeof(struct ethhdr)), additionally the | |
294 | * h_proto field is in the same place so we just leave it alone and | |
295 | * overwrite the remaining fields. | |
296 | */ | |
297 | packet = (struct vl600_pkt_hdr *) skb->data; | |
298 | memset(&packet->dummy, 0, sizeof(packet->dummy)); | |
299 | packet->len = cpu_to_le32(orig_len); | |
300 | ||
301 | frame = (struct vl600_frame_hdr *) skb_push(skb, sizeof(*frame)); | |
302 | memset(frame, 0, sizeof(*frame)); | |
303 | frame->len = cpu_to_le32(full_len); | |
304 | frame->serial = cpu_to_le32(serial++); | |
305 | frame->pkt_cnt = cpu_to_le32(1); | |
306 | ||
307 | if (skb->len < full_len) /* Pad */ | |
308 | skb_put(skb, full_len - skb->len); | |
309 | ||
2ae40ee9 MK |
310 | /* The VL600 wants IPv6 packets to have an IPv4 ethertype |
311 | * Check if this is an IPv6 packet, and set the ethertype | |
312 | * to 0x800 | |
313 | */ | |
314 | if ((skb->data[sizeof(struct vl600_pkt_hdr *) + 0x22] & 0xf0) == 0x60) { | |
315 | skb->data[sizeof(struct vl600_pkt_hdr *) + 0x20] = 0x08; | |
316 | skb->data[sizeof(struct vl600_pkt_hdr *) + 0x21] = 0; | |
317 | } | |
318 | ||
7a635ea9 AZ |
319 | return skb; |
320 | } | |
321 | ||
322 | static const struct driver_info vl600_info = { | |
323 | .description = "LG VL600 modem", | |
324 | .flags = FLAG_ETHER | FLAG_RX_ASSEMBLE, | |
325 | .bind = vl600_bind, | |
326 | .unbind = vl600_unbind, | |
327 | .status = usbnet_cdc_status, | |
328 | .rx_fixup = vl600_rx_fixup, | |
329 | .tx_fixup = vl600_tx_fixup, | |
330 | }; | |
331 | ||
332 | static const struct usb_device_id products[] = { | |
333 | { | |
334 | USB_DEVICE_AND_INTERFACE_INFO(0x1004, 0x61aa, USB_CLASS_COMM, | |
335 | USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), | |
336 | .driver_info = (unsigned long) &vl600_info, | |
337 | }, | |
338 | {}, /* End */ | |
339 | }; | |
340 | MODULE_DEVICE_TABLE(usb, products); | |
341 | ||
342 | static struct usb_driver lg_vl600_driver = { | |
343 | .name = "lg-vl600", | |
344 | .id_table = products, | |
345 | .probe = usbnet_probe, | |
346 | .disconnect = usbnet_disconnect, | |
347 | .suspend = usbnet_suspend, | |
348 | .resume = usbnet_resume, | |
349 | }; | |
350 | ||
351 | static int __init vl600_init(void) | |
352 | { | |
353 | return usb_register(&lg_vl600_driver); | |
354 | } | |
355 | module_init(vl600_init); | |
356 | ||
357 | static void __exit vl600_exit(void) | |
358 | { | |
359 | usb_deregister(&lg_vl600_driver); | |
360 | } | |
361 | module_exit(vl600_exit); | |
362 | ||
363 | MODULE_AUTHOR("Anrzej Zaborowski"); | |
364 | MODULE_DESCRIPTION("LG-VL600 modem's ethernet link"); | |
365 | MODULE_LICENSE("GPL"); |