summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/net/wireless/base.c
blob: 4d0f17a37822b6df13a382b79848ef944928db54 (plain)
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
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
/*------------------------------------------------------------------------------*/
/* 		    Wireless driver base functions for winbond W90N745					*/
/* 				version 1.0.2(used only for Station)							*/
/*------------------------------------------------------------------------------*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kernel.h>      
#include <linux/slab.h>		 
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <asm/uaccess.h>

#include "W90N745Prism.h"
#include "Queue.h"

#if 0
#define PRISM_SSIDLEN_DEFAULT    ((UINT16)0x0008)
char PRISM_DesireSSID_DEFAULT[] = {(char)'P', (char)'C', (char)'3',(char)'0',(char)'W',
                              				(char)'L', (char)'A', (char)'N'};
#else
#define PRISM_SSIDLEN_DEFAULT    ((UINT16)0x000a)
char PRISM_DesireSSID_DEFAULT[] __attribute__ ((aligned (2)))
	= {(char)'P', (char)'C', (char)'3', (char)'2',(char)'W',
                                   				(char)'e', (char)'b', (char)'C', (char)'a',(char)'m'};
#endif// declararion desired SSID

// Access Point's MAC address
char PRISM_BSSID[] = {(char)0x00,(char)0x05,(char)0x5D,(char)0xEB,(char)0x94,(char)0x24};

// 802.11 frame header type RFC1042
char PRISM_LLC[] = {(char)0xaa,(char)0xaa,(char)0x03,(char)0x00,(char)0x00,(char)0x00};

#if 1
	//unsigned char Mac_address[6] = {0x00, 0x60, 0xB3, 0x73, 0x26, 0x3d};
	unsigned char Mac_address[6] = {0x00, 0x60, 0xB3, 0x73, 0x26, 0x3C};
#else
	unsigned char Mac_address[6] = {0x00, 0x60, 0xB3, 0x73, 0x26, 0x3E};
#endif
/* Debug for now */
UINT8 *DebugpBuffer = NULL;

Queue_txfid TxQueueObj;

/* The frequency of each channel in MHz */
const long channel_frequency[] = {
	2412, 2417, 2422, 2427, 2432, 2437, 2442,
	2447, 2452, 2457, 2462, 2467, 2472, 2484
};

#define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )

/* This tables gives the actual meanings of the bitrate IDs returned by the firmware. */
struct {
	int bitrate; /* in 100s of kilbits */
	u16 prism_txratectrl;
} bitrate_table[] = {
	{110, 15}, /* Entry 0 is the default */
	{10, 1},
	{20, 2},
	{20, 3},
	{55, 4},
	{55, 7},
	{110, 8},
};
#define BITRATE_TABLE_SIZE (sizeof(bitrate_table) / sizeof(bitrate_table[0]))
//#define PRSIM_DEBUG
int nums = 5;

/*------------------------------Prism Base Functions ----------------------------------*/
/* */
int prism_cmd_access(UINT16 write, UINT16 rid)
{
	int result = 0;
	UINT16 cmd;

	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_ACCESS)|PRISM_CMD_WRITE_SET(write);
	result = prism_docmd_wait(cmd, rid, 0, 0);
	return result;
}

/* prism transmit a buffer command, fid-->buffer */
int prism_cmd_transmit(UINT16 reclaim, UINT16 fid)
{
	int result = 0;
	UINT16 cmd;

	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_TX)|PRISM_CMD_RECL_SET(reclaim);
	result = prism_docmd_wait(cmd, fid, 0, 0);
	return result;
}

int prism_cmd_inquiry(UINT16 infoType)
{
	int result = 0;
	UINT16 cmd;

	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_INQ);
	result = prism_docmd_nowait(cmd, infoType, 0, 0);

	return result;
}

/* after init, the net device should be enabled */
int prism_cmd_enable(UINT16 macport)
{
	int result = 0;
	UINT16 cmd;

	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_ENABLE)|PRISM_CMD_MACPORT_SET(macport);
	result = prism_docmd_wait(cmd, 0,0,0);
	return result;
}

/* the net device disable function */
int prism_cmd_disable(UINT16 macport)
{
	int result = 0;
	UINT16 cmd;

	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_DISABLE)| PRISM_CMD_MACPORT_SET(macport);
	result = prism_docmd_wait(cmd, 0,0,0);
	return result;
}

/* when status register need us to diagnose, do it */
int prism_cmd_diagnose()
{
    int result = 0;
    UINT16 cmd;

    cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_DIAG);
    result = prism_docmd_wait(cmd, DIAG_PATTERNA, DIAG_PATTERNB, 0);
    return result;
}

/* copy data to buffer access path */
/* Note: only accept the buf which address is align 2 in our arm7 system */
int prism_copy_to_bap(UINT16 bap, UINT16 id, UINT16 offset, void *buf, UINT32 len)
{
	int result = 0;
	UINT8 *d = (UINT8*)buf;
	UINT16 *dw = buf;
	UINT32 selectreg;
	UINT32 offsetreg;
	UINT32 datareg;
	UINT32 i;
	volatile UINT16 reg;
	UINT16 savereg;
	int k = 0;

    selectreg = bap ? PRISM_SELECT1 : PRISM_SELECT0;
	offsetreg = bap ? PRISM_OFFSET1 : PRISM_OFFSET0;
    datareg   = bap ? PRISM_DATA1 : PRISM_DATA0;


	k = BAP_BUSY_TIMEOUT;
    /* Write id to select reg */
    reg = READREG(offsetreg);
    while(PRISM_OFFSET_ISBUSY(reg) && k) {
    	k--;
		udelay(5);
		reg = READREG(offsetreg);
	}

	if(!k) {
		result = -1;
		LOGPRINT;
		goto failed;
	}
	if (PRISM_OFFSET_ISERR(reg)) {
        result = -1;
        LOGPRINT;
        return result;
    }

	WRITEREG(selectreg, id);
	
	k = BAP_BUSY_TIMEOUT;
	/* Write offset to offset reg */
	reg = READREG(offsetreg);
	while (PRISM_OFFSET_ISBUSY(reg) && k) {
		k--;
		udelay(5);
		reg = READREG(offsetreg);
	}
	if(!k) {
		result = -1;
		LOGPRINT;
		goto failed;
	}
	if (PRISM_OFFSET_ISERR(reg)) {
    	result = -1;
    	LOGPRINT;
    	return result;
    }

	WRITEREG(offsetreg, offset);

	k = BAP_BUSY_TIMEOUT;
	/* Wait for offset[busy] to clear */
	reg = READREG(offsetreg);
	while (PRISM_OFFSET_ISBUSY(reg) && k) {
		k--;
		udelay(5);
		reg = READREG(offsetreg);
	}
    if (PRISM_OFFSET_ISERR(reg)) {
    	result = -1;
    	LOGPRINT;
    	return result;
    }
	/* Write even(len) buf contents to data reg */
	for ( i = 0; i < (len>>1); i++ ) {
	    WRITEREG(datareg, *dw++);
	}

    /* If len odd, handle last byte */
	if ( len % 2 )
    {
    	savereg = READREG(datareg);
    	WRITEREG(offsetreg, (offset+(len&0xfffe)));

    	/* Wait for offset[busy] to clear (see BAP_TIMEOUT) */
    	k = BAP_BUSY_TIMEOUT;
    	reg = READREG(offsetreg);
    	while (PRISM_OFFSET_ISBUSY(reg) && k) {
    		k--;
    		udelay(5);
    		reg = READREG(offsetreg);	
    	}
    	if(!k) {
			result = -1;
			LOGPRINT;
			goto failed;
		}
    	((UINT8*)(&savereg))[0] = d[len-1];
    	WRITEREG(datareg, savereg);
    }
failed:
    if (result)
        printk("copy_to_bap failed\r\n");
    return result;
}


