18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2009 Peter Holik
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Intellon usb PLC (Powerline Communications) usb net driver
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * http://www.tandel.be/downloads/INT51X1_Datasheet.pdf
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on the work of Jan 'RedBully' Seiffert
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/ctype.h>
178c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
188c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
198c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/mii.h>
228c2ecf20Sopenharmony_ci#include <linux/usb.h>
238c2ecf20Sopenharmony_ci#include <linux/usb/usbnet.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define INT51X1_VENDOR_ID	0x09e1
268c2ecf20Sopenharmony_ci#define INT51X1_PRODUCT_ID	0x5121
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define INT51X1_HEADER_SIZE	2	/* 2 byte header */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define PACKET_TYPE_PROMISCUOUS		(1 << 0)
318c2ecf20Sopenharmony_ci#define PACKET_TYPE_ALL_MULTICAST	(1 << 1) /* no filter */
328c2ecf20Sopenharmony_ci#define PACKET_TYPE_DIRECTED		(1 << 2)
338c2ecf20Sopenharmony_ci#define PACKET_TYPE_BROADCAST		(1 << 3)
348c2ecf20Sopenharmony_ci#define PACKET_TYPE_MULTICAST		(1 << 4) /* filtered */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define SET_ETHERNET_PACKET_FILTER	0x43
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int int51x1_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	int len;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (!(pskb_may_pull(skb, INT51X1_HEADER_SIZE))) {
438c2ecf20Sopenharmony_ci		netdev_err(dev->net, "unexpected tiny rx frame\n");
448c2ecf20Sopenharmony_ci		return 0;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	len = le16_to_cpu(*(__le16 *)&skb->data[skb->len - 2]);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	skb_trim(skb, len);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return 1;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic struct sk_buff *int51x1_tx_fixup(struct usbnet *dev,
558c2ecf20Sopenharmony_ci		struct sk_buff *skb, gfp_t flags)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int pack_len = skb->len;
588c2ecf20Sopenharmony_ci	int pack_with_header_len = pack_len + INT51X1_HEADER_SIZE;
598c2ecf20Sopenharmony_ci	int headroom = skb_headroom(skb);
608c2ecf20Sopenharmony_ci	int tailroom = skb_tailroom(skb);
618c2ecf20Sopenharmony_ci	int need_tail = 0;
628c2ecf20Sopenharmony_ci	__le16 *len;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* if packet and our header is smaler than 64 pad to 64 (+ ZLP) */
658c2ecf20Sopenharmony_ci	if ((pack_with_header_len) < dev->maxpacket)
668c2ecf20Sopenharmony_ci		need_tail = dev->maxpacket - pack_with_header_len + 1;
678c2ecf20Sopenharmony_ci	/*
688c2ecf20Sopenharmony_ci	 * usbnet would send a ZLP if packetlength mod urbsize == 0 for us,
698c2ecf20Sopenharmony_ci	 * but we need to know ourself, because this would add to the length
708c2ecf20Sopenharmony_ci	 * we send down to the device...
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	else if (!(pack_with_header_len % dev->maxpacket))
738c2ecf20Sopenharmony_ci		need_tail = 1;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (!skb_cloned(skb) &&
768c2ecf20Sopenharmony_ci			(headroom + tailroom >= need_tail + INT51X1_HEADER_SIZE)) {
778c2ecf20Sopenharmony_ci		if (headroom < INT51X1_HEADER_SIZE || tailroom < need_tail) {
788c2ecf20Sopenharmony_ci			skb->data = memmove(skb->head + INT51X1_HEADER_SIZE,
798c2ecf20Sopenharmony_ci					skb->data, skb->len);
808c2ecf20Sopenharmony_ci			skb_set_tail_pointer(skb, skb->len);
818c2ecf20Sopenharmony_ci		}
828c2ecf20Sopenharmony_ci	} else {
838c2ecf20Sopenharmony_ci		struct sk_buff *skb2;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci		skb2 = skb_copy_expand(skb,
868c2ecf20Sopenharmony_ci				INT51X1_HEADER_SIZE,
878c2ecf20Sopenharmony_ci				need_tail,
888c2ecf20Sopenharmony_ci				flags);
898c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
908c2ecf20Sopenharmony_ci		if (!skb2)
918c2ecf20Sopenharmony_ci			return NULL;
928c2ecf20Sopenharmony_ci		skb = skb2;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	pack_len += need_tail;
968c2ecf20Sopenharmony_ci	pack_len &= 0x07ff;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	len = __skb_push(skb, INT51X1_HEADER_SIZE);
998c2ecf20Sopenharmony_ci	*len = cpu_to_le16(pack_len);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if(need_tail)
1028c2ecf20Sopenharmony_ci		__skb_put_zero(skb, need_tail);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return skb;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void int51x1_set_multicast(struct net_device *netdev)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct usbnet *dev = netdev_priv(netdev);
1108c2ecf20Sopenharmony_ci	u16 filter = PACKET_TYPE_DIRECTED | PACKET_TYPE_BROADCAST;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (netdev->flags & IFF_PROMISC) {
1138c2ecf20Sopenharmony_ci		/* do not expect to see traffic of other PLCs */
1148c2ecf20Sopenharmony_ci		filter |= PACKET_TYPE_PROMISCUOUS;
1158c2ecf20Sopenharmony_ci		netdev_info(dev->net, "promiscuous mode enabled\n");
1168c2ecf20Sopenharmony_ci	} else if (!netdev_mc_empty(netdev) ||
1178c2ecf20Sopenharmony_ci		  (netdev->flags & IFF_ALLMULTI)) {
1188c2ecf20Sopenharmony_ci		filter |= PACKET_TYPE_ALL_MULTICAST;
1198c2ecf20Sopenharmony_ci		netdev_dbg(dev->net, "receive all multicast enabled\n");
1208c2ecf20Sopenharmony_ci	} else {
1218c2ecf20Sopenharmony_ci		/* ~PROMISCUOUS, ~MULTICAST */
1228c2ecf20Sopenharmony_ci		netdev_dbg(dev->net, "receive own packets only\n");
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	usbnet_write_cmd_async(dev, SET_ETHERNET_PACKET_FILTER,
1268c2ecf20Sopenharmony_ci			       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1278c2ecf20Sopenharmony_ci			       filter, 0, NULL, 0);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct net_device_ops int51x1_netdev_ops = {
1318c2ecf20Sopenharmony_ci	.ndo_open		= usbnet_open,
1328c2ecf20Sopenharmony_ci	.ndo_stop		= usbnet_stop,
1338c2ecf20Sopenharmony_ci	.ndo_start_xmit		= usbnet_start_xmit,
1348c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= usbnet_tx_timeout,
1358c2ecf20Sopenharmony_ci	.ndo_change_mtu		= usbnet_change_mtu,
1368c2ecf20Sopenharmony_ci	.ndo_get_stats64	= usbnet_get_stats64,
1378c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= eth_mac_addr,
1388c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
1398c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= int51x1_set_multicast,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int int51x1_bind(struct usbnet *dev, struct usb_interface *intf)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	int status = usbnet_get_ethernet_addr(dev, 3);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (status)
1478c2ecf20Sopenharmony_ci		return status;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	dev->net->hard_header_len += INT51X1_HEADER_SIZE;
1508c2ecf20Sopenharmony_ci	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1518c2ecf20Sopenharmony_ci	dev->net->netdev_ops = &int51x1_netdev_ops;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return usbnet_get_endpoints(dev, intf);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic const struct driver_info int51x1_info = {
1578c2ecf20Sopenharmony_ci	.description = "Intellon usb powerline adapter",
1588c2ecf20Sopenharmony_ci	.bind        = int51x1_bind,
1598c2ecf20Sopenharmony_ci	.rx_fixup    = int51x1_rx_fixup,
1608c2ecf20Sopenharmony_ci	.tx_fixup    = int51x1_tx_fixup,
1618c2ecf20Sopenharmony_ci	.in          = 1,
1628c2ecf20Sopenharmony_ci	.out         = 2,
1638c2ecf20Sopenharmony_ci	.flags       = FLAG_ETHER,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct usb_device_id products[] = {
1678c2ecf20Sopenharmony_ci	{
1688c2ecf20Sopenharmony_ci	USB_DEVICE(INT51X1_VENDOR_ID, INT51X1_PRODUCT_ID),
1698c2ecf20Sopenharmony_ci		.driver_info = (unsigned long) &int51x1_info,
1708c2ecf20Sopenharmony_ci	},
1718c2ecf20Sopenharmony_ci	{},
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, products);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct usb_driver int51x1_driver = {
1768c2ecf20Sopenharmony_ci	.name       = "int51x1",
1778c2ecf20Sopenharmony_ci	.id_table   = products,
1788c2ecf20Sopenharmony_ci	.probe      = usbnet_probe,
1798c2ecf20Sopenharmony_ci	.disconnect = usbnet_disconnect,
1808c2ecf20Sopenharmony_ci	.suspend    = usbnet_suspend,
1818c2ecf20Sopenharmony_ci	.resume     = usbnet_resume,
1828c2ecf20Sopenharmony_ci	.disable_hub_initiated_lpm = 1,
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cimodule_usb_driver(int51x1_driver);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Holik");
1888c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Intellon usb powerline adapter");
1898c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
190