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