Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
/*
 * NetChip 1080 Driver (USB Host-to-Host Link)
 * Copyright (C) 2000 by David Brownell <dbrownell@users.sourceforge.net>
 */

/*
 * This talks to the NetChip 1080, which can appear in "network cables"
 * and other designs.  This driver interoperates with the Win32 network
 * drivers from NetChip, using the NetChip reference design.
 *
 * The IP-over-USB protocol here may be of interest.  Embedded devices
 * could implement it at the cost of two bulk endpoints, and whatever
 * other system resources the desired IP-based applications need.
 * Some Linux palmtops could support that today.  (Devices that don't
 * support the TTL-driven data mangling of the net1080 chip won't need
 * the header/trailer support though.)
 * 
 * STATUS:
 *
 * 13-sept-2000		experimental, new
 *
 * This doesn't yet do any network hotplugging, and there's no matching
 * ifup policy script ... it should arrange bridging with "brctl", and
 * should handle static and dynamic ("pump") setups.
 *
 * RX/TX queue sizes currently fixed at one due to URB unlink problems.
 *
 * 10-oct-2000
 * usb_device_id table created. 
 *
 * 28-oct-2000
 * misc fixes; mostly, discard more TTL-mangled rx packets.
 *
 * 01-nov-2000
 * usb_device_id table support added by Adam J. Richter <adam@yggdrasil.com>.
 * 
 * 08-apr-2001 gb
 * Identify version on module load.
 * 
 *-------------------------------------------------------------------------*/

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/random.h>
#include <asm/unaligned.h>

#define DEBUG				// error path messages
// #define VERBOSE			// more; success messages
#define USE_TTL				// timeout our reads

#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
#   define DEBUG
#endif
#include <linux/usb.h>

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.0.0"
#define DRIVER_AUTHOR "David Brownell <dbrownell@users.sourceforge.net>"
#define DRIVER_DESC "NetChip 1080 Driver (USB Host-to-Host Link)"


static const struct usb_device_id	products [] = {
	// reference design
	{ USB_DEVICE(0x1080, 0x525), 
	  driver_info: (unsigned long) "NetChip TurboCONNECT" },
	// Belkin, ...
	{ },		// END
};

MODULE_DEVICE_TABLE (usb, products);

static u8	node_id [ETH_ALEN];


/*-------------------------------------------------------------------------
 *
 * NetChip protocol:  ethernet framing, and only use bulk endpoints (01/81;
 * not mailboxes 02/82 or status interrupt 83).  Expects Ethernet bridging.
 * Odd USB length == always short read.
 *	- nc_header
 *	- payload, in Ethernet framing (14 byte header etc)
 *	- (optional padding byte, if needed so length is odd)
 *	- nc_trailer
 */

struct nc_header {
	u16	hdr_len;		// sizeof nc_header (LE, all)
	u16	packet_len;		// packet size
	u16	packet_id;		// detects dropped packets
#define NC_MIN_HEADER	6

	// all else is optional, and must start with:
	// u16	vendorId;		// from usb-if
	// u16	productId;
};

#define	NC_PAD_BYTE	((unsigned char)0xAC)

struct nc_trailer {
	u16	packet_id;
};

// packetsize == f(mtu setting), with upper limit
#define NC_MAX_PACKET(mtu) (sizeof (struct nc_header) \
				+ (mtu) \
				+ 1 \
				+ sizeof (struct nc_trailer))
#define MAX_PACKET	8191

// zero means no timeout; else, how long a 64 byte bulk
// read may be queued before HW flushes it.
#define	NC_READ_TTL	((u8)255)	// ms


/*-------------------------------------------------------------------------*/

// list of all devices we manage
static DECLARE_MUTEX (net1080_mutex);
static LIST_HEAD (net1080_list);


// Nineteen USB 1.1 max size bulk transactions per frame, max.
#if 0
#define	RX_QLEN		4
#define	TX_QLEN		4

#else
// unlink_urbs() has probs on OHCI without test8-pre patches.
#define	RX_QLEN		1
#define	TX_QLEN		1
#endif

enum skb_state {
	illegal = 0,
	tx_start, tx_done,
	rx_start, rx_done, rx_cleanup
};

struct skb_data {	// skb->cb is one of these
	struct urb		*urb;
	struct net1080		*dev;
	enum skb_state		state;
	size_t			length;
};