/* Set configuration / information record */
int Write_RID_Config(UINT16 rid, void *buf, UINT16 len)
{
    int result = 0;
    prism_rec_t rec;

    rec.rid = rid;
    rec.reclen = (len/2)?((len+1)/2+1):((len/2) + 1);
    
    /* write the record header */
    result = prism_copy_to_bap(USER_BAP, rid, 0, &rec, 4);
    if ( result )
    	printk("Failure writing record header\n");

    /* write the record data (if there is any) */
    if ( len > 0 )
    {
        result = prism_copy_to_bap(USER_BAP, rid, 4, buf, len);
        if ( result )
            printk("Failure writing record data\r\n");
    }
    result = prism_cmd_access(1, rid);
    return result;
}

/* get configuration / information record */
int Read_RID_Config(UINT16 rid, void *buf)
{
    int result = 0;
    prism_rec_t rec;
    short len;
	
	result = prism_cmd_access(0, rid);
    	
    /* read the record header */
    result = prism_copy_from_bap(IRQ_BAP, rid, 0, &rec, 4);
    if ( result ) {
    	result = -1;
    	printk("Failure writing record header\r\n");
    	goto fail;
    }
	
	len = (rec.reclen-1)*2;
#ifdef WDEBUG
	printk("len:%d, rec.len: %d\n", len, rec.reclen);		
#endif
	
    /* read the record data (if there is any) */
    if(len > 0);
    {	
    	result = prism_copy_from_bap(IRQ_BAP, rid, 4, buf, len);
    	if ( result ) {
    		result = -1;
            printk("Failure writing record data\r\n");
            goto fail;
        }
	}
	result = rec.reclen;
fail:
    return result;
}

/* get Communication Tallies record's type and length*/
int Read_CT_InfoType(UINT16 Infofid, UINT16 *len)
{
	int result = 0;
    prism_rec_t rec;
    
    result = prism_copy_from_bap(IRQ_BAP, Infofid, 0, &rec, 4);
    if ( result ) {
    	result = -1;
    	printk("Failure writing record header\r\n");
    	goto fail;
    }
    
    *len = (rec.reclen-1)*2;
    result = rec.rid;
fail:
    return result;
}

/* get Commnication Tallies record's data */
int Read_CT_InfoData(UINT16 Infofid, void *buf, int len)
{
 
    int result = 0;
    
    /* read the record data (if there is any) */
    if(len > 0);
    {	
    	result = prism_copy_from_bap(USER_BAP, Infofid, 4, buf, len);//??user_bap
    	if ( result ) {
    		result = -1;
            printk("Failure writing record data\r\n");
            goto fail;
        }
	}
	result = len;
fail:

    return result;
}


/*------------------------------------------------------------------*/
/* send a command to device and wait for completion */
int prism_docmd_wait(UINT16 cmd, UINT16 parm0, UINT16 parm1, UINT16 parm2)
{
    volatile UINT16 reg;
    volatile UINT16 result;
    volatile UINT16 counter, i;
    	
    volatile UINT16 prismStatus;
    volatile UINT16 prismResp0;
    volatile UINT16 prismResp1;
    volatile UINT16 prismResp2;
    	
    i = CMD_BUSY_TIMEOUT;
    /* wait for the busy bit to clear */
    reg = READREG(PRISM_CMD);
    while (PRISM_CMD_ISBUSY(reg) && i) {
    	i--;
    	udelay(5);
    	reg = READREG(PRISM_CMD);
    }

    if (!PRISM_CMD_ISBUSY(reg))
    {
    	/* busy bit clear, write command */
    	WRITEREG(PRISM_PARAM0, parm0);
    	WRITEREG(PRISM_PARAM1, parm1);
    	WRITEREG(PRISM_PARAM2, parm2);
    	WRITEREG(PRISM_CMD, cmd);

    	/* Now wait for completion */
    	counter = 0;
    	reg = READREG(PRISM_EVSTAT);
		/* if is TX cmd, timout/30 */
    	if((cmd&0x1f) == PRISM_CMDCODE_TX)
    		i = CMD_COMPL_TIMEOUT/30;
    	else
    		i = CMD_COMPL_TIMEOUT;
    	while (!PRISM_EVSTAT_ISCMD(reg) && i)
    	{
            i--;
            udelay(10);
            reg = READREG(PRISM_EVSTAT);
        }
		
        if (PRISM_EVSTAT_ISCMD(reg))
        {
        	result = 0;
        	prismStatus = READREG(PRISM_STATUS);
        	prismResp0 = READREG(PRISM_RESP0);
        	prismResp1 = READREG(PRISM_RESP1);
        	prismResp2 = READREG(PRISM_RESP2);
        	WRITEREG(PRISM_EVACK, 0x0010);
        	result = PRISM_STATUS_RESULT_GET(prismStatus);
        }
        else
        {
	    	//printk("timeout:reg[%x]\n", reg);
        	result = prism_cmd_diagnose();
        	//printk("result [%x]\n", result);
        }
    }
    	
    return result;
}

/* send a command to device and no wait for completion */
int prism_docmd_nowait(UINT16 cmd, UINT16 parm0, UINT16 parm1, UINT16 parm2)
{
    volatile UINT16 reg;
    volatile UINT16 result;
    volatile UINT16 counter, i;
    	
    volatile UINT16 prismStatus;
    volatile UINT16 prismResp0;
    volatile UINT16 prismResp1;
    volatile UINT16 prismResp2;
    	
    i = CMD_BUSY_TIMEOUT;
    /* wait for the busy bit to clear */
    reg = READREG(PRISM_CMD);
    while (PRISM_CMD_ISBUSY(reg) && i) {
    	i--;
    	udelay(5);
    	reg = READREG(PRISM_CMD);
    }

    if (!PRISM_CMD_ISBUSY(reg))
    {
    	/* busy bit clear, write command */
    	WRITEREG(PRISM_PARAM0, parm0);
    	WRITEREG(PRISM_PARAM1, parm1);
    	WRITEREG(PRISM_PARAM2, parm2);
    	WRITEREG(PRISM_CMD, cmd);

    	/* Now wait for completion */
    	counter = 0;
    	reg = READREG(PRISM_EVSTAT);
		/* if is TX cmd, timout/30 */
    	if((cmd&0x1f) == PRISM_CMDCODE_TX)
    		i = CMD_COMPL_TIMEOUT/30;
    	else
    		i = CMD_COMPL_TIMEOUT;
    	while (!PRISM_EVSTAT_ISCMD(reg) && i)
    	{
            i--;
            udelay(10);
            reg = READREG(PRISM_EVSTAT);
        }
		
        result = 0;
        prismStatus = READREG(PRISM_STATUS);
        prismResp0 = READREG(PRISM_RESP0);
        prismResp1 = READREG(PRISM_RESP1);
        prismResp2 = READREG(PRISM_RESP2);
        WRITEREG(PRISM_EVACK, 0x0010);
        result = PRISM_STATUS_RESULT_GET(prismStatus);
    }
    	
    return result;
}


