David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Net1080 based USB host-to-host cables |
| 3 | * Copyright (C) 2000-2005 by David Brownell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 9cb0007 | 2013-12-06 06:28:46 -0800 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | // #define DEBUG // error path messages, extra info |
| 20 | // #define VERBOSE // more; success messages |
| 21 | |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/etherdevice.h> |
| 25 | #include <linux/ethtool.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/mii.h> |
| 28 | #include <linux/usb.h> |
Jussi Kivilinna | 3692e94 | 2008-01-26 00:51:45 +0200 | [diff] [blame] | 29 | #include <linux/usb/usbnet.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/unaligned.h> |
| 33 | |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Netchip 1080 driver ... http://www.netchip.com |
| 37 | * (Sept 2004: End-of-life announcement has been sent.) |
| 38 | * Used in (some) LapLink cables |
| 39 | */ |
| 40 | |
| 41 | #define frame_errors data[1] |
| 42 | |
| 43 | /* |
| 44 | * NetChip framing of ethernet packets, supporting additional error |
| 45 | * checks for links that may drop bulk packets from inside messages. |
| 46 | * Odd USB length == always short read for last usb packet. |
| 47 | * - nc_header |
| 48 | * - Ethernet header (14 bytes) |
| 49 | * - payload |
| 50 | * - (optional padding byte, if needed so length becomes odd) |
| 51 | * - nc_trailer |
| 52 | * |
| 53 | * This framing is to be avoided for non-NetChip devices. |
| 54 | */ |
| 55 | |
| 56 | struct nc_header { // packed: |
| 57 | __le16 hdr_len; // sizeof nc_header (LE, all) |
| 58 | __le16 packet_len; // payload size (including ethhdr) |
| 59 | __le16 packet_id; // detects dropped packets |
| 60 | #define MIN_HEADER 6 |
| 61 | |
| 62 | // all else is optional, and must start with: |
| 63 | // __le16 vendorId; // from usb-if |
| 64 | // __le16 productId; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 65 | } __packed; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 66 | |
| 67 | #define PAD_BYTE ((unsigned char)0xAC) |
| 68 | |
| 69 | struct nc_trailer { |
| 70 | __le16 packet_id; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 71 | } __packed; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 72 | |
| 73 | // packets may use FLAG_FRAMING_NC and optional pad |
| 74 | #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \ |
| 75 | + sizeof (struct ethhdr) \ |
| 76 | + (mtu) \ |
| 77 | + 1 \ |
| 78 | + sizeof (struct nc_trailer)) |
| 79 | |
| 80 | #define MIN_FRAMED FRAMED_SIZE(0) |
| 81 | |
| 82 | /* packets _could_ be up to 64KB... */ |
| 83 | #define NC_MAX_PACKET 32767 |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * Zero means no timeout; else, how long a 64 byte bulk packet may be queued |
| 88 | * before the hardware drops it. If that's done, the driver will need to |
| 89 | * frame network packets to guard against the dropped USB packets. The win32 |
| 90 | * driver sets this for both sides of the link. |
| 91 | */ |
| 92 | #define NC_READ_TTL_MS ((u8)255) // ms |
| 93 | |
| 94 | /* |
| 95 | * We ignore most registers and EEPROM contents. |
| 96 | */ |
| 97 | #define REG_USBCTL ((u8)0x04) |
| 98 | #define REG_TTL ((u8)0x10) |
| 99 | #define REG_STATUS ((u8)0x11) |
| 100 | |
| 101 | /* |
| 102 | * Vendor specific requests to read/write data |
| 103 | */ |
| 104 | #define REQUEST_REGISTER ((u8)0x10) |
| 105 | #define REQUEST_EEPROM ((u8)0x11) |
| 106 | |
| 107 | static int |
| 108 | nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr) |
| 109 | { |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 110 | int status = usbnet_read_cmd(dev, req, |
| 111 | USB_DIR_IN | USB_TYPE_VENDOR | |
| 112 | USB_RECIP_DEVICE, |
| 113 | 0, regnum, retval_ptr, |
| 114 | sizeof *retval_ptr); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 115 | if (status > 0) |
| 116 | status = 0; |
| 117 | if (!status) |
| 118 | le16_to_cpus(retval_ptr); |
| 119 | return status; |
| 120 | } |
| 121 | |
| 122 | static inline int |
| 123 | nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr) |
| 124 | { |
| 125 | return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr); |
| 126 | } |
| 127 | |
| 128 | // no retval ... can become async, usable in_interrupt() |
| 129 | static void |
| 130 | nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value) |
| 131 | { |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 132 | usbnet_write_cmd(dev, req, |
| 133 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 134 | value, regnum, NULL, 0); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static inline void |
| 138 | nc_register_write(struct usbnet *dev, u8 regnum, u16 value) |
| 139 | { |
| 140 | nc_vendor_write(dev, REQUEST_REGISTER, regnum, value); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | #if 0 |
| 145 | static void nc_dump_registers(struct usbnet *dev) |
| 146 | { |
| 147 | u8 reg; |
| 148 | u16 *vp = kmalloc(sizeof (u16)); |
| 149 | |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 150 | if (!vp) |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 151 | return; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 152 | |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 153 | netdev_dbg(dev->net, "registers:\n"); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 154 | for (reg = 0; reg < 0x20; reg++) { |
| 155 | int retval; |
| 156 | |
| 157 | // reading some registers is trouble |
| 158 | if (reg >= 0x08 && reg <= 0xf) |
| 159 | continue; |
| 160 | if (reg >= 0x12 && reg <= 0x1e) |
| 161 | continue; |
| 162 | |
| 163 | retval = nc_register_read(dev, reg, vp); |
| 164 | if (retval < 0) |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 165 | netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n", |
| 166 | reg, retval); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 167 | else |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 168 | netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 169 | } |
| 170 | kfree(vp); |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | |
| 175 | /*-------------------------------------------------------------------------*/ |
| 176 | |
| 177 | /* |
| 178 | * Control register |
| 179 | */ |
| 180 | |
| 181 | #define USBCTL_WRITABLE_MASK 0x1f0f |
| 182 | // bits 15-13 reserved, r/o |
| 183 | #define USBCTL_ENABLE_LANG (1 << 12) |
| 184 | #define USBCTL_ENABLE_MFGR (1 << 11) |
| 185 | #define USBCTL_ENABLE_PROD (1 << 10) |
| 186 | #define USBCTL_ENABLE_SERIAL (1 << 9) |
| 187 | #define USBCTL_ENABLE_DEFAULTS (1 << 8) |
| 188 | // bits 7-4 reserved, r/o |
| 189 | #define USBCTL_FLUSH_OTHER (1 << 3) |
| 190 | #define USBCTL_FLUSH_THIS (1 << 2) |
| 191 | #define USBCTL_DISCONN_OTHER (1 << 1) |
| 192 | #define USBCTL_DISCONN_THIS (1 << 0) |
| 193 | |
| 194 | static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl) |
| 195 | { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 196 | netif_dbg(dev, link, dev->net, |
| 197 | "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n", |
| 198 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 199 | usbctl, |
| 200 | (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "", |
| 201 | (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "", |
| 202 | (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "", |
| 203 | (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "", |
| 204 | (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "", |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 205 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 206 | (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "", |
| 207 | (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "", |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 208 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 209 | (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "", |
| 210 | (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "", |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 211 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 212 | usbctl & ~USBCTL_WRITABLE_MASK); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /*-------------------------------------------------------------------------*/ |
| 216 | |
| 217 | /* |
| 218 | * Status register |
| 219 | */ |
| 220 | |
| 221 | #define STATUS_PORT_A (1 << 15) |
| 222 | |
| 223 | #define STATUS_CONN_OTHER (1 << 14) |
| 224 | #define STATUS_SUSPEND_OTHER (1 << 13) |
| 225 | #define STATUS_MAILBOX_OTHER (1 << 12) |
Jean Delvare | b4ee4a2 | 2006-11-09 22:02:37 +0100 | [diff] [blame] | 226 | #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03) |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 227 | |
| 228 | #define STATUS_CONN_THIS (1 << 6) |
| 229 | #define STATUS_SUSPEND_THIS (1 << 5) |
| 230 | #define STATUS_MAILBOX_THIS (1 << 4) |
Jean Delvare | b4ee4a2 | 2006-11-09 22:02:37 +0100 | [diff] [blame] | 231 | #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03) |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 232 | |
| 233 | #define STATUS_UNSPEC_MASK 0x0c8c |
| 234 | #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK)) |
| 235 | |
| 236 | |
| 237 | static inline void nc_dump_status(struct usbnet *dev, u16 status) |
| 238 | { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 239 | netif_dbg(dev, link, dev->net, |
| 240 | "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n", |
| 241 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 242 | status, |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 243 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 244 | // XXX the packet counts don't seem right |
| 245 | // (1 at reset, not 0); maybe UNSPEC too |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 246 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 247 | (status & STATUS_PORT_A) ? 'A' : 'B', |
| 248 | STATUS_PACKETS_THIS(status), |
| 249 | (status & STATUS_CONN_THIS) ? " CON" : "", |
| 250 | (status & STATUS_SUSPEND_THIS) ? " SUS" : "", |
| 251 | (status & STATUS_MAILBOX_THIS) ? " MBOX" : "", |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 252 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 253 | STATUS_PACKETS_OTHER(status), |
| 254 | (status & STATUS_CONN_OTHER) ? " CON" : "", |
| 255 | (status & STATUS_SUSPEND_OTHER) ? " SUS" : "", |
| 256 | (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "", |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 257 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 258 | status & STATUS_UNSPEC_MASK); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /*-------------------------------------------------------------------------*/ |
| 262 | |
| 263 | /* |
| 264 | * TTL register |
| 265 | */ |
| 266 | |
| 267 | #define TTL_THIS(ttl) (0x00ff & ttl) |
| 268 | #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8)) |
| 269 | #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this)))) |
| 270 | |
| 271 | static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl) |
| 272 | { |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 273 | netif_dbg(dev, link, dev->net, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n", |
| 274 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 275 | ttl, TTL_THIS(ttl), TTL_OTHER(ttl)); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /*-------------------------------------------------------------------------*/ |
| 279 | |
| 280 | static int net1080_reset(struct usbnet *dev) |
| 281 | { |
| 282 | u16 usbctl, status, ttl; |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 283 | u16 vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 284 | int retval; |
| 285 | |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 286 | // nc_dump_registers(dev); |
| 287 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 288 | if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 289 | netdev_dbg(dev->net, "can't read %s-%s status: %d\n", |
| 290 | dev->udev->bus->bus_name, dev->udev->devpath, retval); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 291 | goto done; |
| 292 | } |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 293 | status = vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 294 | nc_dump_status(dev, status); |
| 295 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 296 | if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 297 | netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 298 | goto done; |
| 299 | } |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 300 | usbctl = vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 301 | nc_dump_usbctl(dev, usbctl); |
| 302 | |
| 303 | nc_register_write(dev, REG_USBCTL, |
| 304 | USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER); |
| 305 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 306 | if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 307 | netdev_dbg(dev->net, "can't read TTL, %d\n", retval); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 308 | goto done; |
| 309 | } |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 310 | ttl = vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 311 | // nc_dump_ttl(dev, ttl); |
| 312 | |
| 313 | nc_register_write(dev, REG_TTL, |
| 314 | MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) ); |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 315 | netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 316 | |
Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 317 | netif_info(dev, link, dev->net, "port %c, peer %sconnected\n", |
| 318 | (status & STATUS_PORT_A) ? 'A' : 'B', |
| 319 | (status & STATUS_CONN_OTHER) ? "" : "dis"); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 320 | retval = 0; |
| 321 | |
| 322 | done: |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 323 | return retval; |
| 324 | } |
| 325 | |
| 326 | static int net1080_check_connect(struct usbnet *dev) |
| 327 | { |
| 328 | int retval; |
| 329 | u16 status; |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 330 | u16 vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 331 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 332 | retval = nc_register_read(dev, REG_STATUS, &vp); |
| 333 | status = vp; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 334 | if (retval != 0) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 335 | netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 336 | return retval; |
| 337 | } |
| 338 | if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER) |
| 339 | return -ENOLINK; |
| 340 | return 0; |
| 341 | } |
| 342 | |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 343 | static void nc_ensure_sync(struct usbnet *dev) |
| 344 | { |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 345 | if (++dev->frame_errors <= 5) |
| 346 | return; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 347 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 348 | if (usbnet_write_cmd_async(dev, REQUEST_REGISTER, |
| 349 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 350 | USB_RECIP_DEVICE, |
| 351 | USBCTL_FLUSH_THIS | |
| 352 | USBCTL_FLUSH_OTHER, |
| 353 | REG_USBCTL, NULL, 0)) |
| 354 | return; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 355 | |
Ming Lei | 5ffbe4d | 2012-10-24 19:47:00 +0000 | [diff] [blame] | 356 | netif_dbg(dev, rx_err, dev->net, |
| 357 | "flush net1080; too many framing errors\n"); |
| 358 | dev->frame_errors = 0; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 362 | { |
| 363 | struct nc_header *header; |
| 364 | struct nc_trailer *trailer; |
| 365 | u16 hdr_len, packet_len; |
| 366 | |
Emil Goode | eb85569 | 2014-02-13 17:50:19 +0100 | [diff] [blame] | 367 | /* This check is no longer done by usbnet */ |
| 368 | if (skb->len < dev->net->hard_header_len) |
| 369 | return 0; |
| 370 | |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 371 | if (!(skb->len & 0x01)) { |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 372 | netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n", |
Joe Perches | 127a479 | 2012-09-20 14:10:23 +0000 | [diff] [blame] | 373 | skb->len, dev->net->hard_header_len, dev->hard_mtu, |
| 374 | dev->net->mtu); |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 375 | dev->net->stats.rx_frame_errors++; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 376 | nc_ensure_sync(dev); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | header = (struct nc_header *) skb->data; |
| 381 | hdr_len = le16_to_cpup(&header->hdr_len); |
| 382 | packet_len = le16_to_cpup(&header->packet_len); |
| 383 | if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) { |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 384 | dev->net->stats.rx_frame_errors++; |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 385 | netdev_dbg(dev->net, "packet too big, %d\n", packet_len); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 386 | nc_ensure_sync(dev); |
| 387 | return 0; |
| 388 | } else if (hdr_len < MIN_HEADER) { |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 389 | dev->net->stats.rx_frame_errors++; |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 390 | netdev_dbg(dev->net, "header too short, %d\n", hdr_len); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 391 | nc_ensure_sync(dev); |
| 392 | return 0; |
| 393 | } else if (hdr_len > MIN_HEADER) { |
| 394 | // out of band data for us? |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 395 | netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 396 | nc_ensure_sync(dev); |
| 397 | // switch (vendor/product ids) { ... } |
| 398 | } |
| 399 | skb_pull(skb, hdr_len); |
| 400 | |
| 401 | trailer = (struct nc_trailer *) |
| 402 | (skb->data + skb->len - sizeof *trailer); |
| 403 | skb_trim(skb, skb->len - sizeof *trailer); |
| 404 | |
| 405 | if ((packet_len & 0x01) == 0) { |
| 406 | if (skb->data [packet_len] != PAD_BYTE) { |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 407 | dev->net->stats.rx_frame_errors++; |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 408 | netdev_dbg(dev->net, "bad pad\n"); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | skb_trim(skb, skb->len - 1); |
| 412 | } |
| 413 | if (skb->len != packet_len) { |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 414 | dev->net->stats.rx_frame_errors++; |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 415 | netdev_dbg(dev->net, "bad packet len %d (expected %d)\n", |
| 416 | skb->len, packet_len); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 417 | nc_ensure_sync(dev); |
| 418 | return 0; |
| 419 | } |
| 420 | if (header->packet_id != get_unaligned(&trailer->packet_id)) { |
Herbert Xu | a22d2b3 | 2009-06-29 16:51:40 +0000 | [diff] [blame] | 421 | dev->net->stats.rx_fifo_errors++; |
Greg Kroah-Hartman | 49ae25b | 2012-09-19 09:46:14 +0000 | [diff] [blame] | 422 | netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n", |
| 423 | le16_to_cpu(header->packet_id), |
| 424 | le16_to_cpu(trailer->packet_id)); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | #if 0 |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 428 | netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len, |
| 429 | header->packet_len, header->packet_id); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 430 | #endif |
| 431 | dev->frame_errors = 0; |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static struct sk_buff * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 436 | net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 437 | { |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 438 | struct sk_buff *skb2; |
| 439 | struct nc_header *header = NULL; |
| 440 | struct nc_trailer *trailer = NULL; |
dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 441 | int padlen = sizeof (struct nc_trailer); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 442 | int len = skb->len; |
| 443 | |
dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 444 | if (!((len + padlen + sizeof (struct nc_header)) & 0x01)) |
| 445 | padlen++; |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 446 | if (!skb_cloned(skb)) { |
| 447 | int headroom = skb_headroom(skb); |
| 448 | int tailroom = skb_tailroom(skb); |
| 449 | |
dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 450 | if (padlen <= tailroom && |
| 451 | sizeof(struct nc_header) <= headroom) |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 452 | /* There's enough head and tail room */ |
| 453 | goto encapsulate; |
| 454 | |
dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 455 | if ((sizeof (struct nc_header) + padlen) < |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 456 | (headroom + tailroom)) { |
| 457 | /* There's enough total room, so just readjust */ |
| 458 | skb->data = memmove(skb->head |
| 459 | + sizeof (struct nc_header), |
| 460 | skb->data, skb->len); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 461 | skb_set_tail_pointer(skb, len); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 462 | goto encapsulate; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* Create a new skb to use with the correct size */ |
| 467 | skb2 = skb_copy_expand(skb, |
| 468 | sizeof (struct nc_header), |
dave rientjes | 9bcbcf4 | 2006-07-18 23:23:02 -0700 | [diff] [blame] | 469 | padlen, |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 470 | flags); |
| 471 | dev_kfree_skb_any(skb); |
| 472 | if (!skb2) |
| 473 | return skb2; |
| 474 | skb = skb2; |
| 475 | |
| 476 | encapsulate: |
| 477 | /* header first */ |
| 478 | header = (struct nc_header *) skb_push(skb, sizeof *header); |
| 479 | header->hdr_len = cpu_to_le16(sizeof (*header)); |
| 480 | header->packet_len = cpu_to_le16(len); |
| 481 | header->packet_id = cpu_to_le16((u16)dev->xid++); |
| 482 | |
| 483 | /* maybe pad; then trailer */ |
| 484 | if (!((skb->len + sizeof *trailer) & 0x01)) |
| 485 | *skb_put(skb, 1) = PAD_BYTE; |
| 486 | trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer); |
| 487 | put_unaligned(header->packet_id, &trailer->packet_id); |
| 488 | #if 0 |
Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 489 | netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n", |
| 490 | header->hdr_len, header->packet_len, |
| 491 | header->packet_id); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 492 | #endif |
| 493 | return skb; |
| 494 | } |
| 495 | |
| 496 | static int net1080_bind(struct usbnet *dev, struct usb_interface *intf) |
| 497 | { |
| 498 | unsigned extra = sizeof (struct nc_header) |
| 499 | + 1 |
| 500 | + sizeof (struct nc_trailer); |
| 501 | |
| 502 | dev->net->hard_header_len += extra; |
| 503 | dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu; |
| 504 | dev->hard_mtu = NC_MAX_PACKET; |
| 505 | return usbnet_get_endpoints (dev, intf); |
| 506 | } |
| 507 | |
| 508 | static const struct driver_info net1080_info = { |
| 509 | .description = "NetChip TurboCONNECT", |
Arnd Bergmann | c261344 | 2011-04-01 20:12:02 -0700 | [diff] [blame] | 510 | .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_NC, |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 511 | .bind = net1080_bind, |
| 512 | .reset = net1080_reset, |
| 513 | .check_connect = net1080_check_connect, |
| 514 | .rx_fixup = net1080_rx_fixup, |
| 515 | .tx_fixup = net1080_tx_fixup, |
| 516 | }; |
| 517 | |
| 518 | static const struct usb_device_id products [] = { |
| 519 | { |
| 520 | USB_DEVICE(0x0525, 0x1080), // NetChip ref design |
| 521 | .driver_info = (unsigned long) &net1080_info, |
| 522 | }, { |
| 523 | USB_DEVICE(0x06D0, 0x0622), // Laplink Gold |
| 524 | .driver_info = (unsigned long) &net1080_info, |
| 525 | }, |
| 526 | { }, // END |
| 527 | }; |
| 528 | MODULE_DEVICE_TABLE(usb, products); |
| 529 | |
| 530 | static struct usb_driver net1080_driver = { |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 531 | .name = "net1080", |
| 532 | .id_table = products, |
| 533 | .probe = usbnet_probe, |
| 534 | .disconnect = usbnet_disconnect, |
| 535 | .suspend = usbnet_suspend, |
| 536 | .resume = usbnet_resume, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 537 | .disable_hub_initiated_lpm = 1, |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 538 | }; |
| 539 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 540 | module_usb_driver(net1080_driver); |
David Brownell | 904813c | 2005-08-31 09:53:26 -0700 | [diff] [blame] | 541 | |
| 542 | MODULE_AUTHOR("David Brownell"); |
| 543 | MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links"); |
| 544 | MODULE_LICENSE("GPL"); |