int51x1.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/ctype.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/ethtool.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. deverr(dev, "unexpected tiny rx frame");
  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_async_cmd_callback(struct urb *urb)
  99. {
  100. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  101. int status = urb->status;
  102. if (status < 0)
  103. dev_warn(&urb->dev->dev, "async callback failed with %d\n", status);
  104. kfree(req);
  105. usb_free_urb(urb);
  106. }
  107. static void int51x1_set_multicast(struct net_device *netdev)
  108. {
  109. struct usb_ctrlrequest *req;
  110. int status;
  111. struct urb *urb;
  112. struct usbnet *dev = netdev_priv(netdev);
  113. u16 filter = PACKET_TYPE_DIRECTED | PACKET_TYPE_BROADCAST;
  114. if (netdev->flags & IFF_PROMISC) {
  115. /* do not expect to see traffic of other PLCs */
  116. filter |= PACKET_TYPE_PROMISCUOUS;
  117. devinfo(dev, "promiscuous mode enabled");
  118. } else if (netdev->mc_count ||
  119. (netdev->flags & IFF_ALLMULTI)) {
  120. filter |= PACKET_TYPE_ALL_MULTICAST;
  121. devdbg(dev, "receive all multicast enabled");
  122. } else {
  123. /* ~PROMISCUOUS, ~MULTICAST */
  124. devdbg(dev, "receive own packets only");
  125. }
  126. urb = usb_alloc_urb(0, GFP_ATOMIC);
  127. if (!urb) {
  128. devwarn(dev, "Error allocating URB");
  129. return;
  130. }
  131. req = kmalloc(sizeof(*req), GFP_ATOMIC);
  132. if (!req) {
  133. devwarn(dev, "Error allocating control msg");
  134. goto out;
  135. }
  136. req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  137. req->bRequest = SET_ETHERNET_PACKET_FILTER;
  138. req->wValue = cpu_to_le16(filter);
  139. req->wIndex = 0;
  140. req->wLength = 0;
  141. usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
  142. (void *)req, NULL, 0,
  143. int51x1_async_cmd_callback,
  144. (void *)req);
  145. status = usb_submit_urb(urb, GFP_ATOMIC);
  146. if (status < 0) {
  147. devwarn(dev, "Error submitting control msg, sts=%d", status);
  148. goto out1;
  149. }
  150. return;
  151. out1:
  152. kfree(req);
  153. out:
  154. usb_free_urb(urb);
  155. }
  156. static const struct net_device_ops int51x1_netdev_ops = {
  157. .ndo_open = usbnet_open,
  158. .ndo_stop = usbnet_stop,
  159. .ndo_start_xmit = usbnet_start_xmit,
  160. .ndo_tx_timeout = usbnet_tx_timeout,
  161. .ndo_change_mtu = usbnet_change_mtu,
  162. .ndo_set_mac_address = eth_mac_addr,
  163. .ndo_validate_addr = eth_validate_addr,
  164. .ndo_set_multicast_list = int51x1_set_multicast,
  165. };
  166. static int int51x1_bind(struct usbnet *dev, struct usb_interface *intf)
  167. {
  168. int status = usbnet_get_ethernet_addr(dev, 3);
  169. if (status)
  170. return status;
  171. dev->net->hard_header_len += INT51X1_HEADER_SIZE;
  172. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  173. dev->net->netdev_ops = &int51x1_netdev_ops;
  174. return usbnet_get_endpoints(dev, intf);
  175. }
  176. static const struct driver_info int51x1_info = {
  177. .description = "Intellon usb powerline adapter",
  178. .bind = int51x1_bind,
  179. .rx_fixup = int51x1_rx_fixup,
  180. .tx_fixup = int51x1_tx_fixup,
  181. .in = 1,
  182. .out = 2,
  183. .flags = FLAG_ETHER,
  184. };
  185. static const struct usb_device_id products[] = {
  186. {
  187. USB_DEVICE(INT51X1_VENDOR_ID, INT51X1_PRODUCT_ID),
  188. .driver_info = (unsigned long) &int51x1_info,
  189. },
  190. {},
  191. };
  192. MODULE_DEVICE_TABLE(usb, products);
  193. static struct usb_driver int51x1_driver = {
  194. .name = "int51x1",
  195. .id_table = products,
  196. .probe = usbnet_probe,
  197. .disconnect = usbnet_disconnect,
  198. .suspend = usbnet_suspend,
  199. .resume = usbnet_resume,
  200. };
  201. static int __init int51x1_init(void)
  202. {
  203. return usb_register(&int51x1_driver);
  204. }
  205. module_init(int51x1_init);
  206. static void __exit int51x1_exit(void)
  207. {
  208. usb_deregister(&int51x1_driver);
  209. }
  210. module_exit(int51x1_exit);
  211. MODULE_AUTHOR("Peter Holik");
  212. MODULE_DESCRIPTION("Intellon usb powerline adapter");
  213. MODULE_LICENSE("GPL");