Free Electrons

Embedded Linux Experts

   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
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
/* CopyRight (C) 2007 Qualcomm Inc. All Rights Reserved.
 *
 *
 * This file is part of Express Card USB Driver
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/usb.h>
#include "ft1000_usb.h"
#include <linux/types.h>

#define HARLEY_READ_REGISTER     0x0
#define HARLEY_WRITE_REGISTER    0x01
#define HARLEY_READ_DPRAM_32     0x02
#define HARLEY_READ_DPRAM_LOW    0x03
#define HARLEY_READ_DPRAM_HIGH   0x04
#define HARLEY_WRITE_DPRAM_32    0x05
#define HARLEY_WRITE_DPRAM_LOW   0x06
#define HARLEY_WRITE_DPRAM_HIGH  0x07

#define HARLEY_READ_OPERATION    0xc1
#define HARLEY_WRITE_OPERATION   0x41

#if 0
#define JDEBUG
#endif

static int ft1000_submit_rx_urb(struct ft1000_info *info);

static u8 tempbuffer[1600];

#define MAX_RCV_LOOP   100

/* send a control message via USB interface synchronously
 *  Parameters:  ft1000_usb  - device structure
 *               pipe - usb control message pipe
 *               request - control request
 *               requesttype - control message request type
 *               value - value to be written or 0
 *               index - register index
 *               data - data buffer to hold the read/write values
 *               size - data size
 *               timeout - control message time out value
 */
static int ft1000_control(struct ft1000_usb *ft1000dev, unsigned int pipe,
			  u8 request, u8 requesttype, u16 value, u16 index,
			  void *data, u16 size, int timeout)
{
	int ret;

	if ((ft1000dev == NULL) || (ft1000dev->dev == NULL)) {
		pr_debug("ft1000dev or ft1000dev->dev == NULL, failure\n");
		return -ENODEV;
	}

	ret = usb_control_msg(ft1000dev->dev, pipe, request, requesttype,
			      value, index, data, size, timeout);

	if (ret > 0)
		ret = 0;

	return ret;
}

/* returns the value in a register */
int ft1000_read_register(struct ft1000_usb *ft1000dev, u16 *Data,
			 u16 nRegIndx)
{
	int ret = 0;

	ret = ft1000_control(ft1000dev,
			     usb_rcvctrlpipe(ft1000dev->dev, 0),
			     HARLEY_READ_REGISTER,
			     HARLEY_READ_OPERATION,
			     0,
			     nRegIndx,
			     Data,
			     2,
			     USB_CTRL_GET_TIMEOUT);

	return ret;
}

/* writes the value in a register */
int ft1000_write_register(struct ft1000_usb *ft1000dev, u16 value,
			  u16 nRegIndx)
{
	int ret = 0;

	ret = ft1000_control(ft1000dev,
			     usb_sndctrlpipe(ft1000dev->dev, 0),
			     HARLEY_WRITE_REGISTER,
			     HARLEY_WRITE_OPERATION,
			     value,
			     nRegIndx,
			     NULL,
			     0,
			     USB_CTRL_SET_TIMEOUT);

	return ret;
}

/* read a number of bytes from DPRAM */
int ft1000_read_dpram32(struct ft1000_usb *ft1000dev, u16 indx, u8 *buffer,
			u16 cnt)
{
	int ret = 0;

	ret = ft1000_control(ft1000dev,
			     usb_rcvctrlpipe(ft1000dev->dev, 0),
			     HARLEY_READ_DPRAM_32,
			     HARLEY_READ_OPERATION,
			     0,
			     indx,
			     buffer,
			     cnt,
			     USB_CTRL_GET_TIMEOUT);

	return ret;
}

/* writes into DPRAM a number of bytes */
int ft1000_write_dpram32(struct ft1000_usb *ft1000dev, u16 indx, u8 *buffer,
			 u16 cnt)
{
	int ret = 0;

	if (cnt % 4)
		cnt += cnt - (cnt % 4);

	ret = ft1000_control(ft1000dev,
			     usb_sndctrlpipe(ft1000dev->dev, 0),
			     HARLEY_WRITE_DPRAM_32,
			     HARLEY_WRITE_OPERATION,
			     0,
			     indx,
			     buffer,
			     cnt,
			     USB_CTRL_SET_TIMEOUT);

	return ret;
}

/* read 16 bits from DPRAM */
int ft1000_read_dpram16(struct ft1000_usb *ft1000dev, u16 indx, u8 *buffer,
			u8 highlow)
{
	int ret = 0;
	u8 request;

	if (highlow == 0)
		request = HARLEY_READ_DPRAM_LOW;
	else
		request = HARLEY_READ_DPRAM_HIGH;

	ret = ft1000_control(ft1000dev,
			     usb_rcvctrlpipe(ft1000dev->dev, 0),
			     request,
			     HARLEY_READ_OPERATION,
			     0,
			     indx,
			     buffer,
			     2,
			     USB_CTRL_GET_TIMEOUT);

	return ret;
}

/* write into DPRAM a number of bytes */
int ft1000_write_dpram16(struct ft1000_usb *ft1000dev, u16 indx, u16 value,
			 u8 highlow)
{
	int ret = 0;
	u8 request;

	if (highlow == 0)
		request = HARLEY_WRITE_DPRAM_LOW;
	else
		request = HARLEY_WRITE_DPRAM_HIGH;

	ret = ft1000_control(ft1000dev,
			     usb_sndctrlpipe(ft1000dev->dev, 0),
			     request,
			     HARLEY_WRITE_OPERATION,
			     value,
			     indx,
			     NULL,
			     0,
			     USB_CTRL_SET_TIMEOUT);

	return ret;
}

/* read DPRAM 4 words at a time */
int fix_ft1000_read_dpram32(struct ft1000_usb *ft1000dev, u16 indx,
			    u8 *buffer)
{
	u8 buf[16];
	u16 pos;
	int ret = 0;

	pos = (indx / 4) * 4;
	ret = ft1000_read_dpram32(ft1000dev, pos, buf, 16);

	if (ret == 0) {
		pos = (indx % 4) * 4;
		*buffer++ = buf[pos++];
		*buffer++ = buf[pos++];
		*buffer++ = buf[pos++];
		*buffer++ = buf[pos++];
	} else {
		pr_debug("DPRAM32 Read failed\n");
		*buffer++ = 0;
		*buffer++ = 0;
		*buffer++ = 0;
		*buffer++ = 0;
	}

	return ret;
}


