Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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