summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/sd8797/mlinux/moal_uap_cfg80211.c
blob: 917123f6978be444f77773e298fa1033bc56a9fb (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
/** @file moal_uap_cfg80211.c
  *
  * @brief This file contains the functions for uAP CFG80211. 
  *
  * Copyright (C) 2011-2012, Marvell International Ltd. 
  * 
  * This software file (the "File") is distributed by Marvell International 
  * Ltd. under the terms of the GNU General Public License Version 2, June 1991 
  * (the "License").  You may use, redistribute and/or modify this File in 
  * accordance with the terms and conditions of the License, a copy of which 
  * is available by writing to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  *
  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE 
  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE 
  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about 
  * this warranty disclaimer.
  *
  */

#include "moal_cfg80211.h"
#include "moal_uap_cfg80211.h"
/********************************************************
				Local Variables
********************************************************/

/********************************************************
				Global Variables
********************************************************/

/********************************************************
				Local Functions
********************************************************/

/********************************************************
				Global Functions
********************************************************/

/**
 * @brief Verify RSN IE
 *
 * @param rsn_ie          Pointer RSN IE
 * @param sys_config      Pointer to mlan_uap_bss_param structure
 *
 * @return                MTRUE/MFALSE
 */
static t_u8
woal_check_rsn_ie(IEEEtypes_Rsn_t * rsn_ie, mlan_uap_bss_param * sys_config)
{
    int left = 0;
    int count = 0;
    int i = 0;
    wpa_suite_auth_key_mgmt_t *key_mgmt = NULL;
    left = rsn_ie->len + 2;
    if (left < sizeof(IEEEtypes_Rsn_t))
        return MFALSE;
    sys_config->wpa_cfg.group_cipher = 0;
    sys_config->wpa_cfg.pairwise_cipher_wpa2 = 0;
    sys_config->key_mgmt = 0;
    /* check the group cipher */
    switch (rsn_ie->group_cipher.type) {
    case WPA_CIPHER_TKIP:
        sys_config->wpa_cfg.group_cipher = CIPHER_TKIP;
        break;
    case WPA_CIPHER_AES_CCM:
        sys_config->wpa_cfg.group_cipher = CIPHER_AES_CCMP;
        break;
    default:
        break;
    }
    count = le16_to_cpu(rsn_ie->pairwise_cipher.count);
    for (i = 0; i < count; i++) {
        switch (rsn_ie->pairwise_cipher.list[i].type) {
        case WPA_CIPHER_TKIP:
            sys_config->wpa_cfg.pairwise_cipher_wpa2 |= CIPHER_TKIP;
            break;
        case WPA_CIPHER_AES_CCM:
            sys_config->wpa_cfg.pairwise_cipher_wpa2 |= CIPHER_AES_CCMP;
            break;
        default:
            break;
        }
    }
    left -= sizeof(IEEEtypes_Rsn_t) + (count - 1) * sizeof(wpa_suite);
    if (left < sizeof(wpa_suite_auth_key_mgmt_t))
        return MFALSE;
    key_mgmt =
        (wpa_suite_auth_key_mgmt_t *) ((u8 *) rsn_ie + sizeof(IEEEtypes_Rsn_t) +
                                       (count - 1) * sizeof(wpa_suite));
    count = le16_to_cpu(key_mgmt->count);
    if (left <
        (sizeof(wpa_suite_auth_key_mgmt_t) + (count - 1) * sizeof(wpa_suite)))
        return MFALSE;
    for (i = 0; i < count; i++) {
        switch (key_mgmt->list[i].type) {
        case RSN_AKM_8021X:
            sys_config->key_mgmt |= KEY_MGMT_EAP;
            break;
        case RSN_AKM_PSK:
            sys_config->key_mgmt |= KEY_MGMT_PSK;
            break;
        case RSN_AKM_PSK_SHA256:
            sys_config->key_mgmt |= KEY_MGMT_PSK_SHA256;
            break;
        }
    }
    return MTRUE;
}

/**
 * @brief Verify WPA IE
 *
 * @param wpa_ie          Pointer WPA IE
 * @param sys_config      Pointer to mlan_uap_bss_param structure
 *
 * @return                MTRUE/MFALSE
 */
static t_u8
woal_check_wpa_ie(IEEEtypes_Wpa_t * wpa_ie, mlan_uap_bss_param * sys_config)
{
    int left = 0;
    int count = 0;
    int i = 0;
    wpa_suite_auth_key_mgmt_t *key_mgmt = NULL;
    left = wpa_ie->len + 2;
    if (left < sizeof(IEEEtypes_Wpa_t))
        return MFALSE;
    sys_config->wpa_cfg.group_cipher = 0;
    sys_config->wpa_cfg.pairwise_cipher_wpa = 0;
    switch (wpa_ie->group_cipher.type) {
    case WPA_CIPHER_TKIP:
        sys_config->wpa_cfg.group_cipher = CIPHER_TKIP;
        break;
    case WPA_CIPHER_AES_CCM:
        sys_config->wpa_cfg.group_cipher = CIPHER_AES_CCMP;
        break;
    default:
        break;
    }
    count = le16_to_cpu(wpa_ie->pairwise_cipher.count);
    for (i = 0; i < count; i++) {
        switch (wpa_ie->pairwise_cipher.list[i].type) {
        case WPA_CIPHER_TKIP:
            sys_config->wpa_cfg.pairwise_cipher_wpa |= CIPHER_TKIP;
            break;
        case WPA_CIPHER_AES_CCM:
            sys_config->wpa_cfg.pairwise_cipher_wpa |= CIPHER_AES_CCMP;
            break;
        default:
            break;
        }
    }
    left -= sizeof(IEEEtypes_Wpa_t) + (count - 1) * sizeof(wpa_suite);
    if (left < sizeof(wpa_suite_auth_key_mgmt_t))
        return MFALSE;
    key_mgmt =
        (wpa_suite_auth_key_mgmt_t *) ((u8 *) wpa_ie + sizeof(IEEEtypes_Wpa_t) +
                                       (count - 1) * sizeof(wpa_suite));
    count = le16_to_cpu(key_mgmt->count);
    if (left <
        (sizeof(wpa_suite_auth_key_mgmt_t) + (count - 1) * sizeof(wpa_suite)))
        return MFALSE;
    for (i = 0; i < count; i++) {
        switch (key_mgmt->list[i].type) {
        case RSN_AKM_8021X:
            sys_config->key_mgmt = KEY_MGMT_EAP;
            break;
        case RSN_AKM_PSK:
            sys_config->key_mgmt = KEY_MGMT_PSK;
            break;
        }
    }
    return MTRUE;
}

/**
 * @brief Find RSN/WPA IES
 *
 * @param ie              Pointer IE buffer
 * @param sys_config      Pointer to mlan_uap_bss_param structure
 *
 * @return                MTRUE/MFALSE
 */
static t_u8
woal_find_wpa_ies(const t_u8 * ie, int len, mlan_uap_bss_param * sys_config)
{
    int bytes_left = len;
    const t_u8 *pcurrent_ptr = ie;
    t_u16 total_ie_len;
    t_u8 element_len;
    t_u8 wpa2 = 0;
    t_u8 wpa = 0;
    t_u8 ret = MFALSE;
    IEEEtypes_ElementId_e element_id;
    IEEEtypes_VendorSpecific_t *pvendor_ie;
    const t_u8 wpa_oui[4] = { 0x00, 0x50, 0xf2, 0x01 };

    while (bytes_left >= 2) {
        element_id = (IEEEtypes_ElementId_e) (*((t_u8 *) pcurrent_ptr));
        element_len = *((t_u8 *) pcurrent_ptr + 1);
        total_ie_len = element_len + sizeof(IEEEtypes_Header_t);
        if (bytes_left < total_ie_len) {
            PRINTM(MERROR, "InterpretIE: Error in processing IE, "
                   "bytes left < IE length\n");
            bytes_left = 0;
            continue;
        }
        switch (element_id) {
        case RSN_IE:
            wpa2 =
                woal_check_rsn_ie((IEEEtypes_Rsn_t *) pcurrent_ptr, sys_config);
            break;
        case VENDOR_SPECIFIC_221:
            pvendor_ie = (IEEEtypes_VendorSpecific_t *) pcurrent_ptr;
            if (!memcmp
                (pvendor_ie->vend_hdr.oui, wpa_oui,
                 sizeof(pvendor_ie->vend_hdr.oui)) &&
                (pvendor_ie->vend_hdr.oui_type == wpa_oui[3])) {
                wpa =
                    woal_check_wpa_ie((IEEEtypes_Wpa_t *) pcurrent_ptr,
                                      sys_config);
            }
            break;
        default:
            break;
        }
        pcurrent_ptr += element_len + 2;
        /* Need to account for IE ID and IE Len */
        bytes_left -= (element_len + 2);
    }
    if (wpa && wpa2) {
        sys_config->protocol = PROTOCOL_WPA | PROTOCOL_WPA2;
        ret = MTRUE;
    } else if (wpa2) {
        sys_config->protocol = PROTOCOL_WPA2;
        ret = MTRUE;
    } else if (wpa) {
        sys_config->protocol = PROTOCOL_WPA;
        ret = MTRUE;
    }
    return ret;
}

/** secondary channel is below */
#define SECOND_CHANNEL_BELOW    0x30
/** secondary channel is above */
#define SECOND_CHANNEL_ABOVE    0x10
/** no secondary channel */
#define SECONDARY_CHANNEL_NONE     0x00

/**
 * @brief Get second channel offset 
 *
 * @param chan 			  channel num
 * @return                second channel offset
 */
static t_u8
woal_get_second_channel_offset(int chan)
{
    t_u8 chan2Offset = SECONDARY_CHANNEL_NONE;

    switch (chan) {
    case 36:
    case 44:
    case 52:
    case 60:
    case 100:
    case 108:
    case 116:
    case 124:
    case 132:
    case 149:
    case 157:
        chan2Offset = SECOND_CHANNEL_ABOVE;
        break;
    case 40:
    case 48:
    case 56:
    case 64:
    case 104:
    case 112:
    case 120:
    case 128:
    case 136:
    case 153:
    case 161:
        chan2Offset = SECOND_CHANNEL_BELOW;
        break;
    case 140:
    case 165:
        /* Special Case: 20Mhz-only Channel */
        chan2Offset = SECONDARY_CHANNEL_NONE;
        break;
    }
    return chan2Offset;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
/**
 * @brief initialize AP or GO bss config
 *
 * @param priv            A pointer to moal private structure
 * @param params          A pointer to cfg80211_ap_settings structure 
 * @return                0 -- success, otherwise fail
 */
static int
woal_cfg80211_beacon_config(moal_private * priv,
                            struct cfg80211_ap_settings *params)
#else
/**
 * @brief initialize AP or GO bss config
 *
 * @param priv            A pointer to moal private structure
 * @param params          A pointer to beacon_parameters structure 
 * @return                0 -- success, otherwise fail
 */
static int
woal_cfg80211_beacon_config(moal_private * priv,
                            struct beacon_parameters *params)
#endif
{
    int ret = 0;
    mlan_uap_bss_param sys_config;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0) || defined(COMPAT_WIRELESS)
    int i = 0;
#else
    const t_u8 *ssid_ie = NULL;
    struct ieee80211_mgmt *head = NULL;
    t_u16 capab_info = 0;
#endif
    t_u8 Rates_BG[13] =
        { 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48,
        0x60, 0x6c, 0
    };
    t_u8 Rates_A[9] = { 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c, 0 };
    t_u8 chan2Offset = 0;
#ifdef WIFI_DIRECT_SUPPORT
    t_u8 Rates_WFD[9] = { 0x8c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c, 0 };
#endif

    ENTER();

    if (params == NULL) {
        ret = -EFAULT;
        goto done;
    }

    if (priv->bss_type != MLAN_BSS_TYPE_UAP
#ifdef WIFI_DIRECT_SUPPORT
        && priv->bss_type != MLAN_BSS_TYPE_WIFIDIRECT
#endif
        ) {
        ret = -EFAULT;
        goto done;
    }

    /* Initialize the uap bss values which are uploaded from firmware */
    if (MLAN_STATUS_SUCCESS != woal_set_get_sys_config(priv,
                                                       MLAN_ACT_GET,
                                                       MOAL_IOCTL_WAIT,
                                                       &sys_config)) {
        PRINTM(MERROR, "Error getting AP confiruration\n");
        ret = -EFAULT;
        goto done;
    }

    /* Setting the default values */
    sys_config.channel = 6;
    sys_config.preamble_type = 0;
    sys_config.mgmt_ie_passthru_mask = priv->mgmt_subtype_mask;
    memcpy(sys_config.mac_addr, priv->current_addr, ETH_ALEN);

    if (priv->bss_type == MLAN_BSS_TYPE_UAP) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
        if (params->beacon_interval)
            sys_config.beacon_period = params->beacon_interval;
#else
        if (params->interval)
            sys_config.beacon_period = params->interval;
#endif
        if (params->dtim_period)
            sys_config.dtim_period = params->dtim_period;
    }
    if (priv->channel) {
        memset(sys_config.rates, 0, sizeof(sys_config.rates));
        sys_config.channel = priv->channel;
        if (priv->channel <= MAX_BG_CHANNEL) {
            sys_config.band_cfg = BAND_CONFIG_2G;
#ifdef WIFI_DIRECT_SUPPORT
            if (priv->bss_type == MLAN_BSS_TYPE_WIFIDIRECT)
                memcpy(sys_config.rates, Rates_WFD, sizeof(Rates_WFD));
            else
#endif
                memcpy(sys_config.rates, Rates_BG, sizeof(Rates_BG));
        } else {
            sys_config.band_cfg = BAND_CONFIG_5G;
            chan2Offset = woal_get_second_channel_offset(priv->channel);
            if (chan2Offset) {
                sys_config.band_cfg |= chan2Offset;
                sys_config.ht_cap_info = 0x117e;
                sys_config.ampdu_param = 3;
            }
#ifdef WIFI_DIRECT_SUPPORT
            if (priv->bss_type == MLAN_BSS_TYPE_WIFIDIRECT)
                memcpy(sys_config.rates, Rates_WFD, sizeof(Rates_WFD));
            else
#endif
                memcpy(sys_config.rates, Rates_A, sizeof(Rates_A));
        }
    }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0) || defined(COMPAT_WIRELESS)
    if (!params->ssid || !params->ssid_len) {
        ret = -EINVAL;
        goto done;
    }
    memcpy(sys_config.ssid.ssid, params->ssid,
           MIN(MLAN_MAX_SSID_LENGTH, params->ssid_len));
    sys_config.ssid.ssid_len = MIN(MLAN_MAX_SSID_LENGTH, params->ssid_len);
    if (params->hidden_ssid)
        sys_config.bcast_ssid_ctl = 0;
    else
        sys_config.bcast_ssid_ctl = 1;
    if (params->auth_type == NL80211_AUTHTYPE_SHARED_KEY)
        sys_config.auth_mode = MLAN_AUTH_MODE_SHARED;
    else
        sys_config.auth_mode = MLAN_AUTH_MODE_OPEN;
    if (params->crypto.n_akm_suites) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
        woal_find_wpa_ies(params->beacon.tail, (int) params->beacon.tail_len,
                          &sys_config);