/* Description: This function write to DPRAM 4 words at a time */
int fix_ft1000_write_dpram32(struct ft1000_usb *ft1000dev, u16 indx, u8 *buffer)
{
	u16 pos1;
	u16 pos2;
	u16 i;
	u8 buf[32];
	u8 resultbuffer[32];
	u8 *pdata;
	int ret  = 0;

	pos1 = (indx / 4) * 4;
	pdata = buffer;
	ret = ft1000_read_dpram32(ft1000dev, pos1, buf, 16);

	if (ret == 0) {
		pos2 = (indx % 4)*4;
		buf[pos2++] = *buffer++;
		buf[pos2++] = *buffer++;
		buf[pos2++] = *buffer++;
		buf[pos2++] = *buffer++;
		ret = ft1000_write_dpram32(ft1000dev, pos1, buf, 16);
	} else {
		pr_debug("DPRAM32 Read failed\n");
		return ret;
	}

	ret = ft1000_read_dpram32(ft1000dev, pos1, (u8 *)&resultbuffer[0], 16);

	if (ret == 0) {
		buffer = pdata;
		for (i = 0; i < 16; i++) {
			if (buf[i] != resultbuffer[i])
				ret = -1;
		}
	}

	if (ret == -1) {
		ret = ft1000_write_dpram32(ft1000dev, pos1,
					   (u8 *)&tempbuffer[0], 16);
		ret = ft1000_read_dpram32(ft1000dev, pos1,
					  (u8 *)&resultbuffer[0], 16);
		if (ret == 0) {
			buffer = pdata;
			for (i = 0; i < 16; i++) {
				if (tempbuffer[i] != resultbuffer[i]) {
					ret = -1;
					pr_debug("Failed to write\n");
				}
			}
		}
	}

	return ret;
}

/* reset or activate the DSP */
static void card_reset_dsp(struct ft1000_usb *ft1000dev, bool value)
{
	int status = 0;
	u16 tempword;

	status = ft1000_write_register(ft1000dev, HOST_INTF_BE,
				       FT1000_REG_SUP_CTRL);
	status = ft1000_read_register(ft1000dev, &tempword,
				      FT1000_REG_SUP_CTRL);

	if (value) {
		pr_debug("Reset DSP\n");
		status = ft1000_read_register(ft1000dev, &tempword,
					      FT1000_REG_RESET);
		tempword |= DSP_RESET_BIT;
		status = ft1000_write_register(ft1000dev, tempword,
					       FT1000_REG_RESET);
	} else {
		pr_debug("Activate DSP\n");
		status = ft1000_read_register(ft1000dev, &tempword,
					      FT1000_REG_RESET);
		tempword |= DSP_ENCRYPTED;
		tempword &= ~DSP_UNENCRYPTED;
		status = ft1000_write_register(ft1000dev, tempword,
					       FT1000_REG_RESET);
		status = ft1000_read_register(ft1000dev, &tempword,
					      FT1000_REG_RESET);
		tempword &= ~EFUSE_MEM_DISABLE;
		tempword &= ~DSP_RESET_BIT;
		status = ft1000_write_register(ft1000dev, tempword,
					       FT1000_REG_RESET);
		status = ft1000_read_register(ft1000dev, &tempword,
					      FT1000_REG_RESET);
	}
}

/* send a command to ASIC
 *  Parameters:  ft1000_usb  - device structure
 *               ptempbuffer - command buffer
 *               size - command buffer size
 */
int card_send_command(struct ft1000_usb *ft1000dev, void *ptempbuffer,
		      int size)
{
	int ret;
	unsigned short temp;
	unsigned char *commandbuf;

	pr_debug("enter card_send_command... size=%d\n", size);

	ret = ft1000_read_register(ft1000dev, &temp, FT1000_REG_DOORBELL);
	if (ret)
		return ret;

	commandbuf = kmalloc(size + 2, GFP_KERNEL);
	if (!commandbuf)
		return -ENOMEM;
	memcpy((void *)commandbuf + 2, ptempbuffer, size);

	if (temp & 0x0100)
		usleep_range(900, 1100);

	/* check for odd word */
	size = size + 2;

	/* Must force to be 32 bit aligned */
	if (size % 4)
		size += 4 - (size % 4);

	ret = ft1000_write_dpram32(ft1000dev, 0, commandbuf, size);
	if (ret)
		return ret;
	usleep_range(900, 1100);
	ret = ft1000_write_register(ft1000dev, FT1000_DB_DPRAM_TX,
				    FT1000_REG_DOORBELL);
	if (ret)
		return ret;
	usleep_range(900, 1100);

	ret = ft1000_read_register(ft1000dev, &temp, FT1000_REG_DOORBELL);

#if 0
	if ((temp & 0x0100) == 0)
		pr_debug("Message sent\n");
#endif
	return ret;
}

/* load or reload the DSP */
int dsp_reload(struct ft1000_usb *ft1000dev)
{
	int status;
	u16 tempword;
	u32 templong;

	struct ft1000_info *pft1000info;

	pft1000info = netdev_priv(ft1000dev->net);

	pft1000info->CardReady = 0;

	/* Program Interrupt Mask register */
	status = ft1000_write_register(ft1000dev, 0xffff, FT1000_REG_SUP_IMASK);

	status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
	tempword |= ASIC_RESET_BIT;
	status = ft1000_write_register(ft1000dev, tempword, FT1000_REG_RESET);
	msleep(1000);
	status = ft1000_read_register(ft1000dev, &tempword, FT1000_REG_RESET);
	pr_debug("Reset Register = 0x%x\n", tempword);

	/* Toggle DSP reset */
	card_reset_dsp(ft1000dev, 1);
	msleep(1000);
	card_reset_dsp(ft1000dev, 0);
	msleep(1000);

	status =
		ft1000_write_register(ft1000dev, HOST_INTF_BE, FT1000_REG_SUP_CTRL);

	/* Let's check for FEFE */
	status =
		ft1000_read_dpram32(ft1000dev, FT1000_MAG_DPRAM_FEFE_INDX,
				    (u8 *)&templong, 4);
	pr_debug("templong (fefe) = 0x%8x\n", templong);

	/* call codeloader */
	status = scram_dnldr(ft1000dev, pFileStart, FileLength);

	if (status != 0)
		return -EIO;

	msleep(1000);

	return 0;
}

/* call the Card Service function to reset the ASIC. */
static void ft1000_reset_asic(struct net_device *dev)
{
	struct ft1000_info *info = netdev_priv(dev);
	struct ft1000_usb *ft1000dev = info->priv;
	u16 tempword;

	/* Let's use the register provided by the Magnemite ASIC to reset the
	 * ASIC and DSP.
	 */
	ft1000_write_register(ft1000dev, DSP_RESET_BIT | ASIC_RESET_BIT,
			      FT1000_REG_RESET);

	mdelay(1);

	/* set watermark to -1 in order to not generate an interrupt */
	ft1000_write_register(ft1000dev, 0xffff, FT1000_REG_MAG_WATERMARK);

	/* clear interrupts */
	ft1000_read_register(ft1000dev, &tempword, FT1000_REG_SUP_ISR);
	pr_debug("interrupt status register = 0x%x\n", tempword);
	ft1000_write_register(ft1000dev, tempword, FT1000_REG_SUP_ISR);
	ft1000_read_register(ft1000dev, &tempword, FT1000_REG_SUP_ISR);
	pr_debug("interrupt status register = 0x%x\n", tempword);
}

