summaryrefslogtreecommitdiffstats
path: root/drivers/net/ps3_gelic_wireless.c
blob: 227b141c4fbd3549bd0664bdb5288defa0657473 (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
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
/*
 *  PS3 gelic network driver.
 *
 * Copyright (C) 2007 Sony Computer Entertainment Inc.
 * Copyright 2007 Sony Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#undef DEBUG

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>

#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/wireless.h>
#include <linux/ieee80211.h>
#include <linux/if_arp.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <net/iw_handler.h>

#include <linux/dma-mapping.h>
#include <net/checksum.h>
#include <asm/firmware.h>
#include <asm/ps3.h>
#include <asm/lv1call.h>

#include "ps3_gelic_net.h"
#include "ps3_gelic_wireless.h"


static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
			       u8 *essid, size_t essid_len);
static int gelic_wl_try_associate(struct net_device *netdev);

/*
 * tables
 */

/* 802.11b/g channel to freq in MHz */
static const int channel_freq[] = {
	2412, 2417, 2422, 2427, 2432,
	2437, 2442, 2447, 2452, 2457,
	2462, 2467, 2472, 2484
};
#define NUM_CHANNELS ARRAY_SIZE(channel_freq)

/* in bps */
static const int bitrate_list[] = {
	  1000000,
	  2000000,
	  5500000,
	 11000000,
	  6000000,
	  9000000,
	 12000000,
	 18000000,
	 24000000,
	 36000000,
	 48000000,
	 54000000
};
#define NUM_BITRATES ARRAY_SIZE(bitrate_list)

/*
 * wpa2 support requires the hypervisor version 2.0 or later
 */
static inline int wpa2_capable(void)
{
	return (0 <= ps3_compare_firmware_version(2, 0, 0));
}

static inline int precise_ie(void)
{
	return (0 <= ps3_compare_firmware_version(2, 2, 0));
}
/*
 * post_eurus_cmd helpers
 */
struct eurus_cmd_arg_info {
	int pre_arg; /* command requres arg1, arg2 at POST COMMAND */
	int post_arg; /* command requires arg1, arg2 at GET_RESULT */
};

static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
	[GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
	[GELIC_EURUS_CMD_SET_WEP_CFG]    = { .pre_arg = 1},
	[GELIC_EURUS_CMD_SET_WPA_CFG]    = { .pre_arg = 1},
	[GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
	[GELIC_EURUS_CMD_GET_WEP_CFG]    = { .post_arg = 1},
	[GELIC_EURUS_CMD_GET_WPA_CFG]    = { .post_arg = 1},
	[GELIC_EURUS_CMD_GET_RSSI_CFG]   = { .post_arg = 1},
	[GELIC_EURUS_CMD_START_SCAN]     = { .pre_arg = 1},
	[GELIC_EURUS_CMD_GET_SCAN]       = { .post_arg = 1},
};

#ifdef DEBUG
static const char *cmdstr(enum gelic_eurus_command ix)
{
	switch (ix) {
	case GELIC_EURUS_CMD_ASSOC:
		return "ASSOC";
	case GELIC_EURUS_CMD_DISASSOC:
		return "DISASSOC";
	case GELIC_EURUS_CMD_START_SCAN:
		return "SCAN";
	case GELIC_EURUS_CMD_GET_SCAN:
		return "GET SCAN";
	case GELIC_EURUS_CMD_SET_COMMON_CFG:
		return "SET_COMMON_CFG";
	case GELIC_EURUS_CMD_GET_COMMON_CFG:
		return "GET_COMMON_CFG";
	case GELIC_EURUS_CMD_SET_WEP_CFG:
		return "SET_WEP_CFG";
	case GELIC_EURUS_CMD_GET_WEP_CFG:
		return "GET_WEP_CFG";
	case GELIC_EURUS_CMD_SET_WPA_CFG:
		return "SET_WPA_CFG";
	case GELIC_EURUS_CMD_GET_WPA_CFG:
		return "GET_WPA_CFG";
	case GELIC_EURUS_CMD_GET_RSSI_CFG:
		return "GET_RSSI";
	default:
		break;
	}
	return "";
};
#else
static inline const char *cmdstr(enum gelic_eurus_command ix)
{
	return "";
}
#endif

/* synchronously do eurus commands */
static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
{
	struct gelic_eurus_cmd *cmd;
	struct gelic_card *card;
	struct gelic_wl_info *wl;

	u64 arg1, arg2;

	pr_debug("%s: <-\n", __func__);
	cmd = container_of(work, struct gelic_eurus_cmd, work);
	BUG_ON(cmd_info[cmd->cmd].pre_arg &&
	       cmd_info[cmd->cmd].post_arg);
	wl = cmd->wl;
	card = port_to_card(wl_port(wl));

	if (cmd_info[cmd->cmd].pre_arg) {
		arg1 = (cmd->buffer) ?
			ps3_mm_phys_to_lpar(__pa(cmd->buffer)) :
			0;
		arg2 = cmd->buf_size;
	} else {
		arg1 = 0;
		arg2 = 0;
	}
	init_completion(&wl->cmd_done_intr);
	pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
	cmd->status = lv1_net_control(bus_id(card), dev_id(card),
				      GELIC_LV1_POST_WLAN_CMD,
				      cmd->cmd, arg1, arg2,
				      &cmd->tag, &cmd->size);
	if (cmd->status) {
		complete(&cmd->done);
		pr_info("%s: cmd issue failed\n", __func__);
		return;
	}

	wait_for_completion(&wl->cmd_done_intr);

	if (cmd_info[cmd->cmd].post_arg) {
		arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
		arg2 = cmd->buf_size;
	} else {
		arg1 = 0;
		arg2 = 0;
	}

	cmd->status = lv1_net_control(bus_id(card), dev_id(card),
				      GELIC_LV1_GET_WLAN_CMD_RESULT,
				      cmd->tag, arg1, arg2,
				      &cmd->cmd_status, &cmd->size);
#ifdef DEBUG
	if (cmd->status || cmd->cmd_status) {
	pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
		 cmd->tag, arg1, arg2);
	pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
		 __func__, cmd->status, cmd->cmd_status, cmd->size);
	}
#endif
	complete(&cmd->done);
	pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
}

static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
						    unsigned int eurus_cmd,
						    void *buffer,
						    unsigned int buf_size)
{
	struct gelic_eurus_cmd *cmd;

	/* allocate cmd */
	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
	if (!cmd)
		return NULL;

	/* initialize members */
	cmd->cmd = eurus_cmd;
	cmd->buffer = buffer;
	cmd->buf_size = buf_size;
	cmd->wl = wl;
	INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
	init_completion(&cmd->done);
	queue_work(wl->eurus_cmd_queue, &cmd->work);

	/* wait for command completion */
	wait_for_completion(&cmd->done);

	return cmd;
}

static u32 gelic_wl_get_link(struct net_device *netdev)
{
	struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
	u32 ret;

	pr_debug("%s: <-\n", __func__);
	mutex_lock(&wl->assoc_stat_lock);
	if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
		ret = 1;
	else
		ret = 0;
	mutex_unlock(&wl->assoc_stat_lock);
	pr_debug("%s: ->\n", __func__);
	return ret;
}

static void gelic_wl_send_iwap_event(struct gelic_wl_info *wl, u8 *bssid)
{
	union iwreq_data data;

	memset(&data, 0, sizeof(data));
	if (bssid)
		memcpy(data.ap_addr.sa_data, bssid, ETH_ALEN);
	data.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWAP,
			    &data, NULL);
}

/*
 * wireless extension handlers and helpers
 */

/* SIOGIWNAME */
static int gelic_wl_get_name(struct net_device *dev,
			     struct iw_request_info *info,
			     union iwreq_data *iwreq, char *extra)
{
	strcpy(iwreq->name, "IEEE 802.11bg");
	return 0;
}

static void gelic_wl_get_ch_info(struct gelic_wl_info *wl)
{
	struct gelic_card *card = port_to_card(wl_port(wl));
	u64 ch_info_raw, tmp;
	int status;

	if (!test_and_set_bit(GELIC_WL_STAT_CH_INFO, &wl->stat)) {
		status = lv1_net_control(bus_id(card), dev_id(card),
					 GELIC_LV1_GET_CHANNEL, 0, 0, 0,
					 &ch_info_raw,
					 &tmp);
		/* some fw versions may return error */
		if (status) {
			if (status != LV1_NO_ENTRY)
				pr_info("%s: available ch unknown\n", __func__);
			wl->ch_info = 0x07ff;/* 11 ch */
		} else
			/* 16 bits of MSB has available channels */
			wl->ch_info = ch_info_raw >> 48;
	}
	return;
}

/* SIOGIWRANGE */
static int gelic_wl_get_range(struct net_device *netdev,
			      struct iw_request_info *info,
			      union iwreq_data *iwreq, char *extra)
{
	struct iw_point *point = &iwreq->data;
	struct iw_range *range = (struct iw_range *)extra;
	struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
	unsigned int i, chs;

	pr_debug("%s: <-\n", __func__);
	point->length = sizeof(struct iw_range);
	memset(range, 0, sizeof(struct iw_range));

	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 22;

	/* available channels and frequencies */
	gelic_wl_get_ch_info(wl);

	for (i = 0, chs = 0;
	     i < NUM_CHANNELS && chs < IW_MAX_FREQUENCIES; i++)
		if (wl->ch_info & (1 << i)) {
			range->freq[chs].i = i + 1;
			range->freq[chs].m = channel_freq[i];
			range->freq[chs].e = 6;
			chs++;
		}
	range->num_frequency = chs;
	range->old_num_frequency = chs;
	range->num_channels = chs;
	range->old_num_channels = chs;

	/* bitrates */
	for (i = 0; i < NUM_BITRATES; i++)
		range->bitrate[i] = bitrate_list[i];
	range->num_bitrates = i;

	/* signal levels */
	range->max_qual.qual = 100; /* relative value */
	range->max_qual.level = 100;
	range->avg_qual.qual = 50;
	range->avg_qual.level = 50;
	range->sensitivity = 0;