#else
        woal_find_wpa_ies(params->tail, params->tail_len, &sys_config);
#endif
    }
    for (i = 0; i < params->crypto.n_akm_suites; i++) {
        switch (params->crypto.akm_suites[i]) {
        case WLAN_AKM_SUITE_8021X:
            sys_config.key_mgmt |= KEY_MGMT_EAP;
            if ((params->crypto.wpa_versions & NL80211_WPA_VERSION_1) &&
                (params->crypto.wpa_versions & NL80211_WPA_VERSION_2))
                sys_config.protocol = PROTOCOL_WPA | PROTOCOL_WPA2;
            else if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
                sys_config.protocol = PROTOCOL_WPA2;
            else if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
                sys_config.protocol = PROTOCOL_WPA;
            break;
        case WLAN_AKM_SUITE_PSK:
            sys_config.key_mgmt |= KEY_MGMT_PSK;
            if ((params->crypto.wpa_versions & NL80211_WPA_VERSION_1) &&
                (params->crypto.wpa_versions & NL80211_WPA_VERSION_2))
                sys_config.protocol = PROTOCOL_WPA | PROTOCOL_WPA2;
            else if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
                sys_config.protocol = PROTOCOL_WPA2;
            else if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
                sys_config.protocol = PROTOCOL_WPA;
            break;
        }
    }
    sys_config.wpa_cfg.pairwise_cipher_wpa = 0;
    sys_config.wpa_cfg.pairwise_cipher_wpa2 = 0;
    for (i = 0; i < params->crypto.n_ciphers_pairwise; i++) {
        switch (params->crypto.ciphers_pairwise[i]) {
        case WLAN_CIPHER_SUITE_WEP40:
        case WLAN_CIPHER_SUITE_WEP104:
            break;
        case WLAN_CIPHER_SUITE_TKIP:
            if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
                sys_config.wpa_cfg.pairwise_cipher_wpa |= CIPHER_TKIP;
            if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
                sys_config.wpa_cfg.pairwise_cipher_wpa2 |= CIPHER_TKIP;
            break;
        case WLAN_CIPHER_SUITE_CCMP:
            if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
                sys_config.wpa_cfg.pairwise_cipher_wpa |= CIPHER_AES_CCMP;
            if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
                sys_config.wpa_cfg.pairwise_cipher_wpa2 |= CIPHER_AES_CCMP;
            break;
        }
    }
    switch (params->crypto.cipher_group) {
    case WLAN_CIPHER_SUITE_WEP40:
    case WLAN_CIPHER_SUITE_WEP104:
        if ((priv->cipher == WLAN_CIPHER_SUITE_WEP40) ||
            (priv->cipher == WLAN_CIPHER_SUITE_WEP104)) {
            sys_config.protocol = PROTOCOL_STATIC_WEP;
            sys_config.key_mgmt = KEY_MGMT_NONE;
            sys_config.wpa_cfg.length = 0;
            sys_config.wep_cfg.key0.key_index = priv->key_index;
            sys_config.wep_cfg.key0.is_default = 1;
            sys_config.wep_cfg.key0.length = priv->key_len;
            memcpy(sys_config.wep_cfg.key0.key, priv->key_material,
                   priv->key_len);
        }
        break;
    case WLAN_CIPHER_SUITE_TKIP:
        sys_config.wpa_cfg.group_cipher = CIPHER_TKIP;
        break;
    case WLAN_CIPHER_SUITE_CCMP:
        sys_config.wpa_cfg.group_cipher = CIPHER_AES_CCMP;
        break;
    }