static int ft1000_reset_card(struct net_device *dev)
{
	struct ft1000_info *info = netdev_priv(dev);
	struct ft1000_usb *ft1000dev = info->priv;
	u16 tempword;
	struct prov_record *ptr;
	struct prov_record *tmp;

	ft1000dev->fCondResetPend = true;
	info->CardReady = 0;
	ft1000dev->fProvComplete = false;

	/* Make sure we free any memory reserve for provisioning */
	list_for_each_entry_safe(ptr, tmp, &info->prov_list, list) {
		pr_debug("deleting provisioning record\n");
		list_del(&ptr->list);
		kfree(ptr->pprov_data);
		kfree(ptr);
	}

	pr_debug("reset asic\n");
	ft1000_reset_asic(dev);

	pr_debug("call dsp_reload\n");
	dsp_reload(ft1000dev);

	pr_debug("dsp reload successful\n");

	mdelay(10);

	/* Initialize DSP heartbeat area */
	ft1000_write_dpram16(ft1000dev, FT1000_MAG_HI_HO, ho_mag,
			     FT1000_MAG_HI_HO_INDX);
	ft1000_read_dpram16(ft1000dev, FT1000_MAG_HI_HO, (u8 *)&tempword,
			    FT1000_MAG_HI_HO_INDX);
	pr_debug("hi_ho value = 0x%x\n", tempword);

	info->CardReady = 1;

	ft1000dev->fCondResetPend = false;

	return TRUE;
}

/* callback function when a urb is transmitted */
static void ft1000_usb_transmit_complete(struct urb *urb)
{

	struct ft1000_usb *ft1000dev = urb->context;

	if (urb->status)
		pr_err("%s: TX status %d\n", ft1000dev->net->name, urb->status);

	netif_wake_queue(ft1000dev->net);
}

/* take an ethernet packet and convert it to a Flarion
 *  packet prior to sending it to the ASIC Downlink FIFO.
 */
static int ft1000_copy_down_pkt(struct net_device *netdev, u8 *packet, u16 len)
{
	struct ft1000_info *pInfo = netdev_priv(netdev);
	struct ft1000_usb *pFt1000Dev = pInfo->priv;

	int count, ret;
	u8 *t;
	struct pseudo_hdr hdr;

	if (!pInfo->CardReady) {
		pr_debug("Card Not Ready\n");
		return -ENODEV;
	}

	count = sizeof(struct pseudo_hdr) + len;
	if (count > MAX_BUF_SIZE) {
		pr_debug("Message Size Overflow! size = %d\n", count);
		return -EINVAL;
	}

	if (count % 4)
		count = count + (4 - (count % 4));

	memset(&hdr, 0, sizeof(struct pseudo_hdr));

	hdr.length = ntohs(count);
	hdr.source = 0x10;
	hdr.destination = 0x20;
	hdr.portdest = 0x20;
	hdr.portsrc = 0x10;
	hdr.sh_str_id = 0x91;
	hdr.control = 0x00;

	hdr.checksum = hdr.length ^ hdr.source ^ hdr.destination ^
		hdr.portdest ^ hdr.portsrc ^ hdr.sh_str_id ^ hdr.control;

	memcpy(&pFt1000Dev->tx_buf[0], &hdr, sizeof(hdr));
	memcpy(&pFt1000Dev->tx_buf[sizeof(struct pseudo_hdr)], packet, len);

	netif_stop_queue(netdev);

	usb_fill_bulk_urb(pFt1000Dev->tx_urb,
			  pFt1000Dev->dev,
			  usb_sndbulkpipe(pFt1000Dev->dev,
					  pFt1000Dev->bulk_out_endpointAddr),
			  pFt1000Dev->tx_buf, count,
			  ft1000_usb_transmit_complete, pFt1000Dev);

	t = (u8 *)pFt1000Dev->tx_urb->transfer_buffer;

	ret = usb_submit_urb(pFt1000Dev->tx_urb, GFP_ATOMIC);

	if (ret) {
		pr_debug("failed tx_urb %d\n", ret);
		return ret;
	}
	pInfo->stats.tx_packets++;
	pInfo->stats.tx_bytes += (len + 14);

	return 0;
}

/* transmit an ethernet packet
 *  Parameters:  skb - socket buffer to be sent
 *               dev - network device
 */
static int ft1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ft1000_info *pInfo = netdev_priv(dev);
	struct ft1000_usb *pFt1000Dev = pInfo->priv;
	u8 *pdata;
	int maxlen, pipe;

	if (skb == NULL) {
		pr_debug("skb == NULL!!!\n");
		return NETDEV_TX_OK;
	}

	if (pFt1000Dev->status & FT1000_STATUS_CLOSING) {
		pr_debug("network driver is closed, return\n");
		goto err;
	}

	pipe =
		usb_sndbulkpipe(pFt1000Dev->dev, pFt1000Dev->bulk_out_endpointAddr);
	maxlen = usb_maxpacket(pFt1000Dev->dev, pipe, usb_pipeout(pipe));

	pdata = (u8 *)skb->data;

	if (pInfo->mediastate == 0) {
		/* Drop packet is mediastate is down */
		pr_debug("mediastate is down\n");
		goto err;
	}

	if ((skb->len < ENET_HEADER_SIZE) || (skb->len > ENET_MAX_SIZE)) {
		/* Drop packet which has invalid size */
		pr_debug("invalid ethernet length\n");
		goto err;
	}

	ft1000_copy_down_pkt(dev, pdata + ENET_HEADER_SIZE - 2,
			     skb->len - ENET_HEADER_SIZE + 2);

err:
	dev_kfree_skb(skb);

	return NETDEV_TX_OK;
}

/* open the network driver */
static int ft1000_open(struct net_device *dev)
{
	struct ft1000_info *pInfo = netdev_priv(dev);
	struct ft1000_usb *pFt1000Dev = pInfo->priv;
	struct timeval tv;

	pr_debug("ft1000_open is called for card %d\n", pFt1000Dev->CardNumber);

	pInfo->stats.rx_bytes = 0;
	pInfo->stats.tx_bytes = 0;
	pInfo->stats.rx_packets = 0;
	pInfo->stats.tx_packets = 0;
	do_gettimeofday(&tv);
	pInfo->ConTm = tv.tv_sec;
	pInfo->ProgConStat = 0;

	netif_start_queue(dev);

	netif_carrier_on(dev);

	return ft1000_submit_rx_urb(pInfo);
}

static struct net_device_stats *ft1000_netdev_stats(struct net_device *dev)
{
	struct ft1000_info *info = netdev_priv(dev);

	return &(info->stats);
}

