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>
33 #include <asm/unaligned.h>
37 * Netchip 1080 driver ... http://www.netchip.com
38 * (Sept 2004: End-of-life announcement has been sent.)
39 * Used in (some) LapLink cables
42 #define frame_errors data[1]
45 * NetChip framing of ethernet packets, supporting additional error
46 * checks for links that may drop bulk packets from inside messages.
47 * Odd USB length == always short read for last usb packet.
49 * - Ethernet header (14 bytes)
51 * - (optional padding byte, if needed so length becomes odd)
54 * This framing is to be avoided for non-NetChip devices.
57 struct nc_header
{ // packed:
58 __le16 hdr_len
; // sizeof nc_header (LE, all)
59 __le16 packet_len
; // payload size (including ethhdr)
60 __le16 packet_id
; // detects dropped packets
63 // all else is optional, and must start with:
64 // __le16 vendorId; // from usb-if
66 } __attribute__((__packed__
));
68 #define PAD_BYTE ((unsigned char)0xAC)
72 } __attribute__((__packed__
));
74 // packets may use FLAG_FRAMING_NC and optional pad
75 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
76 + sizeof (struct ethhdr) \
79 + sizeof (struct nc_trailer))
81 #define MIN_FRAMED FRAMED_SIZE(0)
83 /* packets _could_ be up to 64KB... */
84 #define NC_MAX_PACKET 32767
88 * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
89 * before the hardware drops it. If that's done, the driver will need to
90 * frame network packets to guard against the dropped USB packets. The win32
91 * driver sets this for both sides of the link.
93 #define NC_READ_TTL_MS ((u8)255) // ms
96 * We ignore most registers and EEPROM contents.
98 #define REG_USBCTL ((u8)0x04)
99 #define REG_TTL ((u8)0x10)
100 #define REG_STATUS ((u8)0x11)
103 * Vendor specific requests to read/write data
105 #define REQUEST_REGISTER ((u8)0x10)
106 #define REQUEST_EEPROM ((u8)0x11)
109 nc_vendor_read(struct usbnet
*dev
, u8 req
, u8 regnum
, u16
*retval_ptr
)
111 int status
= usb_control_msg(dev
->udev
,
112 usb_rcvctrlpipe(dev
->udev
, 0),
114 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
116 retval_ptr
, sizeof *retval_ptr
,
117 USB_CTRL_GET_TIMEOUT
);
121 le16_to_cpus(retval_ptr
);
126 nc_register_read(struct usbnet
*dev
, u8 regnum
, u16
*retval_ptr
)
128 return nc_vendor_read(dev
, REQUEST_REGISTER
, regnum
, retval_ptr
);
131 // no retval ... can become async, usable in_interrupt()
133 nc_vendor_write(struct usbnet
*dev
, u8 req
, u8 regnum
, u16 value
)
135 usb_control_msg(dev
->udev
,
136 usb_sndctrlpipe(dev
->udev
, 0),
138 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
140 NULL
, 0, // data is in setup packet
141 USB_CTRL_SET_TIMEOUT
);
145 nc_register_write(struct usbnet
*dev
, u8 regnum
, u16 value
)
147 nc_vendor_write(dev
, REQUEST_REGISTER
, regnum
, value
);
152 static void nc_dump_registers(struct usbnet
*dev
)
155 u16
*vp
= kmalloc(sizeof (u16
));
162 dbg("%s registers:", dev
->net
->name
);
163 for (reg
= 0; reg
< 0x20; reg
++) {
166 // reading some registers is trouble
167 if (reg
>= 0x08 && reg
<= 0xf)
169 if (reg
>= 0x12 && reg
<= 0x1e)
172 retval
= nc_register_read(dev
, reg
, vp
);
174 dbg("%s reg [0x%x] ==> error %d",
175 dev
->net
->name
, reg
, retval
);
177 dbg("%s reg [0x%x] = 0x%x",
178 dev
->net
->name
, reg
, *vp
);
185 /*-------------------------------------------------------------------------*/
191 #define USBCTL_WRITABLE_MASK 0x1f0f
192 // bits 15-13 reserved, r/o
193 #define USBCTL_ENABLE_LANG (1 << 12)
194 #define USBCTL_ENABLE_MFGR (1 << 11)
195 #define USBCTL_ENABLE_PROD (1 << 10)
196 #define USBCTL_ENABLE_SERIAL (1 << 9)
197 #define USBCTL_ENABLE_DEFAULTS (1 << 8)
198 // bits 7-4 reserved, r/o
199 #define USBCTL_FLUSH_OTHER (1 << 3)
200 #define USBCTL_FLUSH_THIS (1 << 2)
201 #define USBCTL_DISCONN_OTHER (1 << 1)
202 #define USBCTL_DISCONN_THIS (1 << 0)
204 static inline void nc_dump_usbctl(struct usbnet
*dev
, u16 usbctl
)
206 netif_dbg(dev
, link
, dev
->net
,
207 "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
208 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
210 (usbctl
& USBCTL_ENABLE_LANG
) ? " lang" : "",
211 (usbctl
& USBCTL_ENABLE_MFGR
) ? " mfgr" : "",
212 (usbctl
& USBCTL_ENABLE_PROD
) ? " prod" : "",
213 (usbctl
& USBCTL_ENABLE_SERIAL
) ? " serial" : "",
214 (usbctl
& USBCTL_ENABLE_DEFAULTS
) ? " defaults" : "",
216 (usbctl
& USBCTL_FLUSH_THIS
) ? " FLUSH" : "",
217 (usbctl
& USBCTL_DISCONN_THIS
) ? " DIS" : "",
219 (usbctl
& USBCTL_FLUSH_OTHER
) ? " FLUSH" : "",
220 (usbctl
& USBCTL_DISCONN_OTHER
) ? " DIS" : "",
222 usbctl
& ~USBCTL_WRITABLE_MASK
);
225 /*-------------------------------------------------------------------------*/
231 #define STATUS_PORT_A (1 << 15)
233 #define STATUS_CONN_OTHER (1 << 14)
234 #define STATUS_SUSPEND_OTHER (1 << 13)
235 #define STATUS_MAILBOX_OTHER (1 << 12)
236 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) & 0x03)
238 #define STATUS_CONN_THIS (1 << 6)
239 #define STATUS_SUSPEND_THIS (1 << 5)
240 #define STATUS_MAILBOX_THIS (1 << 4)
241 #define STATUS_PACKETS_THIS(n) (((n) >> 0) & 0x03)
243 #define STATUS_UNSPEC_MASK 0x0c8c
244 #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
247 static inline void nc_dump_status(struct usbnet
*dev
, u16 status
)
249 netif_dbg(dev
, link
, dev
->net
,
250 "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
251 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
254 // XXX the packet counts don't seem right
255 // (1 at reset, not 0); maybe UNSPEC too
257 (status
& STATUS_PORT_A
) ? 'A' : 'B',
258 STATUS_PACKETS_THIS(status
),
259 (status
& STATUS_CONN_THIS
) ? " CON" : "",
260 (status
& STATUS_SUSPEND_THIS
) ? " SUS" : "",
261 (status
& STATUS_MAILBOX_THIS
) ? " MBOX" : "",
263 STATUS_PACKETS_OTHER(status
),
264 (status
& STATUS_CONN_OTHER
) ? " CON" : "",
265 (status
& STATUS_SUSPEND_OTHER
) ? " SUS" : "",
266 (status
& STATUS_MAILBOX_OTHER
) ? " MBOX" : "",
268 status
& STATUS_UNSPEC_MASK
);
271 /*-------------------------------------------------------------------------*/
277 #define TTL_THIS(ttl) (0x00ff & ttl)
278 #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
279 #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
281 static inline void nc_dump_ttl(struct usbnet
*dev
, u16 ttl
)
283 netif_dbg(dev
, link
, dev
->net
, "net1080 %s-%s ttl 0x%x this = %d, other = %d\n",
284 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
285 ttl
, TTL_THIS(ttl
), TTL_OTHER(ttl
));
288 /*-------------------------------------------------------------------------*/
290 static int net1080_reset(struct usbnet
*dev
)
292 u16 usbctl
, status
, ttl
;
293 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
299 // nc_dump_registers(dev);
301 if ((retval
= nc_register_read(dev
, REG_STATUS
, vp
)) < 0) {
302 dbg("can't read %s-%s status: %d",
303 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
, retval
);
307 nc_dump_status(dev
, status
);
309 if ((retval
= nc_register_read(dev
, REG_USBCTL
, vp
)) < 0) {
310 dbg("can't read USBCTL, %d", retval
);
314 nc_dump_usbctl(dev
, usbctl
);
316 nc_register_write(dev
, REG_USBCTL
,
317 USBCTL_FLUSH_THIS
| USBCTL_FLUSH_OTHER
);
319 if ((retval
= nc_register_read(dev
, REG_TTL
, vp
)) < 0) {
320 dbg("can't read TTL, %d", retval
);
324 // nc_dump_ttl(dev, ttl);
326 nc_register_write(dev
, REG_TTL
,
327 MK_TTL(NC_READ_TTL_MS
, TTL_OTHER(ttl
)) );
328 dbg("%s: assigned TTL, %d ms", dev
->net
->name
, NC_READ_TTL_MS
);
330 netif_info(dev
, link
, dev
->net
, "port %c, peer %sconnected\n",
331 (status
& STATUS_PORT_A
) ? 'A' : 'B',
332 (status
& STATUS_CONN_OTHER
) ? "" : "dis");
340 static int net1080_check_connect(struct usbnet
*dev
)
344 u16
*vp
= kmalloc(sizeof (u16
), GFP_KERNEL
);
348 retval
= nc_register_read(dev
, REG_STATUS
, vp
);
352 dbg("%s net1080_check_conn read - %d", dev
->net
->name
, retval
);
355 if ((status
& STATUS_CONN_OTHER
) != STATUS_CONN_OTHER
)
360 static void nc_flush_complete(struct urb
*urb
)
366 static void nc_ensure_sync(struct usbnet
*dev
)
369 if (dev
->frame_errors
> 5) {
371 struct usb_ctrlrequest
*req
;
375 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
379 req
= kmalloc(sizeof *req
, GFP_ATOMIC
);
385 req
->bRequestType
= USB_DIR_OUT
388 req
->bRequest
= REQUEST_REGISTER
;
389 req
->wValue
= cpu_to_le16(USBCTL_FLUSH_THIS
390 | USBCTL_FLUSH_OTHER
);
391 req
->wIndex
= cpu_to_le16(REG_USBCTL
);
392 req
->wLength
= cpu_to_le16(0);
394 /* queue an async control request, we don't need
395 * to do anything when it finishes except clean up.
397 usb_fill_control_urb(urb
, dev
->udev
,
398 usb_sndctrlpipe(dev
->udev
, 0),
399 (unsigned char *) req
,
401 nc_flush_complete
, req
);
402 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
409 netif_dbg(dev
, rx_err
, dev
->net
,
410 "flush net1080; too many framing errors\n");
411 dev
->frame_errors
= 0;
415 static int net1080_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
417 struct nc_header
*header
;
418 struct nc_trailer
*trailer
;
419 u16 hdr_len
, packet_len
;
421 if (!(skb
->len
& 0x01)) {
423 struct net_device
*net
= dev
->net
;
424 dbg("rx framesize %d range %d..%d mtu %d", skb
->len
,
425 net
->hard_header_len
, dev
->hard_mtu
, net
->mtu
);
427 dev
->net
->stats
.rx_frame_errors
++;
432 header
= (struct nc_header
*) skb
->data
;
433 hdr_len
= le16_to_cpup(&header
->hdr_len
);
434 packet_len
= le16_to_cpup(&header
->packet_len
);
435 if (FRAMED_SIZE(packet_len
) > NC_MAX_PACKET
) {
436 dev
->net
->stats
.rx_frame_errors
++;
437 dbg("packet too big, %d", packet_len
);
440 } else if (hdr_len
< MIN_HEADER
) {
441 dev
->net
->stats
.rx_frame_errors
++;
442 dbg("header too short, %d", hdr_len
);
445 } else if (hdr_len
> MIN_HEADER
) {
446 // out of band data for us?
447 dbg("header OOB, %d bytes", hdr_len
- MIN_HEADER
);
449 // switch (vendor/product ids) { ... }
451 skb_pull(skb
, hdr_len
);
453 trailer
= (struct nc_trailer
*)
454 (skb
->data
+ skb
->len
- sizeof *trailer
);
455 skb_trim(skb
, skb
->len
- sizeof *trailer
);
457 if ((packet_len
& 0x01) == 0) {
458 if (skb
->data
[packet_len
] != PAD_BYTE
) {
459 dev
->net
->stats
.rx_frame_errors
++;
463 skb_trim(skb
, skb
->len
- 1);
465 if (skb
->len
!= packet_len
) {
466 dev
->net
->stats
.rx_frame_errors
++;
467 dbg("bad packet len %d (expected %d)",
468 skb
->len
, packet_len
);
472 if (header
->packet_id
!= get_unaligned(&trailer
->packet_id
)) {
473 dev
->net
->stats
.rx_fifo_errors
++;
474 dbg("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
475 le16_to_cpu(header
->packet_id
),
476 le16_to_cpu(trailer
->packet_id
));
480 netdev_dbg(dev
->net
, "frame <rx h %d p %d id %d\n", header
->hdr_len
,
481 header
->packet_len
, header
->packet_id
);
483 dev
->frame_errors
= 0;
487 static struct sk_buff
*
488 net1080_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
490 struct sk_buff
*skb2
;
491 struct nc_header
*header
= NULL
;
492 struct nc_trailer
*trailer
= NULL
;
493 int padlen
= sizeof (struct nc_trailer
);
496 if (!((len
+ padlen
+ sizeof (struct nc_header
)) & 0x01))
498 if (!skb_cloned(skb
)) {
499 int headroom
= skb_headroom(skb
);
500 int tailroom
= skb_tailroom(skb
);
502 if (padlen
<= tailroom
&&
503 sizeof(struct nc_header
) <= headroom
)
504 /* There's enough head and tail room */
507 if ((sizeof (struct nc_header
) + padlen
) <
508 (headroom
+ tailroom
)) {
509 /* There's enough total room, so just readjust */
510 skb
->data
= memmove(skb
->head
511 + sizeof (struct nc_header
),
512 skb
->data
, skb
->len
);
513 skb_set_tail_pointer(skb
, len
);
518 /* Create a new skb to use with the correct size */
519 skb2
= skb_copy_expand(skb
,
520 sizeof (struct nc_header
),
523 dev_kfree_skb_any(skb
);
530 header
= (struct nc_header
*) skb_push(skb
, sizeof *header
);
531 header
->hdr_len
= cpu_to_le16(sizeof (*header
));
532 header
->packet_len
= cpu_to_le16(len
);
533 header
->packet_id
= cpu_to_le16((u16
)dev
->xid
++);
535 /* maybe pad; then trailer */
536 if (!((skb
->len
+ sizeof *trailer
) & 0x01))
537 *skb_put(skb
, 1) = PAD_BYTE
;
538 trailer
= (struct nc_trailer
*) skb_put(skb
, sizeof *trailer
);
539 put_unaligned(header
->packet_id
, &trailer
->packet_id
);
541 netdev_dbg(dev
->net
, "frame >tx h %d p %d id %d\n",
542 header
->hdr_len
, header
->packet_len
,
548 static int net1080_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
550 unsigned extra
= sizeof (struct nc_header
)
552 + sizeof (struct nc_trailer
);
554 dev
->net
->hard_header_len
+= extra
;
555 dev
->rx_urb_size
= dev
->net
->hard_header_len
+ dev
->net
->mtu
;
556 dev
->hard_mtu
= NC_MAX_PACKET
;
557 return usbnet_get_endpoints (dev
, intf
);
560 static const struct driver_info net1080_info
= {
561 .description
= "NetChip TurboCONNECT",
562 .flags
= FLAG_FRAMING_NC
,
563 .bind
= net1080_bind
,
564 .reset
= net1080_reset
,
565 .check_connect
= net1080_check_connect
,
566 .rx_fixup
= net1080_rx_fixup
,
567 .tx_fixup
= net1080_tx_fixup
,
570 static const struct usb_device_id products
[] = {
572 USB_DEVICE(0x0525, 0x1080), // NetChip ref design
573 .driver_info
= (unsigned long) &net1080_info
,
575 USB_DEVICE(0x06D0, 0x0622), // Laplink Gold
576 .driver_info
= (unsigned long) &net1080_info
,
580 MODULE_DEVICE_TABLE(usb
, products
);
582 static struct usb_driver net1080_driver
= {
584 .id_table
= products
,
585 .probe
= usbnet_probe
,
586 .disconnect
= usbnet_disconnect
,
587 .suspend
= usbnet_suspend
,
588 .resume
= usbnet_resume
,
591 static int __init
net1080_init(void)
593 return usb_register(&net1080_driver
);
595 module_init(net1080_init
);
597 static void __exit
net1080_exit(void)
599 usb_deregister(&net1080_driver
);
601 module_exit(net1080_exit
);
603 MODULE_AUTHOR("David Brownell");
604 MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
605 MODULE_LICENSE("GPL");