#else
    /* Since in Android ICS 4.0.1's wpa_supplicant, there is no way to set ssid
       when GO (AP) starts up, so get it from beacon head parameter TODO: right
       now use hard code 24 -- ieee80211 header lenth, 12 -- fixed element
       length for beacon */
#define BEACON_IE_OFFSET	36
    /* Find SSID in head SSID IE id: 0, right now use hard code */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
    ssid_ie = woal_parse_ie_tlv(params->beacon.head + BEACON_IE_OFFSET,
                                params->beacon.head_len - BEACON_IE_OFFSET, 0);
#else
    ssid_ie = woal_parse_ie_tlv(params->head + BEACON_IE_OFFSET,
                                params->head_len - BEACON_IE_OFFSET, 0);
#endif
    if (!ssid_ie) {
        PRINTM(MERROR, "No ssid IE found.\n");
        ret = -EFAULT;
        goto done;
    }
    if (*(ssid_ie + 1) > 32) {
        PRINTM(MERROR, "ssid len error: %d\n", *(ssid_ie + 1));
        ret = -EFAULT;
        goto done;
    }
    memcpy(sys_config.ssid.ssid, ssid_ie + 2, *(ssid_ie + 1));
    sys_config.ssid.ssid_len = *(ssid_ie + 1);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
    head = (struct ieee80211_mgmt *) params->beacon.head;
