int51x1.c 5.4 KB

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