/* prism command allocate mem */
int prism_cmd_allocate(UINT16 len, unsigned short *txfid)
{
    int result = 0;
    UINT16 cmd;
    UINT16 reg;
    UINT32 i;

    if (len % 2)
    	result = -1;
    else
    {
    	cmd = PRISM_CMD_CMDCODE_SET(PRISM_CMDCODE_ALLOC);
    	result = prism_docmd_wait(cmd, len, 0, 0);
    }
    	
    reg = READREG(PRISM_EVSTAT);
    i = ALLOC_COMPL_TIMEOUT;
    while (!PRISM_EVSTAT_ISALLOC(reg) && i)
    {
    	i--;
        udelay(10);
        reg = READREG(PRISM_EVSTAT);
    }
    	
    if(!PRISM_EVSTAT_ISALLOC(reg)) {
    	printk("Alloc event timeout\n");
    	return -ETIMEDOUT;
    }	
    
    *txfid = READREG(PRISM_ALLOCFID);
    WRITEREG(PRISM_EVACK, PRISM_EVACK_ALLOC_SET(1));
    return result;
}

/* prism command definition, for device initialization */
int prism_cmd_initialize()
{
    int result = 0;

    result = prism_docmd_wait(PRISM_CMDCODE_INIT,0,0,0);
    return result;
}

/* this function wish the wireless device has been in slot , :P*/
/* otherwize it will fail :(, map device register to memory, and */
/* access memory to setup our device */
int prism_initmac(struct net_device *dev)
{
	prism_cnfDesireSSID_t ssid;
	unsigned short reg = 0;
	volatile int i;
	int result = 0;
	struct w740prism_priv *priv = (struct w740prism_priv *)dev->priv;
	
	/*---------------------------------------------------------------------------*/
	/*        should init for w90740 extern I/0 2 as programm interrupt, for tx, rx.           */           
	/*	     But our system has inited it :)										   */	
	//*((volatile unsigned int *) 0xFFF83000) = 0x00054554;   	// config GPIO
	//*((volatile unsigned int *) 0xFFF83000) = 0x00054554;   	// config GPIO
//	i = *((volatile unsigned int *) 0xFFF83000);
//	*((volatile unsigned int *) 0xFFF83000) = i & 0xffffcfff;	// config GPIO
	printk("GPIO: %x\n", *((volatile unsigned int *) 0xFFF83000));

//    *((volatile unsigned int *) 0xFFF83004) = 0x00100000;   		// GPIO20 -> output 
//    *((volatile unsigned int *) 0xFFF83008) = 0x00000000;   		// GPIO20 -> 0 
    *((volatile unsigned int *) 0xFFF83004) = 0x00000003;   		// GPIO0 and GPIO1 -> output 
    *((volatile unsigned int *) 0xFFF83008) = 0x00000002;   		// GPIO1 -> 1

    /*        System memory map. map external I/O bank 2 to start address:0x80000000  */
    /*	     length: 32M :P, is right no collusion									      */
    *((volatile unsigned int *) EXT2CON_REG) = 0x8007bffe;  	//ROM2 for Mem access

    // Set external click 0x00003ff8	
    *((volatile unsigned int *) 0xFFF0000c) = 0x00003fe8;   		// PLL OFF, 15MHz

    /*--------------------------------------------------------------------------------*/
#if 1
    *((volatile unsigned int *) 0xFFF83008) = 0x00000000;   		// GPIO1 -> 0 

    *((volatile unsigned short *) 0xC00003e0) = 0x80;       		// set Configuration Option Register 
    for(i = 0; i < 500000; i++);
    if ((reg = *((volatile unsigned short *) 0xC00003e0)) != 0x80) {
		printk("wireless driver reset failed, %x\n", reg);
		result= -1;
		goto out;
	}
    else
    {
    	printk("wireless memory reset to system map adress: %x, length: %dM\n" , 0xc0000000, 32);
	}

    /*be related with little endian,so value 0x4100 ??? */
    *((volatile unsigned short *) 0xC00003e0) = 0x41;       		//set Configuration Option Register
    for(i = 0; i < 30000; i++);
    if ((reg = *((volatile unsigned short *) 0xC00003e0)) != 0x41) {
		printk("wireless driver map to system map failed, %x\n", reg);
		result= -1;
		goto out;
	}
    else
    {
    	printk("wireless memory map to system map adress: %x, length: %dM\n" , 0xc0000000, 32);
	}
	*((volatile unsigned int *) 0xfff82010) = 0x01;
	
#if 0	
	WRITEREG(PRISM_SWSUPPORT0, HERMES_MAGIC);
	printk("magic value: %x\n", READREG(PRISM_SWSUPPORT0));
	printk("magic value: %x\n", READREG(PRISM_SWSUPPORT0));
	printk("magic value: %x\n", READREG(PRISM_SWSUPPORT0));
#endif
	result = prism_cmd_initialize();
    if (result != 0) {
    	printk("Initialize command failed\r\n");
    	result = -1;
    	goto out;
    }
    else
    	;//printk("Initialize command sucess\r\n");
    	
    /* make sure interrupts are disabled and any layabout events cleared */
    WRITEREG(PRISM_INTEN, PRISM_INTEN_DISABLE);
    WRITEREG(PRISM_EVACK, PRISM_EVENT_MASK);

	/* init fid queue */
    Init_Queue();
	Queue_dump();

    /* sets 0xFC84 TxRates */
    priv->bitratemode = DEFAULT_TXRATE_MODE;
    prism_hw_setiwrate(priv);
    
    /* sets Rxsthreshold */
    priv->rts_thresh = DEFAULT_RXSTHRESHOLD;
    prism_hw_setrts(priv);
    
    /* set ap density */
    priv->ap_density = DEFAULT_SENSE;
    prism_hw_setapdensity(priv);
    
    /* set up power */
    priv->pm_on = DEFAULT_POWMANAGE;
    priv->pm_mcast = DEFAULT_MULITRECEIVE;
    priv->pm_period = DEFAULT_MAXSLEEP;
    priv->pm_timeout = DEFAULT_PMHOLDDURATION;
    prism_hw_setpower(priv);
    /* sets 0xFC2A auth type */
    reg = PRISM_Authentication;
    Write_RID_Config(PRISM_RID_CNFAUTHENTICATION, &reg, PRISM_RID_CNFAUTHENTICATION_LEN);
	
	/* sets link type */
	priv->iw_mode = IW_MODE_INFRA;
	reg = priv->port_type = PRISM_MacPortType; 
	Write_RID_Config(PRISM_RID_CNFPORTTYPE, &reg, PRISM_RID_CNFPORTTYPE_LEN);
	
	/* set 0xFC02 desired SSID */
#if 1
    ssid.ssidLen = PRISM_SSIDLEN_DEFAULT;
    memcpy(ssid.ssidName, PRISM_DesireSSID_DEFAULT, ssid.ssidLen);
    Write_RID_Config(PRISM_RID_CNFOWNSSID, (UINT8 *)&ssid, PRISM_RID_CNFOWNSSID_LEN);
    Write_RID_Config(PRISM_RID_CNFDESIREDSSID, (UINT8 *)&ssid, PRISM_RID_CNFDESIREDSSID_LEN);
#endif

    /* Retrieve the maximum frame size */
    reg = FRAME_DATA_MAXLEN;
    Write_RID_Config(PRISM_RID_CNFMAXDATALEN, &reg, PRISM_RID_CNFMAXDATALEN_LEN);
    
    WRITEREG(PRISM_EVSTAT, PRISM_EVENT_MASK);
    WRITEREG(PRISM_INTEN, PRISM_INTEN_ENABLEBITS);

    /* enable MAC port 0 */
    prism_cmd_enable(0);
    //printk("Enable the Mac port 0,?\n");
    

#endif
    result = Read_RID_Config(PRISM_RID_CNFOWNMACADDR, priv->mac_address);
    for(i = 0; i < 6; i++)
    	printk("%x ", priv->mac_address[i]);
    printk("\n");
    result = Read_RID_Config(PRISM_RID_CNFDESIREDSSID, &ssid);

    //printk("*************************************************\n");
   // printk("TX Size: %d\n", sizeof(struct hermes_tx_descriptor));
    memcpy(priv->desired_essid, ssid.ssidName, ssid.ssidLen);
    priv->desired_essid[ssid.ssidLen] = '\0';
    
    printk("essid: %s, addr: %x\n", priv->desired_essid, priv->desired_essid);
    //printk("\n");
out:
	*((volatile unsigned int *) 0xFFF0000c) = 0x00003ff8; 	// 80MHz	 
	return result;
}

/* real device reset, called by prism_reset() */
int prism_reset_device(struct net_device *dev)
{
	prism_cnfDesireSSID_t ssid;
	struct w740prism_priv * priv = dev->priv;
	u16 reg;
	volatile int i;
	int k = CMD_BUSY_TIMEOUT;
	int ret = 0;

	priv = dev->priv;
	*((volatile unsigned int *) 0xFFF0000c) = 0x00003fe8;   	// PLL OFF, 15MHz	
#if 1
	i = *((volatile unsigned int *) 0xFFF83008);
    *((volatile unsigned int *) 0xFFF83008) = i | 0x00000002;   /* GPIO1 -> 1 */
    for (i=0; i<10; i++);
	i = *((volatile unsigned int *) 0xFFF83008);
    *((volatile unsigned int *) 0xFFF83008) = i & 0xfffffffd;   /* GPIO1 -> 0 */
	//printk("<<<reset>>>\n");
    for(i = 0; i < 30000; i++);
	*((volatile unsigned int *) 0xfff82124) = 0x10;		// add by chp; disable interrupt

	*((volatile unsigned short *) 0xC00003e0) = 0x80;       	// set Configuration Option Register 

    for(i = 0; i < 500000; i++);
    if ((reg = *((volatile unsigned short *) 0xC00003e0)) != 0x80) {
		printk("wireless driver reset failed, %x\n", reg);
		ret = -1;
		goto out;
	}
    else
    {
    	printk("wireless memory reset to system map adress: %x, length: %dM\n" , 0xc0000000, 32);
	}

	/*be related with little endian,so value 0x4100 ??? */
    *((volatile unsigned short *) 0xC00003e0) = 0x41;       	// set Configuration Option Register
    for(i = 0; i < 30000; i++);
    if ((reg = *((volatile unsigned short *) 0xC00003e0)) != 0x41) {
		printk("wireless driver map to system map failed, %x\n", reg);
		ret = -1;
		goto out;
	}
    else
    {
    	printk("wireless memory map to system map adress: %x, length: %dM\n" , 0xc0000000, 32);
	}
	*((volatile unsigned int *) 0xfff82010) = 0x01;
#endif	
	/* make sure interrupts are disabled and any layabout events cleared */
    WRITEREG(PRISM_INTEN, PRISM_INTEN_DISABLE);
    WRITEREG(PRISM_EVACK, PRISM_EVENT_MASK);
    
    /* First wait for the command register to unbusy */
	reg = READREG(PRISM_CMD);
	while ( (PRISM_CMD_ISBUSY(reg)) && k ) {
		k--;
		udelay(1);
		reg = READREG(PRISM_CMD);
	}
	//printk("hermes_issue_cmd: did %d retries.\n", CMD_BUSY_TIMEOUT-k);
    	
    reg = READREG(PRISM_EVSTAT);
    WRITEREG(PRISM_EVACK, reg);
    
    
    ret = prism_cmd_initialize();
    if (ret != 0) {
    	printk("Initialize command failed\r\n");
    	ret = -1;
    	goto out;
    }
    else
    	;//printk("Initialize command sucess\r\n");
    
   	Init_Queue();
	Queue_dump();
	 
	/* sets link type */
	reg = priv->port_type; 
	DEBUG("port type: %x\n", priv->port_type);
	Write_RID_Config(PRISM_RID_CNFPORTTYPE, &reg, PRISM_RID_CNFPORTTYPE_LEN);
	DEBUG("allow ibss %x\n", priv->allow_ibss);
#if 0
    reg = PRISM_TxRate;
    Write_RID_Config(PRISM_RID_TXRATECNTL, &reg, PRISM_RID_TXRATECNTL_LEN);
#else
	prism_hw_setiwrate(priv);
#endif
	
	/* set RxSthread */
	prism_hw_setrts(priv);


	/* set ap density */
	prism_hw_setapdensity(priv);
	
	/* set power */
	prism_hw_setpower(priv);
	
    /* set 0xFC02 desired SSID */
    ssid.ssidLen = strlen(priv->desired_essid);
    memcpy(ssid.ssidName, priv->desired_essid, ssid.ssidLen);
    printk("essid: %s; addr: %x\n", priv->desired_essid, priv->desired_essid);
    Write_RID_Config(PRISM_RID_CNFOWNSSID, (UINT8 *)&ssid, PRISM_RID_CNFOWNSSID_LEN);
    Write_RID_Config(PRISM_RID_CNFDESIREDSSID, (UINT8 *)&ssid, PRISM_RID_CNFDESIREDSSID_LEN);
	
    /* Retrieve the maximum frame size */
    reg = FRAME_DATA_MAXLEN;
    Write_RID_Config(PRISM_RID_CNFMAXDATALEN, &reg, PRISM_RID_CNFMAXDATALEN_LEN);
    
    /* set the channel */
    reg = priv->channel;
    Write_RID_Config(PRISM_RID_CNFOWNCHANNEL, &reg, PRISM_RID_CNFOWNCHANNEL_LEN);

    /* Set up encryption */
#if 1
	ret = prism_hw_setup_wep(priv);
	if (ret) {
		printk(KERN_ERR "%s: Error %d activating WEP.\n",
		       dev->name, ret);
		goto out;
	}
#endif
	WRITEREG(PRISM_EVSTAT, PRISM_EVENT_MASK);
    WRITEREG(PRISM_INTEN, PRISM_INTEN_ENABLEBITS);

    /* enable MAC port 0 */
    prism_cmd_enable(0);
    //printk("Enable the Mac port 0,?\n");

	*((volatile unsigned int *) 0xfff82120) = 0x10;		// add by chp; enable interrupt

out:
	 *((volatile unsigned int *) 0xFFF0000c) = 0x00003ff8; 	//80MHz 
    return ret;
}

/*----------------------------------------------------------------
* prism_txexc
*
* Handles the TxExc event.  A Transmit Exception event indicates
* that the MAC's TX process was unsuccessful - so the packet did
* not get transmitted.
*
* Arguments:
*
* Returns: 
*	nothing
*
* Side effects:
*
* Call context:
*	interrupt
----------------------------------------------------------------*/
void prism_txexc()
{
    UINT16 status;
    UINT16 fid;
    int result = 0;

    /* Collect the status and display */
    fid = READREG(PRISM_TXCOMPLFID);
    result = prism_copy_from_bap(IRQ_BAP, fid, 0, &status, sizeof(status));

    //printk(" %x: [%x]\n", fid, status);

    if (result)
    	printk("prism_txexc:copy_from_bap failed\r\n");
}

