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/config.h>
24 #ifdef CONFIG_USB_DEBUG
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/workqueue.h>
34 #include <linux/mii.h>
35 #include <linux/usb.h>
37 #include <asm/unaligned.h>
43 * Netchip 1080 driver ... http://www.netchip.com
44 * (Sept 2004: End-of-life announcement has been sent.)
45 * Used in (some) LapLink cables
48 #define frame_errors data[1]
51 * NetChip framing of ethernet packets, supporting additional error
52 * checks for links that may drop bulk packets from inside messages.
53 * Odd USB length == always short read for last usb packet.
55 * - Ethernet header (14 bytes)
57 * - (optional padding byte, if needed so length becomes odd)
60 * This framing is to be avoided for non-NetChip devices.
63 struct nc_header
{ // packed:
64 __le16 hdr_len
; // sizeof nc_header (LE, all)
65 __le16 packet_len
; // payload size (including ethhdr)
66 __le16 packet_id
; // detects dropped packets
69 // all else is optional, and must start with:
70 // __le16 vendorId; // from usb-if
72 } __attribute__((__packed__
));
74 #define PAD_BYTE ((unsigned char)0xAC)
78 } __attribute__((__packed__
));
80 // packets may use FLAG_FRAMING_NC and optional pad
81 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
82 + sizeof (struct ethhdr) \
85 + sizeof (struct nc_trailer))
87 #define MIN_FRAMED FRAMED_SIZE(0)
89 /* packets _could_ be up to 64KB... */
90 #define NC_MAX_PACKET 32767
94 * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
95 * before the hardware drops it. If that's done, the driver will need to
96 * frame network packets to guard against the dropped USB packets. The win32
97 * driver sets this for both sides of the link.
99 #define NC_READ_TTL_MS ((u8)255) // ms
102 * We ignore most registers and EEPROM contents.
104 #define REG_USBCTL ((u8)0x04)
105 #define REG_TTL ((u8)0x10)
106 #define REG_STATUS ((u8)0x11)
109 * Vendor specific requests to read/write data
111 #define REQUEST_REGISTER ((u8)0x10)
112 #define REQUEST_EEPROM ((u8)0x11)
115 nc_vendor_read(struct usbnet
*dev
, u8 req
, u8 regnum
, u16
*retval_ptr
)
117 int status
= usb_control_msg(dev
->udev
,
118 usb_rcvctrlpipe(dev
->udev
, 0),
120 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
122 retval_ptr
, sizeof *retval_ptr
,
123 USB_CTRL_GET_TIMEOUT
);
127 le16_to_cpus(retval_ptr
);
132 nc_register_read(struct usbnet
*dev
, u8 regnum
, u16
*retval_ptr
)
134 return nc_vendor_read(dev
, REQUEST_REGISTER
, regnum
, retval_ptr
);
137 // no retval ... can become async, usable in_interrupt()
139 nc_vendor_write(struct usbnet
*dev
, u8 req
, u8 regnum
, u16 value
)
141 usb_control_msg(dev
->udev
,
142 usb_sndctrlpipe(dev
->udev
, 0),
144 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
146 NULL
, 0, // data is in setup packet
147 USB_CTRL_SET_TIMEOUT
);
151 nc_register_write(struct usbnet
*dev
, u8 regnum
, u16 value
)
153 nc_vendor_write(dev
, REQUEST_REGISTER
, regnum
, value
);
158 static void nc_dump_registers(struct usbnet
*dev
)
161 u16
*vp
= kmalloc(sizeof (u16
));
168 dbg("%s registers:", dev
->net
->name
);
169 for (reg
= 0; reg
< 0x20; reg
++) {
172 // reading some registers is trouble
173 if (reg
>= 0x08 && reg
<= 0xf)
175 if (reg
>= 0x12 && reg
<= 0x1e)
178 retval
= nc_register_read(dev
, reg
, vp
);
180 dbg("%s reg [0x%x] ==> error %d",
181 dev
->net
->name
, reg
, retval
);
183 dbg("%s reg [0x%x] = 0x%x",
184 dev
->net
->name
, reg
, *vp
);
191 /*-------------------------------------------------------------------------*/
197 #define USBCTL_WRITABLE_MASK 0x1f0f
198 // bits 15-13 reserved, r/o
199 #define USBCTL_ENABLE_LANG (1 << 12)
200 #define USBCTL_ENABLE_MFGR (1 << 11)
201 #define USBCTL_ENABLE_PROD (1 << 10)
202 #define USBCTL_ENABLE_SERIAL (1 << 9)
203 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
204 // bits 7-4 reserved, r/o
205 #define USBCTL_FLUSH_OTHER (1 << 3)
206 #define USBCTL_FLUSH_THIS (1 << 2)
207 #define USBCTL_DISCONN_OTHER (1 << 1)
208 #define USBCTL_DISCONN_THIS (1 << 0)
210 static inline void nc_dump_usbctl(struct usbnet
*dev
, u16 usbctl
)
212 if (!netif_msg_link(dev
))
214 devdbg(dev
, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
216 " other%s%s; r/o 0x%x",
217 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
219 (usbctl
& USBCTL_ENABLE_LANG
) ? " lang" : "",
220 (usbctl
& USBCTL_ENABLE_MFGR
) ? " mfgr" : "",
221 (usbctl
& USBCTL_ENABLE_PROD
) ? " prod" : "",
222 (usbctl
& USBCTL_ENABLE_SERIAL
) ? " serial" : "",
223 (usbctl
& USBCTL_ENABLE_DEFAULTS
) ? " defaults" : "",
225 (usbctl
& USBCTL_FLUSH_OTHER
) ? " FLUSH" : "",
226 (usbctl
& USBCTL_DISCONN_OTHER
) ? " DIS" : "",
227 (usbctl
& USBCTL_FLUSH_THIS
) ? " FLUSH" : "",
228 (usbctl
& USBCTL_DISCONN_THIS
) ? " DIS" : "",
229 usbctl
& ~USBCTL_WRITABLE_MASK
233 /*-------------------------------------------------------------------------*/
239 #define STATUS_PORT_A (1 << 15)
241 #define STATUS_CONN_OTHER (1 << 14)
242 #define STATUS_SUSPEND_OTHER (1 << 13)
243 #define STATUS_MAILBOX_OTHER (1 << 12)
244 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
246 #define STATUS_CONN_THIS (1 << 6)
247 #define STATUS_SUSPEND_THIS (1 << 5)
248 #define STATUS_MAILBOX_THIS (1 << 4)
249 #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03)
251 #define STATUS_UNSPEC_MASK 0x0c8c
252 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
255 static inline void nc_dump_status(struct usbnet
*dev
, u16 status
)
257 if (!netif_msg_link(dev
))
259 devdbg(dev
, "net1080 %s-%s status 0x%x:"
260 " this (%c) PKT=%d%s%s%s;"
261 " other PKT=%d%s%s%s; unspec 0x%x",
262 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
265 // XXX the packet counts don't seem right
266 // (1 at reset, not 0); maybe UNSPEC too
268 (status
& STATUS_PORT_A
) ? 'A' : 'B',
269 STATUS_PACKETS_THIS(status
),
270 (status
& STATUS_CONN_THIS
) ? " CON" : "",
271 (status
& STATUS_SUSPEND_THIS
) ? " SUS" : "",
272 (status
& STATUS_MAILBOX_THIS
) ? " MBOX" : "",
274 STATUS_PACKETS_OTHER(status
),
275 (status
& STATUS_CONN_OTHER
) ? " CON" : "",
276 (status
& STATUS_SUSPEND_OTHER
) ? " SUS" : "",
277 (status
& STATUS_MAILBOX_OTHER
) ? " MBOX" : "",
279 status
& STATUS_UNSPEC_MASK
283 /*-------------------------------------------------------------------------*/
289 #define TTL_THIS(ttl) (0x00ff & ttl)
290 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
291 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
293 static inline void nc_dump_ttl(struct usbnet
*dev
, u16 ttl
)
295 if (netif_msg_link(dev
))
296 devdbg(dev
, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
297 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
298 ttl
, TTL_THIS(ttl
), TTL_OTHER(ttl
));
301 /*-------------------------------------------------------------------------*/
303 static int net1080_reset(struct usbnet
*dev
)
305 u16 usbctl
, status
, ttl
;
306 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
312 // nc_dump_registers(dev);
314 if ((retval
= nc_register_read(dev
, REG_STATUS
, vp
)) < 0) {
315 dbg("can't read %s-%s status: %d",
316 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
, retval
);
320 nc_dump_status(dev
, status
);
322 if ((retval
= nc_register_read(dev
, REG_USBCTL
, vp
)) < 0) {
323 dbg("can't read USBCTL, %d", retval
);
327 nc_dump_usbctl(dev
, usbctl
);
329 nc_register_write(dev
, REG_USBCTL
,
330 USBCTL_FLUSH_THIS
| USBCTL_FLUSH_OTHER
);
332 if ((retval
= nc_register_read(dev
, REG_TTL
, vp
)) < 0) {
333 dbg("can't read TTL, %d", retval
);
337 // nc_dump_ttl(dev, ttl);
339 nc_register_write(dev
, REG_TTL
,
340 MK_TTL(NC_READ_TTL_MS
, TTL_OTHER(ttl
)) );
341 dbg("%s: assigned TTL, %d ms", dev
->net
->name
, NC_READ_TTL_MS
);
343 if (netif_msg_link(dev
))
344 devinfo(dev
, "port %c, peer %sconnected",
345 (status
& STATUS_PORT_A
) ? 'A' : 'B',
346 (status
& STATUS_CONN_OTHER
) ? "" : "dis"
355 static int net1080_check_connect(struct usbnet
*dev
)
359 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
363 retval
= nc_register_read(dev
, REG_STATUS
, vp
);
367 dbg("%s net1080_check_conn read - %d", dev
->net
->name
, retval
);
370 if ((status
& STATUS_CONN_OTHER
) != STATUS_CONN_OTHER
)
375 static void nc_flush_complete(struct urb
*urb
, struct pt_regs
*regs
)
381 static void nc_ensure_sync(struct usbnet
*dev
)
384 if (dev
->frame_errors
> 5) {
386 struct usb_ctrlrequest
*req
;
390 urb
= usb_alloc_urb(0, SLAB_ATOMIC
);
394 req
= kmalloc(sizeof *req
, GFP_ATOMIC
);
400 req
->bRequestType
= USB_DIR_OUT
403 req
->bRequest
= REQUEST_REGISTER
;
404 req
->wValue
= cpu_to_le16(USBCTL_FLUSH_THIS
405 | USBCTL_FLUSH_OTHER
);
406 req
->wIndex
= cpu_to_le16(REG_USBCTL
);
407 req
->wLength
= cpu_to_le16(0);
409 /* queue an async control request, we don't need
410 * to do anything when it finishes except clean up.
412 usb_fill_control_urb(urb
, dev
->udev
,
413 usb_sndctrlpipe(dev
->udev
, 0),
414 (unsigned char *) req
,
416 nc_flush_complete
, req
);
417 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
424 if (netif_msg_rx_err(dev
))
425 devdbg(dev
, "flush net1080; too many framing errors");
426 dev
->frame_errors
= 0;
430 static int net1080_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
432 struct nc_header
*header
;
433 struct nc_trailer
*trailer
;
434 u16 hdr_len
, packet_len
;
436 if (!(skb
->len
& 0x01)) {
438 struct net_device
*net
= dev
->net
;
439 dbg("rx framesize %d range %d..%d mtu %d", skb
->len
,
440 net
->hard_header_len
, dev
->hard_mtu
, net
->mtu
);
442 dev
->stats
.rx_frame_errors
++;
447 header
= (struct nc_header
*) skb
->data
;
448 hdr_len
= le16_to_cpup(&header
->hdr_len
);
449 packet_len
= le16_to_cpup(&header
->packet_len
);
450 if (FRAMED_SIZE(packet_len
) > NC_MAX_PACKET
) {
451 dev
->stats
.rx_frame_errors
++;
452 dbg("packet too big, %d", packet_len
);
455 } else if (hdr_len
< MIN_HEADER
) {
456 dev
->stats
.rx_frame_errors
++;
457 dbg("header too short, %d", hdr_len
);
460 } else if (hdr_len
> MIN_HEADER
) {
461 // out of band data for us?
462 dbg("header OOB, %d bytes", hdr_len
- MIN_HEADER
);
464 // switch (vendor/product ids) { ... }
466 skb_pull(skb
, hdr_len
);
468 trailer
= (struct nc_trailer
*)
469 (skb
->data
+ skb
->len
- sizeof *trailer
);
470 skb_trim(skb
, skb
->len
- sizeof *trailer
);
472 if ((packet_len
& 0x01) == 0) {
473 if (skb
->data
[packet_len
] != PAD_BYTE
) {
474 dev
->stats
.rx_frame_errors
++;
478 skb_trim(skb
, skb
->len
- 1);
480 if (skb
->len
!= packet_len
) {
481 dev
->stats
.rx_frame_errors
++;
482 dbg("bad packet len %d (expected %d)",
483 skb
->len
, packet_len
);
487 if (header
->packet_id
!= get_unaligned(&trailer
->packet_id
)) {
488 dev
->stats
.rx_fifo_errors
++;
489 dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
490 le16_to_cpu(header
->packet_id
),
491 le16_to_cpu(trailer
->packet_id
));
495 devdbg(dev
, "frame <rx h %d p %d id %d", header
->hdr_len
,
496 header
->packet_len
, header
->packet_id
);
498 dev
->frame_errors
= 0;
502 static struct sk_buff
*
503 net1080_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
506 struct sk_buff
*skb2
;
507 struct nc_header
*header
= NULL
;
508 struct nc_trailer
*trailer
= NULL
;
511 padlen
= ((len
+ sizeof (struct nc_header
)
512 + sizeof (struct nc_trailer
)) & 0x01) ? 0 : 1;
513 if (!skb_cloned(skb
)) {
514 int headroom
= skb_headroom(skb
);
515 int tailroom
= skb_tailroom(skb
);
517 if ((padlen
+ sizeof (struct nc_trailer
)) <= tailroom
518 && sizeof (struct nc_header
) <= headroom
)
519 /* There's enough head and tail room */
522 if ((sizeof (struct nc_header
) + padlen
523 + sizeof (struct nc_trailer
)) <
524 (headroom
+ tailroom
)) {
525 /* There's enough total room, so just readjust */
526 skb
->data
= memmove(skb
->head
527 + sizeof (struct nc_header
),
528 skb
->data
, skb
->len
);
529 skb
->tail
= skb
->data
+ len
;
534 /* Create a new skb to use with the correct size */
535 skb2
= skb_copy_expand(skb
,
536 sizeof (struct nc_header
),
537 sizeof (struct nc_trailer
) + padlen
,
539 dev_kfree_skb_any(skb
);
546 header
= (struct nc_header
*) skb_push(skb
, sizeof *header
);
547 header
->hdr_len
= cpu_to_le16(sizeof (*header
));
548 header
->packet_len
= cpu_to_le16(len
);
549 header
->packet_id
= cpu_to_le16((u16
)dev
->xid
++);
551 /* maybe pad; then trailer */
552 if (!((skb
->len
+ sizeof *trailer
) & 0x01))
553 *skb_put(skb
, 1) = PAD_BYTE
;
554 trailer
= (struct nc_trailer
*) skb_put(skb
, sizeof *trailer
);
555 put_unaligned(header
->packet_id
, &trailer
->packet_id
);
557 devdbg(dev
, "frame >tx h %d p %d id %d",
558 header
->hdr_len
, header
->packet_len
,
564 static int net1080_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
566 unsigned extra
= sizeof (struct nc_header
)
568 + sizeof (struct nc_trailer
);
570 dev
->net
->hard_header_len
+= extra
;
571 dev
->rx_urb_size
= dev
->net
->hard_header_len
+ dev
->net
->mtu
;
572 dev
->hard_mtu
= NC_MAX_PACKET
;
573 return usbnet_get_endpoints (dev
, intf
);
576 static const struct driver_info net1080_info
= {
577 .description
= "NetChip TurboCONNECT",
578 .flags
= FLAG_FRAMING_NC
,
579 .bind
= net1080_bind
,
580 .reset
= net1080_reset
,
581 .check_connect
= net1080_check_connect
,
582 .rx_fixup
= net1080_rx_fixup
,
583 .tx_fixup
= net1080_tx_fixup
,
586 static const struct usb_device_id products
[] = {
588 USB_DEVICE(0x0525, 0x1080), // NetChip ref design
589 .driver_info
= (unsigned long) &net1080_info
,
591 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
592 .driver_info
= (unsigned long) &net1080_info
,
596 MODULE_DEVICE_TABLE(usb
, products
);
598 static struct usb_driver net1080_driver
= {
599 .owner
= THIS_MODULE
,
601 .id_table
= products
,
602 .probe
= usbnet_probe
,
603 .disconnect
= usbnet_disconnect
,
604 .suspend
= usbnet_suspend
,
605 .resume
= usbnet_resume
,
608 static int __init
net1080_init(void)
610 return usb_register(&net1080_driver
);
612 module_init(net1080_init
);
614 static void __exit
net1080_exit(void)
616 usb_deregister(&net1080_driver
);
618 module_exit(net1080_exit
);
620 MODULE_AUTHOR("David Brownell");
621 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
622 MODULE_LICENSE("GPL");