static const struct net_device_ops ftnet_ops = {
	.ndo_open = &ft1000_open,
	.ndo_stop = &ft1000_close,
	.ndo_start_xmit = &ft1000_start_xmit,
	.ndo_get_stats = &ft1000_netdev_stats,
};

/* initialize the network device */
static int ft1000_reset(void *dev)
{
	ft1000_reset_card(dev);
	return 0;
}

int init_ft1000_netdev(struct ft1000_usb *ft1000dev)
{
	struct net_device *netdev;
	struct ft1000_info *pInfo = NULL;
	struct dpram_blk *pdpram_blk;
	int i, ret_val;
	struct list_head *cur, *tmp;
	char card_nr[2];
	u8 gCardIndex = 0;

	netdev = alloc_etherdev(sizeof(struct ft1000_info));
	if (!netdev) {
		pr_debug("can not allocate network device\n");
		return -ENOMEM;
	}

	pInfo = netdev_priv(netdev);

	memset(pInfo, 0, sizeof(struct ft1000_info));

	dev_alloc_name(netdev, netdev->name);

	pr_debug("network device name is %s\n", netdev->name);

	if (strncmp(netdev->name, "eth", 3) == 0) {
		card_nr[0] = netdev->name[3];
		card_nr[1] = '\0';
		ret_val = kstrtou8(card_nr, 10, &gCardIndex);
		if (ret_val) {
			netdev_err(ft1000dev->net, "Can't parse netdev\n");
			goto err_net;
		}

		ft1000dev->CardNumber = gCardIndex;
		pr_debug("card number = %d\n", ft1000dev->CardNumber);
	} else {
		netdev_err(ft1000dev->net, "ft1000: Invalid device name\n");
		ret_val = -ENXIO;
		goto err_net;
	}

	memset(&pInfo->stats, 0, sizeof(struct net_device_stats));

	spin_lock_init(&pInfo->dpram_lock);
	pInfo->priv = ft1000dev;
	pInfo->DrvErrNum = 0;
	pInfo->registered = 1;
	pInfo->ft1000_reset = ft1000_reset;
	pInfo->mediastate = 0;
	pInfo->fifo_cnt = 0;
	ft1000dev->DeviceCreated = FALSE;
	pInfo->CardReady = 0;
	pInfo->DSP_TIME[0] = 0;
	pInfo->DSP_TIME[1] = 0;
	pInfo->DSP_TIME[2] = 0;
	pInfo->DSP_TIME[3] = 0;
	ft1000dev->fAppMsgPend = false;
	ft1000dev->fCondResetPend = false;
	ft1000dev->usbboot = 0;
	ft1000dev->dspalive = 0;
	memset(&ft1000dev->tempbuf[0], 0, sizeof(ft1000dev->tempbuf));

	INIT_LIST_HEAD(&pInfo->prov_list);

	INIT_LIST_HEAD(&ft1000dev->nodes.list);

	netdev->netdev_ops = &ftnet_ops;

	ft1000dev->net = netdev;

	pr_debug("Initialize free_buff_lock and freercvpool\n");
	spin_lock_init(&free_buff_lock);

	/* initialize a list of buffers to be use for queuing
	 * up receive command data
	 */
	INIT_LIST_HEAD(&freercvpool);

	/* create list of free buffers */
	for (i = 0; i < NUM_OF_FREE_BUFFERS; i++) {
		/* Get memory for DPRAM_DATA link list */
		pdpram_blk = kmalloc(sizeof(struct dpram_blk), GFP_KERNEL);
		if (pdpram_blk == NULL) {
			ret_val = -ENOMEM;
			goto err_free;
		}
		/* Get a block of memory to store command data */
		pdpram_blk->pbuffer = kmalloc(MAX_CMD_SQSIZE, GFP_KERNEL);
		if (pdpram_blk->pbuffer == NULL) {
			ret_val = -ENOMEM;
			kfree(pdpram_blk);
			goto err_free;
		}
		/* link provisioning data */
		list_add_tail(&pdpram_blk->list, &freercvpool);
	}
	numofmsgbuf = NUM_OF_FREE_BUFFERS;

	return 0;

err_free:
	list_for_each_safe(cur, tmp, &freercvpool) {
		pdpram_blk = list_entry(cur, struct dpram_blk, list);
		list_del(&pdpram_blk->list);
		kfree(pdpram_blk->pbuffer);
		kfree(pdpram_blk);
	}
err_net:
	free_netdev(netdev);
	return ret_val;
}

/* register the network driver */
int reg_ft1000_netdev(struct ft1000_usb *ft1000dev,
		      struct usb_interface *intf)
{
	struct net_device *netdev;
	struct ft1000_info *pInfo;
	int rc;

	netdev = ft1000dev->net;
	pInfo = netdev_priv(ft1000dev->net);

	ft1000_read_register(ft1000dev, &pInfo->AsicID, FT1000_REG_ASIC_ID);

	usb_set_intfdata(intf, pInfo);
	SET_NETDEV_DEV(netdev, &intf->dev);

	rc = register_netdev(netdev);
	if (rc) {
		pr_debug("could not register network device\n");
		free_netdev(netdev);
		return rc;
	}

	ft1000_create_dev(ft1000dev);

	pInfo->CardReady = 1;

	return 0;
}

/* take a packet from the FIFO up link and
 *  convert it into an ethernet packet and deliver it to the IP stack
 */
static int ft1000_copy_up_pkt(struct urb *urb)
{
	struct ft1000_info *info = urb->context;
	struct ft1000_usb *ft1000dev = info->priv;
	struct net_device *net = ft1000dev->net;

	u16 tempword;
	u16 len;
	u16 lena;
	struct sk_buff *skb;
	u16 i;
	u8 *pbuffer = NULL;
	u8 *ptemp = NULL;
	u16 *chksum;

	if (ft1000dev->status & FT1000_STATUS_CLOSING) {
		pr_debug("network driver is closed, return\n");
		return 0;
	}
	/* Read length */
	len = urb->transfer_buffer_length;
	lena = urb->actual_length;

	chksum = (u16 *)ft1000dev->rx_buf;

	tempword = *chksum++;
	for (i = 1; i < 7; i++)
		tempword ^= *chksum++;

	if (tempword != *chksum) {
		info->stats.rx_errors++;
		ft1000_submit_rx_urb(info);
		return -1;
	}

	skb = dev_alloc_skb(len + 12 + 2);

	if (skb == NULL) {
		pr_debug("No Network buffers available\n");
		info->stats.rx_errors++;
		ft1000_submit_rx_urb(info);
		return -1;
	}

	pbuffer = (u8 *)skb_put(skb, len + 12);

	/* subtract the number of bytes read already */
	ptemp = pbuffer;

	/* fake MAC address */
	*pbuffer++ = net->dev_addr[0];
	*pbuffer++ = net->dev_addr[1];
	*pbuffer++ = net->dev_addr[2];
	*pbuffer++ = net->dev_addr[3];
	*pbuffer++ = net->dev_addr[4];
	*pbuffer++ = net->dev_addr[5];
	*pbuffer++ = 0x00;
	*pbuffer++ = 0x07;
	*pbuffer++ = 0x35;
	*pbuffer++ = 0xff;
	*pbuffer++ = 0xff;
	*pbuffer++ = 0xfe;

	memcpy(pbuffer, ft1000dev->rx_buf + sizeof(struct pseudo_hdr),
	       len - sizeof(struct pseudo_hdr));

	skb->dev = net;

	skb->protocol = eth_type_trans(skb, net);
	skb->ip_summed = CHECKSUM_UNNECESSARY;
	netif_rx(skb);

	info->stats.rx_packets++;
	/* Add on 12 bytes for MAC address which was removed */
	info->stats.rx_bytes += (lena + 12);

	ft1000_submit_rx_urb(info);

	return 0;
}


