Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 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 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or. |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/ctype.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/ethtool.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 33 | #include <linux/mii.h> |
| 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb/usbnet.h> |
| 36 | |
| 37 | #define INT51X1_VENDOR_ID 0x09e1 |
| 38 | #define INT51X1_PRODUCT_ID 0x5121 |
| 39 | |
| 40 | #define INT51X1_HEADER_SIZE 2 /* 2 byte header */ |
| 41 | |
| 42 | #define PACKET_TYPE_PROMISCUOUS (1 << 0) |
| 43 | #define PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */ |
| 44 | #define PACKET_TYPE_DIRECTED (1 << 2) |
| 45 | #define PACKET_TYPE_BROADCAST (1 << 3) |
| 46 | #define PACKET_TYPE_MULTICAST (1 << 4) /* filtered */ |
| 47 | |
| 48 | #define SET_ETHERNET_PACKET_FILTER 0x43 |
| 49 | |
| 50 | static int int51x1_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 51 | { |
| 52 | int len; |
| 53 | |
| 54 | if (!(pskb_may_pull(skb, INT51X1_HEADER_SIZE))) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 55 | netdev_err(dev->net, "unexpected tiny rx frame\n"); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | len = le16_to_cpu(*(__le16 *)&skb->data[skb->len - 2]); |
| 60 | |
| 61 | skb_trim(skb, len); |
| 62 | |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static struct sk_buff *int51x1_tx_fixup(struct usbnet *dev, |
| 67 | struct sk_buff *skb, gfp_t flags) |
| 68 | { |
| 69 | int pack_len = skb->len; |
| 70 | int pack_with_header_len = pack_len + INT51X1_HEADER_SIZE; |
| 71 | int headroom = skb_headroom(skb); |
| 72 | int tailroom = skb_tailroom(skb); |
| 73 | int need_tail = 0; |
| 74 | __le16 *len; |
| 75 | |
| 76 | /* if packet and our header is smaler than 64 pad to 64 (+ ZLP) */ |
| 77 | if ((pack_with_header_len) < dev->maxpacket) |
| 78 | need_tail = dev->maxpacket - pack_with_header_len + 1; |
| 79 | /* |
| 80 | * usbnet would send a ZLP if packetlength mod urbsize == 0 for us, |
| 81 | * but we need to know ourself, because this would add to the length |
| 82 | * we send down to the device... |
| 83 | */ |
| 84 | else if (!(pack_with_header_len % dev->maxpacket)) |
| 85 | need_tail = 1; |
| 86 | |
| 87 | if (!skb_cloned(skb) && |
| 88 | (headroom + tailroom >= need_tail + INT51X1_HEADER_SIZE)) { |
| 89 | if (headroom < INT51X1_HEADER_SIZE || tailroom < need_tail) { |
| 90 | skb->data = memmove(skb->head + INT51X1_HEADER_SIZE, |
| 91 | skb->data, skb->len); |
| 92 | skb_set_tail_pointer(skb, skb->len); |
| 93 | } |
| 94 | } else { |
| 95 | struct sk_buff *skb2; |
| 96 | |
| 97 | skb2 = skb_copy_expand(skb, |
| 98 | INT51X1_HEADER_SIZE, |
| 99 | need_tail, |
| 100 | flags); |
| 101 | dev_kfree_skb_any(skb); |
| 102 | if (!skb2) |
| 103 | return NULL; |
| 104 | skb = skb2; |
| 105 | } |
| 106 | |
| 107 | pack_len += need_tail; |
| 108 | pack_len &= 0x07ff; |
| 109 | |
| 110 | len = (__le16 *) __skb_push(skb, INT51X1_HEADER_SIZE); |
| 111 | *len = cpu_to_le16(pack_len); |
| 112 | |
| 113 | if(need_tail) |
| 114 | memset(__skb_put(skb, need_tail), 0, need_tail); |
| 115 | |
| 116 | return skb; |
| 117 | } |
| 118 | |
| 119 | static void int51x1_async_cmd_callback(struct urb *urb) |
| 120 | { |
| 121 | struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; |
| 122 | int status = urb->status; |
| 123 | |
| 124 | if (status < 0) |
| 125 | dev_warn(&urb->dev->dev, "async callback failed with %d\n", status); |
| 126 | |
| 127 | kfree(req); |
| 128 | usb_free_urb(urb); |
| 129 | } |
| 130 | |
| 131 | static void int51x1_set_multicast(struct net_device *netdev) |
| 132 | { |
| 133 | struct usb_ctrlrequest *req; |
| 134 | int status; |
| 135 | struct urb *urb; |
| 136 | struct usbnet *dev = netdev_priv(netdev); |
| 137 | u16 filter = PACKET_TYPE_DIRECTED | PACKET_TYPE_BROADCAST; |
| 138 | |
| 139 | if (netdev->flags & IFF_PROMISC) { |
| 140 | /* do not expect to see traffic of other PLCs */ |
| 141 | filter |= PACKET_TYPE_PROMISCUOUS; |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 142 | netdev_info(dev->net, "promiscuous mode enabled\n"); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 143 | } else if (!netdev_mc_empty(netdev) || |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 144 | (netdev->flags & IFF_ALLMULTI)) { |
| 145 | filter |= PACKET_TYPE_ALL_MULTICAST; |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 146 | netdev_dbg(dev->net, "receive all multicast enabled\n"); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 147 | } else { |
| 148 | /* ~PROMISCUOUS, ~MULTICAST */ |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 149 | netdev_dbg(dev->net, "receive own packets only\n"); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 153 | if (!urb) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 154 | netdev_warn(dev->net, "Error allocating URB\n"); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | req = kmalloc(sizeof(*req), GFP_ATOMIC); |
| 159 | if (!req) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 160 | netdev_warn(dev->net, "Error allocating control msg\n"); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 165 | req->bRequest = SET_ETHERNET_PACKET_FILTER; |
| 166 | req->wValue = cpu_to_le16(filter); |
| 167 | req->wIndex = 0; |
| 168 | req->wLength = 0; |
| 169 | |
| 170 | usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0), |
| 171 | (void *)req, NULL, 0, |
| 172 | int51x1_async_cmd_callback, |
| 173 | (void *)req); |
| 174 | |
| 175 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 176 | if (status < 0) { |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 177 | netdev_warn(dev->net, "Error submitting control msg, sts=%d\n", |
| 178 | status); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 179 | goto out1; |
| 180 | } |
| 181 | return; |
| 182 | out1: |
| 183 | kfree(req); |
| 184 | out: |
| 185 | usb_free_urb(urb); |
| 186 | } |
| 187 | |
| 188 | static const struct net_device_ops int51x1_netdev_ops = { |
| 189 | .ndo_open = usbnet_open, |
| 190 | .ndo_stop = usbnet_stop, |
| 191 | .ndo_start_xmit = usbnet_start_xmit, |
| 192 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 193 | .ndo_change_mtu = usbnet_change_mtu, |
| 194 | .ndo_set_mac_address = eth_mac_addr, |
| 195 | .ndo_validate_addr = eth_validate_addr, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 196 | .ndo_set_rx_mode = int51x1_set_multicast, |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | static int int51x1_bind(struct usbnet *dev, struct usb_interface *intf) |
| 200 | { |
| 201 | int status = usbnet_get_ethernet_addr(dev, 3); |
| 202 | |
| 203 | if (status) |
| 204 | return status; |
| 205 | |
| 206 | dev->net->hard_header_len += INT51X1_HEADER_SIZE; |
| 207 | dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; |
| 208 | dev->net->netdev_ops = &int51x1_netdev_ops; |
| 209 | |
| 210 | return usbnet_get_endpoints(dev, intf); |
| 211 | } |
| 212 | |
| 213 | static const struct driver_info int51x1_info = { |
| 214 | .description = "Intellon usb powerline adapter", |
| 215 | .bind = int51x1_bind, |
| 216 | .rx_fixup = int51x1_rx_fixup, |
| 217 | .tx_fixup = int51x1_tx_fixup, |
| 218 | .in = 1, |
| 219 | .out = 2, |
| 220 | .flags = FLAG_ETHER, |
| 221 | }; |
| 222 | |
| 223 | static const struct usb_device_id products[] = { |
| 224 | { |
| 225 | USB_DEVICE(INT51X1_VENDOR_ID, INT51X1_PRODUCT_ID), |
| 226 | .driver_info = (unsigned long) &int51x1_info, |
| 227 | }, |
| 228 | {}, |
| 229 | }; |
| 230 | MODULE_DEVICE_TABLE(usb, products); |
| 231 | |
| 232 | static struct usb_driver int51x1_driver = { |
| 233 | .name = "int51x1", |
| 234 | .id_table = products, |
| 235 | .probe = usbnet_probe, |
| 236 | .disconnect = usbnet_disconnect, |
| 237 | .suspend = usbnet_suspend, |
| 238 | .resume = usbnet_resume, |
| 239 | }; |
| 240 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 241 | module_usb_driver(int51x1_driver); |
Peter Holik | 4510d7c | 2009-04-18 07:24:21 +0000 | [diff] [blame] | 242 | |
| 243 | MODULE_AUTHOR("Peter Holik"); |
| 244 | MODULE_DESCRIPTION("Intellon usb powerline adapter"); |
| 245 | MODULE_LICENSE("GPL"); |