struct net1080 {
	// housekeeping
	struct usb_device	*udev;
	const struct usb_device_id	*prod_info;
	struct semaphore	mutex;
	struct list_head	dev_list;
	wait_queue_head_t	*wait;

	// protocol/interface state
	struct net_device	net;
	struct net_device_stats	stats;
	u16			packet_id;

	// various kinds of pending driver work
	struct sk_buff_head	rxq;
	struct sk_buff_head	txq;
	struct sk_buff_head	done;
	struct tasklet_struct	bh;
};

#define	mutex_lock(x)	down(x)
#define	mutex_unlock(x)	up(x)

static void defer_bh (struct net1080 *dev, struct sk_buff *skb)
{
	unsigned long	flags;

	skb_unlink (skb);
	spin_lock_irqsave (&dev->done.lock, flags);
	__skb_queue_tail (&dev->done, skb);
	if (dev->done.qlen == 1)
		tasklet_schedule (&dev->bh);
	spin_unlock_irqrestore (&dev->done.lock, flags);
}

/*-------------------------------------------------------------------------
 *
 * We ignore most registers and EEPROM contents.
 */

#define	REG_USBCTL	((u8)0x04)
#define REG_TTL		((u8)0x10)
#define REG_STATUS	((u8)0x11)

/*
 * Vendor specific requests to read/write data
 */

#define	REQUEST_REGISTER	((u8)0x10)
#define	REQUEST_EEPROM		((u8)0x11)

#define	CONTROL_TIMEOUT		(500)			/* msec */

static int
vendor_read (struct net1080 *dev, u8 req, u8 regnum, u16 *retval_ptr)
{
	int status = usb_control_msg (dev->udev,
		usb_rcvctrlpipe (dev->udev, 0),
		req,
		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		0, regnum,
		retval_ptr, sizeof *retval_ptr,
		CONTROL_TIMEOUT);
	if (status > 0)
		status = 0;
	if (!status)
		le16_to_cpus (retval_ptr);
	return status;
}

static inline int
register_read (struct net1080 *dev, u8 regnum, u16 *retval_ptr)
{
	return vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
}

// without retval, this can become fully async (usable in_interrupt)
static void
vendor_write (struct net1080 *dev, u8 req, u8 regnum, u16 value)
{
	usb_control_msg (dev->udev,
		usb_sndctrlpipe (dev->udev, 0),
		req,
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value, regnum,
		0, 0,			// data is in setup packet
		CONTROL_TIMEOUT);
}

static inline void
register_write (struct net1080 *dev, u8 regnum, u16 value)
{
	vendor_write (dev, REQUEST_REGISTER, regnum, value);
}


#if 0
static void dump_registers (struct net1080 *dev)
{
	u8	reg;
	u16	value;

	dbg ("%s registers:", dev->net.name);
	for (reg = 0; reg < 0x20; reg++) {
		int retval;

		// reading some registers is trouble
		if (reg >= 0x08 && reg <= 0xf)
			continue;
		if (reg >= 0x12 && reg <= 0x1e)
			continue;

		retval = register_read (dev, reg, &value);
		if (retval < 0)
			dbg ("%s reg [0x%x] ==> error %d",
				dev->net.name, reg, retval);
		else
			dbg ("%s reg [0x%x] = 0x%x",
				dev->net.name, reg, value);
	}
}
#endif


/*-------------------------------------------------------------------------
 *
 * Control register
 */

#define	USBCTL_WRITABLE_MASK	0x1f0f
// bits 15-13 reserved, r/o
#define	USBCTL_ENABLE_LANG	(1 << 12)
#define	USBCTL_ENABLE_MFGR	(1 << 11)
#define	USBCTL_ENABLE_PROD	(1 << 10)
#define	USBCTL_ENABLE_SERIAL	(1 << 9)
#define	USBCTL_ENABLE_DEFAULTS	(1 << 8)
// bits 7-4 reserved, r/o
#define	USBCTL_FLUSH_OTHER	(1 << 3)
#define	USBCTL_FLUSH_THIS	(1 << 2)
#define	USBCTL_DISCONN_OTHER	(1 << 1)
#define	USBCTL_DISCONN_THIS	(1 << 0)