/* the receiving function of the network driver */
static int ft1000_submit_rx_urb(struct ft1000_info *info)
{
	int result;
	struct ft1000_usb *pFt1000Dev = info->priv;

	if (pFt1000Dev->status & FT1000_STATUS_CLOSING) {
		pr_debug("network driver is closed, return\n");
		return -ENODEV;
	}

	usb_fill_bulk_urb(pFt1000Dev->rx_urb,
			  pFt1000Dev->dev,
			  usb_rcvbulkpipe(pFt1000Dev->dev,
					  pFt1000Dev->bulk_in_endpointAddr),
			  pFt1000Dev->rx_buf, MAX_BUF_SIZE,
			  (usb_complete_t)ft1000_copy_up_pkt, info);

	result = usb_submit_urb(pFt1000Dev->rx_urb, GFP_ATOMIC);

	if (result) {
		pr_err("submitting rx_urb %d failed\n", result);
		return result;
	}

	return 0;
}

/* close the network driver */
int ft1000_close(struct net_device *net)
{
	struct ft1000_info *pInfo = netdev_priv(net);
	struct ft1000_usb *ft1000dev = pInfo->priv;

	ft1000dev->status |= FT1000_STATUS_CLOSING;

	pr_debug("pInfo=%p, ft1000dev=%p\n", pInfo, ft1000dev);
	netif_carrier_off(net);
	netif_stop_queue(net);
	ft1000dev->status &= ~FT1000_STATUS_CLOSING;

	pInfo->ProgConStat = 0xff;

	return 0;
}

/* check if the device is presently available on the system. */
static int ft1000_chkcard(struct ft1000_usb *dev)
{
	u16 tempword;
	int status;

	if (dev->fCondResetPend) {
		pr_debug("Card is being reset, return FALSE\n");
		return TRUE;
	}
	/* Mask register is used to check for device presence since it is never
	 * set to zero.
	 */
	status = ft1000_read_register(dev, &tempword, FT1000_REG_SUP_IMASK);
	if (tempword == 0) {
		pr_debug("IMASK = 0 Card not detected\n");
		return FALSE;
	}
	/* The system will return the value of 0xffff for the version register
	 * if the device is not present.
	 */
	status = ft1000_read_register(dev, &tempword, FT1000_REG_ASIC_ID);
	if (tempword != 0x1b01) {
		dev->status |= FT1000_STATUS_CLOSING;
		pr_debug("Version = 0xffff Card not detected\n");
		return FALSE;
	}
	return TRUE;
}

/* read a message from the dpram area.
 *  Input:
 *    dev - network device structure
 *    pbuffer - caller supply address to buffer
 */
static bool ft1000_receive_cmd(struct ft1000_usb *dev, u16 *pbuffer,
			       int maxsz)
{
	u16 size;
	int ret;
	u16 *ppseudohdr;
	int i;
	u16 tempword;

	ret =
		ft1000_read_dpram16(dev, FT1000_MAG_PH_LEN, (u8 *)&size,
				    FT1000_MAG_PH_LEN_INDX);
	size = ntohs(size) + PSEUDOSZ;
	if (size > maxsz) {
		pr_debug("Invalid command length = %d\n", size);
		return FALSE;
	}
	ppseudohdr = (u16 *)pbuffer;
	ft1000_write_register(dev, FT1000_DPRAM_MAG_RX_BASE,
			      FT1000_REG_DPRAM_ADDR);
	ret =
		ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAH);
	pbuffer++;
	ft1000_write_register(dev, FT1000_DPRAM_MAG_RX_BASE + 1,
			      FT1000_REG_DPRAM_ADDR);
	for (i = 0; i <= (size >> 2); i++) {
		ret =
			ft1000_read_register(dev, pbuffer,
					     FT1000_REG_MAG_DPDATAL);
		pbuffer++;
		ret =
			ft1000_read_register(dev, pbuffer,
					     FT1000_REG_MAG_DPDATAH);
		pbuffer++;
	}
	/* copy odd aligned word */
	ret =
		ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAL);

	pbuffer++;
	ret =
		ft1000_read_register(dev, pbuffer, FT1000_REG_MAG_DPDATAH);

	pbuffer++;
	if (size & 0x0001) {
		/* copy odd byte from fifo */
		ret =
			ft1000_read_register(dev, &tempword,
					     FT1000_REG_DPRAM_DATA);
		*pbuffer = ntohs(tempword);
	}
	/* Check if pseudo header checksum is good
	 * Calculate pseudo header checksum
	 */
	tempword = *ppseudohdr++;
	for (i = 1; i < 7; i++)
		tempword ^= *ppseudohdr++;

	if (tempword != *ppseudohdr)
		return FALSE;

	return TRUE;
}

