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/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>
33 #include <asm/unaligned.h>
39 * Netchip 1080 driver ... http://www.netchip.com
40 * (Sept 2004: End-of-life announcement has been sent.)
41 * Used in (some) LapLink cables
44 #define frame_errors data[1]
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.
51 * - Ethernet header (14 bytes)
53 * - (optional padding byte, if needed so length becomes odd)
56 * This framing is to be avoided for non-NetChip devices.
59 struct 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
65 // all else is optional, and must start with:
66 // __le16 vendorId; // from usb-if
68 } __attribute__((__packed__
));
70 #define PAD_BYTE ((unsigned char)0xAC)
74 } __attribute__((__packed__
));
76 // packets may use FLAG_FRAMING_NC and optional pad
77 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
78 + sizeof (struct ethhdr) \
81 + sizeof (struct nc_trailer))
83 #define MIN_FRAMED FRAMED_SIZE(0)
85 /* packets _could_ be up to 64KB... */
86 #define NC_MAX_PACKET 32767
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.
95 #define NC_READ_TTL_MS ((u8)255) // ms
98 * We ignore most registers and EEPROM contents.
100 #define REG_USBCTL ((u8)0x04)
101 #define REG_TTL ((u8)0x10)
102 #define REG_STATUS ((u8)0x11)
105 * Vendor specific requests to read/write data
107 #define REQUEST_REGISTER ((u8)0x10)
108 #define REQUEST_EEPROM ((u8)0x11)
111 nc_vendor_read(struct usbnet
*dev
, u8 req
, u8 regnum
, u16
*retval_ptr
)
113 int status
= usb_control_msg(dev
->udev
,
114 usb_rcvctrlpipe(dev
->udev
, 0),
116 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
118 retval_ptr
, sizeof *retval_ptr
,
119 USB_CTRL_GET_TIMEOUT
);
123 le16_to_cpus(retval_ptr
);
128 nc_register_read(struct usbnet
*dev
, u8 regnum
, u16
*retval_ptr
)
130 return nc_vendor_read(dev
, REQUEST_REGISTER
, regnum
, retval_ptr
);
133 // no retval ... can become async, usable in_interrupt()
135 nc_vendor_write(struct usbnet
*dev
, u8 req
, u8 regnum
, u16 value
)
137 usb_control_msg(dev
->udev
,
138 usb_sndctrlpipe(dev
->udev
, 0),
140 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
142 NULL
, 0, // data is in setup packet
143 USB_CTRL_SET_TIMEOUT
);
147 nc_register_write(struct usbnet
*dev
, u8 regnum
, u16 value
)
149 nc_vendor_write(dev
, REQUEST_REGISTER
, regnum
, value
);
154 static void nc_dump_registers(struct usbnet
*dev
)
157 u16
*vp
= kmalloc(sizeof (u16
));
164 dbg("%s registers:", dev
->net
->name
);
165 for (reg
= 0; reg
< 0x20; reg
++) {
168 // reading some registers is trouble
169 if (reg
>= 0x08 && reg
<= 0xf)
171 if (reg
>= 0x12 && reg
<= 0x1e)
174 retval
= nc_register_read(dev
, reg
, vp
);
176 dbg("%s reg [0x%x] ==> error %d",
177 dev
->net
->name
, reg
, retval
);
179 dbg("%s reg [0x%x] = 0x%x",
180 dev
->net
->name
, reg
, *vp
);
187 /*-------------------------------------------------------------------------*/
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)
206 static inline void nc_dump_usbctl(struct usbnet
*dev
, u16 usbctl
)
208 if (!netif_msg_link(dev
))
210 devdbg(dev
, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
212 " other%s%s; r/o 0x%x",
213 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
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" : "",
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
229 /*-------------------------------------------------------------------------*/
235 #define STATUS_PORT_A (1 << 15)
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)
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)
247 #define STATUS_UNSPEC_MASK 0x0c8c
248 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
251 static inline void nc_dump_status(struct usbnet
*dev
, u16 status
)
253 if (!netif_msg_link(dev
))
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
,
261 // XXX the packet counts don't seem right
262 // (1 at reset, not 0); maybe UNSPEC too
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" : "",
270 STATUS_PACKETS_OTHER(status
),
271 (status
& STATUS_CONN_OTHER
) ? " CON" : "",
272 (status
& STATUS_SUSPEND_OTHER
) ? " SUS" : "",
273 (status
& STATUS_MAILBOX_OTHER
) ? " MBOX" : "",
275 status
& STATUS_UNSPEC_MASK
279 /*-------------------------------------------------------------------------*/
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))))
289 static inline void nc_dump_ttl(struct usbnet
*dev
, u16 ttl
)
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
));
297 /*-------------------------------------------------------------------------*/
299 static int net1080_reset(struct usbnet
*dev
)
301 u16 usbctl
, status
, ttl
;
302 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
308 // nc_dump_registers(dev);
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
);
316 nc_dump_status(dev
, status
);
318 if ((retval
= nc_register_read(dev
, REG_USBCTL
, vp
)) < 0) {
319 dbg("can't read USBCTL, %d", retval
);
323 nc_dump_usbctl(dev
, usbctl
);
325 nc_register_write(dev
, REG_USBCTL
,
326 USBCTL_FLUSH_THIS
| USBCTL_FLUSH_OTHER
);
328 if ((retval
= nc_register_read(dev
, REG_TTL
, vp
)) < 0) {
329 dbg("can't read TTL, %d", retval
);
333 // nc_dump_ttl(dev, ttl);
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
);
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"
351 static int net1080_check_connect(struct usbnet
*dev
)
355 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
359 retval
= nc_register_read(dev
, REG_STATUS
, vp
);
363 dbg("%s net1080_check_conn read - %d", dev
->net
->name
, retval
);
366 if ((status
& STATUS_CONN_OTHER
) != STATUS_CONN_OTHER
)
371 static void nc_flush_complete(struct urb
*urb
)
377 static void nc_ensure_sync(struct usbnet
*dev
)
380 if (dev
->frame_errors
> 5) {
382 struct usb_ctrlrequest
*req
;
386 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
390 req
= kmalloc(sizeof *req
, GFP_ATOMIC
);
396 req
->bRequestType
= USB_DIR_OUT
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);
405 /* queue an async control request, we don't need
406 * to do anything when it finishes except clean up.
408 usb_fill_control_urb(urb
, dev
->udev
,
409 usb_sndctrlpipe(dev
->udev
, 0),
410 (unsigned char *) req
,
412 nc_flush_complete
, req
);
413 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
420 if (netif_msg_rx_err(dev
))
421 devdbg(dev
, "flush net1080; too many framing errors");
422 dev
->frame_errors
= 0;
426 static int net1080_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
428 struct nc_header
*header
;
429 struct nc_trailer
*trailer
;
430 u16 hdr_len
, packet_len
;
432 if (!(skb
->len
& 0x01)) {
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
);
438 dev
->stats
.rx_frame_errors
++;
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
);
451 } else if (hdr_len
< MIN_HEADER
) {
452 dev
->stats
.rx_frame_errors
++;
453 dbg("header too short, %d", hdr_len
);
456 } else if (hdr_len
> MIN_HEADER
) {
457 // out of band data for us?
458 dbg("header OOB, %d bytes", hdr_len
- MIN_HEADER
);
460 // switch (vendor/product ids) { ... }
462 skb_pull(skb
, hdr_len
);
464 trailer
= (struct nc_trailer
*)
465 (skb
->data
+ skb
->len
- sizeof *trailer
);
466 skb_trim(skb
, skb
->len
- sizeof *trailer
);
468 if ((packet_len
& 0x01) == 0) {
469 if (skb
->data
[packet_len
] != PAD_BYTE
) {
470 dev
->stats
.rx_frame_errors
++;
474 skb_trim(skb
, skb
->len
- 1);
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
);
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
));
491 devdbg(dev
, "frame <rx h %d p %d id %d", header
->hdr_len
,
492 header
->packet_len
, header
->packet_id
);
494 dev
->frame_errors
= 0;
498 static struct sk_buff
*
499 net1080_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
501 struct sk_buff
*skb2
;
502 struct nc_header
*header
= NULL
;
503 struct nc_trailer
*trailer
= NULL
;
504 int padlen
= sizeof (struct nc_trailer
);
507 if (!((len
+ padlen
+ sizeof (struct nc_header
)) & 0x01))
509 if (!skb_cloned(skb
)) {
510 int headroom
= skb_headroom(skb
);
511 int tailroom
= skb_tailroom(skb
);
513 if (padlen
<= tailroom
&&
514 sizeof(struct nc_header
) <= headroom
)
515 /* There's enough head and tail room */
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
;
529 /* Create a new skb to use with the correct size */
530 skb2
= skb_copy_expand(skb
,
531 sizeof (struct nc_header
),
534 dev_kfree_skb_any(skb
);
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
++);
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
);
552 devdbg(dev
, "frame >tx h %d p %d id %d",
553 header
->hdr_len
, header
->packet_len
,
559 static int net1080_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
561 unsigned extra
= sizeof (struct nc_header
)
563 + sizeof (struct nc_trailer
);
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
);
571 static 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
,
581 static const struct usb_device_id products
[] = {
583 USB_DEVICE(0x0525, 0x1080), // NetChip ref design
584 .driver_info
= (unsigned long) &net1080_info
,
586 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
587 .driver_info
= (unsigned long) &net1080_info
,
591 MODULE_DEVICE_TABLE(usb
, products
);
593 static struct usb_driver net1080_driver
= {
595 .id_table
= products
,
596 .probe
= usbnet_probe
,
597 .disconnect
= usbnet_disconnect
,
598 .suspend
= usbnet_suspend
,
599 .resume
= usbnet_resume
,
602 static int __init
net1080_init(void)
604 return usb_register(&net1080_driver
);
606 module_init(net1080_init
);
608 static void __exit
net1080_exit(void)
610 usb_deregister(&net1080_driver
);
612 module_exit(net1080_exit
);
614 MODULE_AUTHOR("David Brownell");
615 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
616 MODULE_LICENSE("GPL");