/* copy data from buffer access path */
/* Note: only accept the buf which address is align 2 */
int prism_copy_from_bap(UINT16 bap, UINT16 id, UINT16 offset, void *buf, UINT32 len)
{
   	int result = 0;
   	UINT8 *d = (UINT8*)buf;
   	UINT16 *dw = buf;
   	UINT32 selectreg;
   	UINT32 offsetreg;
   	UINT32 datareg;
   	UINT32 i;
   	volatile UINT16 reg = 0;
	int k = 0;
	


   	selectreg = bap ? PRISM_SELECT1 : PRISM_SELECT0;
	offsetreg = bap ? PRISM_OFFSET1 : PRISM_OFFSET0;
    datareg   = bap ? PRISM_DATA1 : PRISM_DATA0;

	k = BAP_BUSY_TIMEOUT;	
    /* Write id to select reg */
    reg = READREG(offsetreg);
   	while (PRISM_OFFSET_ISBUSY(reg) && k) {
   		k--;
   		udelay(5);
   		reg = READREG(offsetreg);
   	}
   	if(!k) { 
   		result = -1;
   		LOGPRINT;
   		goto failed;
   	}
   	WRITEREG(selectreg, id);

	k = BAP_BUSY_TIMEOUT;
    /* Write offset to offset reg */
    reg = READREG(offsetreg);
   	while (PRISM_OFFSET_ISBUSY(reg) && k)
	{
		k--;
		udelay(5);
		reg = READREG(offsetreg);
	}
	if(!k) { 
    	result = -1;
    	LOGPRINT;
    	goto failed;
   	}
   	WRITEREG(offsetreg, offset);
    	
	k = BAP_BUSY_TIMEOUT;
    /* Wait for offset[busy] to clear */
    reg=READREG(offsetreg);
   	while (PRISM_OFFSET_ISBUSY(reg) &&k )
   	{
   		k--;
		udelay(5);
		reg=READREG(offsetreg);	
	}
    if (PRISM_OFFSET_ISERR(reg)) { //??
    	//result = reg;
    	LOGPRINT;
    	result = -1;
    	return result;
    }
    /* Read even(len) buf contents from data reg */
    for ( i = 0; i < (len>>1); i++ ) {
    	*dw++ = READREG(datareg);
    }
    /* If len odd, handle last byte */
    if ( len % 2 )
    {
    	reg = READREG(datareg);
    	d[len-1] = ((UINT8*)(&reg))[0];
    }

    if (result) {
    	LOGPRINT;
    	printk("copy_from_bap failed\r\n");
    }
failed:
    return result;

}


static int prism_hw_get_essid(struct w740prism_priv *priv, int *active,
			      char buf[IW_ESSID_MAX_SIZE+1])
{
#if 0
	hermes_t *hw = &priv->hw;
	int err = 0;
	struct hermes_idstring essidbuf;
	char *p = (char *)(&essidbuf.val);
	int len;


	orinoco_lock(priv);

	if (strlen(priv->desired_essid) > 0) {
		/* We read the desired SSID from the hardware rather
		   than from priv->desired_essid, just in case the
		   firmware is allowed to change it on us. I'm not
		   sure about this */
		/* My guess is that the OWNSSID should always be whatever
		 * we set to the card, whereas CURRENT_SSID is the one that
		 * may change... - Jean II */
		u16 rid;

		*active = 1;

		rid = (priv->port_type == 3) ? HERMES_RID_CNFOWNSSID :
			HERMES_RID_CNFDESIREDSSID;
		
		err = hermes_read_ltv(hw, USER_BAP, rid, sizeof(essidbuf),
				      NULL, &essidbuf);
		if (err)
			goto fail_unlock;
	} else {
		*active = 0;

		err = hermes_read_ltv(hw, USER_BAP, HERMES_RID_CURRENTSSID,
				      sizeof(essidbuf), NULL, &essidbuf);
		if (err)
			goto fail_unlock;
	}

	len = le16_to_cpu(essidbuf.len);

	memset(buf, 0, IW_ESSID_MAX_SIZE+1);
	memcpy(buf, p, len);
	buf[len] = '\0';

 fail_unlock:
	orinoco_unlock(priv);

	return err;       
#endif
	return 0;
}

/* Get current connected AP's essid */
int prism_ioctl_getessid(struct net_device *dev, struct iw_point *erq)
{
	struct w740prism_priv *priv = dev->priv;
	char essidbuf[IW_ESSID_MAX_SIZE+1];
	prism_cnfDesireSSID_t our_ssid;
	int err = 0;
	
	err = Read_RID_Config(PRISM_RID_CNFDESIREDSSID, &our_ssid);
	DEBUG("essid len: %d\n", our_ssid.ssidLen);
	if(our_ssid.ssidLen <= 0) {
		erq->flags = 0;
		return 0;
	}
	
	erq->flags = 1;
	erq->length = our_ssid.ssidLen;
	memcpy(essidbuf, our_ssid.ssidName, our_ssid.ssidLen);
	if (erq->pointer)
		if ( copy_to_user(erq->pointer, essidbuf, erq->length) )
			return -EFAULT;
	return 0;
}

/* it should get net working freq or channel, we only get channel */
/* if AD-Hoc mode, return OWNCHANNEL register's value			  */
/* else if infrastructure mode, return CURRENT regiester's value  */
long prism_hw_get_freq(struct w740prism_priv *priv)
{
	int err = 0;
	u16 channel;
	long freq = 0;
	prism_lock(priv);
	DEBUG("Current Mode: %d\n", priv->iw_mode);
	if(priv->iw_mode == IW_MODE_INFRA) {
		err = Read_RID_Config(PRISM_RID_CURRENTCHANNEL, &channel);
	}
	else if(priv->iw_mode == IW_MODE_ADHOC) {
		err = Read_RID_Config(PRISM_RID_CNFOWNCHANNEL, &channel);
	}
	else
		err = -1;
	if(err < 0)
	{
		printk("%s: read channel erro\n");
		err = -EBUSY;
		goto out;
	}
	
	if ( (channel < 1) || (channel > NUM_CHANNELS) ) {
		struct net_device *dev = priv->ndev;

		printk("%s: Channel out of range (%d)!\n", dev->name, channel);
		err = -EBUSY;
		goto out;

	}
	freq = channel;
	//printk("channel: %d, %d\n", channel, freq);
	err = 0;
out:
	prism_unlock(priv);
	return err ? err : freq;
}

/* Set essid */
int prism_ioctl_setessid(struct net_device *dev, struct iw_point *essid)
{
	int err = 0;
	prism_lock(dev->priv);
	memcpy(((struct w740prism_priv *)(dev->priv))->desired_essid, (unsigned char *)essid->pointer, essid->length);
	DEBUG("essid: %s, len: %d\n", ((struct w740prism_priv *)dev->priv)->desired_essid, essid->length);
	prism_unlock(dev->priv);
	
	return err;
}

/* set freq(it refers to channel here) */
int prism_ioctl_setfreq(struct net_device *dev, struct iw_freq *freq) 
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	u16 reg;
	
	//printk("value: %x\n", freq->m);
	if( (freq->m < 1) || (freq->m > NUM_CHANNELS)) {
			printk("%s: Channel out of range (%d)!\n", dev->name, freq->m);
			err = -EFAULT;
			goto out;
	}
	priv->channel = freq->m;
out:
	return err;
}