#else
    head = (struct ieee80211_mgmt *) params->head;
#endif
    capab_info = le16_to_cpu(head->u.beacon.capab_info);
    PRINTM(MIOCTL, "capab_info=0x%x\n", head->u.beacon.capab_info);
    sys_config.auth_mode = MLAN_AUTH_MODE_OPEN;
        /** For ICS, we don't support OPEN mode */
    if ((priv->cipher == WLAN_CIPHER_SUITE_WEP40) ||
        (priv->cipher == WLAN_CIPHER_SUITE_WEP104)) {
        sys_config.protocol = PROTOCOL_STATIC_WEP;
        sys_config.key_mgmt = KEY_MGMT_NONE;
        sys_config.wpa_cfg.length = 0;
        sys_config.wep_cfg.key0.key_index = priv->key_index;
        sys_config.wep_cfg.key0.is_default = 1;
        sys_config.wep_cfg.key0.length = priv->key_len;
        memcpy(sys_config.wep_cfg.key0.key, priv->key_material, priv->key_len);
    } else {
                /** Get cipher and key_mgmt from RSN/WPA IE */
        if (capab_info & WLAN_CAPABILITY_PRIVACY) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
            if (MFALSE ==
                woal_find_wpa_ies(params->beacon.tail,
                                  (int) params->beacon.tail_len, &sys_config))
#else
            if (MFALSE ==
                woal_find_wpa_ies(params->tail, params->tail_len, &sys_config))
#endif
            {
                /* hard code setting to wpa2-psk */
                sys_config.protocol = PROTOCOL_WPA2;
                sys_config.key_mgmt = KEY_MGMT_PSK;
                sys_config.wpa_cfg.pairwise_cipher_wpa2 = CIPHER_AES_CCMP;
                sys_config.wpa_cfg.group_cipher = CIPHER_AES_CCMP;
            }
        }
    }
#endif /* COMPAT_WIRELESS */
    /* If the security mode is configured as WEP or WPA-PSK, it will disable
       11n automatically, and if configured as open(off) or wpa2-psk, it will
       automatically enable 11n */
    if ((sys_config.protocol == PROTOCOL_STATIC_WEP) ||
        (sys_config.protocol == PROTOCOL_WPA))
        woal_uap_set_11n_status(&sys_config, MLAN_ACT_DISABLE);
    else
        woal_uap_set_11n_status(&sys_config, MLAN_ACT_ENABLE);
    if (MLAN_STATUS_SUCCESS != woal_set_get_sys_config(priv,
                                                       MLAN_ACT_SET,
                                                       MOAL_IOCTL_WAIT,
                                                       &sys_config)) {
        ret = -EFAULT;
        goto done;
    }
  done:
    LEAVE();
    return ret;
}

#if defined(WIFI_DIRECT_SUPPORT)
#if LINUX_VERSION_CODE >= WIFI_DIRECT_KERNEL_VERSION
/**
 * @brief Callback function for virtual interface
 * 		setup
 *
 *  @param dev    A pointer to structure net_device
 *
 *  @return       N/A
 */
static void
woal_virt_if_setup(struct net_device *dev)
{
    ENTER();
    ether_setup(dev);
    dev->destructor = free_netdev;
    LEAVE();
}

/**
 * @brief This function adds a new interface. It will
 * 		allocate, initialize and register the device.
 *
 *  @param handle    A pointer to moal_handle structure
 *  @param bss_index BSS index number
 *  @param bss_type  BSS type
 *
 *  @return          A pointer to the new priv structure
 */
moal_private *
woal_alloc_virt_interface(moal_handle * handle, t_u8 bss_index, t_u8 bss_type,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
                          const
#endif
                          char *name)
{
    struct net_device *dev = NULL;
    moal_private *priv = NULL;
    ENTER();

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,29)
#ifndef MAX_WMM_QUEUE
#define MAX_WMM_QUEUE   4
#endif
    /* Allocate an Ethernet device */
    if (!
        (dev =
         alloc_netdev_mq(sizeof(moal_private), name, woal_virt_if_setup,
                         MAX_WMM_QUEUE))) {
#else
    if (!(dev = alloc_netdev(sizeof(moal_private), name, woal_virt_if_setup))) {
#endif
        PRINTM(MFATAL, "Init virtual ethernet device failed\n");
        goto error;
    }
    /* Allocate device name */
    if ((dev_alloc_name(dev, name) < 0)) {
        PRINTM(MERROR, "Could not allocate device name\n");
        goto error;
    }

    priv = (moal_private *) netdev_priv(dev);
    /* Save the priv to handle */
    handle->priv[bss_index] = priv;

    /* Use the same handle structure */
    priv->phandle = handle;
    priv->netdev = dev;
    priv->bss_index = bss_index;
    priv->bss_type = bss_type;
    priv->bss_role = MLAN_BSS_ROLE_STA;

    INIT_LIST_HEAD(&priv->tcp_sess_queue);
    spin_lock_init(&priv->tcp_sess_lock);

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
    SET_MODULE_OWNER(dev);
#endif

    PRINTM(MCMND, "Alloc virtual interface%s\n", dev->name);

    LEAVE();
    return priv;
  error:
    if (dev)
        free_netdev(dev);
    LEAVE();
    return NULL;
}