static int ft1000_dsp_prov(void *arg)
{
	struct ft1000_usb *dev = (struct ft1000_usb *)arg;
	struct ft1000_info *info = netdev_priv(dev->net);
	u16 tempword;
	u16 len;
	u16 i = 0;
	struct prov_record *ptr;
	struct pseudo_hdr *ppseudo_hdr;
	u16 *pmsg;
	int status;
	u16 TempShortBuf[256];

	while (list_empty(&info->prov_list) == 0) {
		pr_debug("DSP Provisioning List Entry\n");

		/* Check if doorbell is available */
		pr_debug("check if doorbell is cleared\n");
		status =
			ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
		if (status) {
			pr_debug("ft1000_read_register error\n");
			break;
		}

		while (tempword & FT1000_DB_DPRAM_TX) {
			mdelay(10);
			i++;
			if (i == 10) {
				pr_debug("message drop\n");
				return -1;
			}
			ft1000_read_register(dev, &tempword,
					     FT1000_REG_DOORBELL);
		}

		if (!(tempword & FT1000_DB_DPRAM_TX)) {
			pr_debug("*** Provision Data Sent to DSP\n");

			/* Send provisioning data */
			ptr =
				list_entry(info->prov_list.next, struct prov_record,
					   list);
			len = *(u16 *)ptr->pprov_data;
			len = htons(len);
			len += PSEUDOSZ;

			pmsg = (u16 *)ptr->pprov_data;
			ppseudo_hdr = (struct pseudo_hdr *)pmsg;
			/* Insert slow queue sequence number */
			ppseudo_hdr->seq_num = info->squeseqnum++;
			ppseudo_hdr->portsrc = 0;
			/* Calculate new checksum */
			ppseudo_hdr->checksum = *pmsg++;
			for (i = 1; i < 7; i++)
				ppseudo_hdr->checksum ^= *pmsg++;

			TempShortBuf[0] = 0;
			TempShortBuf[1] = htons(len);
			memcpy(&TempShortBuf[2], ppseudo_hdr, len);

			status =
				ft1000_write_dpram32(dev, 0,
						     (u8 *)&TempShortBuf[0],
						     (unsigned short)(len + 2));
			status =
				ft1000_write_register(dev, FT1000_DB_DPRAM_TX,
						      FT1000_REG_DOORBELL);

			list_del(&ptr->list);
			kfree(ptr->pprov_data);
			kfree(ptr);
		}
		usleep_range(9000, 11000);
	}

	pr_debug("DSP Provisioning List Entry finished\n");

	msleep(100);

	dev->fProvComplete = true;
	info->CardReady = 1;

	return 0;
}

static int ft1000_proc_drvmsg(struct ft1000_usb *dev, u16 size)
{
	struct ft1000_info *info = netdev_priv(dev->net);
	u16 msgtype;
	u16 tempword;
	struct media_msg *pmediamsg;
	struct dsp_init_msg *pdspinitmsg;
	struct drv_msg *pdrvmsg;
	u16 i;
	struct pseudo_hdr *ppseudo_hdr;
	u16 *pmsg;
	int status;
	union {
		u8 byte[2];
		u16 wrd;
	} convert;

	char *cmdbuffer = kmalloc(1600, GFP_KERNEL);

	if (!cmdbuffer)
		return -ENOMEM;

	status = ft1000_read_dpram32(dev, 0x200, cmdbuffer, size);

#ifdef JDEBUG
	print_hex_dump_debug("cmdbuffer: ", HEX_DUMP_OFFSET, 16, 1,
			     cmdbuffer, size, true);
#endif
	pdrvmsg = (struct drv_msg *)&cmdbuffer[2];
	msgtype = ntohs(pdrvmsg->type);
	pr_debug("Command message type = 0x%x\n", msgtype);
	switch (msgtype) {
	case MEDIA_STATE:{
		pr_debug("Command message type = MEDIA_STATE\n");
		pmediamsg = (struct media_msg *)&cmdbuffer[0];
		if (info->ProgConStat != 0xFF) {
			if (pmediamsg->state) {
				pr_debug("Media is up\n");
				if (info->mediastate == 0) {
					if (dev->NetDevRegDone)
						netif_wake_queue(dev->net);
					info->mediastate = 1;
				}
			} else {
				pr_debug("Media is down\n");
				if (info->mediastate == 1) {
					info->mediastate = 0;
					if (dev->NetDevRegDone)
						info->ConTm = 0;
				}
			}
		} else {
			pr_debug("Media is down\n");
			if (info->mediastate == 1) {
				info->mediastate = 0;
				info->ConTm = 0;
			}
		}
		break;
	}
	case DSP_INIT_MSG:{
		pr_debug("Command message type = DSP_INIT_MSG\n");
		pdspinitmsg = (struct dsp_init_msg *)&cmdbuffer[2];
		memcpy(info->DspVer, pdspinitmsg->DspVer, DSPVERSZ);
		pr_debug("DSPVER = 0x%2x 0x%2x 0x%2x 0x%2x\n",
			 info->DspVer[0], info->DspVer[1], info->DspVer[2],
			 info->DspVer[3]);
		memcpy(info->HwSerNum, pdspinitmsg->HwSerNum,
		       HWSERNUMSZ);
		memcpy(info->Sku, pdspinitmsg->Sku, SKUSZ);
		memcpy(info->eui64, pdspinitmsg->eui64, EUISZ);
		pr_debug("EUI64=%2x.%2x.%2x.%2x.%2x.%2x.%2x.%2x\n",
			 info->eui64[0], info->eui64[1], info->eui64[2],
			 info->eui64[3], info->eui64[4], info->eui64[5],
			 info->eui64[6], info->eui64[7]);
		dev->net->dev_addr[0] = info->eui64[0];
		dev->net->dev_addr[1] = info->eui64[1];
		dev->net->dev_addr[2] = info->eui64[2];
		dev->net->dev_addr[3] = info->eui64[5];
		dev->net->dev_addr[4] = info->eui64[6];
		dev->net->dev_addr[5] = info->eui64[7];

		if (ntohs(pdspinitmsg->length) ==
		    (sizeof(struct dsp_init_msg) - 20)) {
			memcpy(info->ProductMode, pdspinitmsg->ProductMode,
			       MODESZ);
			memcpy(info->RfCalVer, pdspinitmsg->RfCalVer, CALVERSZ);
			memcpy(info->RfCalDate, pdspinitmsg->RfCalDate,
			       CALDATESZ);
			pr_debug("RFCalVer = 0x%2x 0x%2x\n",
				 info->RfCalVer[0], info->RfCalVer[1]);
		}
		break;
	}
	case DSP_PROVISION:{
		pr_debug("Command message type = DSP_PROVISION\n");

		/* kick off dspprov routine to start provisioning
		 * Send provisioning data to DSP
		 */
		if (list_empty(&info->prov_list) == 0) {
			dev->fProvComplete = false;
			status = ft1000_dsp_prov(dev);
			if (status != 0)
				goto out;
		} else {
			dev->fProvComplete = true;
			status = ft1000_write_register(dev, FT1000_DB_HB,
						       FT1000_REG_DOORBELL);
			pr_debug("No more DSP provisioning data in dsp image\n");
		}
		pr_debug("DSP PROVISION is done\n");
		break;
	}
	case DSP_STORE_INFO:{
		pr_debug("Command message type = DSP_STORE_INFO");
		tempword = ntohs(pdrvmsg->length);
		info->DSPInfoBlklen = tempword;
		if (tempword < (MAX_DSP_SESS_REC - 4)) {
			pmsg = (u16 *)&pdrvmsg->data[0];
			for (i = 0; i < ((tempword + 1) / 2); i++) {
				pr_debug("dsp info data = 0x%x\n", *pmsg);
				info->DSPInfoBlk[i + 10] = *pmsg++;
			}
		} else {
			info->DSPInfoBlklen = 0;
		}
		break;
	}
	case DSP_GET_INFO:{
		pr_debug("Got DSP_GET_INFO\n");
		/* copy dsp info block to dsp */
		dev->DrvMsgPend = 1;
		/* allow any outstanding ioctl to finish */
		mdelay(10);
		status = ft1000_read_register(dev, &tempword,
					      FT1000_REG_DOORBELL);
		if (tempword & FT1000_DB_DPRAM_TX) {
			mdelay(10);
			status = ft1000_read_register(dev, &tempword,
						      FT1000_REG_DOORBELL);
			if (tempword & FT1000_DB_DPRAM_TX) {
				mdelay(10);
				status = ft1000_read_register(dev, &tempword,
							      FT1000_REG_DOORBELL);
				if (tempword & FT1000_DB_DPRAM_TX)
					break;
			}
		}
		/* Put message into Slow Queue Form Pseudo header */
		pmsg = (u16 *)info->DSPInfoBlk;
		*pmsg++ = 0;
		*pmsg++ = htons(info->DSPInfoBlklen + 20 + info->DSPInfoBlklen);
		ppseudo_hdr =
			(struct pseudo_hdr *)(u16 *)&info->DSPInfoBlk[2];
		ppseudo_hdr->length = htons(info->DSPInfoBlklen + 4
					    + info->DSPInfoBlklen);
		ppseudo_hdr->source = 0x10;
		ppseudo_hdr->destination = 0x20;
		ppseudo_hdr->portdest = 0;
		ppseudo_hdr->portsrc = 0;
		ppseudo_hdr->sh_str_id = 0;
		ppseudo_hdr->control = 0;
		ppseudo_hdr->rsvd1 = 0;
		ppseudo_hdr->rsvd2 = 0;
		ppseudo_hdr->qos_class = 0;
		/* Insert slow queue sequence number */
		ppseudo_hdr->seq_num = info->squeseqnum++;
		/* Insert application id */
		ppseudo_hdr->portsrc = 0;
		/* Calculate new checksum */
		ppseudo_hdr->checksum = *pmsg++;
		for (i = 1; i < 7; i++)
			ppseudo_hdr->checksum ^= *pmsg++;

		info->DSPInfoBlk[10] = 0x7200;
		info->DSPInfoBlk[11] = htons(info->DSPInfoBlklen);
		status = ft1000_write_dpram32(dev, 0,
					      (u8 *)&info->DSPInfoBlk[0],
					      (unsigned short)(info->DSPInfoBlklen + 22));
		status = ft1000_write_register(dev, FT1000_DB_DPRAM_TX,
					       FT1000_REG_DOORBELL);
		dev->DrvMsgPend = 0;
		break;
	}
	case GET_DRV_ERR_RPT_MSG:{
		pr_debug("Got GET_DRV_ERR_RPT_MSG\n");
		/* copy driver error message to dsp */
		dev->DrvMsgPend = 1;
		/* allow any outstanding ioctl to finish */
		mdelay(10);
		status = ft1000_read_register(dev, &tempword,
					      FT1000_REG_DOORBELL);
		if (tempword & FT1000_DB_DPRAM_TX) {
			mdelay(10);
			status = ft1000_read_register(dev, &tempword,
						      FT1000_REG_DOORBELL);
			if (tempword & FT1000_DB_DPRAM_TX)
				mdelay(10);
		}
		if ((tempword & FT1000_DB_DPRAM_TX) == 0) {
			/* Put message into Slow Queue Form Pseudo header */
			pmsg = (u16 *)&tempbuffer[0];
			ppseudo_hdr = (struct pseudo_hdr *)pmsg;
			ppseudo_hdr->length = htons(0x0012);
			ppseudo_hdr->source = 0x10;
			ppseudo_hdr->destination = 0x20;
			ppseudo_hdr->portdest = 0;
			ppseudo_hdr->portsrc = 0;
			ppseudo_hdr->sh_str_id = 0;
			ppseudo_hdr->control = 0;
			ppseudo_hdr->rsvd1 = 0;
			ppseudo_hdr->rsvd2 = 0;
			ppseudo_hdr->qos_class = 0;
			/* Insert slow queue sequence number */
			ppseudo_hdr->seq_num = info->squeseqnum++;
			/* Insert application id */
			ppseudo_hdr->portsrc = 0;
			/* Calculate new checksum */
			ppseudo_hdr->checksum = *pmsg++;
			for (i = 1; i < 7; i++)
				ppseudo_hdr->checksum ^= *pmsg++;

			pmsg = (u16 *)&tempbuffer[16];
			*pmsg++ = htons(RSP_DRV_ERR_RPT_MSG);
			*pmsg++ = htons(0x000e);
			*pmsg++ = htons(info->DSP_TIME[0]);
			*pmsg++ = htons(info->DSP_TIME[1]);
			*pmsg++ = htons(info->DSP_TIME[2]);
			*pmsg++ = htons(info->DSP_TIME[3]);
			convert.byte[0] = info->DspVer[0];
			convert.byte[1] = info->DspVer[1];
			*pmsg++ = convert.wrd;
			convert.byte[0] = info->DspVer[2];
			convert.byte[1] = info->DspVer[3];
			*pmsg++ = convert.wrd;
			*pmsg++ = htons(info->DrvErrNum);

			status = card_send_command(dev, (unsigned char *)&tempbuffer[0],
						   (u16)(0x0012 + PSEUDOSZ));
			if (status)
				goto out;
			info->DrvErrNum = 0;
		}
		dev->DrvMsgPend = 0;
		break;
	}
	default:
		break;
	}

	status = 0;
out:
	kfree(cmdbuffer);
	return status;
}

