int51x1.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/slab.h>
  31. #include <linux/mii.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/usbnet.h>
  34. #define INT51X1_VENDOR_ID 0x09e1
  35. #define INT51X1_PRODUCT_ID 0x5121
  36. #define INT51X1_HEADER_SIZE 2 /* 2 byte header */
  37. #define PACKET_TYPE_PROMISCUOUS (1 << 0)
  38. #define PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
  39. #define PACKET_TYPE_DIRECTED (1 << 2)
  40. #define PACKET_TYPE_BROADCAST (1 << 3)
  41. #define PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
  42. #define SET_ETHERNET_PACKET_FILTER 0x43
  43. static int int51x1_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  44. {
  45. int len;
  46. if (!(pskb_may_pull(skb, INT51X1_HEADER_SIZE))) {
  47. netdev_err(dev->net, "unexpected tiny rx frame\n");
  48. return 0;
  49. }
  50. len = le16_to_cpu(*(__le16 *)&skb->data[skb->len - 2]);
  51. skb_trim(skb, len);
  52. return 1;
  53. }
  54. static struct sk_buff *int51x1_tx_fixup(struct usbnet *dev,
  55. struct sk_buff *skb, gfp_t flags)
  56. {
  57. int pack_len = skb->len;
  58. int pack_with_header_len = pack_len + INT51X1_HEADER_SIZE;
  59. int headroom = skb_headroom(skb);
  60. int tailroom = skb_tailroom(skb);
  61. int need_tail = 0;
  62. __le16 *len;
  63. /* if packet and our header is smaler than 64 pad to 64 (+ ZLP) */
  64. if ((pack_with_header_len) < dev->maxpacket)
  65. need_tail = dev->maxpacket - pack_with_header_len + 1;
  66. /*
  67. * usbnet would send a ZLP if packetlength mod urbsize == 0 for us,
  68. * but we need to know ourself, because this would add to the length
  69. * we send down to the device...
  70. */
  71. else if (!(pack_with_header_len % dev->maxpacket))
  72. need_tail = 1;
  73. if (!skb_cloned(skb) &&
  74. (headroom + tailroom >= need_tail + INT51X1_HEADER_SIZE)) {
  75. if (headroom < INT51X1_HEADER_SIZE || tailroom < need_tail) {
  76. skb->data = memmove(skb->head + INT51X1_HEADER_SIZE,
  77. skb->data, skb->len);
  78. skb_set_tail_pointer(skb, skb->len);
  79. }
  80. } else {
  81. struct sk_buff *skb2;
  82. skb2 = skb_copy_expand(skb,
  83. INT51X1_HEADER_SIZE,
  84. need_tail,
  85. flags);
  86. dev_kfree_skb_any(skb);
  87. if (!skb2)
  88. return NULL;
  89. skb = skb2;
  90. }
  91. pack_len += need_tail;
  92. pack_len &= 0x07ff;
  93. len = (__le16 *) __skb_push(skb, INT51X1_HEADER_SIZE);
  94. *len = cpu_to_le16(pack_len);
  95. if(need_tail)
  96. memset(__skb_put(skb, need_tail), 0, need_tail);
  97. return skb;
  98. }
  99. static void int51x1_async_cmd_callback(struct urb *urb)
  100. {
  101. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  102. int status = urb->status;
  103. if (status < 0)
  104. dev_warn(&urb->dev->dev, "async callback failed with %d\n", status);
  105. kfree(req);
  106. usb_free_urb(urb);
  107. }
  108. static void int51x1_set_multicast(struct net_device *netdev)
  109. {
  110. struct usb_ctrlrequest *req;
  111. int status;
  112. struct urb *urb;
  113. struct usbnet *dev = netdev_priv(netdev);
  114. u16 filter = PACKET_TYPE_DIRECTED | PACKET_TYPE_BROADCAST;
  115. if (netdev->flags & IFF_PROMISC) {
  116. /* do not expect to see traffic of other PLCs */
  117. filter |= PACKET_TYPE_PROMISCUOUS;
  118. netdev_info(dev->net, "promiscuous mode enabled\n");
  119. } else if (!netdev_mc_empty(netdev) ||
  120. (netdev->flags & IFF_ALLMULTI)) {
  121. filter |= PACKET_TYPE_ALL_MULTICAST;
  122. netdev_dbg(dev->net, "receive all multicast enabled\n");
  123. } else {
  124. /* ~PROMISCUOUS, ~MULTICAST */
  125. netdev_dbg(dev->net, "receive own packets only\n");
  126. }
  127. urb = usb_alloc_urb(0, GFP_ATOMIC);
  128. if (!urb) {
  129. netdev_warn(dev->net, "Error allocating URB\n");
  130. return;
  131. }
  132. req = kmalloc(sizeof(*req), GFP_ATOMIC);
  133. if (!req) {
  134. netdev_warn(dev->net, "Error allocating control msg\n");
  135. goto out;
  136. }
  137. req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  138. req->bRequest = SET_ETHERNET_PACKET_FILTER;
  139. req->wValue = cpu_to_le16(filter);
  140. req->wIndex = 0;
  141. req->wLength = 0;
  142. usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
  143. (void *)req, NULL, 0,
  144. int51x1_async_cmd_callback,
  145. (void *)req);
  146. status = usb_submit_urb(urb, GFP_ATOMIC);
  147. if (status < 0) {
  148. netdev_warn(dev->net, "Error submitting control msg, sts=%d\n",
  149. status);
  150. goto out1;
  151. }
  152. return;
  153. out1:
  154. kfree(req);
  155. out:
  156. usb_free_urb(urb);
  157. }
  158. static const struct net_device_ops int51x1_netdev_ops = {
  159. .ndo_open = usbnet_open,
  160. .ndo_stop = usbnet_stop,
  161. .ndo_start_xmit = usbnet_start_xmit,
  162. .ndo_tx_timeout = usbnet_tx_timeout,
  163. .ndo_change_mtu = usbnet_change_mtu,
  164. .ndo_set_mac_address = eth_mac_addr,
  165. .ndo_validate_addr = eth_validate_addr,
  166. .ndo_set_multicast_list = int51x1_set_multicast,
  167. };
  168. static int int51x1_bind(struct usbnet *dev, struct usb_interface *intf)
  169. {
  170. int status = usbnet_get_ethernet_addr(dev, 3);
  171. if (status)
  172. return status;
  173. dev->net->hard_header_len += INT51X1_HEADER_SIZE;
  174. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  175. dev->net->netdev_ops = &int51x1_netdev_ops;
  176. return usbnet_get_endpoints(dev, intf);
  177. }
  178. static const struct driver_info int51x1_info = {
  179. .description = "Intellon usb powerline adapter",
  180. .bind = int51x1_bind,
  181. .rx_fixup = int51x1_rx_fixup,
  182. .tx_fixup = int51x1_tx_fixup,
  183. .in = 1,
  184. .out = 2,
  185. .flags = FLAG_ETHER,
  186. };
  187. static const struct usb_device_id products[] = {
  188. {
  189. USB_DEVICE(INT51X1_VENDOR_ID, INT51X1_PRODUCT_ID),
  190. .driver_info = (unsigned long) &int51x1_info,
  191. },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(usb, products);
  195. static struct usb_driver int51x1_driver = {
  196. .name = "int51x1",
  197. .id_table = products,
  198. .probe = usbnet_probe,
  199. .disconnect = usbnet_disconnect,
  200. .suspend = usbnet_suspend,
  201. .resume = usbnet_resume,
  202. };
  203. static int __init int51x1_init(void)
  204. {
  205. return usb_register(&int51x1_driver);
  206. }
  207. module_init(int51x1_init);
  208. static void __exit int51x1_exit(void)
  209. {
  210. usb_deregister(&int51x1_driver);
  211. }
  212. module_exit(int51x1_exit);
  213. MODULE_AUTHOR("Peter Holik");
  214. MODULE_DESCRIPTION("Intellon usb powerline adapter");
  215. MODULE_LICENSE("GPL");