	/* Event capability */
	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);

	/* encryption capability */
	range->enc_capa = IW_ENC_CAPA_WPA |
		IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP |
		IW_ENC_CAPA_4WAY_HANDSHAKE;
	if (wpa2_capable())
		range->enc_capa |= IW_ENC_CAPA_WPA2;
	range->encoding_size[0] = 5;	/* 40bit WEP */
	range->encoding_size[1] = 13;	/* 104bit WEP */
	range->encoding_size[2] = 32;	/* WPA-PSK */
	range->num_encoding_sizes = 3;
	range->max_encoding_tokens = GELIC_WEP_KEYS;

	/* scan capability */
	range->scan_capa = IW_SCAN_CAPA_ESSID;

	pr_debug("%s: ->\n", __func__);
	return 0;

}

/* SIOC{G,S}IWSCAN */
static int gelic_wl_set_scan(struct net_device *netdev,
			   struct iw_request_info *info,
			   union iwreq_data *wrqu, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct iw_scan_req *req;
	u8 *essid = NULL;
	size_t essid_len = 0;

	if (wrqu->data.length == sizeof(struct iw_scan_req) &&
	    wrqu->data.flags & IW_SCAN_THIS_ESSID) {
		req = (struct iw_scan_req*)extra;
		essid = req->essid;
		essid_len = req->essid_len;
		pr_debug("%s: ESSID scan =%s\n", __func__, essid);
	}
	return gelic_wl_start_scan(wl, 1, essid, essid_len);
}

#define OUI_LEN 3
static const u8 rsn_oui[OUI_LEN] = { 0x00, 0x0f, 0xac };
static const u8 wpa_oui[OUI_LEN] = { 0x00, 0x50, 0xf2 };

/*
 * synthesize WPA/RSN IE data
 * See WiFi WPA specification and IEEE 802.11-2007 7.3.2.25
 * for the format
 */
static size_t gelic_wl_synthesize_ie(u8 *buf,
				     struct gelic_eurus_scan_info *scan)
{

	const u8 *oui_header;
	u8 *start = buf;
	int rsn;
	int ccmp;

	pr_debug("%s: <- sec=%16x\n", __func__, scan->security);
	switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_MASK) {
	case GELIC_EURUS_SCAN_SEC_WPA:
		rsn = 0;
		break;
	case GELIC_EURUS_SCAN_SEC_WPA2:
		rsn = 1;
		break;
	default:
		/* WEP or none.  No IE returned */
		return 0;
	}

	switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_WPA_MASK) {
	case GELIC_EURUS_SCAN_SEC_WPA_TKIP:
		ccmp = 0;
		break;
	case GELIC_EURUS_SCAN_SEC_WPA_AES:
		ccmp = 1;
		break;
	default:
		if (rsn) {
			ccmp = 1;
			pr_info("%s: no cipher info. defaulted to CCMP\n",
				__func__);
		} else {
			ccmp = 0;
			pr_info("%s: no cipher info. defaulted to TKIP\n",
				__func__);
		}
	}

	if (rsn)
		oui_header = rsn_oui;
	else
		oui_header = wpa_oui;

	/* element id */
	if (rsn)
		*buf++ = WLAN_EID_RSN;
	else
		*buf++ = WLAN_EID_GENERIC;

	/* length filed; set later */
	buf++;

	/* wpa special header */
	if (!rsn) {
		memcpy(buf, wpa_oui, OUI_LEN);
		buf += OUI_LEN;
		*buf++ = 0x01;
	}

	/* version */
	*buf++ = 0x01; /* version 1.0 */
	*buf++ = 0x00;

	/* group cipher */
	memcpy(buf, oui_header, OUI_LEN);
	buf += OUI_LEN;

	if (ccmp)
		*buf++ = 0x04; /* CCMP */
	else
		*buf++ = 0x02; /* TKIP */

	/* pairwise key count always 1 */
	*buf++ = 0x01;
	*buf++ = 0x00;

	/* pairwise key suit */
	memcpy(buf, oui_header, OUI_LEN);
	buf += OUI_LEN;
	if (ccmp)
		*buf++ = 0x04; /* CCMP */
	else
		*buf++ = 0x02; /* TKIP */

	/* AKM count is 1 */
	*buf++ = 0x01;
	*buf++ = 0x00;

	/* AKM suite is assumed as PSK*/
	memcpy(buf, oui_header, OUI_LEN);
	buf += OUI_LEN;
	*buf++ = 0x02; /* PSK */

	/* RSN capabilities is 0 */
	*buf++ = 0x00;
	*buf++ = 0x00;

	/* set length field */
	start[1] = (buf - start - 2);

	pr_debug("%s: ->\n", __func__);
	return (buf - start);
}

struct ie_item {
	u8 *data;
	u8 len;
};

struct ie_info {
	struct ie_item wpa;
	struct ie_item rsn;
};

static void gelic_wl_parse_ie(u8 *data, size_t len,
			      struct ie_info *ie_info)
{
	size_t data_left = len;
	u8 *pos = data;
	u8 item_len;
	u8 item_id;

	pr_debug("%s: data=%p len=%ld \n", __func__,
		 data, len);
	memset(ie_info, 0, sizeof(struct ie_info));

	while (2 <= data_left) {
		item_id = *pos++;
		item_len = *pos++;
		data_left -= 2;

		if (data_left < item_len)
			break;

		switch (item_id) {
		case WLAN_EID_GENERIC:
			if ((OUI_LEN + 1 <= item_len) &&
			    !memcmp(pos, wpa_oui, OUI_LEN) &&
			    pos[OUI_LEN] == 0x01) {
				ie_info->wpa.data = pos - 2;
				ie_info->wpa.len = item_len + 2;
			}
			break;
		case WLAN_EID_RSN:
			ie_info->rsn.data = pos - 2;
			/* length includes the header */
			ie_info->rsn.len = item_len + 2;
			break;
		default:
			pr_debug("%s: ignore %#x,%d\n", __func__,
				 item_id, item_len);
			break;
		}
		pos += item_len;
		data_left -= item_len;
	}
	pr_debug("%s: wpa=%p,%d wpa2=%p,%d\n", __func__,
		 ie_info->wpa.data, ie_info->wpa.len,
		 ie_info->rsn.data, ie_info->rsn.len);
}


/*
 * translate the scan informations from hypervisor to a
 * independent format
 */
static char *gelic_wl_translate_scan(struct net_device *netdev,
				     struct iw_request_info *info,
				     char *ev,
				     char *stop,
				     struct gelic_wl_scan_info *network)
{
	struct iw_event iwe;
	struct gelic_eurus_scan_info *scan = network->hwinfo;
	char *tmp;
	u8 rate;
	unsigned int i, j, len;
	u8 buf[64]; /* arbitrary size large enough */

	pr_debug("%s: <-\n", __func__);

	/* first entry should be AP's mac address */
	iwe.cmd = SIOCGIWAP;
	iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
	memcpy(iwe.u.ap_addr.sa_data, &scan->bssid[2], ETH_ALEN);
	ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_ADDR_LEN);

	/* ESSID */
	iwe.cmd = SIOCGIWESSID;
	iwe.u.data.flags = 1;
	iwe.u.data.length = strnlen(scan->essid, 32);
	ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);

	/* FREQUENCY */
	iwe.cmd = SIOCGIWFREQ;
	iwe.u.freq.m = be16_to_cpu(scan->channel);
	iwe.u.freq.e = 0; /* table value in MHz */
	iwe.u.freq.i = 0;
	ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_FREQ_LEN);

	/* RATES */
	iwe.cmd = SIOCGIWRATE;
	iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
	/* to stuff multiple values in one event */
	tmp = ev + iwe_stream_lcp_len(info);
	/* put them in ascendant order (older is first) */
	i = 0;
	j = 0;
	pr_debug("%s: rates=%d rate=%d\n", __func__,
		 network->rate_len, network->rate_ext_len);
	while (i < network->rate_len) {
		if (j < network->rate_ext_len &&
		    ((scan->ext_rate[j] & 0x7f) < (scan->rate[i] & 0x7f)))
		    rate = scan->ext_rate[j++] & 0x7f;
		else
		    rate = scan->rate[i++] & 0x7f;
		iwe.u.bitrate.value = rate * 500000; /* 500kbps unit */
		tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
					   IW_EV_PARAM_LEN);
	}
	while (j < network->rate_ext_len) {
		iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
		tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
					   IW_EV_PARAM_LEN);
	}
	/* Check if we added any rate */
	if (iwe_stream_lcp_len(info) < (tmp - ev))
		ev = tmp;

	/* ENCODE */
	iwe.cmd = SIOCGIWENCODE;
	if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_PRIVACY)
		iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
	else
		iwe.u.data.flags = IW_ENCODE_DISABLED;
	iwe.u.data.length = 0;
	ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);

	/* MODE */
	iwe.cmd = SIOCGIWMODE;
	if (be16_to_cpu(scan->capability) &
	    (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
		if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_ESS)
			iwe.u.mode = IW_MODE_MASTER;
		else
			iwe.u.mode = IW_MODE_ADHOC;
		ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_UINT_LEN);
	}

	/* QUAL */
	iwe.cmd = IWEVQUAL;
	iwe.u.qual.updated  = IW_QUAL_ALL_UPDATED |
			IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
	iwe.u.qual.level = be16_to_cpu(scan->rssi);
	iwe.u.qual.qual = be16_to_cpu(scan->rssi);
	iwe.u.qual.noise = 0;
	ev  = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_QUAL_LEN);

	/* RSN */
	memset(&iwe, 0, sizeof(iwe));
	if (be16_to_cpu(scan->size) <= sizeof(*scan)) {
		/* If wpa[2] capable station, synthesize IE and put it */
		len = gelic_wl_synthesize_ie(buf, scan);
		if (len) {
			iwe.cmd = IWEVGENIE;
			iwe.u.data.length = len;
			ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
		}
	} else {
		/* this scan info has IE data */
		struct ie_info ie_info;
		size_t data_len;

		data_len = be16_to_cpu(scan->size) - sizeof(*scan);

		gelic_wl_parse_ie(scan->elements, data_len, &ie_info);

		if (ie_info.wpa.len && (ie_info.wpa.len <= sizeof(buf))) {
			memcpy(buf, ie_info.wpa.data, ie_info.wpa.len);
			iwe.cmd = IWEVGENIE;
			iwe.u.data.length = ie_info.wpa.len;
			ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
		}

		if (ie_info.rsn.len && (ie_info.rsn.len <= sizeof(buf))) {
			memset(&iwe, 0, sizeof(iwe));
			memcpy(buf, ie_info.rsn.data, ie_info.rsn.len);
			iwe.cmd = IWEVGENIE;
			iwe.u.data.length = ie_info.rsn.len;
			ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
		}
	}

	pr_debug("%s: ->\n", __func__);
	return ev;
}


