Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Net1080 based USB host-to-host cables
  3 * Copyright (C) 2000-2005 by David Brownell
  4 *
  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.
  9 *
 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.
 14 *
 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
 18 */
 19
 20// #define	DEBUG			// error path messages, extra info
 21// #define	VERBOSE			// more; success messages
 22
 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>
 33
 34#include <asm/unaligned.h>
 35
 36
 37/*
 38 * Netchip 1080 driver ... http://www.netchip.com
 39 * (Sept 2004:  End-of-life announcement has been sent.)
 40 * Used in (some) LapLink cables
 41 */
 42
 43#define frame_errors	data[1]
 44
 45/*
 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.
 49 *	- nc_header
 50 *	- Ethernet header (14 bytes)
 51 *	- payload
 52 *	- (optional padding byte, if needed so length becomes odd)
 53 *	- nc_trailer
 54 *
 55 * This framing is to be avoided for non-NetChip devices.
 56 */
 57
 58struct 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
 62#define MIN_HEADER	6
 63
 64	// all else is optional, and must start with:
 65	// __le16	vendorId;	// from usb-if
 66	// __le16	productId;
 67} __packed;
 68
 69#define	PAD_BYTE	((unsigned char)0xAC)
 70
 71struct nc_trailer {
 72	__le16	packet_id;
 73} __packed;
 74
 75// packets may use FLAG_FRAMING_NC and optional pad
 76#define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
 77				+ sizeof (struct ethhdr) \
 78				+ (mtu) \
 79				+ 1 \
 80				+ sizeof (struct nc_trailer))
 81
 82#define MIN_FRAMED	FRAMED_SIZE(0)
 83
 84/* packets _could_ be up to 64KB... */
 85#define NC_MAX_PACKET	32767
 86
 87
 88/*
 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.
 93 */
 94#define	NC_READ_TTL_MS	((u8)255)	// ms
 95
 96/*
 97 * We ignore most registers and EEPROM contents.
 98 */
 99#define	REG_USBCTL	((u8)0x04)