/**
 * @brief Request the driver to add a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param name            Virtual interface name
 * @param type            Virtual interface type
 * @param flags           Flags for the virtual interface
 * @param params          A pointer to vif_params structure
 * @param new_dev		  new net_device to return
 *
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_add_virt_if(struct wiphy *wiphy,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
                          const
#endif
                          char *name, enum nl80211_iftype type, u32 * flags,
                          struct vif_params *params,
                          struct net_device **new_dev)
{
    int ret = 0;
    struct net_device *ndev = NULL;
    moal_private *priv, *new_priv;
    moal_handle *handle = (moal_handle *) woal_get_wiphy_priv(wiphy);
    struct wireless_dev *wdev = NULL;

    ENTER();
    ASSERT_RTNL();
    priv =
        (moal_private *) woal_get_priv_bss_type(handle,
                                                MLAN_BSS_TYPE_WIFIDIRECT);
    if (priv->phandle->drv_mode.intf_num == priv->phandle->priv_num) {
        PRINTM(MERROR, "max virtual interface limit reached\n");
        LEAVE();
        return -ENOMEM;
    }
    if ((type != NL80211_IFTYPE_P2P_CLIENT) && (type != NL80211_IFTYPE_P2P_GO)) {
        PRINTM(MERROR, "Invalid iftype: %d\n", type);
        LEAVE();
        return -EINVAL;
    }

    handle = priv->phandle;
    /* Cancel previous scan req */
    woal_cancel_scan(priv, MOAL_IOCTL_WAIT);
    new_priv =
        woal_alloc_virt_interface(handle, handle->priv_num,
                                  MLAN_BSS_TYPE_WIFIDIRECT, name);
    if (!new_priv) {
        PRINTM(MERROR, "Add virtual interface fail.");
        LEAVE();
        return -EFAULT;
    }
    handle->priv_num++;

    wdev = (struct wireless_dev *) &new_priv->w_dev;
    memset(wdev, 0, sizeof(struct wireless_dev));
    ndev = new_priv->netdev;
    SET_NETDEV_DEV(ndev, wiphy_dev(wiphy));
    ndev->ieee80211_ptr = wdev;
    wdev->iftype = type;
    wdev->wiphy = wiphy;
    new_priv->wdev = wdev;
    new_priv->bss_virtual = MTRUE;
    new_priv->pa_netdev = priv->netdev;

    woal_init_sta_dev(ndev, new_priv);

    /* Initialize priv structure */
    woal_init_priv(new_priv, MOAL_CMD_WAIT);
    /** Init to GO/CLIENT mode */
    if (type == NL80211_IFTYPE_P2P_CLIENT)
        woal_cfg80211_init_p2p_client(new_priv);
    else if (type == NL80211_IFTYPE_P2P_GO)
        woal_cfg80211_init_p2p_go(new_priv);
    ret = register_netdevice(ndev);
    if (ret) {
        handle->priv[new_priv->bss_index] = NULL;
        handle->priv_num--;
        free_netdev(ndev);
        ndev = NULL;
        PRINTM(MFATAL, "register net_device failed, ret=%d\n", ret);
        goto done;
    }
    netif_carrier_off(ndev);
    woal_stop_queue(ndev);
    if (new_dev)
        *new_dev = ndev;
  done:
    if (ret) {
        if (ndev && ndev->reg_state == NETREG_REGISTERED)
            unregister_netdevice(ndev);
    }
    LEAVE();
    return ret;
}

/** 
 *  @brief Notify mlan BSS will be removed. 
 *   
 *  @param priv          A pointer to moal_private structure
 *
 *  @return              MLAN_STATUS_SUCCESS/MLAN_STATUS_PENDING -- success, otherwise fail
 */
mlan_status
woal_bss_remove(moal_private * priv)
{
    mlan_ioctl_req *req = NULL;
    mlan_ds_bss *bss = NULL;
    mlan_status status;

    ENTER();

    /* Allocate an IOCTL request buffer */
    req = (mlan_ioctl_req *) woal_alloc_mlan_ioctl_req(sizeof(mlan_ds_bss));
    if (req == NULL) {
        status = MLAN_STATUS_FAILURE;
        goto done;
    }

    /* Fill request buffer */
    bss = (mlan_ds_bss *) req->pbuf;
    bss->sub_command = MLAN_OID_BSS_REMOVE;
    req->req_id = MLAN_IOCTL_BSS;
    req->action = MLAN_ACT_SET;
    /* Send IOCTL request to MLAN */
    status = woal_request_ioctl(priv, req, MOAL_CMD_WAIT);

  done:
    if (req && (status != MLAN_STATUS_PENDING))
        kfree(req);
    LEAVE();
    return status;
}

/** 
 *  @brief This function removes an virtual interface.
 *  
 *  @param wiphy    A pointer to the wiphy structure
 *  @param dev      A pointer to the net_device structure
 *
 *  @return         0 -- success, otherwise fail
 */
int
woal_cfg80211_del_virt_if(struct wiphy *wiphy, struct net_device *dev)
{
    int ret = 0;
    int i = 0;
    moal_private *priv = NULL;
    moal_private *vir_priv = NULL;
    moal_private *remain_priv = NULL;
    moal_handle *handle = (moal_handle *) woal_get_wiphy_priv(wiphy);

    priv =
        (moal_private *) woal_get_priv_bss_type(handle,
                                                MLAN_BSS_TYPE_WIFIDIRECT);
    for (i = 0; i < priv->phandle->priv_num; i++) {
        if ((vir_priv = priv->phandle->priv[i])) {
            if (vir_priv->netdev == dev) {
                PRINTM(MIOCTL, "Find virtual interface, index=%d\n", i);
                break;
            }
        }
    }
    if (vir_priv && vir_priv->netdev == dev) {
        woal_stop_queue(dev);
        netif_carrier_off(dev);
        netif_device_detach(dev);
        woal_cancel_scan(vir_priv, MOAL_IOCTL_WAIT);
        /* cancel previous remain on channel to avoid firmware hang */
        if (priv->phandle->remain_on_channel) {
            t_u8 channel_status;
            remain_priv = priv->phandle->priv[priv->phandle->remain_bss_index];
            if (remain_priv) {
                if (woal_cfg80211_remain_on_channel_cfg
                    (remain_priv, MOAL_IOCTL_WAIT, MTRUE, &channel_status, NULL,
                     0, 0))
                    PRINTM(MERROR,
                           "del_virt_if: Fail to cancel remain on channel\n");

                if (priv->phandle->cookie) {
                    cfg80211_remain_on_channel_expired(
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,6,0)
                                                          remain_priv->netdev,
#else
                                                          remain_priv->wdev,
#endif
                                                          priv->phandle->cookie,
                                                          &priv->phandle->chan,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
                                                          priv->phandle->
                                                          channel_type,
#endif
                                                          GFP_ATOMIC);
                    priv->phandle->cookie = 0;
                }
                priv->phandle->remain_on_channel = MFALSE;
            }
        }

        woal_clear_all_mgmt_ies(vir_priv);
        woal_cfg80211_deinit_p2p(vir_priv);
        woal_bss_remove(vir_priv);
        /* Last reference is our one */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
        PRINTM(MINFO, "refcnt = %d\n", atomic_read(&dev->refcnt));
#else
        PRINTM(MINFO, "refcnt = %d\n", netdev_refcnt_read(dev));
#endif
        PRINTM(MINFO, "netdev_finish_unregister: %s\n", dev->name);
        /* Clear the priv in handle */
        vir_priv->phandle->priv[vir_priv->bss_index] = NULL;
        priv->phandle->priv_num--;
        if (dev->reg_state == NETREG_REGISTERED)
            unregister_netdevice(dev);
    }
    return ret;
}

/** 
 *  @brief This function removes an virtual interface.
 *  
 *  @param handle    A pointer to the moal_handle structure
 *
 *  @return        N/A
 */