#ifdef DEBUG
static void dump_usbctl (struct net1080 *dev, u16 usbctl)
{
	dbg ("%s: USB %d dev %d usbctl 0x%x:%s%s%s%s%s;"
			" this%s%s;"
			" other%s%s; r/o 0x%x",
		dev->net.name,
		dev->udev->bus->busnum, dev->udev->devnum,
		usbctl,
		(usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
		(usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
		(usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
		(usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
		(usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",

		(usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
		(usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
		(usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
		(usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
		usbctl & ~USBCTL_WRITABLE_MASK
		);
}
#else
static inline void dump_usbctl (struct net1080 *dev, u16 usbctl) {}
#endif

/*-------------------------------------------------------------------------
 *
 * Status register
 */

#define	STATUS_PORT_A		(1 << 15)

#define	STATUS_CONN_OTHER	(1 << 14)
#define	STATUS_SUSPEND_OTHER	(1 << 13)
#define	STATUS_MAILBOX_OTHER	(1 << 12)
#define	STATUS_PACKETS_OTHER(n)	(((n) >> 8) && 0x03)

#define	STATUS_CONN_THIS	(1 << 6)
#define	STATUS_SUSPEND_THIS	(1 << 5)
#define	STATUS_MAILBOX_THIS	(1 << 4)
#define	STATUS_PACKETS_THIS(n)	(((n) >> 0) && 0x03)

#define	STATUS_UNSPEC_MASK	0x0c8c
#define	STATUS_NOISE_MASK 	((u16)~(0x0303|STATUS_UNSPEC_MASK))


#ifdef DEBUG
static void dump_status (struct net1080 *dev, u16 status)
{
	dbg ("%s: USB %d dev %d status 0x%x:"
			" this (%c) PKT=%d%s%s%s;"
			" other PKT=%d%s%s%s; unspec 0x%x",
		dev->net.name,
		dev->udev->bus->busnum, dev->udev->devnum,
		status,

		// XXX the packet counts don't seem right
		// (1 at reset, not 0); maybe UNSPEC too

		(status & STATUS_PORT_A) ? 'A' : 'B',
		STATUS_PACKETS_THIS (status),
		(status & STATUS_CONN_THIS) ? " CON" : "",
		(status & STATUS_SUSPEND_THIS) ? " SUS" : "",
		(status & STATUS_MAILBOX_THIS) ? " MBOX" : "",

		STATUS_PACKETS_OTHER (status),
		(status & STATUS_CONN_OTHER) ? " CON" : "",
		(status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
		(status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",

		status & STATUS_UNSPEC_MASK
		);
}
#else
static inline void dump_status (struct net1080 *dev, u16 status) {}
#endif

/*-------------------------------------------------------------------------
 *
 * TTL register
 */

#define	TTL_THIS(ttl)	(0x00ff & ttl)
#define	TTL_OTHER(ttl)	(0x00ff & (ttl >> 8))
#define MK_TTL(this,other)	((u16)(((other)<<8)|(0x00ff&(this))))

#ifdef DEBUG
static void dump_ttl (struct net1080 *dev, u16 ttl)
{
	dbg ("%s: USB %d dev %d ttl 0x%x this = %d, other = %d",
		dev->net.name,
		dev->udev->bus->busnum, dev->udev->devnum,
		ttl,

		TTL_THIS (ttl),
		TTL_OTHER (ttl)
		);
}
#else
static inline void dump_ttl (struct net1080 *dev, u16 ttl) {}
#endif

#define	RUN_CONTEXT (in_irq () ? "in_irq" \
			: (in_interrupt () ? "in_interrupt" : "can sleep"))

/*-------------------------------------------------------------------------*/

// ensure that the device is in a known state before using it.

// preconditions:
//	caller owns the device mutex
//	caller has a process context

static int net1080_reset (struct net1080 *dev)
{
	u16		usbctl, status, ttl;
	int		retval;

	if ((retval = register_read (dev, REG_STATUS, &status)) < 0) {
		dbg ("can't read dev %d status: %d", dev->udev->devnum, retval);
		goto done;
	}
	dump_status (dev, status);

	if ((retval = register_read (dev, REG_USBCTL, &usbctl)) < 0) {
		dbg ("can't read USBCTL, %d", retval);
		goto done;
	}
	dump_usbctl (dev, usbctl);

	register_write (dev, REG_USBCTL,
			USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);

	if ((retval = register_read (dev, REG_TTL, &ttl)) < 0) {
		dbg ("can't read TTL, %d", retval);
		goto done;
	}
	dump_ttl (dev, ttl);

#ifdef	USE_TTL
	// Have the chip flush reads that seem to be starving for read
	// bandwidth ... or we're otherwise reading.  Note, Win32 drivers
	// may change our read TTL for us.

	register_write (dev, REG_TTL,
			MK_TTL (NC_READ_TTL, TTL_OTHER (ttl)) );
	dbg ("%s: assigned TTL, %d ms", dev->net.name, NC_READ_TTL);
#endif

	info ("%s: %s, port %c on USB %d dev %d, peer %sconnected",
		dev->net.name, (char *) dev->prod_info->driver_info,
		(status & STATUS_PORT_A) ? 'A' : 'B',
		dev->udev->bus->busnum,
		dev->udev->devnum,
		(status & STATUS_CONN_OTHER) ? "" : "dis"
		);
	retval = 0;

done:
	return retval;
}


/*-------------------------------------------------------------------------
 *
 * Network Device Driver support (peer link to USB Host)
 *
 --------------------------------------------------------------------------*/

static int net1080_change_mtu (struct net_device *net, int new_mtu)
{
	if ((new_mtu < 0) || NC_MAX_PACKET (new_mtu) > MAX_PACKET)
		return -EINVAL;
	net->mtu = new_mtu;
	return 0;
}

/*-------------------------------------------------------------------------*/

static struct net_device_stats *net1080_get_stats (struct net_device *net)
{
	return &((struct net1080 *) net->priv)->stats;
}

/*-------------------------------------------------------------------------*/

static void rx_complete (struct urb *urb);

static void rx_submit (struct net1080 *dev, struct urb *urb, int flags)
{
	struct sk_buff		*skb;
	struct skb_data		*entry;
	int			retval = 0;
	unsigned long		lockflags;

	if ((skb = alloc_skb (NC_MAX_PACKET (dev->net.mtu), flags)) == 0) {
		err ("no rx skb");
		tasklet_schedule (&dev->bh);
		usb_free_urb (urb);
		return;
	}

	entry = (struct skb_data *) skb->cb;
	entry->urb = urb;
	entry->dev = dev;
	entry->state = rx_start;
	entry->length = 0;

	FILL_BULK_URB (urb, dev->udev, usb_rcvbulkpipe (dev->udev, 1),
			skb->data, skb->truesize, rx_complete, skb);
	urb->transfer_flags |= USB_QUEUE_BULK;

	spin_lock_irqsave (&dev->rxq.lock, lockflags);
	if (!netif_queue_stopped (&dev->net)) {
		if ((retval = usb_submit_urb (urb)) != 0) {
			err ("%s rx submit, %d", dev->net.name, retval);
			tasklet_schedule (&dev->bh);
		} else {
			__skb_queue_tail (&dev->rxq, skb);
		}
	} else {
		dbg ("rx: stopped");
		retval = -ENOLINK;
	}
	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
	if (retval) {
		dev_kfree_skb_any (skb);
		usb_free_urb (urb);
	}
}


/*-------------------------------------------------------------------------*/

static void rx_complete (struct urb *urb)
{
	struct sk_buff		*skb = (struct sk_buff *) urb->context;
	struct skb_data		*entry = (struct skb_data *) skb->cb;
	struct net1080		*dev = entry->dev;
	int			urb_status = urb->status;

	urb->dev = 0;
	skb->len = urb->actual_length;
	entry->state = rx_done;
	entry->urb = 0;

	if ((urb->transfer_flags & USB_ASYNC_UNLINK) != 0) {
		dbg ("rx ... shutting down");
		usb_free_urb (urb);
		urb = 0;
	}

	switch (urb_status) {
	    // success
	    case 0:
		if (!(skb->len & 0x01)) {
			entry->state = rx_cleanup;
			dev->stats.rx_errors++;
			dev->stats.rx_length_errors++;
			dbg ("even rx len %d", skb->len);
		} else if (skb->len > MAX_PACKET) {
			entry->state = rx_cleanup;
			dev->stats.rx_errors++;
			dev->stats.rx_frame_errors++;
			dbg ("rx too big, %d", skb->len);
		}
		break;

	    // hardware-reported interface shutdown ... which we
	    // typically see before khubd calls disconnect()
	    case -ETIMEDOUT:		// usb-ohci
	    case -EILSEQ:		// *uhci ... "crc"/timeout error
		// netif_device_detach (&dev->net);
		// FALLTHROUGH
			
	    // software-driven interface shutdown
	    case -ECONNRESET:
		entry->state = rx_cleanup;
		usb_free_urb (urb);
		urb = 0;
		dbg ("%s ... shutdown rx (%d)", dev->net.name, urb_status);
		break;

	    // data overrun ... flush fifo?
	    case -EOVERFLOW:
		dev->stats.rx_over_errors++;
		// FALLTHROUGH
	    
	    default:
		entry->state = rx_cleanup;
		dev->stats.rx_errors++;
		err ("%s rx: status %d", dev->net.name, urb_status);
		break;
	}
	defer_bh (dev, skb);

	if (urb) {
		if (!netif_queue_stopped (&dev->net)) {
			rx_submit (dev, urb, GFP_ATOMIC);
			return;
		} else
			usb_free_urb (urb);
	}
#ifdef	VERBOSE
	dbg ("no read resubmitted");
#endif	/* VERBOSE */
}

/*-------------------------------------------------------------------------*/

// unlink pending rx/tx; completion handlers do all other cleanup

static int unlink_urbs (struct sk_buff_head *q)
{
	unsigned long		flags;
	struct sk_buff		*skb;
	struct skb_data		*entry;
	int			retval;
	int			count = 0;

	spin_lock_irqsave (&q->lock, flags);
	for (skb = q->next; skb != (struct sk_buff *) q; skb = skb->next) {
		entry = (struct skb_data *) skb->cb;
		entry->urb->transfer_flags |= USB_ASYNC_UNLINK;
		retval = usb_unlink_urb (entry->urb);
		if (retval < 0)
			dbg ("unlink urb err, %d", retval);
		else
			count++;
	}
	spin_unlock_irqrestore (&q->lock, flags);
	return count;
}


/*-------------------------------------------------------------------------*/

// precondition: never called in_interrupt

static int net1080_stop (struct net_device *net)
{
	struct net1080		*dev = (struct net1080 *) net->priv;
	int			temp;
	DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
	DECLARE_WAITQUEUE (wait, current);

	mutex_lock (&dev->mutex);
	
	dbg ("%s stop stats: rx/tx %ld/%ld, errs %ld/%ld", net->name,
		dev->stats.rx_packets, dev->stats.tx_packets, 
		dev->stats.rx_errors, dev->stats.tx_errors
		);

	netif_stop_queue(net);

	// ensure there are no more active urbs
	add_wait_queue (&unlink_wakeup, &wait);
	dev->wait = &unlink_wakeup;
	temp = unlink_urbs (&dev->txq) + unlink_urbs (&dev->rxq);

	// maybe wait for deletions to finish.
	if (temp) {
		current->state = TASK_UNINTERRUPTIBLE;
		schedule ();
		dbg ("waited for %d urb completions", temp);
	}
	dev->wait = 0;
	current->state = TASK_RUNNING;
	remove_wait_queue (&unlink_wakeup, &wait); 

	mutex_unlock (&dev->mutex);
	MOD_DEC_USE_COUNT;
	return 0;
}

/*-------------------------------------------------------------------------*/

// posts a read, and enables write queing

// precondition: never called in_interrupt

static int net1080_open (struct net_device *net)
{
	struct net1080		*dev = (struct net1080 *) net->priv;
	int			retval;
	u16			status;
	int			i;

	MOD_INC_USE_COUNT;
	mutex_lock (&dev->mutex);

	// insist peer be connected -- is this the best place?
	if ((retval = register_read (dev, REG_STATUS, &status)) != 0) {
		dbg ("%s open: status read failed - %d", net->name, retval);
		goto done;
	}
	if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)  {
		retval = -ENOLINK;
		dbg ("%s open: peer not connected", net->name);
		goto done;
	}

	MOD_INC_USE_COUNT;
	netif_start_queue (net);
	for (i = 0; i < RX_QLEN; i++)
		rx_submit (dev, usb_alloc_urb (0), GFP_KERNEL);

	dbg ("%s open: started queueing (rx %d, tx %d)",
		net->name, RX_QLEN, TX_QLEN);
done:
	mutex_unlock (&dev->mutex);
	MOD_DEC_USE_COUNT;
	return retval;
}

/*-------------------------------------------------------------------------*/

static void tx_complete (struct urb *urb)
{
	struct sk_buff		*skb = (struct sk_buff *) urb->context;
	struct skb_data		*entry = (struct skb_data *) skb->cb;
	struct net1080		*dev = entry->dev;

	urb->dev = 0;
	entry->state = tx_done;
	defer_bh (dev, skb);
	netif_wake_queue (&dev->net);
}

/*-------------------------------------------------------------------------*/

static struct sk_buff *fixup_skb (struct sk_buff *skb)
{
	int			padlen;
	struct sk_buff		*skb2;

	padlen = ((skb->len + sizeof (struct nc_header)
			+ sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
	if (!skb_cloned (skb)) {
		int	headroom = skb_headroom (skb);
		int	tailroom = skb_tailroom (skb);

		if ((padlen + sizeof (struct nc_trailer)) <= tailroom
			    && sizeof (struct nc_header) <= headroom)
			return skb;

		if ((sizeof (struct nc_header) + padlen
					+ sizeof (struct nc_trailer)) <
				(headroom + tailroom)) {
			skb->data = memmove (skb->head
						+ sizeof (struct nc_header),
					    skb->data, skb->len);
			skb->tail = skb->data + skb->len;
			return skb;
		}
	}
	skb2 = skb_copy_expand (skb,
				sizeof (struct nc_header),
				sizeof (struct nc_trailer) + padlen,
				in_interrupt () ? GFP_ATOMIC : GFP_KERNEL);
	dev_kfree_skb_any (skb);
	return skb2;
}

/*-------------------------------------------------------------------------*/

static int net1080_start_xmit (struct sk_buff *skb, struct net_device *net)
{
	struct net1080		*dev = (struct net1080 *) net->priv;
	int			length = skb->len;
	int			retval = 0;
	struct urb		*urb = 0;
	struct skb_data		*entry;
	struct nc_header	*header;
	struct nc_trailer	*trailer;
	unsigned long		flags;

	if ((skb = fixup_skb (skb)) == 0) {
		dbg ("can't fixup skb");
		goto drop;
	}
	if ((urb = usb_alloc_urb (0)) == 0) {
		dbg ("no urb");
		goto drop;
	}

	entry = (struct skb_data *) skb->cb;
	entry->urb = urb;
	entry->dev = dev;
	entry->state = tx_start;
	entry->length = length;

	header = (struct nc_header *) skb_push (skb, sizeof *header);
	header->hdr_len = cpu_to_le16 (sizeof (*header));
	header->packet_len = cpu_to_le16 (length);
	if (!((skb->len + sizeof *trailer) & 0x01))
		*skb_put (skb, 1) = NC_PAD_BYTE;
	trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);

	FILL_BULK_URB (urb, dev->udev,
			usb_sndbulkpipe (dev->udev, 1),
			skb->data, skb->len, tx_complete, skb);
	urb->transfer_flags |= USB_QUEUE_BULK;
	// FIXME urb->timeout = ...;

	spin_lock_irqsave (&dev->txq.lock, flags);
	if (!netif_queue_stopped (&dev->net)) {
		header->packet_id = cpu_to_le16 (dev->packet_id++);
		put_unaligned (header->packet_id, &trailer->packet_id);

		netif_stop_queue (net);
		if ((retval = usb_submit_urb (urb)) != 0) {
			netif_start_queue (net);
			dbg ("%s tx: submit urb err %d", net->name, retval);
		} else {
			net->trans_start = jiffies;
			__skb_queue_tail (&dev->txq, skb);
			if (dev->txq.qlen < TX_QLEN)
				netif_start_queue (net);
		}
	} else
		retval = -ENOLINK;
	spin_unlock_irqrestore (&dev->txq.lock, flags);

	if (retval) {
		dbg ("drop");
drop:
		dev->stats.tx_dropped++;
		dev_kfree_skb_any (skb);
		usb_free_urb (urb);
#ifdef	VERBOSE
	} else {
		dbg ("%s: tx %p len %d", net->name, skb, length);
#endif
	}
	return retval;
}


/*-------------------------------------------------------------------------*/

static void rx_process (struct net1080 *dev, struct sk_buff *skb)
{
	struct nc_header	*header;
	struct nc_trailer	*trailer;

	header = (struct nc_header *) skb->data;
	le16_to_cpus (&header->hdr_len);
	le16_to_cpus (&header->packet_len);
	if (header->packet_len > MAX_PACKET) {
		dev->stats.rx_frame_errors++;
		dbg ("packet too big, %d", header->packet_len);
		goto error;
	} else if (header->hdr_len < NC_MIN_HEADER) {
		dev->stats.rx_frame_errors++;
		dbg ("header too short, %d", header->hdr_len);
		goto error;
	} else if (header->hdr_len > header->packet_len) {
		dev->stats.rx_frame_errors++;
		dbg ("header too big, %d packet %d", header->hdr_len, header->packet_len);
		goto error;
	} else if (header->hdr_len != sizeof *header) {
		// out of band data for us?
		dbg ("header OOB, %d bytes", header->hdr_len - NC_MIN_HEADER);
		// switch (vendor/product ids) { ... }
	}
	skb_pull (skb, header->hdr_len);

	trailer = (struct nc_trailer *)
		(skb->data + skb->len - sizeof *trailer);
	skb_trim (skb, skb->len - sizeof *trailer);

	if ((header->packet_len & 0x01) == 0) {
		if (skb->data [header->packet_len] != NC_PAD_BYTE) {
			dev->stats.rx_frame_errors++;
			dbg ("bad pad");
			goto error;
		}
		skb_trim (skb, skb->len - 1);
	}
	if (skb->len != header->packet_len) {
		dev->stats.rx_length_errors++;
		dbg ("bad packet len %d (expected %d)",
			skb->len, header->packet_len);
		goto error;
	}
	if (header->packet_id != get_unaligned (&trailer->packet_id)) {
		dev->stats.rx_fifo_errors++;
		dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
			header->packet_id, trailer->packet_id);
		goto error;
	}

	if (skb->len) {
		skb->dev = &dev->net;
		skb->protocol = eth_type_trans (skb, &dev->net);
		dev->stats.rx_packets++;
		dev->stats.rx_bytes += skb->len;

#ifdef	VERBOSE
		dbg ("%s: rx %p len %d, type 0x%x, id 0x%x",
			dev->net.name, skb, skb->len, skb->protocol,
			le16_to_cpu (header->packet_id));
#endif
		netif_rx (skb);
	} else {
		dbg ("drop");
error:
		dev->stats.rx_errors++;
		dev_kfree_skb (skb);
	}
}

/*-------------------------------------------------------------------------*/

// tasklet

// We can have a state machine in this tasklet monitor the link state,
// using async control messaging and calling attach/detach routines.

// But then some listener ought to respond to the changes; do those
// network attach/detach notifications get to userland somehow, such
// as by calling "ifup usb0" and "ifdown usb0"?

static void net1080_bh (unsigned long param)
{
	struct net1080		*dev = (struct net1080 *) param;
	struct sk_buff		*skb;
	struct skb_data		*entry;

	while ((skb = skb_dequeue (&dev->done))) {
		entry = (struct skb_data *) skb->cb;
		switch (entry->state) {
		    case rx_done:
			rx_process (dev, skb);
			continue;
		    case tx_done:
			if (entry->urb->status) {
				// can this statistic become more specific?
				dev->stats.tx_errors++;
				dbg ("%s tx: err %d", dev->net.name,
					entry->urb->status);
			} else {
				dev->stats.tx_packets++;
				dev->stats.tx_bytes += entry->length;
			}
			// FALLTHROUGH:
		    case rx_cleanup:
			usb_free_urb (entry->urb);
			dev_kfree_skb (skb);
			continue;
		    default:
			dbg ("%s: bogus skb state %d",
				dev->net.name, entry->state);
		}
	}

	// waiting for all pending urbs to complete?
	if (dev->wait) {
		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
			wake_up (dev->wait);
		}

	// or are we maybe short a few urbs?
	} else if (!netif_queue_stopped (&dev->net)) {
		if (dev->rxq.qlen < TX_QLEN) {
			struct urb	*urb;
			int		i;
			for (i = 0; i < 3 && dev->rxq.qlen < TX_QLEN; i++) {
				if ((urb = usb_alloc_urb (0)) != 0)
					rx_submit (dev, urb, GFP_ATOMIC);
			}
			dbg ("%s: rxqlen now %d",
				dev->net.name, dev->rxq.qlen);
		}
	}
}

/*-------------------------------------------------------------------------
 *
 * USB Device Driver support
 *
 --------------------------------------------------------------------------*/
 
// precondition: never called in_interrupt

static void net1080_disconnect (struct usb_device *udev, void *ptr)
{
	struct net1080	*dev = (struct net1080 *) ptr;

	info ("%s: USB %d dev %d, %s, disconnected",
		dev->net.name,
		udev->bus->busnum, udev->devnum,
		(char *) dev->prod_info->driver_info);
	
	unregister_netdev (&dev->net);

	mutex_lock (&net1080_mutex);
	mutex_lock (&dev->mutex);
	list_del (&dev->dev_list);
	mutex_unlock (&net1080_mutex);

#ifdef DEBUG
	memset (dev, 0x55, sizeof *dev);
#endif
	kfree (dev);
	usb_dec_dev_use (udev);
}


/*-------------------------------------------------------------------------*/

// precondition: never called in_interrupt

static void *
net1080_probe (struct usb_device *udev, unsigned ifnum, const struct usb_device_id *prod)
{
	struct net1080		*dev;
	struct net_device 	*net;
	struct usb_interface_descriptor	*interface;
	int			retval;

	// sanity check; expect dedicated interface/devices for now.
	interface = &udev->actconfig->interface [ifnum].altsetting[0];
	if (udev->descriptor.bNumConfigurations != 1
			|| udev->config[0].bNumInterfaces != 1
			|| udev->config[0].bNumInterfaces != 1
			|| interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC
			|| interface->bNumEndpoints != 5
			) {
		dbg ("Bogus config info");
		return 0;
	}

	// set up our own records
	if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
		dbg ("can't kmalloc dev");
		return 0;
	}
	memset (dev, 0, sizeof *dev);

	init_MUTEX_LOCKED (&dev->mutex);
	usb_inc_dev_use (udev);
	dev->udev = udev;
	dev->prod_info = prod;
	INIT_LIST_HEAD (&dev->dev_list);
	skb_queue_head_init (&dev->rxq);
	skb_queue_head_init (&dev->txq);
	skb_queue_head_init (&dev->done);
	dev->bh.func = net1080_bh;
	dev->bh.data = (unsigned long) dev;

	// set up network interface records
	net = &dev->net;
	net->priv = dev;
	strcpy (net->name, "usb%d");
	memcpy (net->dev_addr, node_id, sizeof node_id);

	ether_setup (net);
	// net->flags |= IFF_POINTOPOINT;

	net->change_mtu = net1080_change_mtu;
	net->get_stats = net1080_get_stats;
	net->hard_start_xmit = net1080_start_xmit;
	net->open = net1080_open;
	net->stop = net1080_stop;

	register_netdev (&dev->net);

	// ... talk to the device
	// dump_registers (dev);

	if ((retval = net1080_reset (dev)) < 0) {
		err ("%s: init reset fail on USB %d dev %d - %d",
			dev->net.name, udev->bus->busnum, udev->devnum, retval);
		mutex_unlock (&dev->mutex);
		net1080_disconnect (udev, dev);
		return 0;
	}

	// ok, it's ready to go.
	mutex_lock (&net1080_mutex);
	list_add (&dev->dev_list, &net1080_list);
	mutex_unlock (&dev->mutex);

	// start as if the link is up
	netif_device_attach (&dev->net);

	mutex_unlock (&net1080_mutex);

	return dev;
}


/*-------------------------------------------------------------------------*/

static struct usb_driver net1080_driver = {
	name:		"net1080",
	id_table:	products,
	probe:		net1080_probe,
	disconnect:	net1080_disconnect,
};

/*-------------------------------------------------------------------------*/

static int __init net1080_init (void)
{
	// compiler should optimize this out
	if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
		BUG ();

 	if (usb_register (&net1080_driver) < 0)
 		return -1;

	get_random_bytes (node_id, sizeof node_id);
	node_id [0] &= 0x7f;

	info(DRIVER_VERSION ":" DRIVER_DESC);

	return 0;
}
module_init (net1080_init);

static void __exit net1080_exit (void)
{
 	usb_deregister (&net1080_driver);
}
module_exit (net1080_exit);

MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );