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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
// Portions of this file taken from
// Petko Manolov - Petkan (petkan@dce.bg)
// from his driver pegasus.c

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/types.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/usb.h>
#include <linux/module.h>
#include "cdc-ether.h"

static const char *version = __FILE__ ": v0.98.5 22 Sep 2001 Brad Hards and another";

/* Take any CDC device, and sort it out in probe() */
static struct usb_device_id CDCEther_ids[] = {
	{ USB_DEVICE_INFO(USB_CLASS_COMM, 0, 0) },
	{ } /* Terminating null entry */
};

/* 
 * module parameter that provides an alternate upper limit on the 
 * number of multicast filters we use, with a default to use all
 * the filters available to us. Note that the actual number used
 * is the lesser of this parameter and the number returned in the
 * descriptor for the particular device. See Table 41 of the CDC
 * spec for more info on the descriptor limit.
 */
static int multicast_filter_limit = 32767;


//////////////////////////////////////////////////////////////////////////////
// Callback routines from USB device /////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static void read_bulk_callback( struct urb *urb )
{
	ether_dev_t *ether_dev = urb->context;
	struct net_device *net;
	int count = urb->actual_length, res;
	struct sk_buff	*skb;

	// Sanity check 
	if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
		dbg("BULK IN callback but driver is not active!");
		return;
	}

	net = ether_dev->net;
	if ( !netif_device_present(net) ) {
		// Somebody killed our network interface...
		return;
	}

	if ( ether_dev->flags & CDC_ETHER_RX_BUSY ) {
		// Are we already trying to receive a frame???
		ether_dev->stats.rx_errors++;
		dbg("ether_dev Rx busy");
		return;
	}

	// We are busy, leave us alone!
	ether_dev->flags |= CDC_ETHER_RX_BUSY;

	switch ( urb->status ) {
		case 0:
			break;
		case -ETIMEDOUT:
			dbg( "no repsonse in BULK IN" );
			ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
			break;
		default:
			dbg( "%s: RX status %d", net->name, urb->status );
			goto goon;
	}

	// Check to make sure we got some data...
	if ( !count ) {
		// We got no data!!!
		goto goon;
	}

	// Tell the kernel we want some memory
	if ( !(skb = dev_alloc_skb(count)) ) {
		// We got no receive buffer.
		goto goon;
	}

	// Here's where it came from
	skb->dev = net;
	
	// Now we copy it over
	eth_copy_and_sum(skb, ether_dev->rx_buff, count, 0);
	
	// Not sure
	skb_put(skb, count);
	// Not sure here either
	skb->protocol = eth_type_trans(skb, net);
	
	// Ship it off to the kernel
	netif_rx(skb);
	
	// update out statistics
	ether_dev->stats.rx_packets++;
	ether_dev->stats.rx_bytes += count;

goon:
	// Prep the USB to wait for another frame
	FILL_BULK_URB( ether_dev->rx_urb, ether_dev->usb,
			usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
			ether_dev->rx_buff, ether_dev->wMaxSegmentSize, 
			read_bulk_callback, ether_dev );

	// Give this to the USB subsystem so it can tell us
	// when more data arrives.
	if ( (res = usb_submit_urb(ether_dev->rx_urb, GFP_KERNEL)) ) {
		warn("%s failed submint rx_urb %d", __FUNCTION__, res);
	}

	// We are no longer busy, show us the frames!!!
	ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
}

static void write_bulk_callback( struct urb *urb )
{
	ether_dev_t *ether_dev = urb->context;

	// Sanity check
	if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
		// We are insane!!!
		err( "write_bulk_callback: device not running" );
		return;
	}

	// Do we still have a valid kernel network device?
	if ( !netif_device_present(ether_dev->net) ) {
		// Someone killed our network interface.
		err( "write_bulk_callback: net device not present" );
		return;
	}

	// Hmm...  What on Earth could have happened???
	if ( urb->status ) {
		info("%s: TX status %d", ether_dev->net->name, urb->status);
	}

	// Update the network interface and tell it we are
	// ready for another frame
	ether_dev->net->trans_start = jiffies;
	netif_wake_queue( ether_dev->net );
}

//static void intr_callback( struct urb *urb )
//{
//	ether_dev_t *ether_dev = urb->context;
//	struct net_device *net;
//	__u8	*d;
//
//	if ( !ether_dev )
//		return;
//		
//	switch ( urb->status ) {
//		case 0:
//			break;
//		case -ENOENT:
//			return;
//		default:
//			info("intr status %d", urb->status);
//	}
//
//	d = urb->transfer_buffer;
//	net = ether_dev->net;
//	if ( d[0] & 0xfc ) {
//		ether_dev->stats.tx_errors++;
//		if ( d[0] & TX_UNDERRUN )
//			ether_dev->stats.tx_fifo_errors++;
//		if ( d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT) )
//			ether_dev->stats.tx_aborted_errors++;
//		if ( d[0] & LATE_COL )
//			ether_dev->stats.tx_window_errors++;
//		if ( d[0] & (NO_CARRIER | LOSS_CARRIER) )
//			ether_dev->stats.tx_carrier_errors++;
//	}
//}

//////////////////////////////////////////////////////////////////////////////
// Routines for turning net traffic on and off on the USB side ///////////////
//////////////////////////////////////////////////////////////////////////////

static inline int enable_net_traffic( ether_dev_t *ether_dev )
{
	struct usb_device *usb = ether_dev->usb;

	// Here would be the time to set the data interface to the configuration where
	// it has two endpoints that use a protocol we can understand.

	if (usb_set_interface( usb, 
	                        ether_dev->data_bInterfaceNumber, 
	                        ether_dev->data_bAlternateSetting_with_traffic ) )  {
		err("usb_set_interface() failed" );
		err("Attempted to set interface %d", ether_dev->data_bInterfaceNumber);
		err("To alternate setting       %d", ether_dev->data_bAlternateSetting_with_traffic);
		return -1;
	}
	return 0;
}

static inline void disable_net_traffic( ether_dev_t *ether_dev )
{
	// The thing to do is to set the data interface to the alternate setting that has
	// no endpoints.  This is what the spec suggests.

	if (ether_dev->data_interface_altset_num_without_traffic >= 0 ) {
		if (usb_set_interface( ether_dev->usb, 
		                        ether_dev->data_bInterfaceNumber, 
		                        ether_dev->data_bAlternateSetting_without_traffic ) ) 	{
			err("usb_set_interface() failed");
		}
	} else {
		// Some devices just may not support this...
		warn("No way to disable net traffic");
	}
}

//////////////////////////////////////////////////////////////////////////////
// Callback routines for kernel Ethernet Device //////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static void CDCEther_tx_timeout( struct net_device *net )
{
	ether_dev_t *ether_dev = net->priv;

	// Sanity check
	if ( !ether_dev ) {
		// Seems to be a case of insanity here
		return;
	}

	// Tell syslog we are hosed.
	warn("%s: Tx timed out.", net->name);
	
	// Tear the waiting frame off the list
	ether_dev->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
	usb_unlink_urb( ether_dev->tx_urb );
	
	// Update statistics
	ether_dev->stats.tx_errors++;
}

static int CDCEther_start_xmit( struct sk_buff *skb, struct net_device *net )
{
	ether_dev_t	*ether_dev = net->priv;
	int 	count;
	int 	res;

	// If we are told to transmit an ethernet frame that fits EXACTLY 
	// into an integer number of USB packets, we force it to send one 
	// more byte so the device will get a runt USB packet signalling the 
	// end of the ethernet frame
	if ( (skb->len) ^ (ether_dev->data_ep_out_size) ) {
		// It was not an exact multiple
		// no need to add anything extra
		count = skb->len;
	} else {
		// Add one to make it NOT an exact multiple
		count = skb->len + 1;
	}

	// Tell the kernel, "No more frames 'til we are done
	// with this one.'
	netif_stop_queue( net );

	// Copy it from kernel memory to OUR memory
	memcpy(ether_dev->tx_buff, skb->data, skb->len);

	// Fill in the URB for shipping it out.
	FILL_BULK_URB( ether_dev->tx_urb, ether_dev->usb,
			usb_sndbulkpipe(ether_dev->usb, ether_dev->data_ep_out),
			ether_dev->tx_buff, ether_dev->wMaxSegmentSize, 
			write_bulk_callback, ether_dev );

	// Tell the URB how much it will be transporting today
	ether_dev->tx_urb->transfer_buffer_length = count;
	
	// Send the URB on its merry way.
	if ((res = usb_submit_urb(ether_dev->tx_urb, GFP_KERNEL)))  {
		// Hmm...  It didn't go. Tell someone...
		warn("failed tx_urb %d", res);
		// update some stats...
		ether_dev->stats.tx_errors++;
		// and tell the kernel to give us another.
		// Maybe we'll get it right next time.
		netif_start_queue( net );
	} else {
		// Okay, it went out.
		// Update statistics
		ether_dev->stats.tx_packets++;
		ether_dev->stats.tx_bytes += skb->len;
		// And tell the kernel when the last transmit occurred.
		net->trans_start = jiffies;
	}

	// We are done with the kernel's memory
	dev_kfree_skb(skb);

	// We are done here.
	return 0;
}

static struct net_device_stats *CDCEther_netdev_stats( struct net_device *net )
{
	// Easy enough!
	return &((ether_dev_t *)net->priv)->stats;
}

static int CDCEther_open(struct net_device *net)
{
	ether_dev_t *ether_dev = (ether_dev_t *)net->priv;
	int	res;

	// Turn on the USB and let the packets flow!!!
	if ( (res = enable_net_traffic( ether_dev )) ) {
		err("%s can't enable_net_traffic() - %d", __FUNCTION__, res );
		return -EIO;
	}

	// Prep a receive URB
	FILL_BULK_URB( ether_dev->rx_urb, ether_dev->usb,
			usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
			ether_dev->rx_buff, ether_dev->wMaxSegmentSize, 
			read_bulk_callback, ether_dev );

	// Put it out there so the device can send us stuff
	if ( (res = usb_submit_urb(ether_dev->rx_urb, GFP_KERNEL)) )
	{
		// Hmm...  Okay...
		warn("%s failed rx_urb %d", __FUNCTION__, res );
	}

	// Tell the kernel we are ready to start receiving from it
	netif_start_queue( net );
	
	// We are up and running.
	ether_dev->flags |= CDC_ETHER_RUNNING;

	// Let's get ready to move frames!!!
	return 0;
}

static int CDCEther_close( struct net_device *net )
{
	ether_dev_t	*ether_dev = net->priv;

	// We are no longer running.
	ether_dev->flags &= ~CDC_ETHER_RUNNING;
	
	// Tell the kernel to stop sending us stuff
	netif_stop_queue( net );
	
	// If we are not already unplugged, turn off USB
	// traffic
	if ( !(ether_dev->flags & CDC_ETHER_UNPLUG) ) {
		disable_net_traffic( ether_dev );
	}

	// We don't need the URBs anymore.
	usb_unlink_urb( ether_dev->rx_urb );
	usb_unlink_urb( ether_dev->tx_urb );
	usb_unlink_urb( ether_dev->intr_urb );
	
	// That's it.  I'm done.
	return 0;
}

static int CDCEther_ioctl( struct net_device *net, struct ifreq *rq, int cmd )
{
	//__u16 *data = (__u16 *)&rq->ifr_data;
	//ether_dev_t	*ether_dev = net->priv;

	// No support here yet.
	// Do we need support???
	switch(cmd) {
		case SIOCDEVPRIVATE:
			return -EOPNOTSUPP;
		case SIOCDEVPRIVATE+1:
			return -EOPNOTSUPP;
		case SIOCDEVPRIVATE+2:
			//return 0;
			return -EOPNOTSUPP;
		default:
			return -EOPNOTSUPP;
	}
}

#if 0
static void CDC_SetEthernetPacketFilter (ether_dev_t *ether_dev)
{
	usb_control_msg(ether_dev->usb,
			usb_sndctrlpipe(ether_dev->usb, 0),
			SET_ETHERNET_PACKET_FILTER, /* request */
			USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, /* request type */
			cpu_to_le16(ether_dev->mode_flags), /* value */
			cpu_to_le16((u16)ether_dev->comm_interface), /* index */
			NULL,
			0, /* size */
			HZ); /* timeout */
}
#endif


static void CDCEther_set_multicast( struct net_device *net )
{
	ether_dev_t *ether_dev = net->priv;
	int i;
	__u8 *buff;


	// Tell the kernel to stop sending us frames while we get this
	// all set up.
//	netif_stop_queue(net);

// FIXME: We hold xmit_lock. If you want to do the queue stuff you need
//	  to enable it from a completion handler

      /* Note: do not reorder, GCC is clever about common statements. */
        if (net->flags & IFF_PROMISC) {
                /* Unconditionally log net taps. */
                info( "%s: Promiscuous mode enabled", net->name);
		ether_dev->mode_flags = MODE_FLAG_PROMISCUOUS |
			MODE_FLAG_ALL_MULTICAST |
			MODE_FLAG_DIRECTED |
			MODE_FLAG_BROADCAST |
			MODE_FLAG_MULTICAST;
        } else if (net->mc_count > ether_dev->wNumberMCFilters) {
                /* Too many to filter perfectly -- accept all multicasts. */
		info("%s: set too many MC filters, using allmulti", net->name);
		ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
			MODE_FLAG_DIRECTED |
			MODE_FLAG_BROADCAST |
			MODE_FLAG_MULTICAST;
	} else if (net->flags & IFF_ALLMULTI) {
                /* Filter in software */
		info("%s: using allmulti", net->name);
		ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
			MODE_FLAG_DIRECTED |
			MODE_FLAG_BROADCAST |
			MODE_FLAG_MULTICAST;
        } else {
		/* do multicast filtering in hardware */
                struct dev_mc_list *mclist;
		info("%s: set multicast filters", net->name);
		ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
			MODE_FLAG_DIRECTED |
			MODE_FLAG_BROADCAST |
			MODE_FLAG_MULTICAST;
		buff = kmalloc(6 * net->mc_count, GFP_ATOMIC);
                for (i = 0, mclist = net->mc_list;
		     mclist && i < net->mc_count;
                     i++, mclist = mclist->next) {
			memcpy(&mclist->dmi_addr, &buff[i * 6], 6);
		}
#if 0
		usb_control_msg(ether_dev->usb,
// FIXME: We hold a spinlock. You must not use a synchronous API
				usb_sndctrlpipe(ether_dev->usb, 0),
				SET_ETHERNET_MULTICAST_FILTER, /* request */
				USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, /* request type */
				cpu_to_le16(net->mc_count), /* value */
				cpu_to_le16((u16)ether_dev->comm_interface), /* index */
				buff,
				(6* net->mc_count), /* size */
				HZ); /* timeout */
#endif
		kfree(buff);
	}

#if 0 
	CDC_SetEthernetPacketFilter(ether_dev);
#endif	
        // Tell the kernel to start giving frames to us again.
//	netif_wake_queue(net);
}

//////////////////////////////////////////////////////////////////////////////
// Routines used to parse out the Functional Descriptors /////////////////////
//////////////////////////////////////////////////////////////////////////////

static int parse_header_functional_descriptor( int *bFunctionLength, 
                                               int bDescriptorType, 
                                               int bDescriptorSubtype,
                                               unsigned char *data,
                                               ether_dev_t *ether_dev,
                                               int *requirements )
{
	// Check to make sure we haven't seen one of these already.
	if ( (~*requirements) & REQ_HDR_FUNC_DESCR ) {
		err( "Multiple Header Functional Descriptors found." );
		return -1;
	}
	
	// Is it the right size???
	if (*bFunctionLength != 5) {
		info( "Invalid length in Header Functional Descriptor" );
		// This is a hack to get around a particular device (NO NAMES)
		// It has this function length set to the length of the
		// whole class-specific descriptor
		*bFunctionLength = 5;
	}
	
	// Nothing extremely useful here.
	// We'll keep it for posterity
	ether_dev->bcdCDC = data[0] + (data[1] << 8);
	dbg( "Found Header descriptor, CDC version %x", ether_dev->bcdCDC);

	// We've seen one of these
	*requirements &= ~REQ_HDR_FUNC_DESCR;
	
	// It's all good.
	return 0;
}

static int parse_union_functional_descriptor( int *bFunctionLength, 
                                              int bDescriptorType, 
                                              int bDescriptorSubtype,
                                              unsigned char *data,
                                              ether_dev_t *ether_dev,
                                              int *requirements )
{
	// Check to make sure we haven't seen one of these already.
	if ( (~*requirements) & REQ_UNION_FUNC_DESCR ) {
		err( "Multiple Union Functional Descriptors found." );
		return -1;
	}

	// Is it the right size?
	if (*bFunctionLength != 5) {
		// It is NOT the size we expected.
		err( "Unsupported length in Union Functional Descriptor" );
		return -1;
	}
	
	// Sanity check of sorts
	if (ether_dev->comm_interface != data[0]) {
		// This tells us that we are chasing the wrong comm
		// interface or we are crazy or something else weird.
		if (ether_dev->comm_interface == data[1]) {
			info( "Probably broken Union descriptor, fudging data interface" );
			// We'll need this in a few microseconds, 
			// so guess here, and hope for the best
			ether_dev->data_interface = data[0];
		} else {
			err( "Union Functional Descriptor is broken beyond repair" );
			return -1;
		}
	} else{ // Descriptor is OK
       		// We'll need this in a few microseconds!
		ether_dev->data_interface = data[1];
	}

	// We've seen one of these now.
	*requirements &= ~REQ_UNION_FUNC_DESCR;
	
	// Done
	return 0;
}

static int parse_ethernet_functional_descriptor( int *bFunctionLength, 
                                                 int bDescriptorType, 
                                                 int bDescriptorSubtype,
                                                 unsigned char *data,
                                                 ether_dev_t *ether_dev,
                                                 int *requirements )
{
	// Check to make sure we haven't seen one of these already.
	if ( (~*requirements) & REQ_ETH_FUNC_DESCR ) {
		err( "Multiple Ethernet Functional Descriptors found." );
		return -1;
	}
	
	// Is it the right size?
	if (*bFunctionLength != 13) {
		err( "Invalid length in Ethernet Networking Functional Descriptor" );
		return -1;
	}
	
	// Lots of goodies from this one.  They are all important.
	ether_dev->iMACAddress = data[0];
	ether_dev->bmEthernetStatistics = data[1] + (data[2] << 8) + (data[3] << 16) + (data[4] << 24);
	ether_dev->wMaxSegmentSize = data[5] + (data[6] << 8);
	ether_dev->wNumberMCFilters = (data[7] + (data[8] << 8)) & 0x00007FFF;
	if (ether_dev->wNumberMCFilters > multicast_filter_limit) {
		ether_dev->wNumberMCFilters = multicast_filter_limit;
		}	
	ether_dev->bNumberPowerFilters = data[9];
	
	// We've seen one of these now.
	*requirements &= ~REQ_ETH_FUNC_DESCR;
	
	// That's all she wrote.
	return 0;
}

static int parse_protocol_unit_functional_descriptor( int *bFunctionLength, 
                                                      int bDescriptorType, 
                                                      int bDescriptorSubtype,
                                                      unsigned char *data,
                                                      ether_dev_t *ether_dev,
                                                      int *requirements )
{
	// There should only be one type if we are sane
	if (bDescriptorType != CS_INTERFACE) {
		info( "Invalid bDescriptorType found." );
		return -1;
	}

	// The Subtype tells the tale.
	switch (bDescriptorSubtype){
		case 0x00:	// Header Functional Descriptor
			return parse_header_functional_descriptor( bFunctionLength,
			                                           bDescriptorType,
			                                           bDescriptorSubtype,
			                                           data,
			                                           ether_dev,
			                                           requirements );
			break;
		case 0x06:	// Union Functional Descriptor
			return parse_union_functional_descriptor( bFunctionLength,
			                                          bDescriptorType,
			                                          bDescriptorSubtype,
			                                          data,
			                                          ether_dev,
			                                          requirements );
			break;
		case 0x0F:	// Ethernet Networking Functional Descriptor
			return parse_ethernet_functional_descriptor( bFunctionLength,
			                                             bDescriptorType,
			                                             bDescriptorSubtype,
			                                             data,
			                                             ether_dev,
			                                             requirements );
			break;
		default:	// We don't support this at this time...
			// However that doesn't necessarily indicate an error.
			dbg( "Unexpected header type %x:", bDescriptorSubtype );
			return 0;
	}
	// How did we get here???
	return -1;
}

static int parse_ethernet_class_information( unsigned char *data, int length, ether_dev_t *ether_dev )
{
	int loc = 0;
	int rc;
	int bFunctionLength;
	int bDescriptorType;
	int bDescriptorSubtype;
	int requirements = REQUIREMENTS_TOTAL;

	// As long as there is something here, we will try to parse it
	while (loc < length) {
		// Length
		bFunctionLength = data[loc];
		loc++;
		
		// Type
		bDescriptorType = data[loc];
		loc++;
		
		// Subtype
		bDescriptorSubtype = data[loc];
		loc++;
		
		// ship this off to be processed elsewhere.
		rc = parse_protocol_unit_functional_descriptor( &bFunctionLength, 
		                                                bDescriptorType, 
		                                                bDescriptorSubtype, 
		                                                &data[loc],
		                                                ether_dev,
		                                                &requirements );
		// Did it process okay?
		if (rc)	{
			// Something was hosed somewhere.
			// No need to continue;
			err("Bad descriptor parsing: %x", rc );
			return -1;
		}
		// We have already taken three bytes.
		loc += (bFunctionLength - 3);
	}
	// Check to see if we got everything we need.
	if (requirements) {
		// We missed some of the requirements...
		err( "Not all required functional descriptors present 0x%08X", requirements );
		return -1;
	}
	// We got everything.
	return 0;
}

//////////////////////////////////////////////////////////////////////////////
// Routine to check for the existence of the Functional Descriptors //////////
//////////////////////////////////////////////////////////////////////////////

static int find_and_parse_ethernet_class_information( struct usb_device *device, ether_dev_t *ether_dev )
{
	struct usb_config_descriptor *conf = NULL;
	struct usb_interface *comm_intf_group = NULL;
	struct usb_interface_descriptor *comm_intf = NULL;
	int rc = -1;
	// The assumption here is that find_ethernet_comm_interface
	// and find_valid_configuration 
	// have already filled in the information about where to find
	// the a valid commication interface.

	conf = &( device->config[ether_dev->configuration_num] );
	comm_intf_group = &( conf->interface[ether_dev->comm_interface] );
	comm_intf = &( comm_intf_group->altsetting[ether_dev->comm_interface_altset_num] );
	// Let's check and see if it has the extra information we need...

	if (comm_intf->extralen > 0) {
		// This is where the information is SUPPOSED to be.
		rc = parse_ethernet_class_information( comm_intf->extra, comm_intf->extralen, ether_dev );
	} else if (conf->extralen > 0) {
		// This is a hack.  The spec says it should be at the interface 
		// location checked above.  However I have seen it here also.
		// This is the same device that requires the functional descriptor hack above
		warn( "Ethernet information found at device configuration.  This is broken." );
		rc = parse_ethernet_class_information( conf->extra, conf->extralen, ether_dev );
	} else 	{
		// I don't know where else to look.
		warn( "No ethernet information found." );
		rc = -1;
	}
	return rc;
}

//////////////////////////////////////////////////////////////////////////////
// Routines to verify the data interface /////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static int get_data_interface_endpoints( struct usb_device *device, ether_dev_t *ether_dev )
{
	struct usb_config_descriptor *conf = NULL;
	struct usb_interface *data_intf_group = NULL;
	struct usb_interface_descriptor *data_intf = NULL;
	
	// Walk through and get to the data interface we are checking.
	conf = &( device->config[ether_dev->configuration_num] );
	data_intf_group = &( conf->interface[ether_dev->data_interface] );
	data_intf = &( data_intf_group->altsetting[ether_dev->data_interface_altset_num_with_traffic] );

	// Start out assuming we won't find anything we can use
	ether_dev->data_ep_in = 0;
	ether_dev->data_ep_out = 0;
	
	// If these are not BULK endpoints, we don't want them
	if ( data_intf->endpoint[0].bmAttributes != 0x02 ) {
		return -1;
	} if ( data_intf->endpoint[1].bmAttributes != 0x02 ) {
		return -1;
	}

	// Check the first endpoint to see if it is IN or OUT
	if ( data_intf->endpoint[0].bEndpointAddress & 0x80 ) {
		// This endpoint is IN
		ether_dev->data_ep_in = data_intf->endpoint[0].bEndpointAddress & 0x7F;
	} else {
		// This endpoint is OUT
		ether_dev->data_ep_out = data_intf->endpoint[0].bEndpointAddress & 0x7F;
		ether_dev->data_ep_out_size = data_intf->endpoint[0].wMaxPacketSize;
	}

	// Check the second endpoint to see if it is IN or OUT
	if ( data_intf->endpoint[1].bEndpointAddress & 0x80 ) {
		// This endpoint is IN
		ether_dev->data_ep_in = data_intf->endpoint[1].bEndpointAddress & 0x7F;
	} else	{
		// This endpoint is OUT
		ether_dev->data_ep_out = data_intf->endpoint[1].bEndpointAddress & 0x7F;
		ether_dev->data_ep_out_size = data_intf->endpoint[1].wMaxPacketSize;
	}
	
	// Now make sure we got both an IN and an OUT
	if (ether_dev->data_ep_in && ether_dev->data_ep_out) {
		// We did get both, we are in good shape...
		info( "detected BULK OUT packets of size %d", ether_dev->data_ep_out_size );
		return 0;
	}
	return -1;
}

static int verify_ethernet_data_interface( struct usb_device *device, ether_dev_t *ether_dev )
{
	struct usb_config_descriptor *conf = NULL;
	struct usb_interface *data_intf_group = NULL;
	struct usb_interface_descriptor *data_intf = NULL;
	int rc = -1;
	int status;
	int altset_num;

	// The assumption here is that parse_ethernet_class_information()
	// and find_valid_configuration() 
	// have already filled in the information about where to find
	// a data interface
	conf = &( device->config[ether_dev->configuration_num] );
	data_intf_group = &( conf->interface[ether_dev->data_interface] );

	// start out assuming we won't find what we are looking for.
	ether_dev->data_interface_altset_num_with_traffic = -1;
	ether_dev->data_bAlternateSetting_with_traffic = -1;
	ether_dev->data_interface_altset_num_without_traffic = -1;
	ether_dev->data_bAlternateSetting_without_traffic = -1;

	// Walk through every possible setting for this interface until
	// we find what makes us happy.
	for ( altset_num = 0; altset_num < data_intf_group->num_altsetting; altset_num++ ) {
		data_intf = &( data_intf_group->altsetting[altset_num] );

		// Is this a data interface we like?
		if ( ( data_intf->bInterfaceClass == 0x0A )
		   && ( data_intf->bInterfaceSubClass == 0x00 )
		   && ( data_intf->bInterfaceProtocol == 0x00 ) ) {
			if ( data_intf->bNumEndpoints == 2 ) {
				// We are required to have one of these.
				// An interface with 2 endpoints to send Ethernet traffic back and forth
				// It actually may be possible that the device might only
				// communicate in a vendor specific manner.
				// That would not be very nice.
				// We can add that one later.
				ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
				ether_dev->data_interface_altset_num_with_traffic = altset_num;
				ether_dev->data_bAlternateSetting_with_traffic = data_intf->bAlternateSetting;
				status = get_data_interface_endpoints( device, ether_dev );
				if (!status) {
					rc = 0;
				}
			}
			if ( data_intf->bNumEndpoints == 0 ) {
				// According to the spec we are SUPPOSED to have one of these
				// In fact the device is supposed to come up in this state.
				// However, I have seen a device that did not have such an interface.
				// So it must be just optional for our driver...
				ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
				ether_dev->data_interface_altset_num_without_traffic = altset_num;
				ether_dev->data_bAlternateSetting_without_traffic = data_intf->bAlternateSetting;
			}
		}
	}
	return rc;
}

//////////////////////////////////////////////////////////////////////////////
// Routine to find a communication interface /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static int find_ethernet_comm_interface( struct usb_device *device, ether_dev_t *ether_dev )
{
	struct usb_config_descriptor *conf = NULL;
	struct usb_interface *comm_intf_group = NULL;
	struct usb_interface_descriptor *comm_intf = NULL;
	int intf_num;
	int altset_num;
	int rc;

	conf = &( device->config[ether_dev->configuration_num] );

	// We need to check and see if any of these interfaces are something we want.
	// Walk through each interface one at a time
	for ( intf_num = 0; intf_num < conf->bNumInterfaces; intf_num++ ) {
		comm_intf_group = &( conf->interface[intf_num] );
		// Now for each of those interfaces, check every possible
		// alternate setting.
		for ( altset_num = 0; altset_num < comm_intf_group->num_altsetting; altset_num++ ) {
			comm_intf = &( comm_intf_group->altsetting[altset_num] );

			// Is this a communication class of interface of the
			// ethernet subclass variety.
			if ( ( comm_intf->bInterfaceClass == 0x02 )
			   && ( comm_intf->bInterfaceSubClass == 0x06 )
			   && ( comm_intf->bInterfaceProtocol == 0x00 ) ) {
				if ( comm_intf->bNumEndpoints == 1 ) {
					// Good, we found one, we will try this one
					// Fill in the structure...
					ether_dev->comm_interface = intf_num;
					ether_dev->comm_bInterfaceNumber = comm_intf->bInterfaceNumber;
					ether_dev->comm_interface_altset_num = altset_num;
					ether_dev->comm_bAlternateSetting = comm_intf->bAlternateSetting;

					// Look for the Ethernet Functional Descriptors
					rc = find_and_parse_ethernet_class_information( device, ether_dev );
					if (rc) {
						// Nope this was no good after all.
						continue;
					}

					// Check that we really can talk to the data
					// interface 
					// This includes # of endpoints, protocols,
					// etc.
					rc = verify_ethernet_data_interface( device, ether_dev );
					if (rc)	{
						// We got something we didn't like
						continue;
					}
					// This communication interface seems to give us everything
					// we require.  We have all the ethernet info we need.
					// Let's get out of here and go home right now.
					return 0;
				} else {
                                        // bNumEndPoints != 1
					// We found an interface that had the wrong number of 
					// endpoints but would have otherwise been okay
				} // end bNumEndpoints check.
			} // end interface specifics check.
		} // end for altset_num
	} // end for intf_num
	return -1;
}

//////////////////////////////////////////////////////////////////////////////
// Routine to go through all configurations and find one that ////////////////
// is an Ethernet Networking Device //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static int find_valid_configuration( struct usb_device *device, ether_dev_t *ether_dev )
{
	struct usb_config_descriptor *conf = NULL;
	int conf_num;
	int rc;

	// We will try each and every possible configuration
	for ( conf_num = 0; conf_num < device->descriptor.bNumConfigurations; conf_num++ ) {
		conf = &( device->config[conf_num] );

		// Our first requirement : 2 interfaces
		if ( conf->bNumInterfaces != 2 ) {
			// I currently don't know how to handle devices with any number of interfaces
			// other than 2.
			continue;
		}

		// This one passed our first check, fill in some 
		// useful data
		ether_dev->configuration_num = conf_num;
		ether_dev->bConfigurationValue = conf->bConfigurationValue;

		// Now run it through the ringers and see what comes
		// out the other side.
		rc = find_ethernet_comm_interface( device, ether_dev );

		// Check if we found an ethernet Communcation Device
		if ( !rc ) {
			// We found one.
			return 0;
		}
	}
	// None of the configurations suited us.
	return -1;
}

//////////////////////////////////////////////////////////////////////////////
// Routine that checks a given configuration to see if any driver ////////////
// has claimed any of the devices interfaces /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static int check_for_claimed_interfaces( struct usb_config_descriptor *config )
{
	struct usb_interface *comm_intf_group;
	int intf_num;

	// Go through all the interfaces and make sure none are 
	// claimed by anybody else.
	for ( intf_num = 0; intf_num < config->bNumInterfaces; intf_num++ ) {
		comm_intf_group = &( config->interface[intf_num] );
		if ( usb_interface_claimed( comm_intf_group ) )	{
			// Somebody has beat us to this guy.
			// We can't change the configuration out from underneath of whoever
			// is using this device, so we will go ahead and give up.
			return -1;
		}
	}
	// We made it all the way through.
	// I guess no one has claimed any of these interfaces.
	return 0;
}

//////////////////////////////////////////////////////////////////////////////
// Routines to ask for and set the kernel network interface's MAC address ////
// Used by driver's probe routine ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static inline unsigned char hex2dec( unsigned char digit )
{
	// Is there a standard way to do this???
	// I have written this code TOO MANY times.
	if ( (digit >= '0') && (digit <= '9') )	{
		return (digit - '0');
	}
	if ( (digit >= 'a') && (digit <= 'f') )	{
		return (digit - 'a' + 10);
	}
	if ( (digit >= 'A') && (digit <= 'F') )	{
		return (digit - 'A' + 10);
	}
	return 0;
}