/* get Wep key */
int prism_ioctl_getiwencode(struct net_device *dev, struct iw_point *erq)
{
	struct w740prism_priv *priv = dev->priv;
	int index = (erq->flags & IW_ENCODE_INDEX) - 1;
	u16 xlen = 0;
	char keybuf[PRISM_MAX_KEY_SIZE];
	int i, j;
	u16 defaultkeyid, wepflags, authentication;
	
	prism_lock(priv);
	if ((index < 0) || (index >= PRISM_MAX_KEYS))
		index = priv->tx_key;

	erq->flags = 0;
	if (! priv->wep_on)
	erq->flags |= IW_ENCODE_DISABLED;
	erq->flags |= index + 1;
	
	xlen = le16_to_cpu(priv->keys[index].len);
	erq->length = xlen;

	if (erq->pointer) {
		memcpy(keybuf, priv->keys[index].data, PRISM_MAX_KEY_SIZE);
	}
	
	prism_unlock(priv);
	
	if (erq->pointer) {
		if (copy_to_user(erq->pointer, keybuf, xlen))
			return -EFAULT;
	}
//	for(i = 0; i < xlen; i++) {
//		printk("%x", keybuf[i]);
//	}
//	printk("\n");
	for(j = 0; j < 4; j++) {
		Read_RID_Config(PRISM_RID_CNFDEFAULTKEY0+j, keybuf);
//		for(i = 0; i < xlen; i++) {
//			printk("%x", keybuf[i]);
//		}
//		printk("\n");
	}
	Read_RID_Config(PRISM_RID_CNFWEPDEFAULTKEYID, &defaultkeyid);
	Read_RID_Config(PRISM_RID_CNFWEPFLAGS, &wepflags);
	Read_RID_Config(PRISM_RID_CNFAUTHENTICATION, &authentication);
//	printk("defaultkeyid: %x, wepflags: %x, authentication: %x\n", defaultkeyid, wepflags, authentication);
	return 0;
}

/* set Wep key */
int prism_ioctl_setiwencode(struct net_device *dev, struct iw_point *erq)
{
	int i;
	struct w740prism_priv *priv = dev->priv;
	char buf[IW_ENCODING_TOKEN_MAX+1];
	int index = (erq->flags & IW_ENCODE_INDEX) - 1;
	int setindex = priv->tx_key;
	int enable = priv->wep_on;
	int restricted = priv->wep_restrict;
	int xlen = 0;
	int err = 0;
//printk("index: %d, len: %d, nowlen: %d\n", index, erq->length, priv->keys[index].len);	
	if (erq->length > IW_ENCODING_TOKEN_MAX)
	{
		printk("Too long key\n");
		return -EFAULT;
	}
	
	if (erq->pointer) {
		if(copy_from_user(buf, erq->pointer, erq->length))
			return -EFAULT;
	}
	prism_lock(priv);
	if (erq->pointer) {
		if (erq->length > PRISM_MAX_KEY_SIZE) {
			printk("Too long key string\n");
			err = -E2BIG;
			goto out;
		}
		
//		if ( (erq->length > LARGE_KEY_SIZE)
//		     || ( ! priv->has_big_wep && (erq->length > SMALL_KEY_SIZE))  ) {
//			err = -EINVAL;
//			goto out;
//		}
		
		if ((index < 0) || (index >= PRISM_MAX_KEYS))
			index = priv->tx_key;
		
		if (erq->length > SMALL_KEY_SIZE) {
			LOGPRINT;
			xlen = LARGE_KEY_SIZE;
		} else if (erq->length > 0) {
			LOGPRINT;
			xlen = SMALL_KEY_SIZE;
		} else
			xlen = 0;
		
		/* Switch on WEP if off */
		if ((!enable) && (xlen > 0)) {
			setindex = index;
			enable = 1;
		}
	} else {
		/* Important note : if the user do "iwconfig eth0 enc off",
		 * we will arrive there with an index of -1. This is valid
		 * but need to be taken care off... Jean II */
		if ((index < 0) || (index >= PRISM_MAX_KEYS)) {
			if((index != -1) || (erq->flags == 0)) {
				err = -EINVAL;
				goto out;
			}
		} else {
			/* Set the index : Check that the key is valid */
			if(priv->keys[index].len == 0) {
				err = -EINVAL;
				goto out;
			}
			setindex = index;
		}
	}
	
	if (erq->flags & IW_ENCODE_DISABLED)
		enable = 0;
	/* Only for Prism2 & Symbol cards (so far) - Jean II */
	if (erq->flags & IW_ENCODE_OPEN)
		restricted = 0;
	if (erq->flags & IW_ENCODE_RESTRICTED)
		restricted = 1;

	if (erq->pointer) {
		priv->keys[index].len = cpu_to_le16(xlen);
		memset(priv->keys[index].data, 0, sizeof(priv->keys[index].data));
		memcpy(priv->keys[index].data, buf, erq->length);
	}
	priv->tx_key = setindex;
	priv->wep_on = enable;
	priv->wep_restrict = restricted;
	
 out:
	buf[erq->length] = '\0';
//	for(i = 0; i < erq->length; i++)
//		printk("%x", buf[i]);
//	printk("\n");
//	printk("tx_key: %d, wep_on: %d, wep_restrict: %d\n", priv->tx_key, priv->wep_on, priv->wep_restrict);
//	printk("tx_key's length: %d\n", priv->keys[priv->tx_key].len);
	prism_unlock(priv);
	return err;
}

/* real setup the Wep key to hardware */
int prism_hw_setup_wep(struct w740prism_priv *priv)
{
	int err = 0;
	int keylen;
	int i, j;
	int auth_flag;
	int master_wep_flag = 0;		//is off
	
//	printk("prism_hw_setup_wep\n");
	if (priv->wep_on) {
		/* Write all 4 keys */
		keylen = le16_to_cpu(priv->keys[priv->tx_key].len);
		for(i = 0; i < PRISM_MAX_KEYS; i++) {
//			printk("keylen: %d, reallen: %d\n", keylen, priv->keys[i].len);
			if (keylen > LARGE_KEY_SIZE) {
				printk("%s: Key %d has oversize length %d.\n",
			       		priv->ndev->name, i, keylen);
					return -E2BIG;
			}
//			printk("%dkey's len: %d\n", i, keylen);
//			for( j = 0; j < keylen; j++)
//				printk("%x", priv->keys[i].data[j]);
//			printk("\n");
			err = Write_RID_Config(PRISM_RID_CNFDEFAULTKEY0 + i, priv->keys[i].data, keylen);
			if (err)
				return err;
		}

	err = Write_RID_Config(PRISM_RID_CNFWEPDEFAULTKEYID, &priv->tx_key, PRISM_RID_CNFWEPDEFAULTKEYID_LEN);
	if (err)
		return err;
	
	if (priv->wep_restrict) {
		auth_flag = 2;
		master_wep_flag = 3;
	} else {
		auth_flag = 1;
		master_wep_flag = 1; /* Intersil */
	}

	err = Write_RID_Config(PRISM_RID_CNFAUTHENTICATION, &auth_flag, PRISM_RID_CNFAUTHENTICATION_LEN); 
	if (err)
		return err;
	}
	Read_RID_Config(PRISM_RID_CNFAUTHENTICATION, &auth_flag);
//	printk("Auth_flag: %x\n", auth_flag);
	/* Master WEP setting : on/off */
	err = Write_RID_Config(PRISM_RID_CNFWEPFLAGS, &master_wep_flag, PRISM_RID_CNFWEPFLAGS_LEN);
	if (err)
		return err;	
	return 0;
}