static int gelic_wl_get_scan(struct net_device *netdev,
			     struct iw_request_info *info,
			     union iwreq_data *wrqu, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct gelic_wl_scan_info *scan_info;
	char *ev = extra;
	char *stop = ev + wrqu->data.length;
	int ret = 0;
	unsigned long this_time = jiffies;

	pr_debug("%s: <-\n", __func__);
	if (mutex_lock_interruptible(&wl->scan_lock))
		return -EAGAIN;

	switch (wl->scan_stat) {
	case GELIC_WL_SCAN_STAT_SCANNING:
		/* If a scan in progress, caller should call me again */
		ret = -EAGAIN;
		goto out;
		break;

	case GELIC_WL_SCAN_STAT_INIT:
		/* last scan request failed or never issued */
		ret = -ENODEV;
		goto out;
		break;
	case GELIC_WL_SCAN_STAT_GOT_LIST:
		/* ok, use current list */
		break;
	}

	list_for_each_entry(scan_info, &wl->network_list, list) {
		if (wl->scan_age == 0 ||
		    time_after(scan_info->last_scanned + wl->scan_age,
			       this_time))
			ev = gelic_wl_translate_scan(netdev, info,
						     ev, stop,
						     scan_info);
		else
			pr_debug("%s:entry too old\n", __func__);

		if (stop - ev <= IW_EV_ADDR_LEN) {
			ret = -E2BIG;
			goto out;
		}
	}

	wrqu->data.length = ev - extra;
	wrqu->data.flags = 0;
out:
	mutex_unlock(&wl->scan_lock);
	pr_debug("%s: -> %d %d\n", __func__, ret, wrqu->data.length);
	return ret;
}

#ifdef DEBUG
static void scan_list_dump(struct gelic_wl_info *wl)
{
	struct gelic_wl_scan_info *scan_info;
	int i;

	i = 0;
	list_for_each_entry(scan_info, &wl->network_list, list) {
		pr_debug("%s: item %d\n", __func__, i++);
		pr_debug("valid=%d eurusindex=%d last=%lx\n",
			 scan_info->valid, scan_info->eurus_index,
			 scan_info->last_scanned);
		pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
			 scan_info->rate_len, scan_info->rate_ext_len,
			 scan_info->essid_len);
		/* -- */
		pr_debug("bssid=%pM\n", &scan_info->hwinfo->bssid[2]);
		pr_debug("essid=%s\n", scan_info->hwinfo->essid);
	}
}
#endif

static int gelic_wl_set_auth(struct net_device *netdev,
			     struct iw_request_info *info,
			     union iwreq_data *data, char *extra)
{
	struct iw_param *param = &data->param;
	struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
	unsigned long irqflag;
	int ret = 0;

	pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
	spin_lock_irqsave(&wl->lock, irqflag);
	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
		if (param->value & IW_AUTH_WPA_VERSION_DISABLED) {
			pr_debug("%s: NO WPA selected\n", __func__);
			wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
			wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
		}
		if (param->value & IW_AUTH_WPA_VERSION_WPA) {
			pr_debug("%s: WPA version 1 selected\n", __func__);
			wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
			wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
			wl->auth_method = GELIC_EURUS_AUTH_OPEN;
		}
		if (param->value & IW_AUTH_WPA_VERSION_WPA2) {
			/*
			 * As the hypervisor may not tell the cipher
			 * information of the AP if it is WPA2,
			 * you will not decide suitable cipher from
			 * its beacon.
			 * You should have knowledge about the AP's
			 * cipher infomation in other method prior to
			 * the association.
			 */
			if (!precise_ie())
				pr_info("%s: WPA2 may not work\n", __func__);
			if (wpa2_capable()) {
				wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
				wl->group_cipher_method = GELIC_WL_CIPHER_AES;
				wl->pairwise_cipher_method =
					GELIC_WL_CIPHER_AES;
				wl->auth_method = GELIC_EURUS_AUTH_OPEN;
			} else
				ret = -EINVAL;
		}
		break;

	case IW_AUTH_CIPHER_PAIRWISE:
		if (param->value &
		    (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
			pr_debug("%s: WEP selected\n", __func__);
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
		}
		if (param->value & IW_AUTH_CIPHER_TKIP) {
			pr_debug("%s: TKIP selected\n", __func__);
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
		}
		if (param->value & IW_AUTH_CIPHER_CCMP) {
			pr_debug("%s: CCMP selected\n", __func__);
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
		}
		if (param->value & IW_AUTH_CIPHER_NONE) {
			pr_debug("%s: no auth selected\n", __func__);
			wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
		}
		break;
	case IW_AUTH_CIPHER_GROUP:
		if (param->value &
		    (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
			pr_debug("%s: WEP selected\n", __func__);
			wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
		}
		if (param->value & IW_AUTH_CIPHER_TKIP) {
			pr_debug("%s: TKIP selected\n", __func__);
			wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
		}
		if (param->value & IW_AUTH_CIPHER_CCMP) {
			pr_debug("%s: CCMP selected\n", __func__);
			wl->group_cipher_method = GELIC_WL_CIPHER_AES;
		}
		if (param->value & IW_AUTH_CIPHER_NONE) {
			pr_debug("%s: no auth selected\n", __func__);
			wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
		}
		break;
	case IW_AUTH_80211_AUTH_ALG:
		if (param->value & IW_AUTH_ALG_SHARED_KEY) {
			pr_debug("%s: shared key specified\n", __func__);
			wl->auth_method = GELIC_EURUS_AUTH_SHARED;
		} else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
			pr_debug("%s: open system specified\n", __func__);
			wl->auth_method = GELIC_EURUS_AUTH_OPEN;
		} else
			ret = -EINVAL;
		break;

	case IW_AUTH_WPA_ENABLED:
		if (param->value) {
			pr_debug("%s: WPA enabled\n", __func__);
			wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
		} else {
			pr_debug("%s: WPA disabled\n", __func__);
			wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
		}
		break;

	case IW_AUTH_KEY_MGMT:
		if (param->value & IW_AUTH_KEY_MGMT_PSK)
			break;
		/* intentionally fall through */
	default:
		ret = -EOPNOTSUPP;
		break;
	};

	if (!ret)
		set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);

	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> %d\n", __func__, ret);
	return ret;
}

static int gelic_wl_get_auth(struct net_device *netdev,
			     struct iw_request_info *info,
			     union iwreq_data *iwreq, char *extra)
{
	struct iw_param *param = &iwreq->param;
	struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
	unsigned long irqflag;
	int ret = 0;

	pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
	spin_lock_irqsave(&wl->lock, irqflag);
	switch (param->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
		switch (wl->wpa_level) {
		case GELIC_WL_WPA_LEVEL_WPA:
			param->value |= IW_AUTH_WPA_VERSION_WPA;
			break;
		case GELIC_WL_WPA_LEVEL_WPA2:
			param->value |= IW_AUTH_WPA_VERSION_WPA2;
			break;
		default:
			param->value |= IW_AUTH_WPA_VERSION_DISABLED;
		}
		break;

	case IW_AUTH_80211_AUTH_ALG:
		if (wl->auth_method == GELIC_EURUS_AUTH_SHARED)
			param->value = IW_AUTH_ALG_SHARED_KEY;
		else if (wl->auth_method == GELIC_EURUS_AUTH_OPEN)
			param->value = IW_AUTH_ALG_OPEN_SYSTEM;
		break;

	case IW_AUTH_WPA_ENABLED:
		switch (wl->wpa_level) {
		case GELIC_WL_WPA_LEVEL_WPA:
		case GELIC_WL_WPA_LEVEL_WPA2:
			param->value = 1;
			break;
		default:
			param->value = 0;
			break;
		}
		break;
	default:
		ret = -EOPNOTSUPP;
	}

	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> %d\n", __func__, ret);
	return ret;
}

/* SIOC{S,G}IWESSID */
static int gelic_wl_set_essid(struct net_device *netdev,
			      struct iw_request_info *info,
			      union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	unsigned long irqflag;

	pr_debug("%s: <- l=%d f=%d\n", __func__,
		 data->essid.length, data->essid.flags);
	if (IW_ESSID_MAX_SIZE < data->essid.length)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (data->essid.flags) {
		wl->essid_len = data->essid.length;
		memcpy(wl->essid, extra, wl->essid_len);
		pr_debug("%s: essid = '%s'\n", __func__, extra);
		set_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
	} else {
		pr_debug("%s: ESSID any \n", __func__);
		clear_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
	}
	set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
	spin_unlock_irqrestore(&wl->lock, irqflag);


	gelic_wl_try_associate(netdev); /* FIXME */
	pr_debug("%s: -> \n", __func__);
	return 0;
}

static int gelic_wl_get_essid(struct net_device *netdev,
			      struct iw_request_info *info,
			      union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	unsigned long irqflag;

	pr_debug("%s: <- \n", __func__);
	mutex_lock(&wl->assoc_stat_lock);
	spin_lock_irqsave(&wl->lock, irqflag);
	if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat) ||
	    wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
		memcpy(extra, wl->essid, wl->essid_len);
		data->essid.length = wl->essid_len;
		data->essid.flags = 1;
	} else
		data->essid.flags = 0;

	mutex_unlock(&wl->assoc_stat_lock);
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> len=%d \n", __func__, data->essid.length);

	return 0;
}

