int51x1.c 4.8 KB

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