Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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