/* Check which application has registered for dsp broadcast messages */
static int dsp_broadcast_msg_id(struct ft1000_usb *dev)
{
	struct dpram_blk *pdpram_blk;
	unsigned long flags;
	int i;

	for (i = 0; i < MAX_NUM_APP; i++) {
		if ((dev->app_info[i].DspBCMsgFlag)
		    && (dev->app_info[i].fileobject)
		    && (dev->app_info[i].NumOfMsg
			< MAX_MSG_LIMIT)) {
			pdpram_blk = ft1000_get_buffer(&freercvpool);
			if (pdpram_blk == NULL) {
				pr_debug("Out of memory in free receive command pool\n");
				dev->app_info[i].nRxMsgMiss++;
				return -1;
			}
			if (ft1000_receive_cmd(dev, pdpram_blk->pbuffer,
					       MAX_CMD_SQSIZE)) {
				/* Put message into the
				 * appropriate application block
				 */
				dev->app_info[i].nRxMsg++;
				spin_lock_irqsave(&free_buff_lock, flags);
				list_add_tail(&pdpram_blk->list,
					      &dev->app_info[i] .app_sqlist);
				dev->app_info[i].NumOfMsg++;
				spin_unlock_irqrestore(&free_buff_lock, flags);
				wake_up_interruptible(&dev->app_info[i]
						      .wait_dpram_msg);
			} else {
				dev->app_info[i].nRxMsgMiss++;
				ft1000_free_buffer(pdpram_blk, &freercvpool);
				pr_debug("ft1000_get_buffer NULL\n");
				return -1;
			}
		}
	}
	return 0;
}