int prism_ioctl_setscan(struct net_device *dev, struct iw_point *erq)
{
	int err = 0;
	unsigned short len;
	unsigned int i, j, cnt;
	struct w740prism_priv *priv = dev->priv;
//	printk("erq->length: %d\n", erq->length);
	prism_lock(priv);
#if 1
	prism_cmd_inquiry(PRISM_INQ_SCAN);
	if(down_interruptible(&priv->sema))
			err = -1;
	
	
	DEBUG("erq addr: %x\n", erq);
	prism_ioctl_getaplist(dev, erq);
#endif
	prism_unlock(priv);
	return err;
}

int prism_hw_get_bssid(struct net_device *dev, char buf[ETH_ALEN])
{
	struct w740prism_priv *priv = dev->priv;
	int err = 0;
	int i = 0;
	
	err = Read_RID_Config(PRISM_RID_CURRENTBSSID, buf);
	
//	for(i = 0; i < 6; i++)
//		printk("%x.", buf[i]);
//	printk("\n");
	return err;
}

int prism_ioctl_getstat(struct net_device *dev, struct iw_point *erq)
{
	int err = 0;
	int status = 0;
//	printk("%s: %d\n", __FILE__,__LINE__);
	prism_cmd_inquiry(PRISM_INQ_ASSOCIATIONSTATUS);
//	printk("%s: %d\n", __FILE__,__LINE__);
	return err;
}


int prism_ioctl_getlinkstat(struct net_device *dev, struct iw_point *erq)
{
	int err = 0;
	int status = 0;
	struct w740prism_priv *priv = (struct w740prism_priv *)dev->priv;
//	printk("%s: %d\n", __FILE__,__LINE__);
	prism_lock(priv);
	if (erq->pointer) {
		if (copy_to_user(erq->pointer, &(priv->status), erq->length))
			return -EFAULT;
	}
	prism_unlock(priv);
	//prism_cmd_inquiry(PRISM_INQ_LINKSTATUS);
//	printk("%s: %d\n", __FILE__,__LINE__);
	return err;
}

int prism_ioctl_getiwrate(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	int rate = 0;
	int i;
	err = Read_RID_Config(PRISM_RID_CURRENTTXRATE, &rate);
	
	for(i = 0; i < BITRATE_TABLE_SIZE; i++)
	{
		if (bitrate_table[i].prism_txratectrl == rate) {
				break;
		}
	}
	if(i >= BITRATE_TABLE_SIZE)
	{
		printk("Can't get correct rate\n");
		i = 0;
	}
	
	iwp->value = bitrate_table[i].bitrate * 100000;
//	printk("Current rate: %d\n", iwp->value);
	iwp->disabled = 0;
	return err;
}

int prism_ioctl_setiwrate(struct net_device *dev, struct iw_param *iwp)
{
	struct w740prism_priv *priv = dev->priv;
	int err = 0;
	int ratemode = -1;
	int bitrate; /* 100s of kilobits */
	int i;
//printk("rate: %d\n", iwp->value);	
	if (iwp->value == -1)
		bitrate = 110;
	else {
		if (iwp->value % 100000)
			return -EINVAL;
		bitrate = iwp->value / 100000;
	}

	if ( (bitrate != 10) && (bitrate != 20) &&
	     (bitrate != 55) && (bitrate != 110) )
		return -EINVAL;

	for (i = 0; i < BITRATE_TABLE_SIZE; i++)
		if ( (bitrate_table[i].bitrate == bitrate)) {
			ratemode = i;
			break;
		}
	
	if (ratemode == -1)
		return -EINVAL;

	prism_lock(priv);
	priv->bitratemode = ratemode;
	prism_unlock(priv);

	return err;
}

int prism_hw_setiwrate(struct w740prism_priv *priv)
{
	int err = 0;
//	printk("ratemode: %x\n", priv->bitratemode);
//
	if (priv->bitratemode >= BITRATE_TABLE_SIZE) {
		printk("%s: Invalid bitrate mode %d\n",
		       "wlan", priv->bitratemode);
		return -EINVAL;
	}
	err = Write_RID_Config(PRISM_RID_TXRATECNTL, &bitrate_table[priv->bitratemode].prism_txratectrl, PRISM_RID_TXRATECNTL_LEN);
	return err;	
}

int prism_ioctl_setrts(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	int val = iwp->value;

	if (iwp->disabled)
		val = 2347;

	if ( (val < 0) || (val > 3000) )
		return -EINVAL;

	prism_lock(priv);
	priv->rts_thresh = val;
	prism_unlock(priv);
	
	return err;
}

int prism_hw_setrts(struct w740prism_priv *priv)
{
	int err = 0;
	
	/* Set RTS threshold */
	err = Write_RID_Config(PRISM_RID_RTSTHRESHOLD, &priv->rts_thresh, PRISM_RID_RTSTHRESHOLD_LEN);
	
	return err;
}

int prism_ioctl_getsens(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	int val = 0;
	
	Read_RID_Config(PRISM_RID_CNFSYSTEMSCALE, &val);
//	printk("Current sense: %d\n", val);
	iwp->value = val;
	iwp->fixed = 0; /* auto */

	return 0;
}

int prism_ioctl_setsens(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	int val = iwp->value;

	if ((val < 1) || (val > 3))
		return -EINVAL;
	
	prism_lock(priv);
	priv->ap_density = val;
	prism_unlock(priv);

	return err;
}

int prism_hw_setapdensity(struct w740prism_priv *priv)
{
	int err = 0;
	err = Write_RID_Config(PRISM_RID_CNFSYSTEMSCALE, &priv->ap_density, PRISM_RID_CNFSYSTEMSCALE_LEN);
	if (err)
		return err;
	return err;
}

int prism_ioctl_getpower(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	u16 enable, period, timeout, mcast;
	
	prism_lock(priv);
	
	Read_RID_Config(PRISM_RID_CNFPMENABLED, &enable);
	Read_RID_Config(PRISM_RID_CNFMAXSLEEPDURATION, &period);
	Read_RID_Config(PRISM_RID_CNFPMHOLDOVERDURATION, &timeout);
	Read_RID_Config(PRISM_RID_CNFMULTICASTRECEIVE, &mcast);
	
	iwp->disabled = !enable;
	/* Note : by default, display the period */
	if ((iwp->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
		iwp->flags = IW_POWER_TIMEOUT;
		iwp->value = timeout * 1000;
	} else {
		iwp->flags = IW_POWER_PERIOD;
		iwp->value = period * 1000;
	}
	if (mcast)
		iwp->flags |= IW_POWER_ALL_R;
	else
		iwp->flags |= IW_POWER_UNICAST_R;

 out:
	prism_unlock(priv);
	return err;
}

int prism_ioctl_setpower(struct net_device *dev, struct iw_param *iwp)
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	
	prism_lock(priv);
	if (iwp->disabled) {
		priv->pm_on = 0;
	} else {
		switch (iwp->flags & IW_POWER_MODE) {
		case IW_POWER_UNICAST_R:
			priv->pm_mcast = 0;
			priv->pm_on = 1;
			break;
		case IW_POWER_ALL_R:
			priv->pm_mcast = 1;
			priv->pm_on = 1;
			break;
		case IW_POWER_ON:
			break;
		default:
			err = -EINVAL;
		}
		
		if (err)
			goto out;
		
		if (iwp->flags & IW_POWER_TIMEOUT) {
			priv->pm_on = 1;
			priv->pm_timeout = iwp->value / 1000;
		}
		if (iwp->flags & IW_POWER_PERIOD) {
			priv->pm_on = 1;
			priv->pm_period = iwp->value / 1000;
		}
		if(!priv->pm_on) {
			err = -EINVAL;
			goto out;
		}			
	}
out:
	prism_unlock(priv);
	return err;
}


