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 32#include <asm/unaligned.h> 33 34#include "usbnet.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} __attribute__((__packed__)); 68 69#define PAD_BYTE ((unsigned char)0xAC) 70 71struct nc_trailer { 72 __le16 packet_id; 73} __attribute__((__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 if (!netif_msg_link(dev)) 176 return; 177 devdbg(dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;" 178 " this%s%s;" 179 " other%s%s; r/o 0x%x", 180 dev->udev->bus->bus_name, dev->udev->devpath, 181 usbctl, 182 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "", 183 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "", 184 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "", 185 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "", 186 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "", 187 188 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "", 189 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "", 190 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "", 191 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "", 192 usbctl & ~USBCTL_WRITABLE_MASK 193 ); 194} 195 196/*-------------------------------------------------------------------------*/ 197 198/* 199 * Status register 200 */ 201 202#define STATUS_PORT_A (1 << 15) 203 204#define STATUS_CONN_OTHER (1 << 14) 205#define STATUS_SUSPEND_OTHER (1 << 13) 206#define STATUS_MAILBOX_OTHER (1 << 12) 207#define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03) 208 209#define STATUS_CONN_THIS (1 << 6) 210#define STATUS_SUSPEND_THIS (1 << 5) 211#define STATUS_MAILBOX_THIS (1 << 4) 212#define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03) 213 214#define STATUS_UNSPEC_MASK 0x0c8c 215#define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK)) 216 217 218static inline void nc_dump_status(struct usbnet *dev, u16 status) 219{ 220 if (!netif_msg_link(dev)) 221 return; 222 devdbg(dev, "net1080 %s-%s status 0x%x:" 223 " this (%c) PKT=%d%s%s%s;" 224 " other PKT=%d%s%s%s; unspec 0x%x", 225 dev->udev->bus->bus_name, dev->udev->devpath, 226 status, 227 228 // (1 at reset, not 0); maybe UNSPEC too 229 230 (status & STATUS_PORT_A) ? 'A' : 'B', 231 STATUS_PACKETS_THIS(status), 232 (status & STATUS_CONN_THIS) ? " CON" : "", 233 (status & STATUS_SUSPEND_THIS) ? " SUS" : "", 234 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "", 235 236 STATUS_PACKETS_OTHER(status), 237 (status & STATUS_CONN_OTHER) ? " CON" : "", 238 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "", 239 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "", 240 241 status & STATUS_UNSPEC_MASK 242 ); 243} 244 245/*-------------------------------------------------------------------------*/ 246 247/* 248 * TTL register 249 */ 250 251#define TTL_THIS(ttl) (0x00ff & ttl) 252#define TTL_OTHER(ttl) (0x00ff & (ttl >> 8)) 253#define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this)))) 254 255static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl) 256{ 257 if (netif_msg_link(dev)) 258 devdbg(dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d", 259 dev->udev->bus->bus_name, dev->udev->devpath, 260 ttl, TTL_THIS(ttl), TTL_OTHER(ttl)); 261} 262 263/*-------------------------------------------------------------------------*/ 264 265static int net1080_reset(struct usbnet *dev) 266{ 267 u16 usbctl, status, ttl; 268 u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL); 269 int retval; 270 271 if (!vp) 272 return -ENOMEM; 273 274 // nc_dump_registers(dev); 275 276 if ((retval = nc_register_read(dev, REG_STATUS, vp)) < 0) { 277 dbg("can't read %s-%s status: %d", 278 dev->udev->bus->bus_name, dev->udev->devpath, retval); 279 goto done; 280 } 281 status = *vp; 282 nc_dump_status(dev, status); 283 284 if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) { 285 dbg("can't read USBCTL, %d", retval); 286 goto done; 287 } 288 usbctl = *vp; 289 nc_dump_usbctl(dev, usbctl); 290 291 nc_register_write(dev, REG_USBCTL, 292 USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER); 293 294 if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) { 295 dbg("can't read TTL, %d", retval); 296 goto done; 297 } 298 ttl = *vp; 299 // nc_dump_ttl(dev, ttl); 300 301 nc_register_write(dev, REG_TTL, 302 MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) ); 303 dbg("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS); 304 305 if (netif_msg_link(dev)) 306 devinfo(dev, "port %c, peer %sconnected", 307 (status & STATUS_PORT_A) ? 'A' : 'B', 308 (status & STATUS_CONN_OTHER) ? "" : "dis" 309 ); 310 retval = 0; 311 312done: 313 kfree(vp); 314 return retval; 315} 316 317static int net1080_check_connect(struct usbnet *dev) 318{ 319 int retval; 320 u16 status; 321 u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL); 322 323 if (!vp) 324 return -ENOMEM; 325 retval = nc_register_read(dev, REG_STATUS, vp); 326 status = *vp; 327 kfree(vp); 328 if (retval != 0) { 329 dbg("%s net1080_check_conn read - %d", dev->net->name, retval); 330 return retval; 331 } 332 if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER) 333 return -ENOLINK; 334 return 0; 335} 336 337static void nc_flush_complete(struct urb *urb) 338{ 339 kfree(urb->context); 340 usb_free_urb(urb); 341} 342 343static void nc_ensure_sync(struct usbnet *dev) 344{ 345 dev->frame_errors++; 346 if (dev->frame_errors > 5) { 347 struct urb *urb; 348 struct usb_ctrlrequest *req; 349 int status; 350 351 /* Send a flush */ 352 urb = usb_alloc_urb(0, GFP_ATOMIC); 353 if (!urb) 354 return; 355 356 req = kmalloc(sizeof *req, GFP_ATOMIC); 357 if (!req) { 358 usb_free_urb(urb); 359 return; 360 } 361 362 req->bRequestType = USB_DIR_OUT 363 | USB_TYPE_VENDOR 364 | USB_RECIP_DEVICE; 365 req->bRequest = REQUEST_REGISTER; 366 req->wValue = cpu_to_le16(USBCTL_FLUSH_THIS 367 | USBCTL_FLUSH_OTHER); 368 req->wIndex = cpu_to_le16(REG_USBCTL); 369 req->wLength = cpu_to_le16(0); 370 371 /* queue an async control request, we don't need 372 * to do anything when it finishes except clean up. 373 */ 374 usb_fill_control_urb(urb, dev->udev, 375 usb_sndctrlpipe(dev->udev, 0), 376 (unsigned char *) req, 377 NULL, 0, 378 nc_flush_complete, req); 379 status = usb_submit_urb(urb, GFP_ATOMIC); 380 if (status) { 381 kfree(req); 382 usb_free_urb(urb); 383 return; 384 } 385 386 if (netif_msg_rx_err(dev)) 387 devdbg(dev, "flush net1080; too many framing errors"); 388 dev->frame_errors = 0; 389 } 390} 391 392static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 393{ 394 struct nc_header *header; 395 struct nc_trailer *trailer; 396 u16 hdr_len, packet_len; 397 398 if (!(skb->len & 0x01)) { 399#ifdef DEBUG 400 struct net_device *net = dev->net; 401 dbg("rx framesize %d range %d..%d mtu %d", skb->len, 402 net->hard_header_len, dev->hard_mtu, net->mtu); 403#endif 404 dev->stats.rx_frame_errors++; 405 nc_ensure_sync(dev); 406 return 0; 407 } 408 409 header = (struct nc_header *) skb->data; 410 hdr_len = le16_to_cpup(&header->hdr_len); 411 packet_len = le16_to_cpup(&header->packet_len); 412 if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) { 413 dev->stats.rx_frame_errors++; 414 dbg("packet too big, %d", packet_len); 415 nc_ensure_sync(dev); 416 return 0; 417 } else if (hdr_len < MIN_HEADER) { 418 dev->stats.rx_frame_errors++; 419 dbg("header too short, %d", hdr_len); 420 nc_ensure_sync(dev); 421 return 0; 422 } else if (hdr_len > MIN_HEADER) { 423 // out of band data for us? 424 dbg("header OOB, %d bytes", hdr_len - MIN_HEADER); 425 nc_ensure_sync(dev); 426 // switch (vendor/product ids) { ... } 427 } 428 skb_pull(skb, hdr_len); 429 430 trailer = (struct nc_trailer *) 431 (skb->data + skb->len - sizeof *trailer); 432 skb_trim(skb, skb->len - sizeof *trailer); 433 434 if ((packet_len & 0x01) == 0) { 435 if (skb->data [packet_len] != PAD_BYTE) { 436 dev->stats.rx_frame_errors++; 437 dbg("bad pad"); 438 return 0; 439 } 440 skb_trim(skb, skb->len - 1); 441 } 442 if (skb->len != packet_len) { 443 dev->stats.rx_frame_errors++; 444 dbg("bad packet len %d (expected %d)", 445 skb->len, packet_len); 446 nc_ensure_sync(dev); 447 return 0; 448 } 449 if (header->packet_id != get_unaligned(&trailer->packet_id)) { 450 dev->stats.rx_fifo_errors++; 451 dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x", 452 le16_to_cpu(header->packet_id), 453 le16_to_cpu(trailer->packet_id)); 454 return 0; 455 } 456 dev->frame_errors = 0; 457 return 1; 458} 459 460static struct sk_buff * 461net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) 462{ 463 struct sk_buff *skb2; 464 struct nc_header *header = NULL; 465 struct nc_trailer *trailer = NULL; 466 int padlen = sizeof (struct nc_trailer); 467 int len = skb->len; 468 469 if (!((len + padlen + sizeof (struct nc_header)) & 0x01)) 470 padlen++; 471 if (!skb_cloned(skb)) { 472 int headroom = skb_headroom(skb); 473 int tailroom = skb_tailroom(skb); 474 475 if (padlen <= tailroom && 476 sizeof(struct nc_header) <= headroom) 477 /* There's enough head and tail room */ 478 goto encapsulate; 479 480 if ((sizeof (struct nc_header) + padlen) < 481 (headroom + tailroom)) { 482 /* There's enough total room, so just readjust */ 483 skb->data = memmove(skb->head 484 + sizeof (struct nc_header), 485 skb->data, skb->len); 486 skb_set_tail_pointer(skb, len); 487 goto encapsulate; 488 } 489 } 490 491 /* Create a new skb to use with the correct size */ 492 skb2 = skb_copy_expand(skb, 493 sizeof (struct nc_header), 494 padlen, 495 flags); 496 dev_kfree_skb_any(skb); 497 if (!skb2) 498 return skb2; 499 skb = skb2; 500 501encapsulate: 502 /* header first */ 503 header = (struct nc_header *) skb_push(skb, sizeof *header); 504 header->hdr_len = cpu_to_le16(sizeof (*header)); 505 header->packet_len = cpu_to_le16(len); 506 header->packet_id = cpu_to_le16((u16)dev->xid++); 507 508 /* maybe pad; then trailer */ 509 if (!((skb->len + sizeof *trailer) & 0x01)) 510 *skb_put(skb, 1) = PAD_BYTE; 511 trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer); 512 put_unaligned(header->packet_id, &trailer->packet_id); 513 return skb; 514} 515 516static int net1080_bind(struct usbnet *dev, struct usb_interface *intf) 517{ 518 unsigned extra = sizeof (struct nc_header) 519 + 1 520 + sizeof (struct nc_trailer); 521 522 dev->net->hard_header_len += extra; 523 dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu; 524 dev->hard_mtu = NC_MAX_PACKET; 525 return usbnet_get_endpoints (dev, intf); 526} 527 528static const struct driver_info net1080_info = { 529 .description = "NetChip TurboCONNECT", 530 .flags = FLAG_FRAMING_NC, 531 .bind = net1080_bind, 532 .reset = net1080_reset, 533 .check_connect = net1080_check_connect, 534 .rx_fixup = net1080_rx_fixup, 535 .tx_fixup = net1080_tx_fixup, 536}; 537 538static const struct usb_device_id products [] = { 539{ 540 USB_DEVICE(0x0525, 0x1080), // NetChip ref design 541 .driver_info = (unsigned long) &net1080_info, 542}, { 543 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold 544 .driver_info = (unsigned long) &net1080_info, 545}, 546 { }, // END 547}; 548MODULE_DEVICE_TABLE(usb, products); 549 550static struct usb_driver net1080_driver = { 551 .name = "net1080", 552 .id_table = products, 553 .probe = usbnet_probe, 554 .disconnect = usbnet_disconnect, 555 .suspend = usbnet_suspend, 556 .resume = usbnet_resume, 557}; 558 559static int __init net1080_init(void) 560{ 561 return usb_register(&net1080_driver); 562} 563module_init(net1080_init); 564 565static void __exit net1080_exit(void) 566{ 567 usb_deregister(&net1080_driver); 568} 569module_exit(net1080_exit); 570 571MODULE_AUTHOR("David Brownell"); 572MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links"); 573MODULE_LICENSE("GPL"); 574