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