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
));
163 dbg("%s registers:", dev
->net
->name
);
164 for (reg
= 0; reg
< 0x20; reg
++) {
167 // reading some registers is trouble
168 if (reg
>= 0x08 && reg
<= 0xf)
170 if (reg
>= 0x12 && reg
<= 0x1e)
173 retval
= nc_register_read(dev
, reg
, vp
);
175 dbg("%s reg [0x%x] ==> error %d",
176 dev
->net
->name
, reg
, retval
);
178 dbg("%s reg [0x%x] = 0x%x",
179 dev
->net
->name
, reg
, *vp
);
186 /*-------------------------------------------------------------------------*/
192 #define USBCTL_WRITABLE_MASK 0x1f0f
193 // bits 15-13 reserved, r/o
194 #define USBCTL_ENABLE_LANG (1 << 12)
195 #define USBCTL_ENABLE_MFGR (1 << 11)
196 #define USBCTL_ENABLE_PROD (1 << 10)
197 #define USBCTL_ENABLE_SERIAL (1 << 9)
198 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
199 // bits 7-4 reserved, r/o
200 #define USBCTL_FLUSH_OTHER (1 << 3)
201 #define USBCTL_FLUSH_THIS (1 << 2)
202 #define USBCTL_DISCONN_OTHER (1 << 1)
203 #define USBCTL_DISCONN_THIS (1 << 0)
205 static inline void nc_dump_usbctl(struct usbnet
*dev
, u16 usbctl
)
207 netif_dbg(dev
, link
, dev
->net
,
208 "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
209 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
211 (usbctl
& USBCTL_ENABLE_LANG
) ? " lang" : "",
212 (usbctl
& USBCTL_ENABLE_MFGR
) ? " mfgr" : "",
213 (usbctl
& USBCTL_ENABLE_PROD
) ? " prod" : "",
214 (usbctl
& USBCTL_ENABLE_SERIAL
) ? " serial" : "",
215 (usbctl
& USBCTL_ENABLE_DEFAULTS
) ? " defaults" : "",
217 (usbctl
& USBCTL_FLUSH_THIS
) ? " FLUSH" : "",
218 (usbctl
& USBCTL_DISCONN_THIS
) ? " DIS" : "",
220 (usbctl
& USBCTL_FLUSH_OTHER
) ? " FLUSH" : "",
221 (usbctl
& USBCTL_DISCONN_OTHER
) ? " DIS" : "",
223 usbctl
& ~USBCTL_WRITABLE_MASK
);
226 /*-------------------------------------------------------------------------*/
232 #define STATUS_PORT_A (1 << 15)
234 #define STATUS_CONN_OTHER (1 << 14)
235 #define STATUS_SUSPEND_OTHER (1 << 13)
236 #define STATUS_MAILBOX_OTHER (1 << 12)
237 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
239 #define STATUS_CONN_THIS (1 << 6)
240 #define STATUS_SUSPEND_THIS (1 << 5)
241 #define STATUS_MAILBOX_THIS (1 << 4)
242 #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
244 #define STATUS_UNSPEC_MASK 0x0c8c
245 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
248 static inline void nc_dump_status(struct usbnet
*dev
, u16 status
)
250 netif_dbg(dev
, link
, dev
->net
,
251 "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
252 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
255 // XXX the packet counts don't seem right
256 // (1 at reset, not 0); maybe UNSPEC too
258 (status
& STATUS_PORT_A
) ? 'A' : 'B',
259 STATUS_PACKETS_THIS(status
),
260 (status
& STATUS_CONN_THIS
) ? " CON" : "",
261 (status
& STATUS_SUSPEND_THIS
) ? " SUS" : "",
262 (status
& STATUS_MAILBOX_THIS
) ? " MBOX" : "",
264 STATUS_PACKETS_OTHER(status
),
265 (status
& STATUS_CONN_OTHER
) ? " CON" : "",
266 (status
& STATUS_SUSPEND_OTHER
) ? " SUS" : "",
267 (status
& STATUS_MAILBOX_OTHER
) ? " MBOX" : "",
269 status
& STATUS_UNSPEC_MASK
);
272 /*-------------------------------------------------------------------------*/
278 #define TTL_THIS(ttl) (0x00ff & ttl)
279 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
280 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
282 static inline void nc_dump_ttl(struct usbnet
*dev
, u16 ttl
)
284 netif_dbg(dev
, link
, dev
->net
, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n",
285 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
286 ttl
, TTL_THIS(ttl
), TTL_OTHER(ttl
));
289 /*-------------------------------------------------------------------------*/
291 static int net1080_reset(struct usbnet
*dev
)
293 u16 usbctl
, status
, ttl
;
294 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
300 // nc_dump_registers(dev);
302 if ((retval
= nc_register_read(dev
, REG_STATUS
, vp
)) < 0) {
303 dbg("can't read %s-%s status: %d",
304 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
, retval
);
308 nc_dump_status(dev
, status
);
310 if ((retval
= nc_register_read(dev
, REG_USBCTL
, vp
)) < 0) {
311 dbg("can't read USBCTL, %d", retval
);
315 nc_dump_usbctl(dev
, usbctl
);
317 nc_register_write(dev
, REG_USBCTL
,
318 USBCTL_FLUSH_THIS
| USBCTL_FLUSH_OTHER
);
320 if ((retval
= nc_register_read(dev
, REG_TTL
, vp
)) < 0) {
321 dbg("can't read TTL, %d", retval
);
325 // nc_dump_ttl(dev, ttl);
327 nc_register_write(dev
, REG_TTL
,
328 MK_TTL(NC_READ_TTL_MS
, TTL_OTHER(ttl
)) );
329 dbg("%s: assigned TTL, %d ms", dev
->net
->name
, NC_READ_TTL_MS
);
331 netif_info(dev
, link
, dev
->net
, "port %c, peer %sconnected\n",
332 (status
& STATUS_PORT_A
) ? 'A' : 'B',
333 (status
& STATUS_CONN_OTHER
) ? "" : "dis");
341 static int net1080_check_connect(struct usbnet
*dev
)
345 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
349 retval
= nc_register_read(dev
, REG_STATUS
, vp
);
353 dbg("%s net1080_check_conn read - %d", dev
->net
->name
, retval
);
356 if ((status
& STATUS_CONN_OTHER
) != STATUS_CONN_OTHER
)
361 static void nc_flush_complete(struct urb
*urb
)
367 static void nc_ensure_sync(struct usbnet
*dev
)
370 if (dev
->frame_errors
> 5) {
372 struct usb_ctrlrequest
*req
;
376 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
380 req
= kmalloc(sizeof *req
, GFP_ATOMIC
);
386 req
->bRequestType
= USB_DIR_OUT
389 req
->bRequest
= REQUEST_REGISTER
;
390 req
->wValue
= cpu_to_le16(USBCTL_FLUSH_THIS
391 | USBCTL_FLUSH_OTHER
);
392 req
->wIndex
= cpu_to_le16(REG_USBCTL
);
393 req
->wLength
= cpu_to_le16(0);
395 /* queue an async control request, we don't need
396 * to do anything when it finishes except clean up.
398 usb_fill_control_urb(urb
, dev
->udev
,
399 usb_sndctrlpipe(dev
->udev
, 0),
400 (unsigned char *) req
,
402 nc_flush_complete
, req
);
403 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
410 netif_dbg(dev
, rx_err
, dev
->net
,
411 "flush net1080; too many framing errors\n");
412 dev
->frame_errors
= 0;
416 static int net1080_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
418 struct nc_header
*header
;
419 struct nc_trailer
*trailer
;
420 u16 hdr_len
, packet_len
;
422 if (!(skb
->len
& 0x01)) {
424 struct net_device
*net
= dev
->net
;
425 dbg("rx framesize %d range %d..%d mtu %d", skb
->len
,
426 net
->hard_header_len
, dev
->hard_mtu
, net
->mtu
);
428 dev
->net
->stats
.rx_frame_errors
++;
433 header
= (struct nc_header
*) skb
->data
;
434 hdr_len
= le16_to_cpup(&header
->hdr_len
);
435 packet_len
= le16_to_cpup(&header
->packet_len
);
436 if (FRAMED_SIZE(packet_len
) > NC_MAX_PACKET
) {
437 dev
->net
->stats
.rx_frame_errors
++;
438 dbg("packet too big, %d", packet_len
);
441 } else if (hdr_len
< MIN_HEADER
) {
442 dev
->net
->stats
.rx_frame_errors
++;
443 dbg("header too short, %d", hdr_len
);
446 } else if (hdr_len
> MIN_HEADER
) {
447 // out of band data for us?
448 dbg("header OOB, %d bytes", hdr_len
- MIN_HEADER
);
450 // switch (vendor/product ids) { ... }
452 skb_pull(skb
, hdr_len
);
454 trailer
= (struct nc_trailer
*)
455 (skb
->data
+ skb
->len
- sizeof *trailer
);
456 skb_trim(skb
, skb
->len
- sizeof *trailer
);
458 if ((packet_len
& 0x01) == 0) {
459 if (skb
->data
[packet_len
] != PAD_BYTE
) {
460 dev
->net
->stats
.rx_frame_errors
++;
464 skb_trim(skb
, skb
->len
- 1);
466 if (skb
->len
!= packet_len
) {
467 dev
->net
->stats
.rx_frame_errors
++;
468 dbg("bad packet len %d (expected %d)",
469 skb
->len
, packet_len
);
473 if (header
->packet_id
!= get_unaligned(&trailer
->packet_id
)) {
474 dev
->net
->stats
.rx_fifo_errors
++;
475 dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
476 le16_to_cpu(header
->packet_id
),
477 le16_to_cpu(trailer
->packet_id
));
481 netdev_dbg(dev
->net
, "frame <rx h %d p %d id %d\n", header
->hdr_len
,
482 header
->packet_len
, header
->packet_id
);
484 dev
->frame_errors
= 0;
488 static struct sk_buff
*
489 net1080_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
491 struct sk_buff
*skb2
;
492 struct nc_header
*header
= NULL
;
493 struct nc_trailer
*trailer
= NULL
;
494 int padlen
= sizeof (struct nc_trailer
);
497 if (!((len
+ padlen
+ sizeof (struct nc_header
)) & 0x01))
499 if (!skb_cloned(skb
)) {
500 int headroom
= skb_headroom(skb
);
501 int tailroom
= skb_tailroom(skb
);
503 if (padlen
<= tailroom
&&
504 sizeof(struct nc_header
) <= headroom
)
505 /* There's enough head and tail room */
508 if ((sizeof (struct nc_header
) + padlen
) <
509 (headroom
+ tailroom
)) {
510 /* There's enough total room, so just readjust */
511 skb
->data
= memmove(skb
->head
512 + sizeof (struct nc_header
),
513 skb
->data
, skb
->len
);
514 skb_set_tail_pointer(skb
, len
);
519 /* Create a new skb to use with the correct size */
520 skb2
= skb_copy_expand(skb
,
521 sizeof (struct nc_header
),
524 dev_kfree_skb_any(skb
);
531 header
= (struct nc_header
*) skb_push(skb
, sizeof *header
);
532 header
->hdr_len
= cpu_to_le16(sizeof (*header
));
533 header
->packet_len
= cpu_to_le16(len
);
534 header
->packet_id
= cpu_to_le16((u16
)dev
->xid
++);
536 /* maybe pad; then trailer */
537 if (!((skb
->len
+ sizeof *trailer
) & 0x01))
538 *skb_put(skb
, 1) = PAD_BYTE
;
539 trailer
= (struct nc_trailer
*) skb_put(skb
, sizeof *trailer
);
540 put_unaligned(header
->packet_id
, &trailer
->packet_id
);
542 netdev_dbg(dev
->net
, "frame >tx h %d p %d id %d\n",
543 header
->hdr_len
, header
->packet_len
,
549 static int net1080_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
551 unsigned extra
= sizeof (struct nc_header
)
553 + sizeof (struct nc_trailer
);
555 dev
->net
->hard_header_len
+= extra
;
556 dev
->rx_urb_size
= dev
->net
->hard_header_len
+ dev
->net
->mtu
;
557 dev
->hard_mtu
= NC_MAX_PACKET
;
558 return usbnet_get_endpoints (dev
, intf
);
561 static const struct driver_info net1080_info
= {
562 .description
= "NetChip TurboCONNECT",
563 .flags
= FLAG_POINTTOPOINT
| FLAG_FRAMING_NC
,
564 .bind
= net1080_bind
,
565 .reset
= net1080_reset
,
566 .check_connect
= net1080_check_connect
,
567 .rx_fixup
= net1080_rx_fixup
,
568 .tx_fixup
= net1080_tx_fixup
,
571 static const struct usb_device_id products
[] = {
573 USB_DEVICE(0x0525, 0x1080), // NetChip ref design
574 .driver_info
= (unsigned long) &net1080_info
,
576 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
577 .driver_info
= (unsigned long) &net1080_info
,
581 MODULE_DEVICE_TABLE(usb
, products
);
583 static struct usb_driver net1080_driver
= {
585 .id_table
= products
,
586 .probe
= usbnet_probe
,
587 .disconnect
= usbnet_disconnect
,
588 .suspend
= usbnet_suspend
,
589 .resume
= usbnet_resume
,
592 static int __init
net1080_init(void)
594 return usb_register(&net1080_driver
);
596 module_init(net1080_init
);
598 static void __exit
net1080_exit(void)
600 usb_deregister(&net1080_driver
);
602 module_exit(net1080_exit
);
604 MODULE_AUTHOR("David Brownell");
605 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
606 MODULE_LICENSE("GPL");