/* SIO{S,G}IWENCODE */
static int gelic_wl_set_encode(struct net_device *netdev,
			       struct iw_request_info *info,
			       union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct iw_point *enc = &data->encoding;
	__u16 flags;
	unsigned long irqflag;
	int key_index, index_specified;
	int ret = 0;

	pr_debug("%s: <- \n", __func__);
	flags = enc->flags & IW_ENCODE_FLAGS;
	key_index = enc->flags & IW_ENCODE_INDEX;

	pr_debug("%s: key_index = %d\n", __func__, key_index);
	pr_debug("%s: key_len = %d\n", __func__, enc->length);
	pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);

	if (GELIC_WEP_KEYS < key_index)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (key_index) {
		index_specified = 1;
		key_index--;
	} else {
		index_specified = 0;
		key_index = wl->current_key;
	}

	if (flags & IW_ENCODE_NOKEY) {
		/* if just IW_ENCODE_NOKEY, change current key index */
		if (!flags && index_specified) {
			wl->current_key = key_index;
			goto done;
		}

		if (flags & IW_ENCODE_DISABLED) {
			if (!index_specified) {
				/* disable encryption */
				wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
				wl->pairwise_cipher_method =
					GELIC_WL_CIPHER_NONE;
				/* invalidate all key */
				wl->key_enabled = 0;
			} else
				clear_bit(key_index, &wl->key_enabled);
		}

		if (flags & IW_ENCODE_OPEN)
			wl->auth_method = GELIC_EURUS_AUTH_OPEN;
		if (flags & IW_ENCODE_RESTRICTED) {
			pr_info("%s: shared key mode enabled\n", __func__);
			wl->auth_method = GELIC_EURUS_AUTH_SHARED;
		}
	} else {
		if (IW_ENCODING_TOKEN_MAX < enc->length) {
			ret = -EINVAL;
			goto done;
		}
		wl->key_len[key_index] = enc->length;
		memcpy(wl->key[key_index], extra, enc->length);
		set_bit(key_index, &wl->key_enabled);
		wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
		wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
	}
	set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
done:
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> \n", __func__);
	return ret;
}

static int gelic_wl_get_encode(struct net_device *netdev,
			       struct iw_request_info *info,
			       union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct iw_point *enc = &data->encoding;
	unsigned long irqflag;
	unsigned int key_index, index_specified;
	int ret = 0;

	pr_debug("%s: <- \n", __func__);
	key_index = enc->flags & IW_ENCODE_INDEX;
	pr_debug("%s: flag=%#x point=%p len=%d extra=%p\n", __func__,
		 enc->flags, enc->pointer, enc->length, extra);
	if (GELIC_WEP_KEYS < key_index)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (key_index) {
		index_specified = 1;
		key_index--;
	} else {
		index_specified = 0;
		key_index = wl->current_key;
	}

	if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
		switch (wl->auth_method) {
		case GELIC_EURUS_AUTH_OPEN:
			enc->flags = IW_ENCODE_OPEN;
			break;
		case GELIC_EURUS_AUTH_SHARED:
			enc->flags = IW_ENCODE_RESTRICTED;
			break;
		}
	} else
		enc->flags = IW_ENCODE_DISABLED;

	if (test_bit(key_index, &wl->key_enabled)) {
		if (enc->length < wl->key_len[key_index]) {
			ret = -EINVAL;
			goto done;
		}
		enc->length = wl->key_len[key_index];
		memcpy(extra, wl->key[key_index], wl->key_len[key_index]);
	} else {
		enc->length = 0;
		enc->flags |= IW_ENCODE_NOKEY;
	}
	enc->flags |= key_index + 1;
	pr_debug("%s: -> flag=%x len=%d\n", __func__,
		 enc->flags, enc->length);

done:
	spin_unlock_irqrestore(&wl->lock, irqflag);
	return ret;
}

/* SIOC{S,G}IWAP */
static int gelic_wl_set_ap(struct net_device *netdev,
			   struct iw_request_info *info,
			   union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	unsigned long irqflag;

	pr_debug("%s: <-\n", __func__);
	if (data->ap_addr.sa_family != ARPHRD_ETHER)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (is_valid_ether_addr(data->ap_addr.sa_data)) {
		memcpy(wl->bssid, data->ap_addr.sa_data,
		       ETH_ALEN);
		set_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
		set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
		pr_debug("%s: bss=%pM\n", __func__, wl->bssid);
	} else {
		pr_debug("%s: clear bssid\n", __func__);
		clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
		memset(wl->bssid, 0, ETH_ALEN);
	}
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: ->\n", __func__);
	return 0;
}

static int gelic_wl_get_ap(struct net_device *netdev,
			   struct iw_request_info *info,
			   union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	unsigned long irqflag;

	pr_debug("%s: <-\n", __func__);
	mutex_lock(&wl->assoc_stat_lock);
	spin_lock_irqsave(&wl->lock, irqflag);
	if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
		data->ap_addr.sa_family = ARPHRD_ETHER;
		memcpy(data->ap_addr.sa_data, wl->active_bssid,
		       ETH_ALEN);
	} else
		memset(data->ap_addr.sa_data, 0, ETH_ALEN);

	spin_unlock_irqrestore(&wl->lock, irqflag);
	mutex_unlock(&wl->assoc_stat_lock);
	pr_debug("%s: ->\n", __func__);
	return 0;
}

/* SIOC{S,G}IWENCODEEXT */
static int gelic_wl_set_encodeext(struct net_device *netdev,
				  struct iw_request_info *info,
				  union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct iw_point *enc = &data->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
	__u16 alg;
	__u16 flags;
	unsigned long irqflag;
	int key_index;
	int ret = 0;

	pr_debug("%s: <- \n", __func__);
	flags = enc->flags & IW_ENCODE_FLAGS;
	alg = ext->alg;
	key_index = enc->flags & IW_ENCODE_INDEX;

	pr_debug("%s: key_index = %d\n", __func__, key_index);
	pr_debug("%s: key_len = %d\n", __func__, enc->length);
	pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
	pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
	pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);

	if (GELIC_WEP_KEYS < key_index)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (key_index)
		key_index--;
	else
		key_index = wl->current_key;

	if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
		/* reques to change default key index */
		pr_debug("%s: request to change default key to %d\n",
			 __func__, key_index);
		wl->current_key = key_index;
		goto done;
	}

	if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
		pr_debug("%s: alg disabled\n", __func__);
		wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
		wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
		wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
		wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
	} else if (alg == IW_ENCODE_ALG_WEP) {
		pr_debug("%s: WEP requested\n", __func__);
		if (flags & IW_ENCODE_OPEN) {
			pr_debug("%s: open key mode\n", __func__);
			wl->auth_method = GELIC_EURUS_AUTH_OPEN;
		}
		if (flags & IW_ENCODE_RESTRICTED) {
			pr_debug("%s: shared key mode\n", __func__);
			wl->auth_method = GELIC_EURUS_AUTH_SHARED;
		}
		if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
			pr_info("%s: key is too long %d\n", __func__,
				ext->key_len);
			ret = -EINVAL;
			goto done;
		}
		/* OK, update the key */
		wl->key_len[key_index] = ext->key_len;
		memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
		memcpy(wl->key[key_index], ext->key, ext->key_len);
		set_bit(key_index, &wl->key_enabled);
		/* remember wep info changed */
		set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
	} else if (alg == IW_ENCODE_ALG_PMK) {
		if (ext->key_len != WPA_PSK_LEN) {
			pr_err("%s: PSK length wrong %d\n", __func__,
			       ext->key_len);
			ret = -EINVAL;
			goto done;
		}
		memset(wl->psk, 0, sizeof(wl->psk));
		memcpy(wl->psk, ext->key, ext->key_len);
		wl->psk_len = ext->key_len;
		wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
		/* remember PSK configured */
		set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
	}
done:
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> \n", __func__);
	return ret;
}

static int gelic_wl_get_encodeext(struct net_device *netdev,
				  struct iw_request_info *info,
				  union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct iw_point *enc = &data->encoding;
	struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
	unsigned long irqflag;
	int key_index;
	int ret = 0;
	int max_key_len;

	pr_debug("%s: <- \n", __func__);

	max_key_len = enc->length - sizeof(struct iw_encode_ext);
	if (max_key_len < 0)
		return -EINVAL;
	key_index = enc->flags & IW_ENCODE_INDEX;

	pr_debug("%s: key_index = %d\n", __func__, key_index);
	pr_debug("%s: key_len = %d\n", __func__, enc->length);
	pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);

	if (GELIC_WEP_KEYS < key_index)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (key_index)
		key_index--;
	else
		key_index = wl->current_key;

	memset(ext, 0, sizeof(struct iw_encode_ext));
	switch (wl->group_cipher_method) {
	case GELIC_WL_CIPHER_WEP:
		ext->alg = IW_ENCODE_ALG_WEP;
		enc->flags |= IW_ENCODE_ENABLED;
		break;
	case GELIC_WL_CIPHER_TKIP:
		ext->alg = IW_ENCODE_ALG_TKIP;
		enc->flags |= IW_ENCODE_ENABLED;
		break;
	case GELIC_WL_CIPHER_AES:
		ext->alg = IW_ENCODE_ALG_CCMP;
		enc->flags |= IW_ENCODE_ENABLED;
		break;
	case GELIC_WL_CIPHER_NONE:
	default:
		ext->alg = IW_ENCODE_ALG_NONE;
		enc->flags |= IW_ENCODE_NOKEY;
		break;
	}

	if (!(enc->flags & IW_ENCODE_NOKEY)) {
		if (max_key_len < wl->key_len[key_index]) {
			ret = -E2BIG;
			goto out;
		}
		if (test_bit(key_index, &wl->key_enabled))
			memcpy(ext->key, wl->key[key_index],
			       wl->key_len[key_index]);
		else
			pr_debug("%s: disabled key requested ix=%d\n",
				 __func__, key_index);
	}