void
woal_remove_virtual_interface(moal_handle * handle)
{
    moal_private *priv = NULL;
    int vir_intf = 0;
    int i = 0;
    ENTER();
    rtnl_lock();
    for (i = 0; i < handle->priv_num; i++) {
        if ((priv = handle->priv[i])) {
            if (priv->bss_virtual) {
                PRINTM(MCMND, "Remove virtual interface %s\n",
                       priv->netdev->name);
                netif_device_detach(priv->netdev);
                if (priv->netdev->reg_state == NETREG_REGISTERED)
                    unregister_netdevice(priv->netdev);
                handle->priv[i] = NULL;
                vir_intf++;
            }
        }
    }
    rtnl_unlock();
    handle->priv_num -= vir_intf;
    LEAVE();
}
#endif
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,6,0)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,37) || defined(COMPAT_WIRELESS)
/**
 * @brief Request the driver to add a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param name            Virtual interface name
 * @param type            Virtual interface type
 * @param flags           Flags for the virtual interface
 * @param params          A pointer to vif_params structure
 *
 * @return                A pointer to net_device -- success, otherwise null
 */
struct net_device *
woal_cfg80211_add_virtual_intf(struct wiphy *wiphy,
                               char *name, enum nl80211_iftype type,
                               u32 * flags, struct vif_params *params)
#else
/**
 * @brief Request the driver to add a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param name            Virtual interface name
 * @param type            Virtual interface type
 * @param flags           Flags for the virtual interface
 * @param params          A pointer to vif_params structure
 *
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_add_virtual_intf(struct wiphy *wiphy,
                               char *name, enum nl80211_iftype type,
                               u32 * flags, struct vif_params *params)
#endif
#else
/**
 * @brief Request the driver to add a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param name            Virtual interface name
 * @param type            Virtual interface type
 * @param flags           Flags for the virtual interface
 * @param params          A pointer to vif_params structure
 *
 * @return                A pointer to wireless_dev -- success, otherwise null
 */
struct wireless_dev *
woal_cfg80211_add_virtual_intf(struct wiphy *wiphy,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
                               const
#endif
                               char *name, enum nl80211_iftype type,
                               u32 * flags, struct vif_params *params)
#endif
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,37) || defined(COMPAT_WIRELESS)
    struct net_device *ndev = NULL;
#endif
    int ret = 0;

    ENTER();
    PRINTM(MIOCTL, "add virtual intf: %d name: %s\n", type, name);
    switch (type) {
#if defined(WIFI_DIRECT_SUPPORT)
#if LINUX_VERSION_CODE >= WIFI_DIRECT_KERNEL_VERSION
    case NL80211_IFTYPE_P2P_CLIENT:
    case NL80211_IFTYPE_P2P_GO:
        ret =
            woal_cfg80211_add_virt_if(wiphy, name, type, flags, params, &ndev);
        break;
#endif
#endif
    default:
        PRINTM(MWARN, "Not supported if type: %d\n", type);
        ret = -EFAULT;
        break;
    }
    LEAVE();
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,6,0)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,37) || defined(COMPAT_WIRELESS)
    if (ret)
        return NULL;
    else
        return ndev;
#else
    return ret;
#endif
#else
    if (ret)
        return NULL;
    else
        return (ndev->ieee80211_ptr);
#endif
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,6,0)
/**
 * @brief Request the driver to del a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             The pointer to net_device
 *
 * @return               0 -- success, otherwise fail
 */
int
woal_cfg80211_del_virtual_intf(struct wiphy *wiphy, struct net_device *dev)
#else
/**
 * @brief Request the driver to del a virtual interface
 *
 * @param wiphy           A pointer to wiphy structure
 * @param wdev            The pointer to wireless_dev
 *
 * @return               0 -- success, otherwise fail
 */
int
woal_cfg80211_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
#endif
{
    int ret = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,6,0)
    struct net_device *dev = wdev->netdev;
#endif
    ENTER();

    PRINTM(MIOCTL, "del virtual intf %s\n", dev->name);
    ASSERT_RTNL();
#if defined(WIFI_DIRECT_SUPPORT)
#if LINUX_VERSION_CODE >= WIFI_DIRECT_KERNEL_VERSION
    ret = woal_cfg80211_del_virt_if(wiphy, dev);
#endif
#endif
    LEAVE();
    return ret;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
/**
 * @brief initialize AP or GO parameters

 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param params          A pointer to cfg80211_ap_settings structure 
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_add_beacon(struct wiphy *wiphy,
                         struct net_device *dev,
                         struct cfg80211_ap_settings *params)
#else
/**
 * @brief initialize AP or GO parameters

 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param params          A pointer to beacon_parameters structure 
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_add_beacon(struct wiphy *wiphy,
                         struct net_device *dev,
                         struct beacon_parameters *params)
#endif
{
    moal_private *priv = (moal_private *) woal_get_netdev_priv(dev);
    int ret = 0;

    ENTER();

    PRINTM(MMSG, "wlan: Starting AP\n");
#ifdef STA_CFG80211
        /*** cancel pending scan */
    woal_cancel_scan(priv, MOAL_IOCTL_WAIT);
#endif
    if (params != NULL) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
        priv->channel =
            ieee80211_frequency_to_channel(params->channel->center_freq);
#endif
        /* bss config */
        if (MLAN_STATUS_SUCCESS != woal_cfg80211_beacon_config(priv, params)) {
            ret = -EFAULT;
            goto done;
        }

        /* set mgmt frame ies */
        if (MLAN_STATUS_SUCCESS != woal_cfg80211_mgmt_frame_ie(priv,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0) && !defined(COMPAT_WIRELESS)
                                                               params->tail,
                                                               params->tail_len,
                                                               NULL, 0, NULL, 0,
                                                               NULL, 0,
                                                               MGMT_MASK_BEACON
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
                                                               params->beacon.
                                                               tail,
                                                               params->beacon.
                                                               tail_len,
                                                               params->beacon.
                                                               proberesp_ies,
                                                               params->beacon.
                                                               proberesp_ies_len,
                                                               params->beacon.
                                                               assocresp_ies,
                                                               params->beacon.
                                                               assocresp_ies_len,
#else
                                                               params->tail,
                                                               params->tail_len,
                                                               params->
                                                               proberesp_ies,
                                                               params->
                                                               proberesp_ies_len,
                                                               params->
                                                               assocresp_ies,
                                                               params->
                                                               assocresp_ies_len,
#endif
                                                               NULL, 0,
                                                               MGMT_MASK_BEACON
                                                               |
                                                               MGMT_MASK_PROBE_RESP
                                                               |
                                                               MGMT_MASK_ASSOC_RESP
#endif
            )) {
            ret = -EFAULT;
            goto done;
        }
    }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0) || defined(COMPAT_WIRELESS)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
    if (params->beacon.beacon_ies && params->beacon.beacon_ies_len) {
        if (MLAN_STATUS_SUCCESS != woal_cfg80211_mgmt_frame_ie(priv,
                                                               params->beacon.
                                                               beacon_ies,
                                                               params->beacon.
                                                               beacon_ies_len,
                                                               NULL, 0, NULL, 0,
                                                               NULL, 0,
                                                               MGMT_MASK_BEACON_WPS_P2P))
        {
            PRINTM(MERROR, "Failed to set beacon wps/p2p ie\n");
            ret = -EFAULT;
            goto done;
        }
    }