100#define REG_TTL		((u8)0x10)
101#define REG_STATUS	((u8)0x11)
102
103/*
104 * Vendor specific requests to read/write data
105 */
106#define	REQUEST_REGISTER	((u8)0x10)
107#define	REQUEST_EEPROM		((u8)0x11)
108
109static int
110nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
111{
112	int status = usb_control_msg(dev->udev,
113		usb_rcvctrlpipe(dev->udev, 0),
114		req,
115		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116		0, regnum,
117		retval_ptr, sizeof *retval_ptr,
118		USB_CTRL_GET_TIMEOUT);
119	if (status > 0)
120		status = 0;
121	if (!status)
122		le16_to_cpus(retval_ptr);
123	return status;
124}
125
126static inline int
127nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
128{
129	return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
130}
131
132// no retval ... can become async, usable in_interrupt()
133static void
134nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
135{
136	usb_control_msg(dev->udev,
137		usb_sndctrlpipe(dev->udev, 0),
138		req,
139		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
140		value, regnum,
141		NULL, 0,			// data is in setup packet
142		USB_CTRL_SET_TIMEOUT);
143}
144
145static inline void
146nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
147{
148	nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
149}
150
151
152#if 0
153static void nc_dump_registers(struct usbnet *dev)
154{
155	u8	reg;
156	u16	*vp = kmalloc(sizeof (u16));
157
158	if (!vp) {
159		dbg("no memory?");
160		return;
161	}
162
163	dbg("%s registers:", dev->net->name);
164	for (reg = 0; reg < 0x20; reg++) {
165		int retval;
166
167		// reading some registers is trouble
168		if (reg >= 0x08 && reg <= 0xf)
169			continue;
170		if (reg >= 0x12 && reg <= 0x1e)
171			continue;
172
173		retval = nc_register_read(dev, reg, vp);
174		if (retval < 0)
175			dbg("%s reg [0x%x] ==> error %d",
176				dev->net->name, reg, retval);
177		else
178			dbg("%s reg [0x%x] = 0x%x",
179				dev->net->name, reg, *vp);
180	}
181	kfree(vp);
182}
183#endif
184
185
186/*-------------------------------------------------------------------------*/
187
188/*
189 * Control register
190 */
191
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)
204
205static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
206{
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,
210		  usbctl,
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" : "",
216
217		  (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
218		  (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
219
220		  (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
221		  (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
222
223		  usbctl & ~USBCTL_WRITABLE_MASK);
224}
225
226/*-------------------------------------------------------------------------*/
227
228/*
229 * Status register
230 */
231
232#define	STATUS_PORT_A		(1 << 15)
233
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)
238
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)
243
244#define	STATUS_UNSPEC_MASK	0x0c8c
245#define	STATUS_NOISE_MASK 	((u16)~(0x0303|STATUS_UNSPEC_MASK))
246
247
248static inline void nc_dump_status(struct usbnet *dev, u16 status)
249{
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,
253		  status,
254
255		  // XXX the packet counts don't seem right
256		  // (1 at reset, not 0); maybe UNSPEC too
257
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" : "",
263
264		  STATUS_PACKETS_OTHER(status),
265		  (status & STATUS_CONN_OTHER) ? " CON" : "",
266		  (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
267		  (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
268
269		  status & STATUS_UNSPEC_MASK);
270}
271
272/*-------------------------------------------------------------------------*/
273
274/*
275 * TTL register
276 */
277
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))))
281
282static inline void nc_dump_ttl(struct usbnet *dev, u16 ttl)
283{
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));
287}
288
289/*-------------------------------------------------------------------------*/
290
291static int net1080_reset(struct usbnet *dev)
292{
293	u16		usbctl, status, ttl;
294	u16		*vp = kmalloc(sizeof (u16), GFP_KERNEL);
295	int		retval;
296
297	if (!vp)
298		return -ENOMEM;
299
300	// nc_dump_registers(dev);
301
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);
305		goto done;
306	}
307	status = *vp;
308	nc_dump_status(dev, status);
309
310	if ((retval = nc_register_read(dev, REG_USBCTL, vp)) < 0) {
311		dbg("can't read USBCTL, %d", retval);
312		goto done;
313	}
314	usbctl = *vp;
315	nc_dump_usbctl(dev, usbctl);
316
317	nc_register_write(dev, REG_USBCTL,
318			USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
319
320	if ((retval = nc_register_read(dev, REG_TTL, vp)) < 0) {
321		dbg("can't read TTL, %d", retval);
322		goto done;
323	}
324	ttl = *vp;
325	// nc_dump_ttl(dev, ttl);
326
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);
330
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");
334	retval = 0;
335
336done:
337	kfree(vp);
338	return retval;
339}
340
341static int net1080_check_connect(struct usbnet *dev)
342{
343	int			retval;
344	u16			status;
345	u16			*vp = kmalloc(sizeof (u16), GFP_KERNEL);
346
347	if (!vp)
348		return -ENOMEM;
349	retval = nc_register_read(dev, REG_STATUS, vp);
350	status = *vp;
351	kfree(vp);
352	if (retval != 0) {
353		dbg("%s net1080_check_conn read - %d", dev->net->name, retval);
354		return retval;
355	}
356	if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
357		return -ENOLINK;
358	return 0;
359}
360
361static void nc_flush_complete(struct urb *urb)
362{
363	kfree(urb->context);
364	usb_free_urb(urb);
365}
366
367static void nc_ensure_sync(struct usbnet *dev)
368{
369	dev->frame_errors++;
370	if (dev->frame_errors > 5) {
371		struct urb		*urb;
372		struct usb_ctrlrequest	*req;
373		int			status;
374
375		/* Send a flush */
376		urb = usb_alloc_urb(0, GFP_ATOMIC);
377		if (!urb)
378			return;
379
380		req = kmalloc(sizeof *req, GFP_ATOMIC);
381		if (!req) {
382			usb_free_urb(urb);
383			return;
384		}
385
386		req->bRequestType = USB_DIR_OUT
387			| USB_TYPE_VENDOR
388			| USB_RECIP_DEVICE;
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);
394
395		/* queue an async control request, we don't need
396		 * to do anything when it finishes except clean up.
397		 */
398		usb_fill_control_urb(urb, dev->udev,
399			usb_sndctrlpipe(dev->udev, 0),
400			(unsigned char *) req,
401			NULL, 0,
402			nc_flush_complete, req);
403		status = usb_submit_urb(urb, GFP_ATOMIC);
404		if (status) {
405			kfree(req);
406			usb_free_urb(urb);
407			return;
408		}
409
410		netif_dbg(dev, rx_err, dev->net,
411			  "flush net1080; too many framing errors\n");
412		dev->frame_errors = 0;
413	}
414}
415
416static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
417{
418	struct nc_header	*header;
419	struct nc_trailer	*trailer;
420	u16			hdr_len, packet_len;
421
 
 
 
 
422	if (!(skb->len & 0x01)) {
423#ifdef DEBUG
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);
427#endif
428		dev->net->stats.rx_frame_errors++;
429		nc_ensure_sync(dev);
430		return 0;
431	}
432
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);
439		nc_ensure_sync(dev);
440		return 0;
441	} else if (hdr_len < MIN_HEADER) {
442		dev->net->stats.rx_frame_errors++;
443		dbg("header too short, %d", hdr_len);
444		nc_ensure_sync(dev);
445		return 0;
446	} else if (hdr_len > MIN_HEADER) {
447		// out of band data for us?
448		dbg("header OOB, %d bytes", hdr_len - MIN_HEADER);
449		nc_ensure_sync(dev);
450		// switch (vendor/product ids) { ... }
451	}
452	skb_pull(skb, hdr_len);
453
454	trailer = (struct nc_trailer *)
455		(skb->data + skb->len - sizeof *trailer);
456	skb_trim(skb, skb->len - sizeof *trailer);
457
458	if ((packet_len & 0x01) == 0) {
459		if (skb->data [packet_len] != PAD_BYTE) {
460			dev->net->stats.rx_frame_errors++;
461			dbg("bad pad");
462			return 0;
463		}
464		skb_trim(skb, skb->len - 1);
465	}
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);
470		nc_ensure_sync(dev);
471		return 0;
472	}
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));
478		return 0;
479	}
480#if 0
481	netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
482		   header->packet_len, header->packet_id);
483#endif
484	dev->frame_errors = 0;
485	return 1;
486}
487
488static struct sk_buff *
489net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
490{
491	struct sk_buff		*skb2;
492	struct nc_header	*header = NULL;
493	struct nc_trailer	*trailer = NULL;
494	int			padlen = sizeof (struct nc_trailer);
495	int			len = skb->len;
496
497	if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
498		padlen++;
499	if (!skb_cloned(skb)) {
500		int	headroom = skb_headroom(skb);
501		int	tailroom = skb_tailroom(skb);
502
503		if (padlen <= tailroom &&
504		    sizeof(struct nc_header) <= headroom)
505			/* There's enough head and tail room */
506			goto encapsulate;
507
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);
515			goto encapsulate;
516		}
517	}
518
519	/* Create a new skb to use with the correct size */
520	skb2 = skb_copy_expand(skb,
521				sizeof (struct nc_header),
522				padlen,
523				flags);
524	dev_kfree_skb_any(skb);
525	if (!skb2)
526		return skb2;
527	skb = skb2;
528
529encapsulate:
530	/* header first */
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++);
535
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);
541#if 0
542	netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
543		   header->hdr_len, header->packet_len,
544		   header->packet_id);
545#endif
546	return skb;
547}
548
549static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
550{
551	unsigned	extra = sizeof (struct nc_header)
552				+ 1
553				+ sizeof (struct nc_trailer);
554
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);
559}
560
561static 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,
569};
570
571static const struct usb_device_id	products [] = {
572{
573	USB_DEVICE(0x0525, 0x1080),	// NetChip ref design
574	.driver_info =	(unsigned long) &net1080_info,
575}, {
576	USB_DEVICE(0x06D0, 0x0622),	// Laplink Gold
577	.driver_info =	(unsigned long) &net1080_info,
578},
579	{ },		// END
580};
581MODULE_DEVICE_TABLE(usb, products);
582
583static struct usb_driver net1080_driver = {
584	.name =		"net1080",
585	.id_table =	products,
586	.probe =	usbnet_probe,
587	.disconnect =	usbnet_disconnect,
588	.suspend =	usbnet_suspend,
589	.resume =	usbnet_resume,
 
590};
591
592static int __init net1080_init(void)
593{
594 	return usb_register(&net1080_driver);
595}
596module_init(net1080_init);
597
598static void __exit net1080_exit(void)
599{
600 	usb_deregister(&net1080_driver);
601}
602module_exit(net1080_exit);
603
604MODULE_AUTHOR("David Brownell");
605MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
606MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Net1080 based USB host-to-host cables
  4 * Copyright (C) 2000-2005 by David Brownell
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7// #define	DEBUG			// error path messages, extra info
  8// #define	VERBOSE			// more; success messages
  9
 10#include <linux/module.h>
 
 11#include <linux/netdevice.h>
 12#include <linux/etherdevice.h>
 13#include <linux/ethtool.h>
 14#include <linux/workqueue.h>
 15#include <linux/mii.h>
 16#include <linux/usb.h>
 17#include <linux/usb/usbnet.h>
 18#include <linux/slab.h>
 19
 20#include <linux/unaligned.h>
 21
 22
 23/*
 24 * Netchip 1080 driver ... http://www.netchip.com
 25 * (Sept 2004:  End-of-life announcement has been sent.)
 26 * Used in (some) LapLink cables
 27 */
 28
 29#define frame_errors	data[1]
 30
 31/*
 32 * NetChip framing of ethernet packets, supporting additional error
 33 * checks for links that may drop bulk packets from inside messages.
 34 * Odd USB length == always short read for last usb packet.
 35 *	- nc_header
 36 *	- Ethernet header (14 bytes)
 37 *	- payload
 38 *	- (optional padding byte, if needed so length becomes odd)
 39 *	- nc_trailer
 40 *
 41 * This framing is to be avoided for non-NetChip devices.
 42 */
 43
 44struct nc_header {		// packed:
 45	__le16	hdr_len;		// sizeof nc_header (LE, all)
 46	__le16	packet_len;		// payload size (including ethhdr)
 47	__le16	packet_id;		// detects dropped packets
 48#define MIN_HEADER	6
 49
 50	// all else is optional, and must start with:
 51	// __le16	vendorId;	// from usb-if
 52	// __le16	productId;
 53} __packed;
 54
 55#define	PAD_BYTE	((unsigned char)0xAC)
 56
 57struct nc_trailer {
 58	__le16	packet_id;
 59} __packed;
 60
 61// packets may use FLAG_FRAMING_NC and optional pad
 62#define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
 63				+ sizeof (struct ethhdr) \
 64				+ (mtu) \
 65				+ 1 \
 66				+ sizeof (struct nc_trailer))
 67
 68#define MIN_FRAMED	FRAMED_SIZE(0)
 69
 70/* packets _could_ be up to 64KB... */
 71#define NC_MAX_PACKET	32767
 72
 73
 74/*
 75 * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
 76 * before the hardware drops it.  If that's done, the driver will need to
 77 * frame network packets to guard against the dropped USB packets.  The win32
 78 * driver sets this for both sides of the link.
 79 */
 80#define	NC_READ_TTL_MS	((u8)255)	// ms
 81
 82/*
 83 * We ignore most registers and EEPROM contents.
 84 */
 85#define	REG_USBCTL	((u8)0x04)
 86#define REG_TTL		((u8)0x10)
 87#define REG_STATUS	((u8)0x11)
 88
 89/*
 90 * Vendor specific requests to read/write data
 91 */
 92#define	REQUEST_REGISTER	((u8)0x10)
 93#define	REQUEST_EEPROM		((u8)0x11)
 94
 95static int
 96nc_vendor_read(struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
 97{
 98	int status = usbnet_read_cmd(dev, req,
 99				     USB_DIR_IN | USB_TYPE_VENDOR |
100				     USB_RECIP_DEVICE,
101				     0, regnum, retval_ptr,
102				     sizeof *retval_ptr);
 
 
103	if (status > 0)
104		status = 0;
105	if (!status)
106		le16_to_cpus(retval_ptr);
107	return status;
108}
109
110static inline int
111nc_register_read(struct usbnet *dev, u8 regnum, u16 *retval_ptr)
112{
113	return nc_vendor_read(dev, REQUEST_REGISTER, regnum, retval_ptr);
114}
115
 
116static void
117nc_vendor_write(struct usbnet *dev, u8 req, u8 regnum, u16 value)
118{
119	usbnet_write_cmd(dev, req,
120			 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121			 value, regnum, NULL, 0);
 
 
 
 
122}
123
124static inline void
125nc_register_write(struct usbnet *dev, u8 regnum, u16 value)
126{
127	nc_vendor_write(dev, REQUEST_REGISTER, regnum, value);
128}
129
130
131#if 0
132static void nc_dump_registers(struct usbnet *dev)
133{
134	u8	reg;
135	u16	*vp = kmalloc(sizeof (u16));
136
137	if (!vp)
 
138		return;
 
139
140	netdev_dbg(dev->net, "registers:\n");
141	for (reg = 0; reg < 0x20; reg++) {
142		int retval;
143
144		// reading some registers is trouble
145		if (reg >= 0x08 && reg <= 0xf)
146			continue;
147		if (reg >= 0x12 && reg <= 0x1e)
148			continue;
149
150		retval = nc_register_read(dev, reg, vp);
151		if (retval < 0)
152			netdev_dbg(dev->net, "reg [0x%x] ==> error %d\n",
153				   reg, retval);
154		else
155			netdev_dbg(dev->net, "reg [0x%x] = 0x%x\n", reg, *vp);
 
156	}
157	kfree(vp);
158}
159#endif
160
161
162/*-------------------------------------------------------------------------*/
163
164/*
165 * Control register
166 */
167
168#define	USBCTL_WRITABLE_MASK	0x1f0f
169// bits 15-13 reserved, r/o
170#define	USBCTL_ENABLE_LANG	(1 << 12)
171#define	USBCTL_ENABLE_MFGR	(1 << 11)
172#define	USBCTL_ENABLE_PROD	(1 << 10)
173#define	USBCTL_ENABLE_SERIAL	(1 << 9)
174#define	USBCTL_ENABLE_DEFAULTS	(1 << 8)
175// bits 7-4 reserved, r/o
176#define	USBCTL_FLUSH_OTHER	(1 << 3)
177#define	USBCTL_FLUSH_THIS	(1 << 2)
178#define	USBCTL_DISCONN_OTHER	(1 << 1)
179#define	USBCTL_DISCONN_THIS	(1 << 0)
180
181static inline void nc_dump_usbctl(struct usbnet *dev, u16 usbctl)
182{
183	netif_dbg(dev, link, dev->net,
184		  "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s; this%s%s; other%s%s; r/o 0x%x\n",
185		  dev->udev->bus->bus_name, dev->udev->devpath,
186		  usbctl,
187		  (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
188		  (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
189		  (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
190		  (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
191		  (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
192
193		  (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
194		  (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
195
196		  (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
197		  (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
198
199		  usbctl & ~USBCTL_WRITABLE_MASK);
200}
201
202/*-------------------------------------------------------------------------*/
203
204/*
205 * Status register
206 */
207
208#define	STATUS_PORT_A		(1 << 15)
209
210#define	STATUS_CONN_OTHER	(1 << 14)
211#define	STATUS_SUSPEND_OTHER	(1 << 13)
212#define	STATUS_MAILBOX_OTHER	(1 << 12)
213#define	STATUS_PACKETS_OTHER(n)	(((n) >> 8) & 0x03)
214
215#define	STATUS_CONN_THIS	(1 << 6)
216#define	STATUS_SUSPEND_THIS	(1 << 5)
217#define	STATUS_MAILBOX_THIS	(1 << 4)
218#define	STATUS_PACKETS_THIS(n)	(((n) >> 0) & 0x03)
219
220#define	STATUS_UNSPEC_MASK	0x0c8c
221#define	STATUS_NOISE_MASK 	((u16)~(0x0303|STATUS_UNSPEC_MASK))
222
223
224static inline void nc_dump_status(struct usbnet *dev, u16 status)
225{
226	netif_dbg(dev, link, dev->net,
227		  "net1080 %s-%s status 0x%x: this (%c) PKT=%d%s%s%s; other PKT=%d%s%s%s; unspec 0x%x\n",
228		  dev->udev->bus->bus_name, dev->udev->devpath,
229		  status,
230
231		  // XXX the packet counts don't seem right
232		  // (1 at reset, not 0); maybe UNSPEC too
233
234		  (status & STATUS_PORT_A) ? 'A' : 'B',
235		  STATUS_PACKETS_THIS(status),
236		  (status & STATUS_CONN_THIS) ? " CON" : "",
237		  (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
238		  (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
239
240		  STATUS_PACKETS_OTHER(status),
241		  (status & STATUS_CONN_OTHER) ? " CON" : "",
242		  (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
243		  (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
244
245		  status & STATUS_UNSPEC_MASK);
246}
247
248/*-------------------------------------------------------------------------*/
249
250/*
251 * TTL register
252 */
253
 
254#define	TTL_OTHER(ttl)	(0x00ff & (ttl >> 8))
255#define MK_TTL(this,other)	((u16)(((other)<<8)|(0x00ff&(this))))
256
 
 
 
 
 
 
 
257/*-------------------------------------------------------------------------*/
258
259static int net1080_reset(struct usbnet *dev)
260{
261	u16		usbctl, status, ttl;
262	u16		vp;
263	int		retval;
264
 
 
 
265	// nc_dump_registers(dev);
266
267	if ((retval = nc_register_read(dev, REG_STATUS, &vp)) < 0) {
268		netdev_dbg(dev->net, "can't read %s-%s status: %d\n",
269			   dev->udev->bus->bus_name, dev->udev->devpath, retval);
270		goto done;
271	}
272	status = vp;
273	nc_dump_status(dev, status);
274
275	if ((retval = nc_register_read(dev, REG_USBCTL, &vp)) < 0) {
276		netdev_dbg(dev->net, "can't read USBCTL, %d\n", retval);
277		goto done;
278	}
279	usbctl = vp;
280	nc_dump_usbctl(dev, usbctl);
281
282	nc_register_write(dev, REG_USBCTL,
283			USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
284
285	if ((retval = nc_register_read(dev, REG_TTL, &vp)) < 0) {
286		netdev_dbg(dev->net, "can't read TTL, %d\n", retval);
287		goto done;
288	}
289	ttl = vp;
 
290
291	nc_register_write(dev, REG_TTL,
292			MK_TTL(NC_READ_TTL_MS, TTL_OTHER(ttl)) );
293	netdev_dbg(dev->net, "assigned TTL, %d ms\n", NC_READ_TTL_MS);
294
295	netif_info(dev, link, dev->net, "port %c, peer %sconnected\n",
296		   (status & STATUS_PORT_A) ? 'A' : 'B',
297		   (status & STATUS_CONN_OTHER) ? "" : "dis");
298	retval = 0;
299
300done:
 
301	return retval;
302}
303
304static int net1080_check_connect(struct usbnet *dev)
305{
306	int			retval;
307	u16			status;
308	u16			vp;
309
310	retval = nc_register_read(dev, REG_STATUS, &vp);
311	status = vp;
 
 
 
312	if (retval != 0) {
313		netdev_dbg(dev->net, "net1080_check_conn read - %d\n", retval);
314		return retval;
315	}
316	if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
317		return -ENOLINK;
318	return 0;
319}
320
 
 
 
 
 
 
321static void nc_ensure_sync(struct usbnet *dev)
322{
323	if (++dev->frame_errors <= 5)
324		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
326	if (usbnet_write_cmd_async(dev, REQUEST_REGISTER,
327					USB_DIR_OUT | USB_TYPE_VENDOR |
328					USB_RECIP_DEVICE,
329					USBCTL_FLUSH_THIS |
330					USBCTL_FLUSH_OTHER,
331					REG_USBCTL, NULL, 0))
332		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334	netif_dbg(dev, rx_err, dev->net,
335		  "flush net1080; too many framing errors\n");
336	dev->frame_errors = 0;
 
337}
338
339static int net1080_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
340{
341	struct nc_header	*header;
342	struct nc_trailer	*trailer;
343	u16			hdr_len, packet_len;
344
345	/* This check is no longer done by usbnet */
346	if (skb->len < dev->net->hard_header_len)
347		return 0;
348
349	if (!(skb->len & 0x01)) {
350		netdev_dbg(dev->net, "rx framesize %d range %d..%d mtu %d\n",
351			   skb->len, dev->net->hard_header_len, dev->hard_mtu,
352			   dev->net->mtu);
 
 
353		dev->net->stats.rx_frame_errors++;
354		nc_ensure_sync(dev);
355		return 0;
356	}
357
358	header = (struct nc_header *) skb->data;
359	hdr_len = le16_to_cpup(&header->hdr_len);
360	packet_len = le16_to_cpup(&header->packet_len);
361	if (FRAMED_SIZE(packet_len) > NC_MAX_PACKET) {
362		dev->net->stats.rx_frame_errors++;
363		netdev_dbg(dev->net, "packet too big, %d\n", packet_len);
364		nc_ensure_sync(dev);
365		return 0;
366	} else if (hdr_len < MIN_HEADER) {
367		dev->net->stats.rx_frame_errors++;
368		netdev_dbg(dev->net, "header too short, %d\n", hdr_len);
369		nc_ensure_sync(dev);
370		return 0;
371	} else if (hdr_len > MIN_HEADER) {
372		// out of band data for us?
373		netdev_dbg(dev->net, "header OOB, %d bytes\n", hdr_len - MIN_HEADER);
374		nc_ensure_sync(dev);
375		// switch (vendor/product ids) { ... }
376	}
377	skb_pull(skb, hdr_len);
378
379	trailer = (struct nc_trailer *)
380		(skb->data + skb->len - sizeof *trailer);
381	skb_trim(skb, skb->len - sizeof *trailer);
382
383	if ((packet_len & 0x01) == 0) {
384		if (skb->data [packet_len] != PAD_BYTE) {
385			dev->net->stats.rx_frame_errors++;
386			netdev_dbg(dev->net, "bad pad\n");
387			return 0;
388		}
389		skb_trim(skb, skb->len - 1);
390	}
391	if (skb->len != packet_len) {
392		dev->net->stats.rx_frame_errors++;
393		netdev_dbg(dev->net, "bad packet len %d (expected %d)\n",
394			   skb->len, packet_len);
395		nc_ensure_sync(dev);
396		return 0;
397	}
398	if (header->packet_id != get_unaligned(&trailer->packet_id)) {
399		dev->net->stats.rx_fifo_errors++;
400		netdev_dbg(dev->net, "(2+ dropped) rx packet_id mismatch 0x%x 0x%x\n",
401			   le16_to_cpu(header->packet_id),
402			   le16_to_cpu(trailer->packet_id));
403		return 0;
404	}
405#if 0
406	netdev_dbg(dev->net, "frame <rx h %d p %d id %d\n", header->hdr_len,
407		   header->packet_len, header->packet_id);
408#endif
409	dev->frame_errors = 0;
410	return 1;
411}
412
413static struct sk_buff *
414net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
415{
416	struct sk_buff		*skb2;
417	struct nc_header	*header = NULL;
418	struct nc_trailer	*trailer = NULL;
419	int			padlen = sizeof (struct nc_trailer);
420	int			len = skb->len;
421
422	if (!((len + padlen + sizeof (struct nc_header)) & 0x01))
423		padlen++;
424	if (!skb_cloned(skb)) {
425		int	headroom = skb_headroom(skb);
426		int	tailroom = skb_tailroom(skb);
427
428		if (padlen <= tailroom &&
429		    sizeof(struct nc_header) <= headroom)
430			/* There's enough head and tail room */
431			goto encapsulate;
432
433		if ((sizeof (struct nc_header) + padlen) <
434				(headroom + tailroom)) {
435			/* There's enough total room, so just readjust */
436			skb->data = memmove(skb->head
437						+ sizeof (struct nc_header),
438					    skb->data, skb->len);
439			skb_set_tail_pointer(skb, len);
440			goto encapsulate;
441		}
442	}
443
444	/* Create a new skb to use with the correct size */
445	skb2 = skb_copy_expand(skb,
446				sizeof (struct nc_header),
447				padlen,
448				flags);
449	dev_kfree_skb_any(skb);
450	if (!skb2)
451		return skb2;
452	skb = skb2;
453
454encapsulate:
455	/* header first */
456	header = skb_push(skb, sizeof *header);
457	header->hdr_len = cpu_to_le16(sizeof (*header));
458	header->packet_len = cpu_to_le16(len);
459	header->packet_id = cpu_to_le16((u16)dev->xid++);
460
461	/* maybe pad; then trailer */
462	if (!((skb->len + sizeof *trailer) & 0x01))
463		skb_put_u8(skb, PAD_BYTE);
464	trailer = skb_put(skb, sizeof *trailer);
465	put_unaligned(header->packet_id, &trailer->packet_id);
466#if 0
467	netdev_dbg(dev->net, "frame >tx h %d p %d id %d\n",
468		   header->hdr_len, header->packet_len,
469		   header->packet_id);
470#endif
471	return skb;
472}
473
474static int net1080_bind(struct usbnet *dev, struct usb_interface *intf)
475{
476	unsigned	extra = sizeof (struct nc_header)
477				+ 1
478				+ sizeof (struct nc_trailer);
479
480	dev->net->hard_header_len += extra;
481	dev->rx_urb_size = dev->net->hard_header_len + dev->net->mtu;
482	dev->hard_mtu = NC_MAX_PACKET;
483	return usbnet_get_endpoints (dev, intf);
484}
485
486static const struct driver_info	net1080_info = {
487	.description =	"NetChip TurboCONNECT",
488	.flags =	FLAG_POINTTOPOINT | FLAG_FRAMING_NC,
489	.bind =		net1080_bind,
490	.reset =	net1080_reset,
491	.check_connect = net1080_check_connect,
492	.rx_fixup =	net1080_rx_fixup,
493	.tx_fixup =	net1080_tx_fixup,
494};
495
496static const struct usb_device_id	products [] = {
497{
498	USB_DEVICE(0x0525, 0x1080),	// NetChip ref design
499	.driver_info =	(unsigned long) &net1080_info,
500}, {
501	USB_DEVICE(0x06D0, 0x0622),	// Laplink Gold
502	.driver_info =	(unsigned long) &net1080_info,
503},
504	{ },		// END
505};
506MODULE_DEVICE_TABLE(usb, products);
507
508static struct usb_driver net1080_driver = {
509	.name =		"net1080",
510	.id_table =	products,
511	.probe =	usbnet_probe,
512	.disconnect =	usbnet_disconnect,
513	.suspend =	usbnet_suspend,
514	.resume =	usbnet_resume,
515	.disable_hub_initiated_lpm = 1,
516};
517
518module_usb_driver(net1080_driver);
 
 
 
 
 
 
 
 
 
 
519
520MODULE_AUTHOR("David Brownell");
521MODULE_DESCRIPTION("NetChip 1080 based USB Host-to-Host Links");
522MODULE_LICENSE("GPL");