out:
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s: -> \n", __func__);
	return ret;
}
/* SIOC{S,G}IWMODE */
static int gelic_wl_set_mode(struct net_device *netdev,
			     struct iw_request_info *info,
			     union iwreq_data *data, char *extra)
{
	__u32 mode = data->mode;
	int ret;

	pr_debug("%s: <- \n", __func__);
	if (mode == IW_MODE_INFRA)
		ret = 0;
	else
		ret = -EOPNOTSUPP;
	pr_debug("%s: -> %d\n", __func__, ret);
	return ret;
}

static int gelic_wl_get_mode(struct net_device *netdev,
			     struct iw_request_info *info,
			     union iwreq_data *data, char *extra)
{
	__u32 *mode = &data->mode;
	pr_debug("%s: <- \n", __func__);
	*mode = IW_MODE_INFRA;
	pr_debug("%s: ->\n", __func__);
	return 0;
}

#ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
/* SIOCIWFIRSTPRIV */
static int hex2bin(u8 *str, u8 *bin, unsigned int len)
{
	unsigned int i;
	static unsigned char *hex = "0123456789ABCDEF";
	unsigned char *p, *q;
	u8 tmp;

	if (len != WPA_PSK_LEN * 2)
		return -EINVAL;

	for (i = 0; i < WPA_PSK_LEN * 2; i += 2) {
		p = strchr(hex, toupper(str[i]));
		q = strchr(hex, toupper(str[i + 1]));
		if (!p || !q) {
			pr_info("%s: unconvertible PSK digit=%d\n",
				__func__, i);
			return -EINVAL;
		}
		tmp = ((p - hex) << 4) + (q - hex);
		*bin++ = tmp;
	}
	return 0;
};

static int gelic_wl_priv_set_psk(struct net_device *net_dev,
				 struct iw_request_info *info,
				 union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
	unsigned int len;
	unsigned long irqflag;
	int ret = 0;

	pr_debug("%s:<- len=%d\n", __func__, data->data.length);
	len = data->data.length - 1;
	if (len <= 2)
		return -EINVAL;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (extra[0] == '"' && extra[len - 1] == '"') {
		pr_debug("%s: passphrase mode\n", __func__);
		/* pass phrase */
		if (GELIC_WL_EURUS_PSK_MAX_LEN < (len - 2)) {
			pr_info("%s: passphrase too long\n", __func__);
			ret = -E2BIG;
			goto out;
		}
		memset(wl->psk, 0, sizeof(wl->psk));
		wl->psk_len = len - 2;
		memcpy(wl->psk, &(extra[1]), wl->psk_len);
		wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
	} else {
		ret = hex2bin(extra, wl->psk, len);
		if (ret)
			goto out;
		wl->psk_len = WPA_PSK_LEN;
		wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
	}
	set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
out:
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s:->\n", __func__);
	return ret;
}

static int gelic_wl_priv_get_psk(struct net_device *net_dev,
				 struct iw_request_info *info,
				 union iwreq_data *data, char *extra)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
	char *p;
	unsigned long irqflag;
	unsigned int i;

	pr_debug("%s:<-\n", __func__);
	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	spin_lock_irqsave(&wl->lock, irqflag);
	p = extra;
	if (test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat)) {
		if (wl->psk_type == GELIC_EURUS_WPA_PSK_BIN) {
			for (i = 0; i < wl->psk_len; i++) {
				sprintf(p, "%02xu", wl->psk[i]);
				p += 2;
			}
			*p = '\0';
			data->data.length = wl->psk_len * 2;
		} else {
			*p++ = '"';
			memcpy(p, wl->psk, wl->psk_len);
			p += wl->psk_len;
			*p++ = '"';
			*p = '\0';
			data->data.length = wl->psk_len + 2;
		}
	} else
		/* no psk set */
		data->data.length = 0;
	spin_unlock_irqrestore(&wl->lock, irqflag);
	pr_debug("%s:-> %d\n", __func__, data->data.length);
	return 0;
}
#endif

/* SIOCGIWNICKN */
static int gelic_wl_get_nick(struct net_device *net_dev,
				  struct iw_request_info *info,
				  union iwreq_data *data, char *extra)
{
	strcpy(extra, "gelic_wl");
	data->data.length = strlen(extra);
	data->data.flags = 1;
	return 0;
}


/* --- */

static struct iw_statistics *gelic_wl_get_wireless_stats(
	struct net_device *netdev)
{

	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	struct gelic_eurus_cmd *cmd;
	struct iw_statistics *is;
	struct gelic_eurus_rssi_info *rssi;
	void *buf;

	pr_debug("%s: <-\n", __func__);

	buf = (void *)__get_free_page(GFP_KERNEL);
	if (!buf)
		return NULL;

	is = &wl->iwstat;
	memset(is, 0, sizeof(*is));
	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
				   buf, sizeof(*rssi));
	if (cmd && !cmd->status && !cmd->cmd_status) {
		rssi = buf;
		is->qual.level = be16_to_cpu(rssi->rssi);
		is->qual.updated = IW_QUAL_LEVEL_UPDATED |
			IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
	} else
		/* not associated */
		is->qual.updated = IW_QUAL_ALL_INVALID;

	kfree(cmd);
	free_page((unsigned long)buf);
	pr_debug("%s: ->\n", __func__);
	return is;
}

/*
 *  scanning helpers
 */
static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
			       u8 *essid, size_t essid_len)
{
	struct gelic_eurus_cmd *cmd;
	int ret = 0;
	void *buf = NULL;
	size_t len;

	pr_debug("%s: <- always=%d\n", __func__, always_scan);
	if (mutex_lock_interruptible(&wl->scan_lock))
		return -ERESTARTSYS;

	/*
	 * If already a scan in progress, do not trigger more
	 */
	if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
		pr_debug("%s: scanning now\n", __func__);
		goto out;
	}

	init_completion(&wl->scan_done);
	/*
	 * If we have already a bss list, don't try to get new
	 */
	if (!always_scan && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
		pr_debug("%s: already has the list\n", __func__);
		complete(&wl->scan_done);
		goto out;
	}

	/* ESSID scan ? */
	if (essid_len && essid) {
		buf = (void *)__get_free_page(GFP_KERNEL);
		if (!buf) {
			ret = -ENOMEM;
			goto out;
		}
		len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
		memset(buf, 0, len);
		memcpy(buf, essid, essid_len);
		pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
	} else
		len = 0;

	/*
	 * issue start scan request
	 */
	wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
				   buf, len);
	if (!cmd || cmd->status || cmd->cmd_status) {
		wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
		complete(&wl->scan_done);
		ret = -ENOMEM;
		goto out;
	}
	kfree(cmd);
out:
	free_page((unsigned long)buf);
	mutex_unlock(&wl->scan_lock);
	pr_debug("%s: ->\n", __func__);
	return ret;
}

/*
 * retrieve scan result from the chip (hypervisor)
 * this function is invoked by schedule work.
 */
static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
{
	struct gelic_eurus_cmd *cmd = NULL;
	struct gelic_wl_scan_info *target, *tmp;
	struct gelic_wl_scan_info *oldest = NULL;
	struct gelic_eurus_scan_info *scan_info;
	unsigned int scan_info_size;
	union iwreq_data data;
	unsigned long this_time = jiffies;
	unsigned int data_len, i, found, r;
	void *buf;

	pr_debug("%s:start\n", __func__);
	mutex_lock(&wl->scan_lock);

	buf = (void *)__get_free_page(GFP_KERNEL);
	if (!buf) {
		pr_info("%s: scan buffer alloc failed\n", __func__);
		goto out;
	}

	if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
		/*
		 * stop() may be called while scanning, ignore result
		 */
		pr_debug("%s: scan complete when stat != scanning(%d)\n",
			 __func__, wl->scan_stat);
		goto out;
	}

	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
				   buf, PAGE_SIZE);
	if (!cmd || cmd->status || cmd->cmd_status) {
		wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
		pr_info("%s:cmd failed\n", __func__);
		kfree(cmd);
		goto out;
	}
	data_len = cmd->size;
	pr_debug("%s: data_len = %d\n", __func__, data_len);
	kfree(cmd);

	/* OK, bss list retrieved */
	wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;

	/* mark all entries are old */
	list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
		target->valid = 0;
		/* expire too old entries */
		if (time_before(target->last_scanned + wl->scan_age,
				this_time)) {
			kfree(target->hwinfo);
			target->hwinfo = NULL;
			list_move_tail(&target->list, &wl->network_free_list);
		}
	}

	/* put them in the newtork_list */
	for (i = 0, scan_info_size = 0, scan_info = buf;
	     scan_info_size < data_len;
	     i++, scan_info_size += be16_to_cpu(scan_info->size),
	     scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
		pr_debug("%s:size=%d bssid=%pM scan_info=%p\n", __func__,
			 be16_to_cpu(scan_info->size),
			 &scan_info->bssid[2], scan_info);

		/*
		 * The wireless firmware may return invalid channel 0 and/or
		 * invalid rate if the AP emits zero length SSID ie. As this
		 * scan information is useless, ignore it
		 */
		if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
			pr_debug("%s: invalid scan info\n", __func__);
			continue;
		}

		found = 0;
		oldest = NULL;
		list_for_each_entry(target, &wl->network_list, list) {
			if (!compare_ether_addr(&target->hwinfo->bssid[2],
						&scan_info->bssid[2])) {
				found = 1;
				pr_debug("%s: same BBS found scanned list\n",
					 __func__);
				break;
			}
			if (!oldest ||
			    (target->last_scanned < oldest->last_scanned))
				oldest = target;
		}

		if (!found) {
			/* not found in the list */
			if (list_empty(&wl->network_free_list)) {
				/* expire oldest */
				target = oldest;
			} else {
				target = list_entry(wl->network_free_list.next,
						    struct gelic_wl_scan_info,
						    list);
			}
		}

		/* update the item */
		target->last_scanned = this_time;
		target->valid = 1;
		target->eurus_index = i;
		kfree(target->hwinfo);
		target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
					 GFP_KERNEL);
		if (!target->hwinfo) {
			pr_info("%s: kzalloc failed\n", __func__);
			continue;
		}
		/* copy hw scan info */
		memcpy(target->hwinfo, scan_info, scan_info->size);
		target->essid_len = strnlen(scan_info->essid,
					    sizeof(scan_info->essid));
		target->rate_len = 0;
		for (r = 0; r < 12; r++)
			if (scan_info->rate[r])
				target->rate_len++;
		if (8 < target->rate_len)
			pr_info("%s: AP returns %d rates\n", __func__,
				target->rate_len);
		target->rate_ext_len = 0;
		for (r = 0; r < 16; r++)
			if (scan_info->ext_rate[r])
				target->rate_ext_len++;
		list_move_tail(&target->list, &wl->network_list);
	}
	memset(&data, 0, sizeof(data));
	wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
			    NULL);
