2 * Ethernet interface part of the LG VL600 LTE modem (4G dongle)
4 * Copyright (C) 2011 Intel Corporation
5 * Author: Andrzej Zaborowski <balrogg@gmail.com>
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.
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.
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
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>
33 * The device has a CDC ACM port for modem control (it claims to be
34 * CDC ACM anyway) and a CDC Ethernet port for actual network data.
35 * It will however ignore data on both ports that is not encapsulated
36 * in a specific way, any data returned is also encapsulated the same
37 * way. The headers don't seem to follow any popular standard.
39 * This driver adds and strips these headers from the ethernet frames
40 * sent/received from the CDC Ethernet port. The proprietary header
41 * replaces the standard ethernet header in a packet so only actual
42 * ethernet frames are allowed. The headers allow some form of
43 * multiplexing by using non standard values of the .h_proto field.
44 * Windows/Mac drivers do send a couple of such frames to the device
45 * during initialisation, with protocol set to 0x0906 or 0x0b06 and (what
46 * seems to be) a flag in the .dummy_flags. This doesn't seem necessary
47 * for modem operation but can possibly be used for GPS or other funcitons.
50 struct vl600_frame_hdr
{
57 } __attribute__((packed
));
59 struct vl600_pkt_hdr
{
63 } __attribute__((packed
));
66 struct sk_buff
*current_rx_buf
;
69 static int vl600_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
72 struct vl600_state
*s
= kzalloc(sizeof(struct vl600_state
), GFP_KERNEL
);
77 ret
= usbnet_cdc_bind(dev
, intf
);
85 /* ARP packets don't go through, but they're also of no use. The
86 * subnet has only two hosts anyway: us and the gateway / DHCP
87 * server (probably simulated by modem firmware or network operator)
88 * whose address changes everytime we connect to the intarwebz and
89 * who doesn't bother answering ARP requests either. So hardware
90 * addresses have no meaning, the destination and the source of every
91 * packet depend only on whether it is on the IN or OUT endpoint. */
92 dev
->net
->flags
|= IFF_NOARP
;
93 /* IPv6 NDP relies on multicast. Enable it by default. */
94 dev
->net
->flags
|= IFF_MULTICAST
;
99 static void vl600_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
101 struct vl600_state
*s
= dev
->driver_priv
;
103 if (s
->current_rx_buf
)
104 dev_kfree_skb(s
->current_rx_buf
);
108 return usbnet_cdc_unbind(dev
, intf
);
111 static int vl600_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
113 struct vl600_frame_hdr
*frame
;
114 struct vl600_pkt_hdr
*packet
;
115 struct ethhdr
*ethhdr
;
116 int packet_len
, count
;
117 struct sk_buff
*buf
= skb
;
118 struct sk_buff
*clone
;
119 struct vl600_state
*s
= dev
->driver_priv
;
121 /* Frame lengths are generally 4B multiplies but every couple of
122 * hours there's an odd number of bytes sized yet correct frame,
123 * so don't require this. */
125 /* Allow a packet (or multiple packets batched together) to be
126 * split across many frames. We don't allow a new batch to
127 * begin in the same frame another one is ending however, and no
128 * leading or trailing pad bytes. */
129 if (s
->current_rx_buf
) {
130 frame
= (struct vl600_frame_hdr
*) s
->current_rx_buf
->data
;
131 if (skb
->len
+ s
->current_rx_buf
->len
>
132 le32_to_cpup(&frame
->len
)) {
133 netif_err(dev
, ifup
, dev
->net
, "Fragment too long\n");
134 dev
->net
->stats
.rx_length_errors
++;
138 buf
= s
->current_rx_buf
;
139 memcpy(skb_put(buf
, skb
->len
), skb
->data
, skb
->len
);
140 } else if (skb
->len
< 4) {
141 netif_err(dev
, ifup
, dev
->net
, "Frame too short\n");
142 dev
->net
->stats
.rx_length_errors
++;
146 frame
= (struct vl600_frame_hdr
*) buf
->data
;
147 /* NOTE: Should check that frame->magic == 0x53544448?
148 * Otherwise if we receive garbage at the beginning of the frame
149 * we may end up allocating a huge buffer and saving all the
150 * future incoming data into it. */
152 if (buf
->len
< sizeof(*frame
) ||
153 buf
->len
!= le32_to_cpup(&frame
->len
)) {
154 /* Save this fragment for later assembly */
155 if (s
->current_rx_buf
)
158 s
->current_rx_buf
= skb_copy_expand(skb
, 0,
159 le32_to_cpup(&frame
->len
), GFP_ATOMIC
);
160 if (!s
->current_rx_buf
) {
161 netif_err(dev
, ifup
, dev
->net
, "Reserving %i bytes "
162 "for packet assembly failed.\n",
163 le32_to_cpup(&frame
->len
));
164 dev
->net
->stats
.rx_errors
++;
170 count
= le32_to_cpup(&frame
->pkt_cnt
);
172 skb_pull(buf
, sizeof(*frame
));
175 if (buf
->len
< sizeof(*packet
)) {
176 netif_err(dev
, ifup
, dev
->net
, "Packet too short\n");
180 packet
= (struct vl600_pkt_hdr
*) buf
->data
;
181 packet_len
= sizeof(*packet
) + le32_to_cpup(&packet
->len
);
182 if (packet_len
> buf
->len
) {
183 netif_err(dev
, ifup
, dev
->net
,
184 "Bad packet length stored in header\n");
188 /* Packet header is same size as the ethernet header
189 * (sizeof(*packet) == sizeof(*ethhdr)), additionally
190 * the h_proto field is in the same place so we just leave it
191 * alone and fill in the remaining fields.
193 ethhdr
= (struct ethhdr
*) skb
->data
;
194 if (be16_to_cpup(ðhdr
->h_proto
) == ETH_P_ARP
&&
196 /* Copy the addresses from packet contents */
197 memcpy(ethhdr
->h_source
,
198 &buf
->data
[sizeof(*ethhdr
) + 0x8],
200 memcpy(ethhdr
->h_dest
,
201 &buf
->data
[sizeof(*ethhdr
) + 0x12],
204 memset(ethhdr
->h_source
, 0, ETH_ALEN
);
205 memcpy(ethhdr
->h_dest
, dev
->net
->dev_addr
, ETH_ALEN
);
207 /* Inbound IPv6 packets have an IPv4 ethertype (0x800)
208 * for some reason. Peek at the L3 header to check
209 * for IPv6 packets, and set the ethertype to IPv6
210 * (0x86dd) so Linux can understand it.
212 if ((buf
->data
[sizeof(*ethhdr
)] & 0xf0) == 0x60)
213 ethhdr
->h_proto
= __constant_htons(ETH_P_IPV6
);
217 /* Not the last packet in this batch */
218 clone
= skb_clone(buf
, GFP_ATOMIC
);
222 skb_trim(clone
, packet_len
);
223 usbnet_skb_return(dev
, clone
);
225 skb_pull(buf
, (packet_len
+ 3) & ~3);
227 skb_trim(buf
, packet_len
);
229 if (s
->current_rx_buf
) {
230 usbnet_skb_return(dev
, buf
);
231 s
->current_rx_buf
= NULL
;
240 if (s
->current_rx_buf
) {
241 dev_kfree_skb_any(s
->current_rx_buf
);
242 s
->current_rx_buf
= NULL
;
244 dev
->net
->stats
.rx_errors
++;
248 static struct sk_buff
*vl600_tx_fixup(struct usbnet
*dev
,
249 struct sk_buff
*skb
, gfp_t flags
)
252 struct vl600_frame_hdr
*frame
;
253 struct vl600_pkt_hdr
*packet
;
254 static uint32_t serial
= 1;
255 int orig_len
= skb
->len
- sizeof(struct ethhdr
);
256 int full_len
= (skb
->len
+ sizeof(struct vl600_frame_hdr
) + 3) & ~3;
258 frame
= (struct vl600_frame_hdr
*) skb
->data
;
259 if (skb
->len
> sizeof(*frame
) && skb
->len
== le32_to_cpup(&frame
->len
))
260 return skb
; /* Already encapsulated? */
262 if (skb
->len
< sizeof(struct ethhdr
))
263 /* Drop, device can only deal with ethernet packets */
266 if (!skb_cloned(skb
)) {
267 int headroom
= skb_headroom(skb
);
268 int tailroom
= skb_tailroom(skb
);
270 if (tailroom
>= full_len
- skb
->len
- sizeof(*frame
) &&
271 headroom
>= sizeof(*frame
))
272 /* There's enough head and tail room */
275 if (headroom
+ tailroom
+ skb
->len
>= full_len
) {
276 /* There's enough total room, just readjust */
277 skb
->data
= memmove(skb
->head
+ sizeof(*frame
),
278 skb
->data
, skb
->len
);
279 skb_set_tail_pointer(skb
, skb
->len
);
284 /* Alloc a new skb with the required size */
285 ret
= skb_copy_expand(skb
, sizeof(struct vl600_frame_hdr
), full_len
-
286 skb
->len
- sizeof(struct vl600_frame_hdr
), flags
);
287 dev_kfree_skb_any(skb
);
293 /* Packet header is same size as ethernet packet header
294 * (sizeof(*packet) == sizeof(struct ethhdr)), additionally the
295 * h_proto field is in the same place so we just leave it alone and
296 * overwrite the remaining fields.
298 packet
= (struct vl600_pkt_hdr
*) skb
->data
;
299 memset(&packet
->dummy
, 0, sizeof(packet
->dummy
));
300 packet
->len
= cpu_to_le32(orig_len
);
302 frame
= (struct vl600_frame_hdr
*) skb_push(skb
, sizeof(*frame
));
303 memset(frame
, 0, sizeof(*frame
));
304 frame
->len
= cpu_to_le32(full_len
);
305 frame
->serial
= cpu_to_le32(serial
++);
306 frame
->pkt_cnt
= cpu_to_le32(1);
308 if (skb
->len
< full_len
) /* Pad */
309 skb_put(skb
, full_len
- skb
->len
);
311 /* The VL600 wants IPv6 packets to have an IPv4 ethertype
312 * Check if this is an IPv6 packet, and set the ethertype
315 if ((skb
->data
[sizeof(struct vl600_pkt_hdr
*) + 0x22] & 0xf0) == 0x60) {
316 skb
->data
[sizeof(struct vl600_pkt_hdr
*) + 0x20] = 0x08;
317 skb
->data
[sizeof(struct vl600_pkt_hdr
*) + 0x21] = 0;
323 static const struct driver_info vl600_info
= {
324 .description
= "LG VL600 modem",
325 .flags
= FLAG_ETHER
| FLAG_RX_ASSEMBLE
,
327 .unbind
= vl600_unbind
,
328 .status
= usbnet_cdc_status
,
329 .rx_fixup
= vl600_rx_fixup
,
330 .tx_fixup
= vl600_tx_fixup
,
333 static const struct usb_device_id products
[] = {
335 USB_DEVICE_AND_INTERFACE_INFO(0x1004, 0x61aa, USB_CLASS_COMM
,
336 USB_CDC_SUBCLASS_ETHERNET
, USB_CDC_PROTO_NONE
),
337 .driver_info
= (unsigned long) &vl600_info
,
341 MODULE_DEVICE_TABLE(usb
, products
);
343 static struct usb_driver lg_vl600_driver
= {
345 .id_table
= products
,
346 .probe
= usbnet_probe
,
347 .disconnect
= usbnet_disconnect
,
348 .suspend
= usbnet_suspend
,
349 .resume
= usbnet_resume
,
352 module_usb_driver(lg_vl600_driver
);
354 MODULE_AUTHOR("Anrzej Zaborowski");
355 MODULE_DESCRIPTION("LG-VL600 modem's ethernet link");
356 MODULE_LICENSE("GPL");