2 * Net1080 based USB host-to-host cables
3 * Copyright (C) 2000-2005 by David Brownell
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.
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.
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
20 // #define DEBUG // error path messages, extra info
21 // #define VERBOSE // more; success messages
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>
34 #include <asm/unaligned.h>
38 * Netchip 1080 driver ... http://www.netchip.com
39 * (Sept 2004: End-of-life announcement has been sent.)
40 * Used in (some) LapLink cables
43 #define frame_errors data[1]
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.
50 * - Ethernet header (14 bytes)
52 * - (optional padding byte, if needed so length becomes odd)
55 * This framing is to be avoided for non-NetChip devices.
58 struct 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
64 // all else is optional, and must start with:
65 // __le16 vendorId; // from usb-if
69 #define PAD_BYTE ((unsigned char)0xAC)
75 // packets may use FLAG_FRAMING_NC and optional pad
76 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
77 + sizeof (struct ethhdr) \
80 + sizeof (struct nc_trailer))
82 #define MIN_FRAMED FRAMED_SIZE(0)
84 /* packets _could_ be up to 64KB... */
85 #define NC_MAX_PACKET 32767
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.
94 #define NC_READ_TTL_MS ((u8)255) // ms
97 * We ignore most registers and EEPROM contents.
99 #define REG_USBCTL ((u8)0x04)
100 #define REG_TTL ((u8)0x10)
101 #define REG_STATUS ((u8)0x11)
104 * Vendor specific requests to read/write data
106 #define REQUEST_REGISTER ((u8)0x10)
107 #define REQUEST_EEPROM ((u8)0x11)
110 nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
112 int status = usb_control_msg(dev->udev,
113 usb_rcvctrlpipe(dev->udev, 0),
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
117 retval_ptr, sizeof *retval_ptr,
118 USB_CTRL_GET_TIMEOUT);
122 le16_to_cpus(retval_ptr);
127 nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
129 return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
132 // no retval ... can become async, usable in_interrupt()
134 nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
136 usb_control_msg(dev->udev,
137 usb_sndctrlpipe(dev->udev, 0),
139 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
141 NULL, 0, // data is in setup packet
142 USB_CTRL_SET_TIMEOUT);
146 nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
148 nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
153 static void nc_dump_registers(struct usbnet *dev)
156 u16 *vp = kmalloc(sizeof (u16));
161 netdev_dbg(dev->net, "registers:\n");
162 for (reg = 0; reg < 0x20; reg++) {
165 // reading some registers is trouble
166 if (reg >= 0x08 && reg <= 0xf)
168 if (reg >= 0x12 && reg <= 0x1e)
171 retval = nc_register_read(dev, reg, vp);
173 netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
176 netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
183 /*-------------------------------------------------------------------------*/
189 #define USBCTL_WRITABLE_MASK 0x1f0f
190 // bits 15-13 reserved, r/o
191 #define USBCTL_ENABLE_LANG (1 << 12)
192 #define USBCTL_ENABLE_MFGR (1 << 11)
193 #define USBCTL_ENABLE_PROD (1 << 10)
194 #define USBCTL_ENABLE_SERIAL (1 << 9)
195 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
196 // bits 7-4 reserved, r/o
197 #define USBCTL_FLUSH_OTHER (1 << 3)
198 #define USBCTL_FLUSH_THIS (1 << 2)
199 #define USBCTL_DISCONN_OTHER (1 << 1)
200 #define USBCTL_DISCONN_THIS (1 << 0)
202 static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
204 netif_dbg(dev, link, dev->net,
205 "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
206 dev->udev->bus->bus_name, dev->udev->devpath,
208 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
209 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
210 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
211 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
212 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
214 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
215 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
217 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
218 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
220 usbctl & ~USBCTL_WRITABLE_MASK);
223 /*-------------------------------------------------------------------------*/
229 #define STATUS_PORT_A (1 << 15)
231 #define STATUS_CONN_OTHER (1 << 14)
232 #define STATUS_SUSPEND_OTHER (1 << 13)
233 #define STATUS_MAILBOX_OTHER (1 << 12)
234 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
236 #define STATUS_CONN_THIS (1 << 6)
237 #define STATUS_SUSPEND_THIS (1 << 5)
238 #define STATUS_MAILBOX_THIS (1 << 4)
239 #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
241 #define STATUS_UNSPEC_MASK 0x0c8c
242 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
245 static inline void nc_dump_status(struct usbnet *dev, u16 status)
247 netif_dbg(dev, link, dev->net,
248 "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
249 dev->udev->bus->bus_name, dev->udev->devpath,
252 // XXX the packet counts don't seem right
253 // (1 at reset, not 0); maybe UNSPEC too
255 (status & STATUS_PORT_A) ? 'A' : 'B',
256 STATUS_PACKETS_THIS(status),
257 (status & STATUS_CONN_THIS) ? " CON" : "",
258 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
259 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
261 STATUS_PACKETS_OTHER(status),
262 (status & STATUS_CONN_OTHER) ? " CON" : "",
263 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
264 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
266 status & STATUS_UNSPEC_MASK);
269 /*-------------------------------------------------------------------------*/
275 #define TTL_THIS(ttl) (0x00ff & ttl)
276 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
277 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
279 static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
281 netif_dbg(dev, link, dev->net, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n",
282 dev->udev->bus->bus_name, dev->udev->devpath,
283 ttl, TTL_THIS(ttl), TTL_OTHER(ttl));
286 /*-------------------------------------------------------------------------*/
288 static int net1080_reset(struct usbnet *dev)
290 u16 usbctl, status, ttl;
291 u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL);
297 // nc_dump_registers(dev);
299 if ((retval = nc_register_read(dev, REG_STATUS, vp)) < 0) {
300 netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
301 dev->udev->bus->bus_name, dev->udev->devpath, retval);
305 nc_dump_status(dev, status);
307 if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) {
308 netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
312 nc_dump_usbctl(dev, usbctl);
314 nc_register_write(dev, REG_USBCTL,
315 USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
317 if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) {
318 netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
322 // nc_dump_ttl(dev, ttl);
324 nc_register_write(dev, REG_TTL,
325 MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
326 netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
328 netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
329 (status & STATUS_PORT_A) ? 'A' : 'B',
330 (status & STATUS_CONN_OTHER) ? "" : "dis");
338 static int net1080_check_connect(struct usbnet *dev)
342 u16 *vp = kmalloc(sizeof (u16), GFP_KERNEL);
346 retval = nc_register_read(dev, REG_STATUS, vp);
350 netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
353 if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
358 static void nc_flush_complete(struct urb *urb)
364 static void nc_ensure_sync(struct usbnet *dev)
367 if (dev->frame_errors > 5) {
369 struct usb_ctrlrequest *req;
373 urb = usb_alloc_urb(0, GFP_ATOMIC);
377 req = kmalloc(sizeof *req, GFP_ATOMIC);
383 req->bRequestType = USB_DIR_OUT
386 req->bRequest = REQUEST_REGISTER;
387 req->wValue = cpu_to_le16(USBCTL_FLUSH_THIS
388 | USBCTL_FLUSH_OTHER);
389 req->wIndex = cpu_to_le16(REG_USBCTL);
390 req->wLength = cpu_to_le16(0);
392 /* queue an async control request, we don't need
393 * to do anything when it finishes except clean up.
395 usb_fill_control_urb(urb, dev->udev,
396 usb_sndctrlpipe(dev->udev, 0),
397 (unsigned char *) req,
399 nc_flush_complete, req);
400 status = usb_submit_urb(urb, GFP_ATOMIC);
407 netif_dbg(dev, rx_err, dev->net,
408 "flush net1080; too many framing errors\n");
409 dev->frame_errors = 0;
413 static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
415 struct nc_header *header;
416 struct nc_trailer *trailer;
417 u16 hdr_len, packet_len;
419 if (!(skb->len & 0x01)) {
420 netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
421 skb->len, dev->net->hard_header_len, dev->hard_mtu,
423 dev->net->stats.rx_frame_errors++;
428 header = (struct nc_header *) skb->data;
429 hdr_len = le16_to_cpup(&header->hdr_len);
430 packet_len = le16_to_cpup(&header->packet_len);
431 if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
432 dev->net->stats.rx_frame_errors++;
433 netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
436 } else if (hdr_len < MIN_HEADER) {
437 dev->net->stats.rx_frame_errors++;
438 netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
441 } else if (hdr_len > MIN_HEADER) {
442 // out of band data for us?
443 netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
445 // switch (vendor/product ids) { ... }
447 skb_pull(skb, hdr_len);
449 trailer = (struct nc_trailer *)
450 (skb->data + skb->len - sizeof *trailer);
451 skb_trim(skb, skb->len - sizeof *trailer);
453 if ((packet_len & 0x01) == 0) {
454 if (skb->data [packet_len] != PAD_BYTE) {
455 dev->net->stats.rx_frame_errors++;
456 netdev_dbg(dev->net, "bad pad\n");
459 skb_trim(skb, skb->len - 1);
461 if (skb->len != packet_len) {
462 dev->net->stats.rx_frame_errors++;
463 netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
464 skb->len, packet_len);
468 if (header->packet_id != get_unaligned(&trailer->packet_id)) {
469 dev->net->stats.rx_fifo_errors++;
470 netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
471 le16_to_cpu(header->packet_id),
472 le16_to_cpu(trailer->packet_id));
476 netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
477 header->packet_len, header->packet_id);
479 dev->frame_errors = 0;
483 static struct sk_buff *
484 net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
486 struct sk_buff *skb2;
487 struct nc_header *header = NULL;
488 struct nc_trailer *trailer = NULL;
489 int padlen = sizeof (struct nc_trailer);
492 if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
494 if (!skb_cloned(skb)) {
495 int headroom = skb_headroom(skb);
496 int tailroom = skb_tailroom(skb);
498 if (padlen <= tailroom &&
499 sizeof(struct nc_header) <= headroom)
500 /* There's enough head and tail room */
503 if ((sizeof (struct nc_header) + padlen) <
504 (headroom + tailroom)) {
505 /* There's enough total room, so just readjust */
506 skb->data = memmove(skb->head
507 + sizeof (struct nc_header),
508 skb->data, skb->len);
509 skb_set_tail_pointer(skb, len);
514 /* Create a new skb to use with the correct size */
515 skb2 = skb_copy_expand(skb,
516 sizeof (struct nc_header),
519 dev_kfree_skb_any(skb);
526 header = (struct nc_header *) skb_push(skb, sizeof *header);
527 header->hdr_len = cpu_to_le16(sizeof (*header));
528 header->packet_len = cpu_to_le16(len);
529 header->packet_id = cpu_to_le16((u16)dev->xid++);
531 /* maybe pad; then trailer */
532 if (!((skb->len + sizeof *trailer) & 0x01))
533 *skb_put(skb, 1) = PAD_BYTE;
534 trailer = (struct nc_trailer *) skb_put(skb, sizeof *trailer);
535 put_unaligned(header->packet_id, &trailer->packet_id);
537 netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
538 header->hdr_len, header->packet_len,
544 static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
546 unsigned extra = sizeof (struct nc_header)
548 + sizeof (struct nc_trailer);
550 dev->net->hard_header_len += extra;
551 dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
552 dev->hard_mtu = NC_MAX_PACKET;
553 return usbnet_get_endpoints (dev, intf);
556 static const struct driver_info net1080_info = {
557 .description = "NetChip TurboCONNECT",
558 .flags = FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
559 .bind = net1080_bind,
560 .reset = net1080_reset,
561 .check_connect = net1080_check_connect,
562 .rx_fixup = net1080_rx_fixup,
563 .tx_fixup = net1080_tx_fixup,
566 static const struct usb_device_id products [] = {
568 USB_DEVICE(0x0525, 0x1080), // NetChip ref design
569 .driver_info = (unsigned long) &net1080_info,
571 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
572 .driver_info = (unsigned long) &net1080_info,
576 MODULE_DEVICE_TABLE(usb, products);
578 static struct usb_driver net1080_driver = {
580 .id_table = products,
581 .probe = usbnet_probe,
582 .disconnect = usbnet_disconnect,
583 .suspend = usbnet_suspend,
584 .resume = usbnet_resume,
585 .disable_hub_initiated_lpm = 1,
588 module_usb_driver(net1080_driver);
590 MODULE_AUTHOR("David Brownell");
591 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
592 MODULE_LICENSE("GPL");