out:
	free_page((unsigned long)buf);
	complete(&wl->scan_done);
	mutex_unlock(&wl->scan_lock);
	pr_debug("%s:end\n", __func__);
}

/*
 * Select an appropriate bss from current scan list regarding
 * current settings from userspace.
 * The caller must hold wl->scan_lock,
 * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
 */
static void update_best(struct gelic_wl_scan_info **best,
			struct gelic_wl_scan_info *candid,
			int *best_weight,
			int *weight)
{
	if (*best_weight < ++(*weight)) {
		*best_weight = *weight;
		*best = candid;
	}
}

static
struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
{
	struct gelic_wl_scan_info *scan_info;
	struct gelic_wl_scan_info *best_bss;
	int weight, best_weight;
	u16 security;

	pr_debug("%s: <-\n", __func__);

	best_bss = NULL;
	best_weight = 0;

	list_for_each_entry(scan_info, &wl->network_list, list) {
		pr_debug("%s: station %p\n", __func__, scan_info);

		if (!scan_info->valid) {
			pr_debug("%s: station invalid\n", __func__);
			continue;
		}

		/* If bss specified, check it only */
		if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
			if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
						wl->bssid)) {
				best_bss = scan_info;
				pr_debug("%s: bssid matched\n", __func__);
				break;
			} else {
				pr_debug("%s: bssid unmached\n", __func__);
				continue;
			}
		}

		weight = 0;

		/* security */
		security = be16_to_cpu(scan_info->hwinfo->security) &
			GELIC_EURUS_SCAN_SEC_MASK;
		if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
			if (security == GELIC_EURUS_SCAN_SEC_WPA2)
				update_best(&best_bss, scan_info,
					    &best_weight, &weight);
			else
				continue;
		} else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
			if (security == GELIC_EURUS_SCAN_SEC_WPA)
				update_best(&best_bss, scan_info,
					    &best_weight, &weight);
			else
				continue;
		} else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
			   wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
			if (security == GELIC_EURUS_SCAN_SEC_WEP)
				update_best(&best_bss, scan_info,
					    &best_weight, &weight);
			else
				continue;
		}

		/* If ESSID is set, check it */
		if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
			if ((scan_info->essid_len == wl->essid_len) &&
			    !strncmp(wl->essid,
				     scan_info->hwinfo->essid,
				     scan_info->essid_len))
				update_best(&best_bss, scan_info,
					    &best_weight, &weight);
			else
				continue;
		}
	}

#ifdef DEBUG
	pr_debug("%s: -> bss=%p\n", __func__, best_bss);
	if (best_bss) {
		pr_debug("%s:addr=%pM\n", __func__,
			 &best_bss->hwinfo->bssid[2]);
	}
#endif
	return best_bss;
}

/*
 * Setup WEP configuration to the chip
 * The caller must hold wl->scan_lock,
 * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
 */
static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
{
	unsigned int i;
	struct gelic_eurus_wep_cfg *wep;
	struct gelic_eurus_cmd *cmd;
	int wep104 = 0;
	int have_key = 0;
	int ret = 0;

	pr_debug("%s: <-\n", __func__);
	/* we can assume no one should uses the buffer */
	wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
	if (!wep)
		return -ENOMEM;

	memset(wep, 0, sizeof(*wep));

	if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
		pr_debug("%s: WEP mode\n", __func__);
		for (i = 0; i < GELIC_WEP_KEYS; i++) {
			if (!test_bit(i, &wl->key_enabled))
				continue;

			pr_debug("%s: key#%d enabled\n", __func__, i);
			have_key = 1;
			if (wl->key_len[i] == 13)
				wep104 = 1;
			else if (wl->key_len[i] != 5) {
				pr_info("%s: wrong wep key[%d]=%d\n",
					__func__, i, wl->key_len[i]);
				ret = -EINVAL;
				goto out;
			}
			memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
		}

		if (!have_key) {
			pr_info("%s: all wep key disabled\n", __func__);
			ret = -EINVAL;
			goto out;
		}

		if (wep104) {
			pr_debug("%s: 104bit key\n", __func__);
			wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
		} else {
			pr_debug("%s: 40bit key\n", __func__);
			wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
		}
	} else {
		pr_debug("%s: NO encryption\n", __func__);
		wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
	}

	/* issue wep setup */
	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
				   wep, sizeof(*wep));
	if (!cmd)
		ret = -ENOMEM;
	else if (cmd->status || cmd->cmd_status)
		ret = -ENXIO;

	kfree(cmd);
out:
	free_page((unsigned long)wep);
	pr_debug("%s: ->\n", __func__);
	return ret;
}

#ifdef DEBUG
static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
{
	switch (sec) {
	case GELIC_EURUS_WPA_SEC_NONE:
		return "NONE";
		break;
	case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
		return "WPA_TKIP_TKIP";
		break;
	case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
		return "WPA_TKIP_AES";
		break;
	case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
		return "WPA_AES_AES";
		break;
	case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
		return "WPA2_TKIP_TKIP";
		break;
	case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
		return "WPA2_TKIP_AES";
		break;
	case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
		return "WPA2_AES_AES";
		break;
	}
	return "";
};
#endif

static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
{
	struct gelic_eurus_wpa_cfg *wpa;
	struct gelic_eurus_cmd *cmd;
	u16 security;
	int ret = 0;

	pr_debug("%s: <-\n", __func__);
	/* we can assume no one should uses the buffer */
	wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
	if (!wpa)
		return -ENOMEM;

	memset(wpa, 0, sizeof(*wpa));

	if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
		pr_info("%s: PSK not configured yet\n", __func__);

	/* copy key */
	memcpy(wpa->psk, wl->psk, wl->psk_len);

	/* set security level */
	if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
		if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
			security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
		} else {
			if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
			    precise_ie())
				security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
			else
				security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
		}
	} else {
		if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
			security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
		} else {
			if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
			    precise_ie())
				security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
			else
				security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
		}
	}
	wpa->security = cpu_to_be16(security);

	/* PSK type */
	wpa->psk_type = cpu_to_be16(wl->psk_type);
#ifdef DEBUG
	pr_debug("%s: sec=%s psktype=%s\nn", __func__,
		 wpasecstr(wpa->security),
		 (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
		 "BIN" : "passphrase");
#if 0
	/*
	 * don't enable here if you plan to submit
	 * the debug log because this dumps your precious
	 * passphrase/key.
	 */
	pr_debug("%s: psk=%s\n",
		 (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
		 (char *)"N/A" : (char *)wpa->psk);
#endif
#endif
	/* issue wpa setup */
	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
				   wpa, sizeof(*wpa));
	if (!cmd)
		ret = -ENOMEM;
	else if (cmd->status || cmd->cmd_status)
		ret = -ENXIO;
	kfree(cmd);
	free_page((unsigned long)wpa);
	pr_debug("%s: --> %d\n", __func__, ret);
	return ret;
}

/*
 * Start association. caller must hold assoc_stat_lock
 */
static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
				  struct gelic_wl_scan_info *bss)
{
	struct gelic_eurus_cmd *cmd;
	struct gelic_eurus_common_cfg *common;
	int ret = 0;
	unsigned long rc;

	pr_debug("%s: <-\n", __func__);

	/* do common config */
	common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
	if (!common)
		return -ENOMEM;

	memset(common, 0, sizeof(*common));
	common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
	common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);

	common->scan_index = cpu_to_be16(bss->eurus_index);
	switch (wl->auth_method) {
	case GELIC_EURUS_AUTH_OPEN:
		common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
		break;
	case GELIC_EURUS_AUTH_SHARED:
		common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
		break;
	}

#ifdef DEBUG
	scan_list_dump(wl);
#endif
	pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
		 be16_to_cpu(common->scan_index),
		 be16_to_cpu(common->bss_type),
		 be16_to_cpu(common->auth_method));

	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
				   common, sizeof(*common));
	if (!cmd || cmd->status || cmd->cmd_status) {
		ret = -ENOMEM;
		kfree(cmd);
		goto out;
	}
	kfree(cmd);

	/* WEP/WPA */
	switch (wl->wpa_level) {
	case GELIC_WL_WPA_LEVEL_NONE:
		/* If WEP or no security, setup WEP config */
		ret = gelic_wl_do_wep_setup(wl);
		break;
	case GELIC_WL_WPA_LEVEL_WPA:
	case GELIC_WL_WPA_LEVEL_WPA2:
		ret = gelic_wl_do_wpa_setup(wl);
		break;
	};

	if (ret) {
		pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
			 ret);
		ret = -EPERM;
		gelic_wl_send_iwap_event(wl, NULL);
		goto out;
	}

	/* start association */
	init_completion(&wl->assoc_done);
	wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
				   NULL, 0);
	if (!cmd || cmd->status || cmd->cmd_status) {
		pr_debug("%s: assoc request failed\n", __func__);
		wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
		kfree(cmd);
		ret = -ENOMEM;
		gelic_wl_send_iwap_event(wl, NULL);
		goto out;
	}
	kfree(cmd);

	/* wait for connected event */
	rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/

	if (!rc) {
		/* timeouted.  Maybe key or cyrpt mode is wrong */
		pr_info("%s: connect timeout \n", __func__);
		cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
					   NULL, 0);
		kfree(cmd);
		wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
		gelic_wl_send_iwap_event(wl, NULL);
		ret = -ENXIO;
	} else {
		wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
		/* copy bssid */
		memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);

		/* send connect event */
		gelic_wl_send_iwap_event(wl, wl->active_bssid);
		pr_info("%s: connected\n", __func__);
	}