#else
    if (params->beacon_ies && params->beacon_ies_len) {
        if (MLAN_STATUS_SUCCESS != woal_cfg80211_mgmt_frame_ie(priv,
                                                               params->
                                                               beacon_ies,
                                                               params->
                                                               beacon_ies_len,
                                                               NULL, 0, NULL, 0,
                                                               NULL, 0,
                                                               MGMT_MASK_BEACON_WPS_P2P))
        {
            PRINTM(MERROR, "Failed to set beacon wps/p2p ie\n");
            ret = -EFAULT;
            goto done;
        }
    }
#endif
#endif

    /* if the bss is stopped, then start it */
    if (priv->bss_started == MFALSE) {
        if (MLAN_STATUS_SUCCESS !=
            woal_uap_bss_ctrl(priv, MOAL_IOCTL_WAIT, UAP_BSS_START)) {
            ret = -EFAULT;
            goto done;
        }
    }

    PRINTM(MMSG, "wlan: AP started\n");
  done:
    LEAVE();
    return ret;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
/**
 * @brief set AP or GO parameter
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param params          A pointer to cfg80211_beacon_data structure 
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_set_beacon(struct wiphy *wiphy,
                         struct net_device *dev,
                         struct cfg80211_beacon_data *params)
#else
/**
 * @brief set AP or GO parameter
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param params          A pointer to beacon_parameters structure 
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_set_beacon(struct wiphy *wiphy,
                         struct net_device *dev,
                         struct beacon_parameters *params)
#endif
{
    moal_private *priv = (moal_private *) woal_get_netdev_priv(dev);
    int ret = 0;

    ENTER();

    PRINTM(MIOCTL, "set beacon\n");
    if (params != NULL) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0) && !defined(COMPAT_WIRELESS)
        if (params->tail && params->tail_len) {
            if (MLAN_STATUS_SUCCESS !=
                woal_cfg80211_mgmt_frame_ie(priv,
                                            params->tail, params->tail_len,
                                            NULL, 0, NULL, 0, NULL, 0,
                                            MGMT_MASK_BEACON)) {
                ret = -EFAULT;
                goto done;
            }
        }
#else
        if (params->tail && params->tail_len) {
            if (MLAN_STATUS_SUCCESS !=
                woal_cfg80211_mgmt_frame_ie(priv, params->tail,
                                            params->tail_len, NULL, 0, NULL, 0,
                                            NULL, 0, MGMT_MASK_BEACON)) {
                ret = -EFAULT;
                goto done;
            }
        }
        if (params->beacon_ies && params->beacon_ies_len) {
            if (MLAN_STATUS_SUCCESS != woal_cfg80211_mgmt_frame_ie(priv,
                                                                   params->
                                                                   beacon_ies,
                                                                   params->
                                                                   beacon_ies_len,
                                                                   NULL, 0,
                                                                   NULL, 0,
                                                                   NULL, 0,
                                                                   MGMT_MASK_BEACON_WPS_P2P))
            {
                PRINTM(MERROR, "Failed to set beacon wps/p2p ie\n");
                ret = -EFAULT;
                goto done;
            }
        }
        if (params->proberesp_ies && params->proberesp_ies_len) {
            if (MLAN_STATUS_SUCCESS !=
                woal_cfg80211_mgmt_frame_ie(priv, NULL, 0,
                                            params->proberesp_ies,
                                            params->proberesp_ies_len, NULL, 0,
                                            NULL, 0, MGMT_MASK_PROBE_RESP)) {
                ret = -EFAULT;
                goto done;
            }
        }
        if (params->assocresp_ies && params->assocresp_ies_len) {
            if (MLAN_STATUS_SUCCESS !=
                woal_cfg80211_mgmt_frame_ie(priv, NULL, 0, NULL, 0,
                                            params->assocresp_ies,
                                            params->assocresp_ies_len, NULL, 0,
                                            MGMT_MASK_ASSOC_RESP)) {
                ret = -EFAULT;
                goto done;
            }
        }
#endif
    }

  done:
    LEAVE();
    return ret;
}

/**
 * @brief reset AP or GO parameters
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 *
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
{
    moal_private *priv = (moal_private *) woal_get_netdev_priv(dev);
    int ret = 0;
#ifdef STA_SUPPORT
    moal_private *pmpriv = NULL;
#endif

    ENTER();

    PRINTM(MMSG, "wlan: Stoping AP\n");
    /* if the bss is still running, then stop it */
    if (priv->bss_started == MTRUE) {
        if (MLAN_STATUS_SUCCESS !=
            woal_uap_bss_ctrl(priv, MOAL_IOCTL_WAIT, UAP_BSS_STOP)) {
            ret = -EFAULT;
            goto done;
        }
        if (MLAN_STATUS_SUCCESS !=
            woal_uap_bss_ctrl(priv, MOAL_IOCTL_WAIT, UAP_BSS_RESET)) {
            ret = -EFAULT;
            goto done;
        }
        /* Set WLAN MAC addresses */
        if (MLAN_STATUS_SUCCESS != woal_request_set_mac_address(priv)) {
            PRINTM(MERROR, "Set MAC address failed\n");
            ret = -EFAULT;
            goto done;
        }
    }
    woal_clear_all_mgmt_ies(priv);

#ifdef STA_SUPPORT
    if (!woal_is_any_interface_active(priv->phandle)) {
        if ((pmpriv =
             woal_get_priv((moal_handle *) priv->phandle, MLAN_BSS_ROLE_STA))) {
            woal_set_scan_time(pmpriv, ACTIVE_SCAN_CHAN_TIME,
                               PASSIVE_SCAN_CHAN_TIME, SPECIFIC_SCAN_CHAN_TIME);
        }
    }
#endif

    priv->cipher = 0;
    priv->key_len = 0;
    priv->channel = 0;
    PRINTM(MMSG, "wlan: AP stopped\n");
  done:
    LEAVE();
    return ret;
}

/**
 * @brief send deauth to station
 *
 * @param                 A pointer to moal_private
 * @param mac			  A pointer to station mac address
 *
 * @return                0 -- success, otherwise fail
 */