static void set_ethernet_addr( ether_dev_t *ether_dev )
{
	unsigned char	mac_addr[6];
	int		i;
	int 		len;
	unsigned char	buffer[13];

	// Let's assume we don't get anything...
	mac_addr[0] = 0x00;
	mac_addr[1] = 0x00;
	mac_addr[2] = 0x00;
	mac_addr[3] = 0x00;
	mac_addr[4] = 0x00;
	mac_addr[5] = 0x00;

	// Let's ask the device...
	len = usb_string(ether_dev->usb, ether_dev->iMACAddress, buffer, 13);

	// Sanity check!
	if (len != 12)	{
		// You gotta love failing sanity checks
		err("Attempting to get MAC address returned %d bytes", len);
		return;
	}

	// Fill in the mac_addr
	for (i = 0; i < 6; i++)	{
		mac_addr[i] = ( hex2dec( buffer[2 * i] ) << 4 ) + hex2dec( buffer[2 * i + 1] );
	}

	// Now copy it over to the kernel's network driver.
	memcpy( ether_dev->net->dev_addr, mac_addr, sizeof(mac_addr) );
}

//////////////////////////////////////////////////////////////////////////////
// Routine to print to syslog information about the driver ///////////////////
// Used by driver's probe routine ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

void log_device_info(ether_dev_t *ether_dev)
{
	int len;
	int string_num;
	unsigned char manu[256];
	unsigned char prod[256];
	unsigned char sern[256];
	unsigned char *mac_addr;

	// Default empty strings in case we don't find a real one
	manu[0] = 0x00;
	prod[0] = 0x00;
	sern[0] = 0x00;

	// Try to get the device Manufacturer
	string_num = ether_dev->usb->descriptor.iManufacturer;
	if (string_num)	{
		// Put it into its buffer
		len = usb_string(ether_dev->usb, string_num, manu, 255);
		// Just to be safe
		manu[len] = 0x00;
	}

	// Try to get the device Product Name
	string_num = ether_dev->usb->descriptor.iProduct;
	if (string_num)	{
		// Put it into its buffer
		len = usb_string(ether_dev->usb, string_num, prod, 255);
		// Just to be safe
		prod[len] = 0x00;
	}

	// Try to get the device Serial Number
	string_num = ether_dev->usb->descriptor.iSerialNumber;
	if (string_num)	{
		// Put it into its buffer
		len = usb_string(ether_dev->usb, string_num, sern, 255);
		// Just to be safe
		sern[len] = 0x00;
	}

	// This makes it easier for us to print
	mac_addr = ether_dev->net->dev_addr;

	// Now send everything we found to the syslog
	info( "%s: %s %s %s %02X:%02X:%02X:%02X:%02X:%02X", 
	      ether_dev->net->name, manu, prod, sern, mac_addr[0], 
	      mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], 
	      mac_addr[5] );
}

/* Forward declaration */
static struct usb_driver CDCEther_driver ;

//////////////////////////////////////////////////////////////////////////////
// Module's probe routine ////////////////////////////////////////////////////
// claims interfaces if they are for an Ethernet CDC /////////////////////////
//////////////////////////////////////////////////////////////////////////////

static int CDCEther_probe( struct usb_interface *intf,
			   const struct usb_device_id *id)
{
	struct usb_device	*usb = interface_to_usbdev(intf);
	struct net_device	*net;
	ether_dev_t		*ether_dev;
	int 			rc;

	// First we should check the active configuration to see if 
	// any other driver has claimed any of the interfaces.
	if ( check_for_claimed_interfaces( usb->actconfig ) ) {
		// Someone has already put there grubby paws on this device.
		// We don't want it now...
		return -ENODEV;
	}

	// We might be finding a device we can use.
	// We all go ahead and allocate our storage space.
	// We need to because we have to start filling in the data that
	// we are going to need later.
	if(!(ether_dev = kmalloc(sizeof(ether_dev_t), GFP_KERNEL))) {
		err("out of memory allocating device structure");
		return -ENOMEM;
	}

	// Zero everything out.
	memset(ether_dev, 0, sizeof(ether_dev_t));

	ether_dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!ether_dev->rx_urb) {
		kfree(ether_dev);
		return -ENOMEM;
	}
	ether_dev->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!ether_dev->tx_urb) {
		usb_free_urb(ether_dev->rx_urb);
		kfree(ether_dev);
		return -ENOMEM;
	}
	ether_dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!ether_dev->intr_urb) {
		usb_free_urb(ether_dev->tx_urb);
		usb_free_urb(ether_dev->rx_urb);
		kfree(ether_dev);
		return -ENOMEM;
	}

	// Let's see if we can find a configuration we can use.
	rc = find_valid_configuration( usb, ether_dev );
	if (rc)	{
		// Nope we couldn't find one we liked.
		// This device was not meant for us to control.
		goto error_all;
	}

	// Now that we FOUND a configuration. let's try to make the
	// device go into it.
	if ( usb_set_configuration( usb, ether_dev->bConfigurationValue ) ) {
		err("usb_set_configuration() failed");
		goto error_all;
	}

	// Now set the communication interface up as required.
	if (usb_set_interface(usb, ether_dev->comm_bInterfaceNumber, ether_dev->comm_bAlternateSetting)) {
		err("usb_set_interface() failed");
		goto error_all;
	}

	// Only turn traffic on right now if we must...
	if (ether_dev->data_interface_altset_num_without_traffic >= 0)	{
		// We found an alternate setting for the data
		// interface that allows us to turn off traffic.
		// We should use it.
		if (usb_set_interface( usb,
		                       ether_dev->data_bInterfaceNumber,
		                       ether_dev->data_bAlternateSetting_without_traffic)) {
			err("usb_set_interface() failed");
			goto error_all;
		}
	} else	{
		// We didn't find an alternate setting for the data
		// interface that would let us turn off traffic.
		// Oh well, let's go ahead and do what we must...
		if (usb_set_interface( usb,
		                       ether_dev->data_bInterfaceNumber,
		                       ether_dev->data_bAlternateSetting_with_traffic)) {
			err("usb_set_interface() failed");
			goto error_all;
		}
	}

	// Now we need to get a kernel Ethernet interface.
	net = init_etherdev( NULL, 0 );
	if ( !net ) {
		// Hmm...  The kernel is not sharing today...
		// Fine, we didn't want it anyway...
		err( "Unable to initialize ethernet device" );
		goto error_all;
	}

	// Now that we have an ethernet device, let's set it up
	// (And I don't mean "set [it] up the bomb".)
	net->priv = ether_dev;
	SET_MODULE_OWNER(net);
	net->open = CDCEther_open;
	net->stop = CDCEther_close;
	net->watchdog_timeo = CDC_ETHER_TX_TIMEOUT;
	net->tx_timeout = CDCEther_tx_timeout;   // TX timeout function
	net->do_ioctl = CDCEther_ioctl;
	net->hard_start_xmit = CDCEther_start_xmit;
	net->set_multicast_list = CDCEther_set_multicast;
	net->get_stats = CDCEther_netdev_stats;
	net->mtu = ether_dev->wMaxSegmentSize - 14;

	// We'll keep track of this information for later...
	ether_dev->usb = usb;
	ether_dev->net = net;

	// and don't forget the MAC address.
	set_ethernet_addr( ether_dev );

	// Send a message to syslog about what we are handling
	log_device_info( ether_dev );

	// I claim this interface to be a CDC Ethernet Networking device
	usb_driver_claim_interface( &CDCEther_driver,
	                            &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]),
	                            ether_dev );
	// I claim this interface to be a CDC Ethernet Networking device
	usb_driver_claim_interface( &CDCEther_driver,
	                            &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]),
	                            ether_dev );

	// Does this REALLY do anything???
	usb_get_dev( usb );

	// TODO - last minute HACK
	ether_dev->comm_ep_in = 5;