out:
	free_page((unsigned long)common);
	pr_debug("%s: ->\n", __func__);
	return ret;
}

/*
 * connected event
 */
static void gelic_wl_connected_event(struct gelic_wl_info *wl,
				     u64 event)
{
	u64 desired_event = 0;

	switch (wl->wpa_level) {
	case GELIC_WL_WPA_LEVEL_NONE:
		desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
		break;
	case GELIC_WL_WPA_LEVEL_WPA:
	case GELIC_WL_WPA_LEVEL_WPA2:
		desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
		break;
	}

	if (desired_event == event) {
		pr_debug("%s: completed \n", __func__);
		complete(&wl->assoc_done);
		netif_carrier_on(port_to_netdev(wl_port(wl)));
	} else
		pr_debug("%s: event %#llx under wpa\n",
				 __func__, event);
}

/*
 * disconnect event
 */
static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
				      u64 event)
{
	struct gelic_eurus_cmd *cmd;
	int lock;

	/*
	 * If we fall here in the middle of association,
	 * associate_bss() should be waiting for complation of
	 * wl->assoc_done.
	 * As it waits with timeout, just leave assoc_done
	 * uncompleted, then it terminates with timeout
	 */
	if (!mutex_trylock(&wl->assoc_stat_lock)) {
		pr_debug("%s: already locked\n", __func__);
		lock = 0;
	} else {
		pr_debug("%s: obtain lock\n", __func__);
		lock = 1;
	}

	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
	kfree(cmd);

	/* send disconnected event to the supplicant */
	if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
		gelic_wl_send_iwap_event(wl, NULL);

	wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
	netif_carrier_off(port_to_netdev(wl_port(wl)));

	if (lock)
		mutex_unlock(&wl->assoc_stat_lock);
}
/*
 * event worker
 */
#ifdef DEBUG
static const char *eventstr(enum gelic_lv1_wl_event event)
{
	static char buf[32];
	char *ret;
	if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
		ret = "EURUS_READY";
	else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
		ret = "SCAN_COMPLETED";
	else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
		ret = "DEAUTH";
	else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
		ret = "BEACON_LOST";
	else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
		ret = "CONNECTED";
	else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
		ret = "WPA_CONNECTED";
	else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
		ret = "WPA_ERROR";
	else {
		sprintf(buf, "Unknown(%#x)", event);
		ret = buf;
	}
	return ret;
}
#else
static const char *eventstr(enum gelic_lv1_wl_event event)
{
	return NULL;
}
#endif
static void gelic_wl_event_worker(struct work_struct *work)
{
	struct gelic_wl_info *wl;
	struct gelic_port *port;
	u64 event, tmp;
	int status;

	pr_debug("%s:start\n", __func__);
	wl = container_of(work, struct gelic_wl_info, event_work.work);
	port = wl_port(wl);
	while (1) {
		status = lv1_net_control(bus_id(port->card), dev_id(port->card),
					 GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
					 &event, &tmp);
		if (status) {
			if (status != LV1_NO_ENTRY)
				pr_debug("%s:wlan event failed %d\n",
					 __func__, status);
			/* got all events */
			pr_debug("%s:end\n", __func__);
			return;
		}
		pr_debug("%s: event=%s\n", __func__, eventstr(event));
		switch (event) {
		case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
			gelic_wl_scan_complete_event(wl);
			break;
		case GELIC_LV1_WL_EVENT_BEACON_LOST:
		case GELIC_LV1_WL_EVENT_DEAUTH:
			gelic_wl_disconnect_event(wl, event);
			break;
		case GELIC_LV1_WL_EVENT_CONNECTED:
		case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
			gelic_wl_connected_event(wl, event);
			break;
		default:
			break;
		}
	} /* while */
}
/*
 * association worker
 */
static void gelic_wl_assoc_worker(struct work_struct *work)
{
	struct gelic_wl_info *wl;

	struct gelic_wl_scan_info *best_bss;
	int ret;
	unsigned long irqflag;
	u8 *essid;
	size_t essid_len;

	wl = container_of(work, struct gelic_wl_info, assoc_work.work);

	mutex_lock(&wl->assoc_stat_lock);

	if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
		goto out;

	spin_lock_irqsave(&wl->lock, irqflag);
	if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
		pr_debug("%s: assoc ESSID configured %s\n", __func__,
			 wl->essid);
		essid = wl->essid;
		essid_len = wl->essid_len;
	} else {
		essid = NULL;
		essid_len = 0;
	}
	spin_unlock_irqrestore(&wl->lock, irqflag);

	ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
	if (ret == -ERESTARTSYS) {
		pr_debug("%s: scan start failed association\n", __func__);
		schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
		goto out;
	} else if (ret) {
		pr_info("%s: scan prerequisite failed\n", __func__);
		goto out;
	}

	/*
	 * Wait for bss scan completion
	 * If we have scan list already, gelic_wl_start_scan()
	 * returns OK and raises the complete.  Thus,
	 * it's ok to wait unconditionally here
	 */
	wait_for_completion(&wl->scan_done);

	pr_debug("%s: scan done\n", __func__);
	mutex_lock(&wl->scan_lock);
	if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
		gelic_wl_send_iwap_event(wl, NULL);
		pr_info("%s: no scan list. association failed\n", __func__);
		goto scan_lock_out;
	}

	/* find best matching bss */
	best_bss = gelic_wl_find_best_bss(wl);
	if (!best_bss) {
		gelic_wl_send_iwap_event(wl, NULL);
		pr_info("%s: no bss matched. association failed\n", __func__);
		goto scan_lock_out;
	}

	/* ok, do association */
	ret = gelic_wl_associate_bss(wl, best_bss);
	if (ret)
		pr_info("%s: association failed %d\n", __func__, ret);
scan_lock_out:
	mutex_unlock(&wl->scan_lock);
out:
	mutex_unlock(&wl->assoc_stat_lock);
}
/*
 * Interrupt handler
 * Called from the ethernet interrupt handler
 * Processes wireless specific virtual interrupts only
 */
void gelic_wl_interrupt(struct net_device *netdev, u64 status)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));

	if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
		pr_debug("%s:cmd complete\n", __func__);
		complete(&wl->cmd_done_intr);
	}

	if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
		pr_debug("%s:event received\n", __func__);
		queue_delayed_work(wl->event_queue, &wl->event_work, 0);
	}
}

/*
 * driver helpers
 */
#define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
static const iw_handler gelic_wl_wext_handler[] =
{
	IW_IOCTL(SIOCGIWNAME)		= gelic_wl_get_name,
	IW_IOCTL(SIOCGIWRANGE)		= gelic_wl_get_range,
	IW_IOCTL(SIOCSIWSCAN)		= gelic_wl_set_scan,
	IW_IOCTL(SIOCGIWSCAN)		= gelic_wl_get_scan,
	IW_IOCTL(SIOCSIWAUTH)		= gelic_wl_set_auth,
	IW_IOCTL(SIOCGIWAUTH)		= gelic_wl_get_auth,
	IW_IOCTL(SIOCSIWESSID)		= gelic_wl_set_essid,
	IW_IOCTL(SIOCGIWESSID)		= gelic_wl_get_essid,
	IW_IOCTL(SIOCSIWENCODE)		= gelic_wl_set_encode,
	IW_IOCTL(SIOCGIWENCODE)		= gelic_wl_get_encode,
	IW_IOCTL(SIOCSIWAP)		= gelic_wl_set_ap,
	IW_IOCTL(SIOCGIWAP)		= gelic_wl_get_ap,
	IW_IOCTL(SIOCSIWENCODEEXT)	= gelic_wl_set_encodeext,
	IW_IOCTL(SIOCGIWENCODEEXT)	= gelic_wl_get_encodeext,
	IW_IOCTL(SIOCSIWMODE)		= gelic_wl_set_mode,
	IW_IOCTL(SIOCGIWMODE)		= gelic_wl_get_mode,
	IW_IOCTL(SIOCGIWNICKN)		= gelic_wl_get_nick,
};

#ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
static struct iw_priv_args gelic_wl_private_args[] =
{
	{
		.cmd = GELIC_WL_PRIV_SET_PSK,
		.set_args = IW_PRIV_TYPE_CHAR |
		(GELIC_WL_EURUS_PSK_MAX_LEN + 2),
		.name = "set_psk"
	},
	{
		.cmd = GELIC_WL_PRIV_GET_PSK,
		.get_args = IW_PRIV_TYPE_CHAR |
		(GELIC_WL_EURUS_PSK_MAX_LEN + 2),
		.name = "get_psk"
	}
};

static const iw_handler gelic_wl_private_handler[] =
{
	gelic_wl_priv_set_psk,
	gelic_wl_priv_get_psk,
};
#endif

static const struct iw_handler_def gelic_wl_wext_handler_def = {
	.num_standard		= ARRAY_SIZE(gelic_wl_wext_handler),
	.standard		= gelic_wl_wext_handler,
	.get_wireless_stats	= gelic_wl_get_wireless_stats,
#ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
	.num_private		= ARRAY_SIZE(gelic_wl_private_handler),
	.num_private_args	= ARRAY_SIZE(gelic_wl_private_args),
	.private		= gelic_wl_private_handler,
	.private_args		= gelic_wl_private_args,
#endif
};

static struct net_device * __devinit gelic_wl_alloc(struct gelic_card *card)
{
	struct net_device *netdev;
	struct gelic_port *port;
	struct gelic_wl_info *wl;
	unsigned int i;

	pr_debug("%s:start\n", __func__);
	netdev = alloc_etherdev(sizeof(struct gelic_port) +
				sizeof(struct gelic_wl_info));
	pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
	if (!netdev)
		return NULL;

	strcpy(netdev->name, "wlan%d");