int prism_hw_setpower(struct w740prism_priv *priv)
{
	int err = 0;
	err = Write_RID_Config(PRISM_RID_CNFPMENABLED, &priv->pm_on, PRISM_RID_CNFPMENABLED_LEN);
	if (err)
		return err;
	err = Write_RID_Config(PRISM_RID_CNFMAXSLEEPDURATION, &priv->pm_period, PRISM_RID_CNFMAXSLEEPDURATION_LEN);
	if (err)
		return err;
	err = Write_RID_Config(PRISM_RID_CNFPMHOLDOVERDURATION, &priv->pm_timeout, PRISM_RID_CNFPMHOLDOVERDURATION_LEN);
	if (err)
		return err;		
	err = Write_RID_Config(PRISM_RID_CNFMULTICASTRECEIVE, &priv->pm_mcast, PRISM_RID_CNFMULTICASTRECEIVE_LEN);
	if (err)
		return err;	
	return err;
}

int get_scan_result(UINT16 infoid, int lens, struct w740prism_priv *priv)
{
	int result = 0;
	int len, j, i, cnt;
#ifdef SCAN_ENABLE
	struct prism_ScanResult * scan;
	prism_lock(priv);
	 
	len = Read_CT_InfoData(infoid, &priv->scan_res, lens);
	scan = &priv->scan_res;
//	printk("len=%d\n", len);
	len = len/2;
	cnt = (len - 2) / 31;
	priv->scan_res_len = cnt;
//	printk("reason[%d], cnt: %d\n", scan->scanreason, cnt);
	for (j=0; j<cnt; j++)
	{
#if 1
    	if (scan->result[j].chid > 14) {
    		LOGPRINT;
    		break;
    	}
//		printk("off: %d, j: %d\n", scan->result+j, j);
//   		printk("channel[%d]\n", scan->result[j].chid);
//
//		printk("bssid [");
//   		for (i=0; i<6; i+=2)
//   			printk(" %02x %02x", scan->result[j].bssid[i+1], scan->result[j].bssid[i]);
//		printk("]\n");
//		printk("ssid [");
//		len = scan->result[j].ssid.ssidLen;
//		if (len % 2)
//		{
//			for (i=0; i<len-1; i+=2)
//				printk(" %02x %02x", scan->result[j].ssid.ssidName[i+1], scan->result[j].ssid.ssidName[i]);
//			printk(" %02x", scan->result[j].ssid.ssidName[len]);
//		}
//		else
//		{
//			for (i=0; i<len; i+=2)
//				printk(" %02x %02x", scan->result[j].ssid.ssidName[i+1], scan->result[j].ssid.ssidName[i]);
//		}
//		printk("]\n");
#endif
	}	
	up(&priv->sema);
	prism_unlock(priv);
#endif
	return result;
}

int prism_ioctl_getaplist(struct net_device *dev, struct iw_point * iwp)
{
	int err = 0;
	struct w740prism_priv *priv = dev->priv;
	int num = 0;
	int i, len;
#if 1
	int j;
#endif
	prism_ScanResultSub_t *resultp = &(priv->scan_res.result[0]);
DEBUG("iwp: %x, iwp->length: %d\n", iwp, iwp->length);	
	prism_lock(priv);

	if(iwp->length == 0)
	{
		iwp->length = priv->scan_res_len;
		DEBUG("iwp->length: %d\n", iwp->length);
	}
#if 1
	else
	{
		len = (iwp->length > priv->scan_res_len ? priv->scan_res_len:iwp->length);
		for(i = 0; i < len; i++)
		{
			if (iwp->pointer)
				if ( copy_to_user(iwp->pointer + sizeof(prism_cnfDesireSSID_t)*i, 
									&((resultp+i)->ssid), 
									sizeof(prism_cnfDesireSSID_t) ) 
					)
					return -EFAULT;	
#if 0
			for(j = 0; j < ((resultp+i)->ssid).ssidLen; j++)
			{
				printk("%c", ((resultp+i)->ssid).ssidName[j]);
			}
			printk("\n");
#endif
		}
	}
#endif
	prism_unlock(priv);
	return err;
}

void set_port_type(struct w740prism_priv *priv)
{
	switch (priv->iw_mode) {
	case IW_MODE_INFRA:
		priv->port_type = 1;
		priv->allow_ibss = 0;
		break;
	case IW_MODE_ADHOC:
		if (priv->prefer_port3) {
			priv->port_type = 3;
			priv->allow_ibss = 0;
		} else {
			priv->port_type = 0;//priv->ibss_port;
			priv->allow_ibss = 1;
		}
		break;
	default:
		printk("%s: Invalid priv->iw_mode\n",
		       priv->ndev->name);
	}
}

int prism_ioctl_setspy(struct net_device *dev, struct iw_point *iwp)
{
	int result = 0;
	struct w740prism_priv *priv = dev->priv;
	struct sockaddr address[IW_MAX_SPY];
	int number = iwp->length;
	int i;
	
	if (number > IW_MAX_SPY)
		return -E2BIG;

	if (iwp->pointer) {
		if (copy_from_user(address, iwp->pointer,
				   sizeof(struct sockaddr) * number))
			return -EFAULT;
	}

	prism_lock(priv);
	priv->spy_number = 0;

	if (number > 0) {
		for (i = 0; i < number; i++)
			memcpy(priv->spy_address[i], address[i].sa_data,
			       ETH_ALEN);
		memset(priv->spy_stat, 0,
		       sizeof(struct iw_quality) * IW_MAX_SPY);
		priv->spy_number = number;
	}

	DEBUG("%s: New spy list:\n", dev->name);
	for (i = 0; i < number; i++) {
		DEBUG("%s: %d - %02x:%02x:%02x:%02x:%02x:%02x\n",
		      dev->name, i+1,
		      priv->spy_address[i][0], priv->spy_address[i][1],
		      priv->spy_address[i][2], priv->spy_address[i][3],
		      priv->spy_address[i][4], priv->spy_address[i][5]);
	}

	prism_unlock(priv);
	return result;
}

int prism_ioctl_getspy(struct net_device *dev, struct iw_point *iwp)
{
	struct w740prism_priv *priv = dev->priv;
	struct sockaddr address[IW_MAX_SPY];
	struct iw_quality spy_stat[IW_MAX_SPY];
	int number;
	int i;

	prism_lock(priv);

	number = priv->spy_number;
	if ((number > 0) && (iwp->pointer)) {
		for (i = 0; i < number; i++) {
			memcpy(address[i].sa_data, priv->spy_address[i],
			       ETH_ALEN);
			address[i].sa_family = AF_UNIX;
		}
		/* Copy stats */
		memcpy(&spy_stat, priv->spy_stat,
		       sizeof(struct iw_quality) * IW_MAX_SPY);
		for (i=0; i < number; i++)
			priv->spy_stat[i].updated = 0;
	}

	prism_unlock(priv);

	iwp->length = number;
	if(copy_to_user(iwp->pointer, address,
			 sizeof(struct sockaddr) * number))
		return -EFAULT;
	if(copy_to_user(iwp->pointer + (sizeof(struct sockaddr)*number),
			&spy_stat, sizeof(struct iw_quality) * number))
		return -EFAULT;

	return 0;
}