/* FIXME!!! This driver needs to be fixed to work with the new USB interface logic
 * this is not the correct thing to be doing here, we need to set the interface
 * driver specific data field.
 */
	// Okay, we are finally done...
	return 0;

	// bailing out with our tail between our knees
error_all:
	usb_free_urb(ether_dev->tx_urb);
	usb_free_urb(ether_dev->rx_urb);
	usb_free_urb(ether_dev->intr_urb);
	kfree( ether_dev );
	return -EIO;
}


//////////////////////////////////////////////////////////////////////////////
// Module's disconnect routine ///////////////////////////////////////////////
// Called when the driver is unloaded or the device is unplugged /////////////
// (Whichever happens first assuming the driver suceeded at its probe) ///////
//////////////////////////////////////////////////////////////////////////////

static void CDCEther_disconnect( struct usb_interface *intf )
{
	ether_dev_t *ether_dev = dev_get_drvdata (&intf->dev);
	struct usb_device *usb;

	dev_set_drvdata (&intf->dev, NULL);

	// Sanity check!!!
	if ( !ether_dev || !ether_dev->usb ) {
		// We failed.  We are insane!!!
		warn("unregistering non-existant device");
		return;
	}

	// Make sure we fail the sanity check if we try this again.
	ether_dev->usb = NULL;
	
	usb = interface_to_usbdev(intf);

	// It is possible that this function is called before
	// the "close" function.
	// This tells the close function we are already disconnected
	ether_dev->flags |= CDC_ETHER_UNPLUG;
	
	// We don't need the network device any more
	unregister_netdev( ether_dev->net );
	
	// For sanity checks
	ether_dev->net = NULL;

	// I ask again, does this do anything???
	usb_put_dev( usb );

	// We are done with this interface
	usb_driver_release_interface( &CDCEther_driver, 
	                              &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]) );

	// We are done with this interface too
	usb_driver_release_interface( &CDCEther_driver, 
	                              &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]) );

	// No more tied up kernel memory
	usb_free_urb(ether_dev->intr_urb);
	usb_free_urb(ether_dev->rx_urb);
	usb_free_urb(ether_dev->rx_urb);
	kfree( ether_dev );
	
	// This does no good, but it looks nice!
	ether_dev = NULL;
}

//////////////////////////////////////////////////////////////////////////////
// Driver info ///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

static struct usb_driver CDCEther_driver = {
	.name =		"CDCEther",
	.probe =	CDCEther_probe,
	.disconnect =	CDCEther_disconnect,
	.id_table =	CDCEther_ids,
};

//////////////////////////////////////////////////////////////////////////////
// init and exit routines called when driver is installed and uninstalled ////
//////////////////////////////////////////////////////////////////////////////

int __init CDCEther_init(void)
{
	info( "%s", version );
	return usb_register( &CDCEther_driver );
}

void __exit CDCEther_exit(void)
{
	usb_deregister( &CDCEther_driver );
}

//////////////////////////////////////////////////////////////////////////////
// Module info ///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

module_init( CDCEther_init );
module_exit( CDCEther_exit );

MODULE_AUTHOR("Brad Hards and another");
MODULE_DESCRIPTION("USB CDC Ethernet driver");
MODULE_LICENSE("GPL");

MODULE_PARM (multicast_filter_limit, "i");
MODULE_PARM_DESC (multicast_filter_limit, "CDCEther maximum number of filtered multicast addresses");

MODULE_DEVICE_TABLE (usb, CDCEther_ids);

//////////////////////////////////////////////////////////////////////////////
// End of file ///////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////