	port = netdev_priv(netdev);
	port->netdev = netdev;
	port->card = card;
	port->type = GELIC_PORT_WIRELESS;

	wl = port_wl(port);
	pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);

	/* allocate scan list */
	wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
			       GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);

	if (!wl->networks)
		goto fail_bss;

	wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
	if (!wl->eurus_cmd_queue)
		goto fail_cmd_workqueue;

	wl->event_queue = create_singlethread_workqueue("gelic_event");
	if (!wl->event_queue)
		goto fail_event_workqueue;

	INIT_LIST_HEAD(&wl->network_free_list);
	INIT_LIST_HEAD(&wl->network_list);
	for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
		list_add_tail(&wl->networks[i].list,
			      &wl->network_free_list);
	init_completion(&wl->cmd_done_intr);

	INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
	INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
	mutex_init(&wl->scan_lock);
	mutex_init(&wl->assoc_stat_lock);

	init_completion(&wl->scan_done);
	/* for the case that no scan request is issued and stop() is called */
	complete(&wl->scan_done);

	spin_lock_init(&wl->lock);

	wl->scan_age = 5*HZ; /* FIXME */

	/* buffer for receiving scanned list etc */
	BUILD_BUG_ON(PAGE_SIZE <
		     sizeof(struct gelic_eurus_scan_info) *
		     GELIC_EURUS_MAX_SCAN);
	pr_debug("%s:end\n", __func__);
	return netdev;

fail_event_workqueue:
	destroy_workqueue(wl->eurus_cmd_queue);
fail_cmd_workqueue:
	kfree(wl->networks);
fail_bss:
	free_netdev(netdev);
	pr_debug("%s:end error\n", __func__);
	return NULL;

}

static void gelic_wl_free(struct gelic_wl_info *wl)
{
	struct gelic_wl_scan_info *scan_info;
	unsigned int i;

	pr_debug("%s: <-\n", __func__);

	pr_debug("%s: destroy queues\n", __func__);
	destroy_workqueue(wl->eurus_cmd_queue);
	destroy_workqueue(wl->event_queue);

	scan_info = wl->networks;
	for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
		kfree(scan_info->hwinfo);
	kfree(wl->networks);

	free_netdev(port_to_netdev(wl_port(wl)));

	pr_debug("%s: ->\n", __func__);
}

static int gelic_wl_try_associate(struct net_device *netdev)
{
	struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
	int ret = -1;
	unsigned int i;

	pr_debug("%s: <-\n", __func__);

	/* check constraits for start association */
	/* for no access restriction AP */
	if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
		if (test_bit(GELIC_WL_STAT_CONFIGURED,
			     &wl->stat))
			goto do_associate;
		else {
			pr_debug("%s: no wep, not configured\n", __func__);
			return ret;
		}
	}

	/* for WEP, one of four keys should be set */
	if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
		/* one of keys set */
		for (i = 0; i < GELIC_WEP_KEYS; i++) {
			if (test_bit(i, &wl->key_enabled))
			    goto do_associate;
		}
		pr_debug("%s: WEP, but no key specified\n", __func__);
		return ret;
	}

	/* for WPA[2], psk should be set */
	if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
	    (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
		if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
			     &wl->stat))
			goto do_associate;
		else {
			pr_debug("%s: AES/TKIP, but PSK not configured\n",
				 __func__);
			return ret;
		}
	}

do_associate:
	ret = schedule_delayed_work(&wl->assoc_work, 0);
	pr_debug("%s: start association work %d\n", __func__, ret);
	return ret;
}

/*
 * netdev handlers
 */
static int gelic_wl_open(struct net_device *netdev)
{
	struct gelic_card *card = netdev_card(netdev);

	pr_debug("%s:->%p\n", __func__, netdev);

	gelic_card_up(card);

	/* try to associate */
	gelic_wl_try_associate(netdev);

	netif_start_queue(netdev);

	pr_debug("%s:<-\n", __func__);
	return 0;
}

/*
 * reset state machine
 */
static int gelic_wl_reset_state(struct gelic_wl_info *wl)
{
	struct gelic_wl_scan_info *target;
	struct gelic_wl_scan_info *tmp;

	/* empty scan list */
	list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
		list_move_tail(&target->list, &wl->network_free_list);
	}
	wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;

	/* clear configuration */
	wl->auth_method = GELIC_EURUS_AUTH_OPEN;
	wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
	wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
	wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;

	wl->key_enabled = 0;
	wl->current_key = 0;

	wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
	wl->psk_len = 0;

	wl->essid_len = 0;
	memset(wl->essid, 0, sizeof(wl->essid));
	memset(wl->bssid, 0, sizeof(wl->bssid));
	memset(wl->active_bssid, 0, sizeof(wl->active_bssid));

	wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;

	memset(&wl->iwstat, 0, sizeof(wl->iwstat));
	/* all status bit clear */
	wl->stat = 0;
	return 0;
}

/*
 * Tell eurus to terminate association
 */
static void gelic_wl_disconnect(struct net_device *netdev)
{
	struct gelic_port *port = netdev_priv(netdev);
	struct gelic_wl_info *wl = port_wl(port);
	struct gelic_eurus_cmd *cmd;

	/*
	 * If scann process is running on chip,
	 * further requests will be rejected
	 */
	if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
		wait_for_completion_timeout(&wl->scan_done, HZ);

	cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
	kfree(cmd);
	gelic_wl_send_iwap_event(wl, NULL);
};

static int gelic_wl_stop(struct net_device *netdev)
{
	struct gelic_port *port = netdev_priv(netdev);
	struct gelic_wl_info *wl = port_wl(port);
	struct gelic_card *card = netdev_card(netdev);

	pr_debug("%s:<-\n", __func__);

	/*
	 * Cancel pending association work.
	 * event work can run after netdev down
	 */
	cancel_delayed_work(&wl->assoc_work);

	if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
		gelic_wl_disconnect(netdev);

	/* reset our state machine */
	gelic_wl_reset_state(wl);

	netif_stop_queue(netdev);

	gelic_card_down(card);

	pr_debug("%s:->\n", __func__);
	return 0;
}

/* -- */

static const struct net_device_ops gelic_wl_netdevice_ops = {
	.ndo_open = gelic_wl_open,
	.ndo_stop = gelic_wl_stop,
	.ndo_start_xmit = gelic_net_xmit,
	.ndo_set_multicast_list = gelic_net_set_multi,
	.ndo_change_mtu = gelic_net_change_mtu,
	.ndo_tx_timeout = gelic_net_tx_timeout,
	.ndo_set_mac_address = eth_mac_addr,
	.ndo_validate_addr = eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = gelic_net_poll_controller,
#endif
};

static const struct ethtool_ops gelic_wl_ethtool_ops = {
	.get_drvinfo	= gelic_net_get_drvinfo,
	.get_link	= gelic_wl_get_link,
	.get_tx_csum	= ethtool_op_get_tx_csum,
	.set_tx_csum	= ethtool_op_set_tx_csum,
	.get_rx_csum	= gelic_net_get_rx_csum,
	.set_rx_csum	= gelic_net_set_rx_csum,
};

static void __devinit gelic_wl_setup_netdev_ops(struct net_device *netdev)
{
	struct gelic_wl_info *wl;
	wl = port_wl(netdev_priv(netdev));
	BUG_ON(!wl);
	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;

	netdev->ethtool_ops = &gelic_wl_ethtool_ops;
	netdev->netdev_ops = &gelic_wl_netdevice_ops;
	netdev->wireless_data = &wl->wireless_data;
	netdev->wireless_handlers = &gelic_wl_wext_handler_def;
}

/*
 * driver probe/remove
 */
int __devinit gelic_wl_driver_probe(struct gelic_card *card)
{
	int ret;
	struct net_device *netdev;

	pr_debug("%s:start\n", __func__);

	if (ps3_compare_firmware_version(1, 6, 0) < 0)
		return 0;
	if (!card->vlan[GELIC_PORT_WIRELESS].tx)
		return 0;

	/* alloc netdevice for wireless */
	netdev = gelic_wl_alloc(card);
	if (!netdev)
		return -ENOMEM;

	/* setup net_device structure */
	SET_NETDEV_DEV(netdev, &card->dev->core);
	gelic_wl_setup_netdev_ops(netdev);

	/* setup some of net_device and register it */
	ret = gelic_net_setup_netdev(netdev, card);
	if (ret)
		goto fail_setup;
	card->netdev[GELIC_PORT_WIRELESS] = netdev;

	/* add enable wireless interrupt */
	card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
		GELIC_CARD_WLAN_COMMAND_COMPLETED;
	/* to allow wireless commands while both interfaces are down */
	gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
				GELIC_CARD_WLAN_COMMAND_COMPLETED);
	pr_debug("%s:end\n", __func__);
	return 0;

fail_setup:
	gelic_wl_free(port_wl(netdev_port(netdev)));

	return ret;
}

int gelic_wl_driver_remove(struct gelic_card *card)
{
	struct gelic_wl_info *wl;
	struct net_device *netdev;

	pr_debug("%s:start\n", __func__);

	if (ps3_compare_firmware_version(1, 6, 0) < 0)
		return 0;
	if (!card->vlan[GELIC_PORT_WIRELESS].tx)
		return 0;

	netdev = card->netdev[GELIC_PORT_WIRELESS];
	wl = port_wl(netdev_priv(netdev));

	/* if the interface was not up, but associated */
	if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
		gelic_wl_disconnect(netdev);

	complete(&wl->cmd_done_intr);

	/* cancel all work queue */
	cancel_delayed_work(&wl->assoc_work);
	cancel_delayed_work(&wl->event_work);
	flush_workqueue(wl->eurus_cmd_queue);
	flush_workqueue(wl->event_queue);

	unregister_netdev(netdev);

	/* disable wireless interrupt */
	pr_debug("%s: disable intr\n", __func__);
	card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
			    GELIC_CARD_WLAN_COMMAND_COMPLETED);
	/* free bss list, netdev*/
	gelic_wl_free(wl);
	pr_debug("%s:end\n", __func__);
	return 0;
}
OpenPOWER on IntegriCloud