^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) * Copyright (c) 2009 Peter Holik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Intellon usb PLC (Powerline Communications) usb net driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * http://www.tandel.be/downloads/INT51X1_Datasheet.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Based on the work of Jan 'RedBully' Seiffert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/ethtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/mii.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/usb/usbnet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define INT51X1_VENDOR_ID 0x09e1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define INT51X1_PRODUCT_ID 0x5121
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define INT51X1_HEADER_SIZE 2 /* 2 byte header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define PACKET_TYPE_PROMISCUOUS (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define PACKET_TYPE_DIRECTED (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define PACKET_TYPE_BROADCAST (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define SET_ETHERNET_PACKET_FILTER 0x43
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int int51x1_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!(pskb_may_pull(skb, INT51X1_HEADER_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) netdev_err(dev->net, "unexpected tiny rx frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) len = le16_to_cpu(*(__le16 *)&skb->data[skb->len - 2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) skb_trim(skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static struct sk_buff *int51x1_tx_fixup(struct usbnet *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct sk_buff *skb, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int pack_len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int pack_with_header_len = pack_len + INT51X1_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int headroom = skb_headroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int tailroom = skb_tailroom(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int need_tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __le16 *len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* if packet and our header is smaler than 64 pad to 64 (+ ZLP) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if ((pack_with_header_len) < dev->maxpacket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) need_tail = dev->maxpacket - pack_with_header_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * usbnet would send a ZLP if packetlength mod urbsize == 0 for us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * but we need to know ourself, because this would add to the length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * we send down to the device...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else if (!(pack_with_header_len % dev->maxpacket))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) need_tail = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!skb_cloned(skb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) (headroom + tailroom >= need_tail + INT51X1_HEADER_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (headroom < INT51X1_HEADER_SIZE || tailroom < need_tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) skb->data = memmove(skb->head + INT51X1_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) skb_set_tail_pointer(skb, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct sk_buff *skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) skb2 = skb_copy_expand(skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) INT51X1_HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) need_tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!skb2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) skb = skb2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pack_len += need_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pack_len &= 0x07ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) len = __skb_push(skb, INT51X1_HEADER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *len = cpu_to_le16(pack_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if(need_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) __skb_put_zero(skb, need_tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void int51x1_set_multicast(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct usbnet *dev = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u16 filter = PACKET_TYPE_DIRECTED | PACKET_TYPE_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (netdev->flags & IFF_PROMISC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* do not expect to see traffic of other PLCs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) filter |= PACKET_TYPE_PROMISCUOUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) netdev_info(dev->net, "promiscuous mode enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) } else if (!netdev_mc_empty(netdev) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) (netdev->flags & IFF_ALLMULTI)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) filter |= PACKET_TYPE_ALL_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) netdev_dbg(dev->net, "receive all multicast enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* ~PROMISCUOUS, ~MULTICAST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) netdev_dbg(dev->net, "receive own packets only\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) usbnet_write_cmd_async(dev, SET_ETHERNET_PACKET_FILTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) filter, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static const struct net_device_ops int51x1_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .ndo_open = usbnet_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .ndo_stop = usbnet_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .ndo_start_xmit = usbnet_start_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .ndo_tx_timeout = usbnet_tx_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .ndo_change_mtu = usbnet_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .ndo_get_stats64 = usbnet_get_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .ndo_set_mac_address = eth_mac_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .ndo_validate_addr = eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .ndo_set_rx_mode = int51x1_set_multicast,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int int51x1_bind(struct usbnet *dev, struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int status = usbnet_get_ethernet_addr(dev, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev->net->hard_header_len += INT51X1_HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev->net->netdev_ops = &int51x1_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return usbnet_get_endpoints(dev, intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static const struct driver_info int51x1_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .description = "Intellon usb powerline adapter",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .bind = int51x1_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .rx_fixup = int51x1_rx_fixup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .tx_fixup = int51x1_tx_fixup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .in = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .out = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .flags = FLAG_ETHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static const struct usb_device_id products[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) USB_DEVICE(INT51X1_VENDOR_ID, INT51X1_PRODUCT_ID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .driver_info = (unsigned long) &int51x1_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) MODULE_DEVICE_TABLE(usb, products);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static struct usb_driver int51x1_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .name = "int51x1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .id_table = products,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .probe = usbnet_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .disconnect = usbnet_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .suspend = usbnet_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .resume = usbnet_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .disable_hub_initiated_lpm = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) module_usb_driver(int51x1_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) MODULE_AUTHOR("Peter Holik");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) MODULE_DESCRIPTION("Intellon usb powerline adapter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) MODULE_LICENSE("GPL");