static int
woal_deauth_station(moal_private * priv, u8 * mac_addr)
{
    mlan_ioctl_req *ioctl_req = NULL;
    mlan_ds_bss *bss = NULL;
    int ret = 0;

    ENTER();

    ioctl_req = woal_alloc_mlan_ioctl_req(sizeof(mlan_ds_bss));
    if (ioctl_req == NULL) {
        ret = -ENOMEM;
        goto done;
    }
    bss = (mlan_ds_bss *) ioctl_req->pbuf;
    bss->sub_command = MLAN_OID_UAP_DEAUTH_STA;
    ioctl_req->req_id = MLAN_IOCTL_BSS;
    ioctl_req->action = MLAN_ACT_SET;

    memcpy(bss->param.deauth_param.mac_addr, mac_addr, MLAN_MAC_ADDR_LENGTH);
#define  REASON_CODE_DEAUTH_LEAVING 3
    bss->param.deauth_param.reason_code = REASON_CODE_DEAUTH_LEAVING;
    if (MLAN_STATUS_SUCCESS !=
        woal_request_ioctl(priv, ioctl_req, MOAL_IOCTL_WAIT)) {
        ret = -EFAULT;
        goto done;
    }

  done:
    if (ioctl_req)
        kfree(ioctl_req);
    LEAVE();
    return ret;
}

/**
 * @brief del station 
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param mac			  A pointer to station mac address
 *
 * @return                0 -- success, otherwise fail
 */
int
woal_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev,
                          u8 * mac_addr)
{
    moal_private *priv = (moal_private *) woal_get_netdev_priv(dev);
    int ret = -EFAULT;
    int i = 0;
    mlan_ds_get_info *info = NULL;
    mlan_ioctl_req *ioctl_req = NULL;

    ENTER();
    if (priv->media_connected == MFALSE) {
        PRINTM(MINFO, "cfg80211: Media not connected!\n");
        LEAVE();
        return 0;
    }

    if (mac_addr) {
        PRINTM(MIOCTL, "del station: " MACSTR "\n", MAC2STR(mac_addr));
        ret = woal_deauth_station(priv, mac_addr);
        woal_sched_timeout(100);
        LEAVE();
        return ret;
    }
    PRINTM(MIOCTL, "del all station\n");
    /* Allocate an IOCTL request buffer */
    ioctl_req =
        (mlan_ioctl_req *) woal_alloc_mlan_ioctl_req(sizeof(mlan_ds_get_info));
    if (ioctl_req == NULL) {
        ret = -ENOMEM;
        goto done;
    }

    info = (mlan_ds_get_info *) ioctl_req->pbuf;
    info->sub_command = MLAN_OID_UAP_STA_LIST;
    ioctl_req->req_id = MLAN_IOCTL_GET_INFO;
    ioctl_req->action = MLAN_ACT_GET;

    if (MLAN_STATUS_SUCCESS !=
        woal_request_ioctl(priv, ioctl_req, MOAL_IOCTL_WAIT)) {
        goto done;
    }
    if (!info->param.sta_list.sta_count)
        goto done;
    for (i = 0; i < info->param.sta_list.sta_count; i++) {
        PRINTM(MIOCTL, "deauth station " MACSTR "\n",
               MAC2STR(info->param.sta_list.info[i].mac_address));
        ret =
            woal_deauth_station(priv, info->param.sta_list.info[i].mac_address);
    }
    woal_sched_timeout(100);
  done:
    if (ioctl_req)
        kfree(ioctl_req);
    LEAVE();
    return ret;

}

/**
 * @brief Get station info
 *
 * @param wiphy           A pointer to wiphy structure
 * @param dev             A pointer to net_device structure
 * @param mac			  A pointer to station mac address
 * @param stainfo		  A pointer to station_info structure
 *
 * @return                0 -- success, otherwise fail
 */
int
woal_uap_cfg80211_get_station(struct wiphy *wiphy, struct net_device *dev,
                              u8 * mac, struct station_info *stainfo)
{
    moal_private *priv = (moal_private *) woal_get_netdev_priv(dev);
    int ret = -EFAULT;
    int i = 0;
    mlan_ds_get_info *info = NULL;
    mlan_ioctl_req *ioctl_req = NULL;

    ENTER();
    if (priv->media_connected == MFALSE) {
        PRINTM(MINFO, "cfg80211: Media not connected!\n");
        LEAVE();
        return -ENOENT;
    }

    /* Allocate an IOCTL request buffer */
    ioctl_req =
        (mlan_ioctl_req *) woal_alloc_mlan_ioctl_req(sizeof(mlan_ds_get_info));
    if (ioctl_req == NULL) {
        ret = -ENOMEM;
        goto done;
    }

    info = (mlan_ds_get_info *) ioctl_req->pbuf;
    info->sub_command = MLAN_OID_UAP_STA_LIST;
    ioctl_req->req_id = MLAN_IOCTL_GET_INFO;
    ioctl_req->action = MLAN_ACT_GET;

    if (MLAN_STATUS_SUCCESS !=
        woal_request_ioctl(priv, ioctl_req, MOAL_IOCTL_WAIT)) {
        goto done;
    }
    for (i = 0; i < info->param.sta_list.sta_count; i++) {
        if (!memcmp(info->param.sta_list.info[i].mac_address, mac, ETH_ALEN)) {
            PRINTM(MIOCTL, "Get station: " MACSTR " RSSI=%d\n", MAC2STR(mac),
                   (int) info->param.sta_list.info[i].rssi);
            stainfo->filled = STATION_INFO_INACTIVE_TIME | STATION_INFO_SIGNAL;
            stainfo->inactive_time = 0;
            stainfo->signal = info->param.sta_list.info[i].rssi;
            ret = 0;
            break;
        }
    }
  done:
    if (ioctl_req)
        kfree(ioctl_req);
    LEAVE();
    return ret;
}

/**
 * @brief Register the device with cfg80211
 *
 * @param dev       A pointer to net_device structure
 * @param bss_type  BSS type
 *
 * @return          MLAN_STATUS_SUCCESS or MLAN_STATUS_FAILURE
 */
mlan_status
woal_register_uap_cfg80211(struct net_device * dev, t_u8 bss_type)
{
    mlan_status ret = MLAN_STATUS_SUCCESS;
    moal_private *priv = (moal_private *) netdev_priv(dev);
    struct wireless_dev *wdev = NULL;

    ENTER();

    wdev = (struct wireless_dev *) &priv->w_dev;
    memset(wdev, 0, sizeof(struct wireless_dev));

    wdev->wiphy = priv->phandle->wiphy;
    if (!wdev->wiphy) {
        LEAVE();
        return MLAN_STATUS_FAILURE;
    }
    if (bss_type == MLAN_BSS_TYPE_UAP)
        wdev->iftype = NL80211_IFTYPE_AP;

    dev_net_set(dev, wiphy_net(wdev->wiphy));
    dev->ieee80211_ptr = wdev;
    SET_NETDEV_DEV(dev, wiphy_dev(wdev->wiphy));
    priv->wdev = wdev;

    LEAVE();
    return ret;
}