static int handle_misc_portid(struct ft1000_usb *dev)
{
	struct dpram_blk *pdpram_blk;
	int i;

	pdpram_blk = ft1000_get_buffer(&freercvpool);
	if (pdpram_blk == NULL) {
		pr_debug("Out of memory in free receive command pool\n");
		return -1;
	}
	if (!ft1000_receive_cmd(dev, pdpram_blk->pbuffer, MAX_CMD_SQSIZE))
		goto exit_failure;

	/* Search for correct application block */
	for (i = 0; i < MAX_NUM_APP; i++) {
		if (dev->app_info[i].app_id == ((struct pseudo_hdr *)
						pdpram_blk->pbuffer)->portdest)
			break;
	}
	if (i == MAX_NUM_APP) {
		pr_debug("No application matching id = %d\n",
			 ((struct pseudo_hdr *)pdpram_blk->pbuffer)->portdest);
		goto exit_failure;
	} else if (dev->app_info[i].NumOfMsg > MAX_MSG_LIMIT) {
		goto exit_failure;
	} else {
		dev->app_info[i].nRxMsg++;
		/* Put message into the appropriate application block */
		list_add_tail(&pdpram_blk->list, &dev->app_info[i].app_sqlist);
		dev->app_info[i].NumOfMsg++;
	}
	return 0;

exit_failure:
	ft1000_free_buffer(pdpram_blk, &freercvpool);
	return -1;
}

int ft1000_poll(void *dev_id)
{
	struct ft1000_usb *dev = (struct ft1000_usb *)dev_id;
	struct ft1000_info *info = netdev_priv(dev->net);
	u16 tempword;
	int status;
	u16 size;
	int i;
	u16 data;
	u16 modulo;
	u16 portid;

	if (ft1000_chkcard(dev) == FALSE) {
		pr_debug("failed\n");
		return -1;
	}
	status = ft1000_read_register(dev, &tempword, FT1000_REG_DOORBELL);
	if (!status) {
		if (tempword & FT1000_DB_DPRAM_RX) {
			status = ft1000_read_dpram16(dev,
						     0x200, (u8 *)&data, 0);
			size = ntohs(data) + 16 + 2;
			if (size % 4) {
				modulo = 4 - (size % 4);
				size = size + modulo;
			}
			status = ft1000_read_dpram16(dev, 0x201,
						     (u8 *)&portid, 1);
			portid &= 0xff;
			if (size < MAX_CMD_SQSIZE) {
				switch (portid) {
				case DRIVERID:
					pr_debug("FT1000_REG_DOORBELL message type: FT1000_DB_DPRAM_RX : portid DRIVERID\n");
					status = ft1000_proc_drvmsg(dev, size);
					if (status != 0)
						return status;
					break;
				case DSPBCMSGID:
					status = dsp_broadcast_msg_id(dev);
					break;
				default:
					status = handle_misc_portid(dev);
					break;
				}
			} else
				pr_debug("Invalid total length for SlowQ = %d\n",
					 size);
			status = ft1000_write_register(dev,
						       FT1000_DB_DPRAM_RX,
						       FT1000_REG_DOORBELL);
		} else if (tempword & FT1000_DSP_ASIC_RESET) {
			/* Let's reset the ASIC from the Host side as well */
			status = ft1000_write_register(dev, ASIC_RESET_BIT,
						       FT1000_REG_RESET);
			status = ft1000_read_register(dev, &tempword,
						      FT1000_REG_RESET);
			i = 0;
			while (tempword & ASIC_RESET_BIT) {
				status = ft1000_read_register(dev, &tempword,
							      FT1000_REG_RESET);
				usleep_range(9000, 11000);
				i++;
				if (i == 100)
					break;
			}
			if (i == 100) {
				pr_debug("Unable to reset ASIC\n");
				return 0;
			}
			usleep_range(9000, 11000);
			/* Program WMARK register */
			status = ft1000_write_register(dev, 0x600,
						       FT1000_REG_MAG_WATERMARK);
			/* clear ASIC reset doorbell */
			status = ft1000_write_register(dev,
						       FT1000_DSP_ASIC_RESET,
						       FT1000_REG_DOORBELL);
			usleep_range(9000, 11000);
		} else if (tempword & FT1000_ASIC_RESET_REQ) {
			pr_debug("FT1000_REG_DOORBELL message type: FT1000_ASIC_RESET_REQ\n");
			/* clear ASIC reset request from DSP */
			status = ft1000_write_register(dev,
						       FT1000_ASIC_RESET_REQ,
						       FT1000_REG_DOORBELL);
			status = ft1000_write_register(dev, HOST_INTF_BE,
						       FT1000_REG_SUP_CTRL);
			/* copy dsp session record from Adapter block */
			status = ft1000_write_dpram32(dev, 0,
						      (u8 *)&info->DSPSess.Rec[0], 1024);
			status = ft1000_write_register(dev, 0x600,
						       FT1000_REG_MAG_WATERMARK);
			/* ring doorbell to tell DSP that
			 * ASIC is out of reset
			 * */
			status = ft1000_write_register(dev,
						       FT1000_ASIC_RESET_DSP,
						       FT1000_REG_DOORBELL);
		} else if (tempword & FT1000_DB_COND_RESET) {
			pr_debug("FT1000_REG_DOORBELL message type: FT1000_DB_COND_RESET\n");
			if (!dev->fAppMsgPend) {
				/* Reset ASIC and DSP */
				status = ft1000_read_dpram16(dev,
							     FT1000_MAG_DSP_TIMER0,
							     (u8 *)&info->DSP_TIME[0],
							     FT1000_MAG_DSP_TIMER0_INDX);
				status = ft1000_read_dpram16(dev,
							     FT1000_MAG_DSP_TIMER1,
							     (u8 *)&info->DSP_TIME[1],
							     FT1000_MAG_DSP_TIMER1_INDX);
				status = ft1000_read_dpram16(dev,
							     FT1000_MAG_DSP_TIMER2,
							     (u8 *)&info->DSP_TIME[2],
							     FT1000_MAG_DSP_TIMER2_INDX);
				status = ft1000_read_dpram16(dev,
							     FT1000_MAG_DSP_TIMER3,
							     (u8 *)&info->DSP_TIME[3],
							     FT1000_MAG_DSP_TIMER3_INDX);
				info->CardReady = 0;
				info->DrvErrNum = DSP_CONDRESET_INFO;
				pr_debug("DSP conditional reset requested\n");
				info->ft1000_reset(dev->net);
			} else {
				dev->fProvComplete = false;
				dev->fCondResetPend = true;
			}
			ft1000_write_register(dev, FT1000_DB_COND_RESET,
					      FT1000_REG_DOORBELL);
		}
	}
	return 0;
}