summaryrefstree
path: root/kernel/drivers/net/wireless/bcmdhd4359/wl_statreport.c
blob: 151325ffc8375c9959b5eb71d84c884e782ed6a5 (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
/*
* Wifi dongle status logging and report
*
* Copyright (C) 1999-2019, Broadcom Corporation
* 
*      Unless you and Broadcom execute a separate written software license
* agreement governing use of this software, this software is licensed to you
* under the terms of the GNU General Public License version 2 (the "GPL"),
* available at http://www.broadcom.com/licenses/GPLv2.php, with the
* following added to such license:
* 
*      As a special exception, the copyright holders of this software give you
* permission to link this software with independent modules, and to copy and
* distribute the resulting executable under terms of your choice, provided that
* you also meet, for each linked independent module, the terms and conditions of
* the license of that module.  An independent module is a module which is not
* derived from this software.  The special exception does not apply to any
* modifications of the software.
* 
*      Notwithstanding the above, under no circumstances may you combine this
* software in any way with any other Broadcom software provided under a license
* other than the GPL, without Broadcom's express prior written consent.
*
* <<Broadcom-WL-IPTag/Open:>>
*
* $Id: wl_statreport.c 735359 2017-12-08 10:56:04Z $
*/
#include <wlc_types.h>
#include <bcmutils.h>
#include <wlioctl.h>
#include <wl_statreport.h>
#include <wl_cfg80211.h>
#include <wldev_common.h>
#include <dngl_stats.h>
#include <dhd.h>
#include <dhd_linux.h>
#include <linux/fs.h>

#ifndef LINKSTAT_SUPPORT
#error "LINK STAT is NOT supported"
#endif /* LINKSTAT_SUPPORT */
#ifndef DHD_LOG_DUMP
#error "DHD_LOG_DUMP is not supported"
#endif /* DHD_LOG_DUMP */

#define WSR_REPORT_VERSION	20170620
#define WSR_REPORT_MAX_DATA	64
#define WSR_REPORT_MAX_WPA_CNT	5
#define WSR_INVALID	(-1)

#define WSR_CNT_SIZE	(1300 + 256)

/* restrict maximum value less than 100M to reduce string length */
#define WSR_CNT_VALUE_MAX	100000000

#define WSR_NONSEC_TO_SEC		1000000000
#define WSR_REPORT_YEAR_MUL	10000
#define WSR_REPORT_MON_MUL		100
#define WSR_REPORT_HOUR_MUL	10000
#define WSR_REPORT_MIN_MUL		100
#define WSR_REPORT_MINUTES		60
#define WSR_REPORT_YEAR_BASE	1900

#define WSR_MAX_OUI	3
#define OUI_PAD_LEN (4 - (WSR_MAX_OUI * DOT11_OUI_LEN) % 4)

#define WSR_MINIMIZE_REPORT

/* logging connection info to follow up connection changes */
typedef struct stat_report_conn {
	uint8 bssid[ETHER_ADDR_LEN]; /* connected bssid */
	uint8 oui_cnt;				/* number of vndr OUI of connected AP */
	uint8 pad1;
	uint32 dhd_opmode;			/* dhd operation mode : dhd_op_flags */
	uint8 oui_list[WSR_MAX_OUI][DOT11_OUI_LEN]; /* vndr OUI list */
	uint8 pad2[OUI_PAD_LEN];
} wsr_conn_t;

/* Kernel local clock is not elapsed at suspend mode
 * but kernel logs show this value at the logs
 * to avoid calculation print local clock
 * and boot up time(of date/time format)
 */
typedef struct stat_report_clock {
	uint32 kernel_clock;		/* kernel local clock */
	uint32 date;				/* date of WSR format */
	uint32 time;				/* time of WSR format */
} wsr_clock_t;

typedef struct stat_report_adps {
	uint8 mode;				/* ADPS mdoe 0=off, 1 = on */
	uint8 flags;			/* ADPS restrict flags 0 = adps work, not 0 = paused */
	uint8 current_step;		/* ADPS step 0 = LOW PM STEP, 1 = HIGH PM STEP */
	uint8 pad;
	uint32 duration_high;	/* total duration in HIGH PM STEP in mili-second unit */
	uint32 counts_high;		/* total number of HIGH PM STEP */
	uint32 duration_low;	/* total duration in LOW PM STEP in mili-second unit */
	uint32 counts_low;		/* total inumber of LOW PM STEP */
} wsr_adps_t;

typedef struct stat_report_link {
	int8 lqcm;				/* lqcm value -1 : INVALID, 0~8 : LQCM step */
	int8 rssi;				/* RSSI : -128 ~ 0 */
	uint8 snr;				/* SNR : 0 ~ 100 */
	uint8 pad;
} wsr_link_t;

typedef struct stat_report_phy_tx_power {
	uint8 sar_enable;			/* SAR Enable/Disable */
	uint8 phy_rsdb_mode;		/* PHY RSDB mode 0-MIMO, 1-RSDB */

	/* 2.4G/5G SAR power capping value per core[0/1], Unit [qdBm] */
	uint8 sar_limit_2g_core0;
	uint8 sar_limit_2g_core1;
	uint8 sar_limit_5g_core0;
	uint8 sar_limit_5g_core1;
	uint8 fcc_enable;			/* FCC Enable/Disable */
	uint8 fccpwrch12;			/* FCC power capping value of 2.4G 12ch [qdBm] */
	uint8 fccpwrch13;			/* FCC power capping value of 2.4G 13ch [qdBm] */
	uint8 bss_local_max;		/* BSS Local Max [qdBm] */
	uint8 bss_local_constraint;	/* BSS Local Constraint [qdBm] */
	uint8 dsss_1mbps_2g;		/* Target power - 2.4G DSSS 1Mbps rate [qdBm] */
	uint8 ofdm_6mbps_2g;		/* Target power - 2.4G OFDM 6Mbps rate [qdBm] */
	uint8 mcs_0_2g;				/* Target power - 2.4G MCS 0 rate [qdBm] */
	uint8 ofdm_6mbps_5g;		/* Target power - 5G OFDM 6Mbps rate [qdBm] */
	uint8 mcs_0_5g;				/* Target power - 5G MCS 0 rate [qdBm] */
} wsr_tx_power_t;

typedef struct stat_report_elem {
	wsr_clock_t clock;
	wsr_conn_t conn;
	char cntinfo[WSR_CNT_SIZE];
	wsr_link_t link_quality;
	wsr_adps_t adps;
	wsr_tx_power_t tx_power;
} wsr_elem_t;

typedef struct wl_stat_report_info {
	uint32 version; /* WSR version */

	int enabled;	/* enabled/disabled */

	/* ring buf variables */
	struct mutex ring_sync; /* mutex for critical section of ring buff */
	int write_idx;	/* next write index, -1 : not started */
	int read_idx;	/* next read index, -1 : not start */
	/* conn idx : first connection of this connection */
	/* -1 : not started if read = -1, same to read if read is not -1 */
	/* counts between conn & write shall be less than MAX_REPORT */
	int conn_idx;

	/* protected elements during seiralization */
	int lock_idx;	/* start index of locked, element will not be overried */
	int lock_count; /* number of locked, from lock idx */

	/* serialzation variables */
	int seral_mode;	/* serialize mode */
	int trans_buf_len;	/* configed buffer length per serialize transaction */
	int seral_state;	/* state of serialize */
	int serial_sub_state;	/* sub state of each state, currently idx only */

	/* saved data elements */
	wsr_elem_t elem[WSR_REPORT_MAX_DATA];
} wsr_info_t;

/* ring buf control functions */
static inline int wsr_get_write_idx(wsr_info_t *wsr_info);
static inline int wsr_get_first_idx(wsr_info_t *wsr_info);
static inline int wsr_get_last_idx(wsr_info_t *wsr_info);
static inline int wsr_get_next_idx(wsr_info_t *wsr_info, int cur_idx);
static inline void wsr_set_conn_idx(wsr_info_t *wsr_info);
static inline int wsr_get_conn_idx(wsr_info_t *wsr_info, int max_cnt);
static inline int wsr_get_count(int start_idx, int end_idx);

/* ring buf serializing record protection lock */
static inline void wsr_set_lock_idx(wsr_info_t *wsr_info, int max_cnt);
static inline void wsr_free_lock_idx(wsr_info_t *wsr_info);
static inline int wsr_get_lock_idx(wsr_info_t *wsr_info);
static inline void wsr_decr_lock_count(wsr_info_t *wsr_info);
static inline int wsr_get_lock_cnt(wsr_info_t *wsr_info);
/* ring buff internal functions , no mutex lock */
static int __wsr_get_write_idx(wsr_info_t *wsr_info);
static inline int __wsr_get_first_idx(wsr_info_t *wsr_info);
static int __wsr_get_next_idx(wsr_info_t *wsr_info, int cur_idx);
static int __wsr_get_conn_idx(wsr_info_t *wsr_info, int max_cnt);

/* status gathering functions */
static inline void wsr_copy_cntinfo(wsr_elem_t *elem, wl_cnt_info_t *cntinfo);
static void wsr_get_conn_info(struct bcm_cfg80211 *cfg, wsr_elem_t *elem);
static void wsr_get_clock_info(wsr_elem_t *elem);
static void wsr_gather_link_quality(struct bcm_cfg80211 *cfg, wsr_elem_t *elem);
static void wsr_gather_adps(struct bcm_cfg80211 *cfg, wsr_elem_t *elem);

#if defined(WSR_DEBUG)

#define WSR_DEBUG_PRD	5	/* print debug every 5 recrod */
typedef struct wpa_stat_report_elem {
	wsr_clock_t clock;
	wsr_conn_t conn;

	/* Counters */
	uint32 txframe;
	uint32 rxmgocast;

	/* Other Values */
	wsr_link_t link_quality;
	wsr_adps_t adps;
} wsr_dbg_elem_t;

static void wsr_dbg_main(wsr_info_t *wsr_info);
static void wsr_dbg_copy_elem(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static void wsr_dbg_copy_cnts(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static inline void wsr_dbg_copy_conn(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static inline void wsr_dbg_copy_clock(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static inline void wsr_dbg_copy_link(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static inline void wsr_dbg_copy_adps(wsr_dbg_elem_t *dest, wsr_elem_t *src);
static void wsr_dbg_print_elem(wsr_dbg_elem_t *elem);
#endif /* WSR_DEBUG */

#ifdef STAT_REPORT_TEMP_STATIC
#undef DHD_OS_PREALLOC
#undef DHD_OS_PREFREE

#define DHD_OS_PREALLOC(dhdpub, section, size) ({\
		int kflags; \
		void *__ret; \
		kflags = in_atomic() ?GFP_ATOMIC : GFP_KERNEL; \
		__ret = kzalloc(size, kflags); \
		__ret; \
})

#define DHD_OS_PREFREE(dhdpub, addr, size) kfree(addr)
#endif /* STAT_REPORT_TEMP_STATIC */

/* ========= Module functions : exposed to others ============= */
int
wl_attach_stat_report(void *cfg)
{
	struct bcm_cfg80211 *pcfg = (struct bcm_cfg80211 *)cfg;
	wsr_info_t *wsr_info;
	dhd_pub_t *dhdp = pcfg->pub;

	if (!pcfg) {
		WL_ERR(("FAIL to ATTACH STAT REPORT\n"));
		return BCME_ERROR;
	}

	BCM_REFERENCE(dhdp);
	wsr_info = (wsr_info_t *)DHD_OS_PREALLOC(dhdp,
		DHD_PREALLOC_STAT_REPORT_BUF, sizeof(wsr_info_t));
	if (unlikely(!wsr_info)) {
		WL_ERR(("FAIL to alloc for STAT REPORT INFO\n"));
		return BCME_ERROR;
	}

	/* Initialize control block */
	mutex_init(&wsr_info->ring_sync);
	wsr_info->version = WSR_REPORT_VERSION;
	wsr_info->enabled = TRUE;
	wsr_info->read_idx = WSR_INVALID;
	wsr_info->write_idx = WSR_INVALID;
	wsr_info->conn_idx = WSR_INVALID;
	wsr_info->lock_idx = WSR_INVALID;
	pcfg->stat_report_info = wsr_info;
	((dhd_pub_t *)(pcfg->pub))->stat_report_info = wsr_info;
	return BCME_OK;
}

void
wl_detach_stat_report(void *cfg)
{
	struct bcm_cfg80211 *pcfg = (struct bcm_cfg80211 *)cfg;
	wsr_info_t *wsr_info;
	dhd_pub_t *dhdp = pcfg->pub;

	if (!pcfg) {
		return;
	}
	BCM_REFERENCE(dhdp);
	if (pcfg->stat_report_info) {
		wsr_info = (wsr_info_t *)pcfg->stat_report_info;
		mutex_destroy(&wsr_info->ring_sync);
		DHD_OS_PREFREE(dhdp, pcfg->stat_report_info,
			sizeof(wsr_info_t));
		pcfg->stat_report_info = NULL;
	}
}

/* query dongle status data */
void
wl_stat_report_gather(void *cfg, void *cnt)
{
	int write_idx;
	wl_cnt_info_t *cntinfo = (wl_cnt_info_t *)cnt;
	struct bcm_cfg80211 *pcfg = (struct bcm_cfg80211 *)cfg;
	wsr_info_t *wsr_info = pcfg->stat_report_info;
	wsr_elem_t *elem;

	/* Check Validation of counters */
	if (cntinfo->version < WL_CNT_VERSION_XTLV) {
		WL_ERR(("NOT supported CNT version\n"));
		return;
	}
	if (wsr_info == NULL) {
		WL_INFORM(("NOT enabled\n"));
		return;
	}

	/* no available slot, due to oldest slot is locked */
	write_idx = wsr_get_write_idx(wsr_info);
	if (write_idx == WSR_INVALID) {
		WL_ERR(("SKIP to logging due to locking\n"));
		return;
	}

	elem = &wsr_info->elem[write_idx];
	wsr_get_conn_info(pcfg, elem);
	wsr_get_clock_info(elem);
	wsr_copy_cntinfo(elem, cntinfo);
	wsr_gather_link_quality(pcfg, elem);
	wsr_gather_adps(pcfg, elem);

#if defined(WSR_DEBUG)
	{
		static int pr_cnt = 0;
		if ((pr_cnt++) % WSR_DEBUG_PRD == 0) {
			wsr_dbg_main(wsr_info);
		}
	}
#endif /* WSR_DEBUG */
}

void
wl_stat_report_notify_connected(void *cfg)
{
	struct bcm_cfg80211 *pcfg = (struct bcm_cfg80211 *)cfg;
	wsr_info_t *wsr_info = pcfg->stat_report_info;

	/* Set conn idx to next write idx */
	wsr_set_conn_idx(wsr_info);
}

/* ========= Ring buffer management ============= */
/* Get next index can be written
 * will overwrite which doesn't read
 * will return -1 if next pointer is locked
 */
static int
__wsr_get_write_idx(wsr_info_t *wsr_info)
{
	int tmp_idx;

	if (wsr_info->read_idx == WSR_INVALID) {
		wsr_info->read_idx = wsr_info->write_idx = 0;
		return wsr_info->write_idx;
	}

	/* check next index is not locked */
	tmp_idx = (wsr_info->write_idx + 1) % WSR_REPORT_MAX_DATA;
	if (wsr_info->lock_idx == tmp_idx) {
		return WSR_INVALID;
	}

	wsr_info->write_idx = tmp_idx;
	if (wsr_info->write_idx == wsr_info->read_idx) {
		/* record is full, drop oldest one */
		wsr_info->read_idx = (wsr_info->read_idx + 1) % WSR_REPORT_MAX_DATA;

		/* set all data is for this connection */
		if (wsr_info->read_idx == wsr_info->conn_idx) {
			wsr_info->conn_idx = WSR_INVALID;
		}
	}
	return wsr_info->write_idx;

}

static inline int
wsr_get_write_idx(wsr_info_t *wsr_info)
{
	int ret_idx;
	mutex_lock(&wsr_info->ring_sync);
	ret_idx = __wsr_get_write_idx(wsr_info);
	mutex_unlock(&wsr_info->ring_sync);
	return ret_idx;
}

/* Get read index : oldest element */
static inline int
__wsr_get_first_idx(wsr_info_t *wsr_info)
{
	if (wsr_info->read_idx == WSR_INVALID) {
		return WSR_INVALID;
	}

	return wsr_info->read_idx;
}

static inline int
wsr_get_first_idx(wsr_info_t *wsr_info)
{
	int ret_idx;
	mutex_lock(&wsr_info->ring_sync);
	ret_idx = __wsr_get_first_idx(wsr_info);
	mutex_unlock(&wsr_info->ring_sync);
	return ret_idx;
}

/* Get latest element */
static inline int
wsr_get_last_idx(wsr_info_t *wsr_info)
{
	int ret_idx;
	mutex_lock(&wsr_info->ring_sync);
	ret_idx = wsr_info->write_idx;
	mutex_unlock(&wsr_info->ring_sync);
	return ret_idx;
}

/* get counts between two indexes of ring buffer */
static inline int
wsr_get_count(int start_idx, int end_idx)
{
	if (start_idx == WSR_INVALID || end_idx == WSR_INVALID) {
		return 0;
	}

	return (WSR_REPORT_MAX_DATA + end_idx - start_idx) % WSR_REPORT_MAX_DATA + 1;
}

/* Set conn idx to next write idx */
static inline void
wsr_set_conn_idx(wsr_info_t *wsr_info)
{
	mutex_lock(&wsr_info->ring_sync);
	wsr_info->conn_idx = __wsr_get_next_idx(wsr_info, wsr_info->write_idx);
	mutex_unlock(&wsr_info->ring_sync);
}

/* Get first record of this connection
 * if conn_idx = -1, all record is for this connection, return first
 * if counts btwn. conn and write, then return (write - max)
 * else return conn idx
 */
static int
__wsr_get_conn_idx(wsr_info_t *wsr_info, int max_cnt)
{
	int counts;

	if (wsr_info->read_idx == WSR_INVALID) {
		return WSR_INVALID;
	}

	if (wsr_info->conn_idx == WSR_INVALID) {
		counts = wsr_get_count(wsr_info->read_idx, wsr_info->write_idx);
	} else {
		counts = wsr_get_count(wsr_info->conn_idx, wsr_info->write_idx);
	}

	counts = MIN(counts, max_cnt);
	return (WSR_REPORT_MAX_DATA + wsr_info->write_idx - counts + 1) % WSR_REPORT_MAX_DATA;
}

static inline int
wsr_get_conn_idx(wsr_info_t *wsr_info, int max_cnt)
{
	int con_idx;
	mutex_lock(&wsr_info->ring_sync);
	con_idx = __wsr_get_conn_idx(wsr_info, max_cnt);
	mutex_unlock(&wsr_info->ring_sync);
	return con_idx;
}

static int
__wsr_get_next_idx(wsr_info_t *wsr_info, int cur_idx)
{
	if (wsr_info->read_idx == WSR_INVALID) {
		return WSR_INVALID;
	}

	if (cur_idx == wsr_info->write_idx) {
		/* no more new record */
		return WSR_INVALID;
	}

	return ((cur_idx +1) % WSR_REPORT_MAX_DATA);
}

static inline int
wsr_get_next_idx(wsr_info_t *wsr_info, int cur_idx)
{
	int next_idx;
	mutex_lock(&wsr_info->ring_sync);
	next_idx = __wsr_get_next_idx(wsr_info, cur_idx);
	mutex_unlock(&wsr_info->ring_sync);
	return next_idx;
}

static inline void
wsr_set_lock_idx(wsr_info_t *wsr_info, int max_cnt)
{
	mutex_lock(&wsr_info->ring_sync);
	wsr_info->lock_idx = __wsr_get_conn_idx(wsr_info, max_cnt);
	wsr_info->lock_count = wsr_get_count(wsr_info->lock_idx, wsr_info->write_idx);
	mutex_unlock(&wsr_info->ring_sync);
}

static inline void
wsr_free_lock_idx(wsr_info_t *wsr_info)
{
	mutex_lock(&wsr_info->ring_sync);
	wsr_info->lock_idx = WSR_INVALID;
	wsr_info->lock_count = 0;
	mutex_unlock(&wsr_info->ring_sync);
}

static inline int
wsr_get_lock_idx(wsr_info_t *wsr_info)
{
	int ret_idx;
	mutex_lock(&wsr_info->ring_sync);
	ret_idx = wsr_info->lock_idx;
	mutex_unlock(&wsr_info->ring_sync);
	return ret_idx;
}

static inline void
wsr_decr_lock_count(wsr_info_t *wsr_info)
{

	mutex_lock(&wsr_info->ring_sync);
	wsr_info->lock_count--;
	if (wsr_info->lock_count <= 0) {
		wsr_info->lock_idx = WSR_INVALID;
	} else {
		wsr_info->lock_idx = __wsr_get_next_idx(wsr_info, wsr_info->lock_idx);
	}
	mutex_unlock(&wsr_info->ring_sync);
}

static inline int
wsr_get_lock_cnt(wsr_info_t *wsr_info)
{
	int cnt;
	mutex_lock(&wsr_info->ring_sync);
	cnt = wsr_info->lock_count;
	mutex_unlock(&wsr_info->ring_sync);
	return cnt;
}

/* ========= Status Query Functions ============= */
static inline void
wsr_get_op_mode(struct bcm_cfg80211 *cfg, wsr_elem_t *elem)
{
	dhd_pub_t *dhd = (dhd_pub_t *)(cfg->pub);

	elem->conn.dhd_opmode = dhd->op_mode;
}

static void
wsr_get_conn_info(struct bcm_cfg80211 *cfg, wsr_elem_t *elem)
{
	struct wl_profile *profile = wl_get_profile_by_netdev(cfg, bcmcfg_to_prmry_ndev(cfg));

	memset(&elem->conn, 0, sizeof(elem->conn));
	if (!profile) {
		return;
	}

	memcpy(elem->conn.bssid, profile->bssid, ETHER_ADDR_LEN);
	elem->conn.oui_cnt = wl_cfg80211_get_vndr_ouilist(cfg,
		(uint8 *)&elem->conn.oui_list, WSR_MAX_OUI);

	wsr_get_op_mode(cfg, elem);
	return;
}

static void
wsr_get_clock_info(wsr_elem_t *elem)
{
	struct timespec ts;
	struct tm tm;
	u64 tv_kernel =  local_clock();
	wsr_clock_t *clock = &elem->clock;

	getnstimeofday(&ts);
	time_to_tm((ts.tv_sec - (sys_tz.tz_minuteswest*WSR_REPORT_MINUTES)), 0, &tm);

	/* save seconds only */
	clock->kernel_clock = (uint32)(tv_kernel/WSR_NONSEC_TO_SEC);
	clock->date = (tm.tm_year + WSR_REPORT_YEAR_BASE) * WSR_REPORT_YEAR_MUL;
	clock->date += (tm.tm_mon +1) * WSR_REPORT_MON_MUL;
	clock->date += tm.tm_mday;
	clock->time = tm.tm_hour * WSR_REPORT_HOUR_MUL;
	clock->time += tm.tm_min * WSR_REPORT_MIN_MUL;
	clock->time += tm.tm_sec;
}

static inline void
wsr_copy_cntinfo(wsr_elem_t *elem, wl_cnt_info_t *cntinfo)
{
	uint32 copy_len;
	copy_len = MIN((uint32)OFFSETOF(wl_cnt_info_t, data)+ cntinfo->datalen, WSR_CNT_SIZE);

	memcpy(elem->cntinfo, cntinfo, copy_len);
}

#ifndef LQCM_ENAB_MASK
#define LQCM_ENAB_MASK			0x000000FF	/* LQCM enable flag mask */
#define LQCM_TX_INDEX_MASK		0x0000FF00	/* LQCM tx index mask */
#define LQCM_RX_INDEX_MASK		0x00FF0000	/* LQCM rx index mask */

#define LQCM_TX_INDEX_SHIFT		8	/* LQCM tx index shift */
#define LQCM_RX_INDEX_SHIFT		16	/* LQCM rx index shift */
#endif /* LQCM_ENAB_MASK */

static void
wsr_gather_link_quality(struct bcm_cfg80211 *cfg, wsr_elem_t *elem)
{
	int err;
	uint32 val;
	uint8 uint8_v;
	scb_val_t scbval;

	memset(&elem->link_quality, 0, sizeof(elem->link_quality));

	err = wldev_iovar_getint(bcmcfg_to_prmry_ndev(cfg), "lqcm", &val);
	if (err != BCME_OK) {
		WL_ERR(("failed to get lqcm report, error = %d\n", err));
		return;
	}
	if (!(val & LQCM_ENAB_MASK)) {
		elem->link_quality.lqcm = -1;
	} else {
		uint8_v = (val & LQCM_TX_INDEX_MASK) >> LQCM_TX_INDEX_SHIFT;
		elem->link_quality.lqcm = (int8)uint8_v;
	}

	err = wldev_iovar_getint(bcmcfg_to_prmry_ndev(cfg), "snr", &val);
	if (err != BCME_OK) {
		WL_ERR((" Failed to get SNR %d, error = %d\n", val, err));
		return;
	}
	elem->link_quality.snr = *(uint8 *)&val;

	err = wldev_get_rssi(bcmcfg_to_prmry_ndev(cfg), &scbval);
	if (err != BCME_OK) {
		WL_ERR(("failed to get rssi, error = %d\n", err));
		return;
	}
	elem->link_quality.rssi = *((int8 *)((int32 *)&scbval.val));

}

#if defined(WLADPS) || defined(WLADPS_PRIVATE_CMD)
#ifndef ADPS_MODE_OFF
#define ADPS_MODE_OFF	0
#define ADPS_STEP_HIGH	1
#define ADPS_STEP_LOW	0
#endif /* ADPS_MODE_OFF */

static void
wsr_gather_adps(struct bcm_cfg80211 *cfg, wsr_elem_t *elem)
{
	int err;
	uint8 dump_sub_cmd = 0;
	uint8 *data;
	bcm_iov_buf_t iov_buf;
	bcm_iov_buf_t *resp;
	bcm_tlv_t *tlv;
	char adps_buf[WLC_IOCTL_MEDLEN];
	wl_adps_dump_summary_v1_t *summary;
	wsr_adps_t *adps = &elem->adps;

	memset(&elem->adps, 0, sizeof(elem->adps));

	iov_buf.version = WL_ADPS_IOV_VER;
	iov_buf.len = sizeof(dump_sub_cmd);
	iov_buf.id = WL_ADPS_IOV_DUMP;
	data = (uint8 *)iov_buf.data;
	data[0] = dump_sub_cmd;

	err = wldev_iovar_getbuf(bcmcfg_to_prmry_ndev(cfg), "adps",
		&iov_buf, sizeof(bcm_iov_buf_t), adps_buf, WLC_IOCTL_MEDLEN, NULL);
	if (err) {
		WL_ERR(("failed to get ADPS dump error = %d\n", err));
		return;
	}

	resp = (bcm_iov_buf_t *)adps_buf;
	tlv = (bcm_tlv_t *)resp->data;

	if (!bcm_valid_tlv(tlv, resp->len)) {
		WL_ERR(("Invalid TLV\n"));
		return;
	}

	summary = (wl_adps_dump_summary_v1_t *)tlv->data;
	if (tlv->len < OFFSETOF(wl_adps_dump_summary_v1_t, flags)) {
		WL_ERR(("Id: %x - invaild length (%d)\n", tlv->id, tlv->len));
		return;
	}
	if (summary->mode == ADPS_MODE_OFF) {
		return;
	}

	adps->mode = summary->mode;
	adps->flags = summary->flags;
	adps->current_step = summary->current_step;
	adps->duration_high = summary->stat[ADPS_STEP_HIGH].duration;
	adps->duration_low = summary->stat[ADPS_STEP_LOW].duration;
	adps->counts_high = summary->stat[ADPS_STEP_HIGH].counts;
	adps->counts_low = summary->stat[ADPS_STEP_LOW].counts;
}
#else
static void
wsr_gather_adps(struct bcm_cfg80211 *cfg, wsr_elem_t *elem)
{
	WL_ERR(("ADPS is not enabled\n"));
}
#endif /* defined(WLADPS) || defined(WLADPS_PRIVATE_CMD) */
/* ========= Debug Functions ============= */
#if defined(WSR_DEBUG)
static void
wsr_dbg_main(wsr_info_t *wsr_info)
{
	int read_idx;
	wsr_dbg_elem_t report_elem;

	read_idx = wsr_get_conn_idx(wsr_info, WSR_REPORT_MAX_WPA_CNT);
	WL_ERR(("WSR_DBG\n"));
	WL_ERR(("WSR_DBG\n"));
	WL_ERR(("WSR_DBG\n"));
	WL_ERR(("WSR_DBG : counts: %d %d\n",
		wsr_get_count(read_idx, wsr_info->write_idx), read_idx));
	WL_ERR(("WSR_DBG : r:%d w:%d c:%d\n",
		wsr_info->read_idx, wsr_info->write_idx, wsr_info->conn_idx));
	while (read_idx != WSR_INVALID) {
		wsr_elem_t *elem = &wsr_info->elem[read_idx];
		wsr_dbg_copy_elem(&report_elem, elem);
		wsr_dbg_print_elem(&report_elem);
		read_idx = wsr_get_next_ptr(wsr_info, read_idx);
	}

}

static void
wsr_dbg_copy_elem(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	wsr_dbg_copy_conn(dest, src);
	wsr_dbg_copy_cnts(dest, src);
	wsr_dbg_copy_clock(dest, src);
	wsr_dbg_copy_link(dest, src);
	wsr_dbg_copy_adps(dest, src);
}

static void
wsr_dbg_copy_cnts(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	wl_cnt_info_t *cntinfo = (wl_cnt_info_t *)src->cntinfo;
	wl_cnt_wlc_t *wlc_cnt;
	wl_cnt_ge40mcst_v1_t *macstat_cnt;

	/* COPY WLC counts */
	wlc_cnt = bcm_get_data_from_xtlv_buf(cntinfo->data, cntinfo->datalen,
		WL_CNT_XTLV_WLC, NULL, BCM_XTLV_OPTION_ALIGN32);
	if (wlc_cnt == NULL) {
		WL_ERR(("WSR_DBG : NO CNT\n"));
		return;
	}
	dest->txframe = wlc_cnt->txframe;

	/* COPY MAC STAT counts */
	macstat_cnt = bcm_get_data_from_xtlv_buf(cntinfo->data, cntinfo->datalen,
		WL_CNT_XTLV_GE40_UCODE_V1, NULL, BCM_XTLV_OPTION_ALIGN32);
	if (macstat_cnt == NULL) {
		WL_ERR(("WSR_DBG : NO MAC STAT\n"));
		return;
	}
	dest->rxmgocast = macstat_cnt->rxmgocast;
}

static inline void
wsr_dbg_copy_conn(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	memcpy(&dest->conn, &src->conn, sizeof(src->conn));
}
static inline void
wsr_dbg_copy_clock(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	memcpy(&dest->clock, &src->clock, sizeof(src->clock));
}

static inline void
wsr_dbg_copy_link(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	memcpy(&dest->link_quality, &src->link_quality, sizeof(src->link_quality));
}

static inline void
wsr_dbg_copy_adps(wsr_dbg_elem_t *dest, wsr_elem_t *src)
{
	memcpy(&dest->adps, &src->adps, sizeof(src->adps));
}

static void
wsr_dbg_print_elem(wsr_dbg_elem_t *elem)
{
	wsr_link_t *link = &elem->link_quality;
	wsr_adps_t *adps = &elem->adps;
	wsr_clock_t *clock = &elem->clock;
	wsr_conn_t *conn = &elem->conn;
	int idx;

	WL_ERR(("WSR_DBG: DATE [%u] %d-%02d-%02d %02d:%02d:%02d\n", clock->kernel_clock,
		clock->date/WSR_REPORT_YEAR_MUL,
		(clock->date%WSR_REPORT_YEAR_MUL)/WSR_REPORT_MON_MUL,
		(clock->date%WSR_REPORT_YEAR_MUL)%WSR_REPORT_MON_MUL,
		clock->time/WSR_REPORT_HOUR_MUL,
		(clock->time%WSR_REPORT_HOUR_MUL)/WSR_REPORT_MIN_MUL,
		(clock->time%WSR_REPORT_HOUR_MUL)%WSR_REPORT_MIN_MUL));
	WL_ERR(("WSR_DBG: CONN bssid: "MACDBG"\n",
		MAC2STRDBG(conn->bssid)));
	WL_ERR(("WSR_DBG: CONN  op_mode:%x vndr oui: %d", conn->dhd_opmode, conn->oui_cnt));
	for (idx = 0; idx < conn->oui_cnt; idx++) {
		WL_ERR((" "MACOUIDBG,
			MACOUI2STRDBG(conn->oui_list[idx])));
	}
	WL_ERR(("WSR_DBG\n"));
	WL_ERR(("WSR_DBG: CNT txframe:%d rxmgocast:%d\n", elem->txframe, elem->rxmgocast));
	WL_ERR(("WSR_DBG: LINK LQCM %d SNR %d RSSI %d\n", link->lqcm, link->snr, link->rssi));
	WL_ERR(("WSR_DBG: ADPS M:%d F:%d S:%d H:(%d %d) L:(%d %d)\n", adps->mode, adps->flags,
		adps->current_step, adps->duration_high, adps->counts_high,
		adps->duration_low, adps->counts_low));
}
#endif /* WSR_DEBUG */

/* ========= File Save ============= */
#define WSR_TYPE_MAGIC	"WIFI_STAT_REPORT"
void wl_stat_report_file_save(void *dhdp, void *fp)
{
	struct file *pfp = (struct file *)fp;
	dhd_pub_t *ppub = (dhd_pub_t *)dhdp;
	wsr_info_t *wsr_info;
	int first_idx, last_idx, count, wr_count;
	int wr_size;
	int err;

	if (!pfp || !ppub) {
		WL_ERR(("pfp and/or dhdp is NULL\n"));
		return;
	}

	wsr_info =  (wsr_info_t *)ppub->stat_report_info;
	if (!wsr_info) {
		WL_ERR(("stat control block is not exist\n"));
		return;
	}

	err = generic_file_llseek(pfp, 0, SEEK_END);
	if (err < 0) {
		WL_ERR(("WRITE ERROR!!!! err = %d\n", err));
		return;
	}

	/* WRITE MAGICS TYPE + TYPE + VER */
	vfs_write(pfp, WSR_TYPE_MAGIC, strlen(WSR_TYPE_MAGIC), &pfp->f_pos);
	vfs_write(pfp, WSR_TYPE_MAGIC, strlen(WSR_TYPE_MAGIC), &pfp->f_pos);
	vfs_write(pfp, (const char *)&wsr_info->version, sizeof(wsr_info->version), &pfp->f_pos);

	/* WRITE total data size */
	first_idx = wsr_get_first_idx(wsr_info);
	last_idx = wsr_get_last_idx(wsr_info);
	count = wsr_get_count(first_idx, last_idx);
	wr_size = count * sizeof(wsr_elem_t) + strlen(WSR_TYPE_MAGIC);
	vfs_write(pfp, (const char *)&wr_size, sizeof(wr_size), &pfp->f_pos);
	if (count <= 0) {
		goto final_magic;
	}

	/* WRITE TO THE END OF ARRAY */
	wr_count = (WSR_REPORT_MAX_DATA - first_idx);
	vfs_write(pfp, (const char *)&wsr_info->elem[first_idx],
		sizeof(wsr_elem_t) * wr_count, &pfp->f_pos);

	/* WRITE WRAP UP */
	count = count - wr_count;
	if (count <= 0) {
		goto final_magic;
	}
	vfs_write(pfp, (const char *)&wsr_info->elem[0], sizeof(wsr_elem_t) * count, &pfp->f_pos);

final_magic:
	/* WRITE MAIC */
	vfs_write(pfp, WSR_TYPE_MAGIC, strlen(WSR_TYPE_MAGIC), &pfp->f_pos);

}

/* ========= Private Command ============= */
#define WSR_REPORT_ELEM_PRINT_BUF	256
#define WSR_REPORT_STATE_NORMAL 0
#define WSR_REPORT_STATE_WLC	1
#define WSR_REPORT_STATE_LAST	0xffff
#define WSR_REPORT_NAME_MAX	20

#define WSR_DEC	1
#define WSR_HEX	2

#define WSR_UINT8	1
#define WSR_UINT32	2
#define WSR_DATE	3
#define WSR_TIME	4
#define WSR_BSSID	5
#define WSR_OUI	6
#define WSR_CNT_WLC	7
#define WSR_CNT_MAC	8

#define WSR_SERIAL_MODE_RECORD_FIRST	1
#define WSR_SERIAL_MODE_ITEM_FIRST		2
typedef struct {
	char name[WSR_REPORT_NAME_MAX];
	uint32 offset;
	int format;
	int type;
	int f_signed;
} wsr_serial_info_normal_t;

typedef int (*wsr_serial_sub_func)
	(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem);

#define WSR_SERIAL_CLOCK(name, value, fmt, type, f_s) \
{#name, (uint32)(OFFSETOF(wsr_elem_t, clock) + \
		OFFSETOF(wsr_clock_t, value)), fmt, type, f_s}

#define WSR_SERIAL_CONN(name, value, fmt, type, f_s) \
{#name, (uint32)(OFFSETOF(wsr_elem_t, conn) + \
		OFFSETOF(wsr_conn_t, value)), fmt, type, f_s}

#define WSR_SERIAL_LINK(name, value, fmt, type, f_s) \
{#name, (uint32)(OFFSETOF(wsr_elem_t, link_quality) + \
		OFFSETOF(wsr_link_t, value)), fmt, type, f_s}

#define WSR_SERIAL_ADPS(name, value, fmt, type, f_s) \
{#name, (uint32)(OFFSETOF(wsr_elem_t, adps) + \
		OFFSETOF(wsr_adps_t, value)), fmt, type, f_s}

#define WSR_SERIAL_TXP(name, value, fmt, type, f_s) \
{#name, (uint32)(OFFSETOF(wsr_elem_t, tx_power) + \
		OFFSETOF(wsr_tx_power_t, value)), fmt, type, f_s}

static wsr_serial_info_normal_t
wsr_serial_info_normal_tbl[] = {
	/* clock */
	WSR_SERIAL_CLOCK(CLOCK, kernel_clock, WSR_DEC, WSR_UINT32, FALSE),
	WSR_SERIAL_CLOCK(DATE, date, WSR_DEC, WSR_DATE, FALSE),
	WSR_SERIAL_CLOCK(TIME, time, WSR_DEC, WSR_TIME, FALSE),

	/* connection */
	WSR_SERIAL_CONN(ADDR, bssid, WSR_HEX, WSR_BSSID, FALSE),
	WSR_SERIAL_CONN(OPMODE, dhd_opmode, WSR_HEX, WSR_UINT32, FALSE),
	WSR_SERIAL_CONN(OUI, oui_list, WSR_HEX, WSR_OUI, FALSE),

	/* link quality */
	WSR_SERIAL_LINK(LQCM, lqcm, WSR_DEC, WSR_UINT8, TRUE),
	WSR_SERIAL_LINK(RSSI, rssi, WSR_DEC, WSR_UINT8, TRUE),
	WSR_SERIAL_LINK(SNR, snr, WSR_DEC, WSR_UINT8, FALSE),

	/* adps */
	WSR_SERIAL_ADPS(ADPS_MODE, mode, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_ADPS(ADPS_LFAG, flags, WSR_HEX, WSR_UINT8, FALSE),
	WSR_SERIAL_ADPS(ADPS_STEP, current_step, WSR_DEC, WSR_UINT8, FALSE),
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_ADPS(ADPS_DH, duration_high, WSR_DEC, WSR_UINT32, FALSE),
	WSR_SERIAL_ADPS(ADPS_DL, duration_low, WSR_DEC, WSR_UINT32, FALSE),
	WSR_SERIAL_ADPS(ADPS_CH, counts_high, WSR_DEC, WSR_UINT32, FALSE),
	WSR_SERIAL_ADPS(ADPS_CL, counts_low, WSR_DEC, WSR_UINT32, FALSE),
#endif /* WSR_MINIMIZE_REPORT */

	/* TX PWR */
	WSR_SERIAL_TXP(SAR_ENAB, sar_enable, WSR_DEC, WSR_UINT8, FALSE),
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_TXP(RSDB_MOD, phy_rsdb_mode, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(SAR_2G_C0, sar_limit_2g_core0, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(SAR_2G_C1, sar_limit_2g_core1, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(SAR_5G_C0, sar_limit_5g_core0, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(SAR_5G_C1, sar_limit_5g_core1, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(FCC, fcc_enable, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(FCC_CH12, fccpwrch12, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(FCC_CH13, fccpwrch13, WSR_DEC, WSR_UINT8, FALSE),
#endif /* WSR_MINIMIZE_REPORT */
	WSR_SERIAL_TXP(BSS_L_MAX, bss_local_max, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(BSS_L_CST, bss_local_constraint, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(DSSS_1M, dsss_1mbps_2g, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(OFDM_6M_2G, ofdm_6mbps_2g, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(MCS0_2G, mcs_0_2g, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(OFDM_6M_5G, ofdm_6mbps_5g, WSR_DEC, WSR_UINT8, FALSE),
	WSR_SERIAL_TXP(MCS0_5G, mcs_0_5g, WSR_DEC, WSR_UINT8, FALSE),
	{"", 0, 0, 0, 0}
};

/* Serialize 1 byte variable with requested format/signed */
static int
wsr_serial_uint8(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	uint8 *ptr;
	int8 *ptr2;
	char *fmt;
	char *null_fmt = " -255";

	if (buf_len <= strlen(null_fmt)) {
		return -1;
	}

	if (elem == NULL) {
		bytes_written = scnprintf(buf, buf_len, " 0");
		return bytes_written;
	}

	ptr = ptr2 = (uint8 *)((char *)elem + info->offset);
	switch (info->format) {
		case WSR_DEC:
			if (info->f_signed) {
				fmt = " %d";
			} else {
				fmt = " %u";
			}
			break;
		case WSR_HEX:
			fmt = " 0x%02x";
			break;
		default:
			WL_ERR(("UKNOWN FMT\n"));
			return -1;
	}

	bytes_written = scnprintf(buf, buf_len, fmt, info->f_signed?(*ptr2) :(*ptr));
	return bytes_written;
}

/* Serialize 4 byte variable with requested format/signed */
static int
wsr_serial_uint32(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	uint32 *ptr;
	int32 *ptr2;
	char *fmt;
	char *null_fmt = " -4294967295";

	if (buf_len <= strlen(null_fmt)) {
		return -1;
	}

	if (elem == NULL) {
		bytes_written = scnprintf(buf, buf_len, " 0");
		return bytes_written;
	}

	ptr = ptr2 = (uint32 *)((char *)elem + info->offset);
	switch (info->format) {
		case WSR_DEC:
			if (info->f_signed) {
				fmt = " %d";
			} else {
				fmt = " %u";
			}
			break;
		case WSR_HEX:
			fmt = " 0x%08x";
			break;
		default:
			WL_ERR(("UKNOWN FMT\n"));
			return -1;
	}

	bytes_written = scnprintf(buf, buf_len, fmt, info->f_signed?(*ptr2) :(*ptr));
	return bytes_written;
}

/* serialize WSR date variable to date format */
static int
wsr_serial_date(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	uint32 *ptr;
	char *null_fmt = " 0-0-0";
	char *norm_fmt = " 0000-00-00";

	if (buf_len <= strlen(norm_fmt)) {
		return -1;
	}

	if (elem == NULL) {
		bytes_written = scnprintf(buf, buf_len, null_fmt);
		return bytes_written;
	}

	ptr = (uint32 *)((char *)elem + info->offset);
	bytes_written = scnprintf(buf, buf_len, " %d-%02d-%02d",
			(*ptr)/WSR_REPORT_YEAR_MUL,
			((*ptr)%WSR_REPORT_YEAR_MUL)/WSR_REPORT_MON_MUL,
			((*ptr)%WSR_REPORT_YEAR_MUL)%WSR_REPORT_MON_MUL);

	return bytes_written;
}

/* serialize WSR time variable to time format */
static int
wsr_serial_time(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	uint32 *ptr;
	char *null_fmt = " 00:00:00";

	if (buf_len <= strlen(null_fmt)) {
		return -1;
	}

	if (elem == NULL) {
		bytes_written = scnprintf(buf, buf_len, null_fmt);
		return bytes_written;
	}

	ptr = (uint32 *)((char *)elem + info->offset);
	bytes_written = scnprintf(buf, buf_len, " %02d:%02d:%02d",
			(*ptr)/WSR_REPORT_HOUR_MUL,
			((*ptr)%WSR_REPORT_HOUR_MUL)/WSR_REPORT_MIN_MUL,
			((*ptr)%WSR_REPORT_HOUR_MUL)%WSR_REPORT_MIN_MUL);

	return bytes_written;
}

/* serialize BSSID type */
static int
wsr_serial_bssid(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	int idx;
	uint8 *ptr;
	char *null_fmt = " 00:00:00:00:00:00";


	if (buf_len <= strlen(null_fmt)) {
		return -1;
	}

	if (elem == NULL) {
		bytes_written = scnprintf(buf, buf_len, null_fmt);
		return bytes_written;
	}

	ptr = (uint8 *)((char *)elem + info->offset);

	for (idx = 0; idx < ETHER_ADDR_LEN; idx++) {
		bytes_written += scnprintf(&buf[bytes_written],
			buf_len - bytes_written, ":%02x", *ptr);
		ptr++;
	}

	/* start with white space */
	buf[0] = ' ';

	return bytes_written;
}

/* Serialize OUI type */
static int
wsr_serial_oui(wsr_serial_info_normal_t *info, char *buf, int buf_len, wsr_elem_t *elem)
{
	int bytes_written = 0;
	int idx;
	uint8 *ptr;
	char *null_fmt = " 00-00-00";


	if (buf_len <= strlen(null_fmt) * WSR_MAX_OUI) {
		return -1;
	}

	if (elem == NULL) {
		for (idx = 0; idx < WSR_MAX_OUI; idx++) {
			bytes_written += scnprintf(&buf[bytes_written],
				buf_len - bytes_written, null_fmt);
		}
		goto serial_oui_finish;
	}

	ptr = (uint8 *)((char *)elem + info->offset);

	for (idx = 0; idx < DOT11_OUI_LEN * WSR_MAX_OUI; idx++) {
		bytes_written += scnprintf(&buf[bytes_written],
			buf_len - bytes_written, "-%02x", *ptr);
		ptr++;
	}

serial_oui_finish:
	/* start with white space */
	for (idx = 0; idx < WSR_MAX_OUI; idx++) {
		ptr = &buf[idx * strlen(null_fmt)];
		*ptr = ' ';
	}

	return bytes_written;
}

static int
wsr_serial_normal(char *buf, int *sub_idx, wsr_elem_t **list, int list_cnt)
{
	int bytes_written = 0;
	int cur_written = 0;
	wsr_serial_info_normal_t *info;
	char tmp_buf[WSR_REPORT_ELEM_PRINT_BUF] = {0, };
	wsr_serial_sub_func func;
	int idx;

	if (strlen(wsr_serial_info_normal_tbl[*sub_idx].name) == 0) {
		/* Serialize normal is finished */
		return 0;
	}

	info = &wsr_serial_info_normal_tbl[*sub_idx];
	/* set sub function */
	switch (info->type) {
		case WSR_UINT8:
			func = wsr_serial_uint8;
			break;
		case WSR_UINT32:
			func = wsr_serial_uint32;
			break;
		case WSR_BSSID:
			func = wsr_serial_bssid;
			break;
		case WSR_OUI:
			func = wsr_serial_oui;
			break;
		case WSR_DATE:
			func = wsr_serial_date;
			break;
		case WSR_TIME:
			func = wsr_serial_time;
			break;
		default:
			WL_ERR(("UNKNOWN SERIALIZE TYPE\n"));
			return -1;
	}

	for (idx = 0; idx < list_cnt; idx++) {
		cur_written = func(info, &tmp_buf[bytes_written],
			sizeof(tmp_buf) - bytes_written, list[idx]);
		if (cur_written <= 0) {
			/* need bigger buffer */
			return -1;
		}

		bytes_written += cur_written;
	}

	memcpy(buf, tmp_buf, bytes_written);
	(*sub_idx)++;
	return bytes_written;
}

#define WSR_SERIAL_WLC(a) {#a, OFFSETOF(wl_cnt_wlc_t, a), WSR_CNT_WLC}
#define WSR_SERIAL_MAC(a) {#a, OFFSETOF(wl_cnt_ge40mcst_v1_t, a), WSR_CNT_MAC}
typedef struct {
	char name[WSR_REPORT_NAME_MAX];
	uint32 offset;
	int type;
} wsr_serial_info_count_t;

static wsr_serial_info_count_t
wsr_serial_info_wlc_tbl[] = {
	/* counts of wlc */
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_WLC(txframe),
#endif /* WSR_MINIMIZE_REPORT */
	WSR_SERIAL_WLC(txretrans),
	WSR_SERIAL_WLC(txfail),
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_WLC(rxframe),
	WSR_SERIAL_WLC(txphyerr),
	WSR_SERIAL_WLC(rxnobuf),
	WSR_SERIAL_WLC(rxrtry),
#endif /* WSR_MINIMIZE_REPORT */
	WSR_SERIAL_WLC(txprobereq),
	WSR_SERIAL_WLC(rxprobersp),
	WSR_SERIAL_MAC(txallfrm),
	WSR_SERIAL_MAC(txrtsfrm),
	WSR_SERIAL_MAC(txctsfrm),
	WSR_SERIAL_WLC(txback),
	WSR_SERIAL_MAC(txucast),
	WSR_SERIAL_MAC(rxrsptmout),
	WSR_SERIAL_MAC(txrtsfail),
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_MAC(txphyerror),
#endif /* WSR_MINIMIZE_REPORT */
	WSR_SERIAL_MAC(rxstrt),
	WSR_SERIAL_MAC(rxbadplcp),
	WSR_SERIAL_MAC(rxcrsglitch),
	WSR_SERIAL_MAC(rxnodelim),
	WSR_SERIAL_MAC(bphy_badplcp),
	WSR_SERIAL_MAC(bphy_rxcrsglitch),
	WSR_SERIAL_MAC(rxbadfcs),
	WSR_SERIAL_MAC(rxf0ovfl),
	WSR_SERIAL_MAC(rxf1ovfl),
	WSR_SERIAL_MAC(rxhlovfl),
	WSR_SERIAL_MAC(rxrtsucast),
	WSR_SERIAL_MAC(rxctsucast),
	WSR_SERIAL_MAC(rxackucast),
	WSR_SERIAL_WLC(rxback),
	WSR_SERIAL_MAC(rxbeaconmbss),
	WSR_SERIAL_MAC(rxdtucastmbss),
	WSR_SERIAL_MAC(rxbeaconobss),
	WSR_SERIAL_MAC(rxdtucastobss),
	WSR_SERIAL_MAC(rxdtocast),
	WSR_SERIAL_MAC(rxrtsocast),
	WSR_SERIAL_MAC(rxctsocast),
	WSR_SERIAL_MAC(rxdtmcast),
#ifndef WSR_MINIMIZE_REPORT
	WSR_SERIAL_WLC(rxmpdu_mu),
	WSR_SERIAL_WLC(txassocreq),
	WSR_SERIAL_WLC(txreassocreq),
	WSR_SERIAL_WLC(txdisassoc),
	WSR_SERIAL_WLC(txassocrsp),
	WSR_SERIAL_WLC(txreassocrsp),
	WSR_SERIAL_WLC(txauth),
	WSR_SERIAL_WLC(txdeauth),
	WSR_SERIAL_WLC(rxassocreq),
	WSR_SERIAL_WLC(rxreassocreq),
	WSR_SERIAL_WLC(rxdisassoc),
	WSR_SERIAL_WLC(rxassocrsp),
	WSR_SERIAL_WLC(rxreassocrsp),
	WSR_SERIAL_WLC(rxauth),
	WSR_SERIAL_WLC(rxdeauth),
	WSR_SERIAL_MAC(txackfrm),
	WSR_SERIAL_MAC(txampdu),
	WSR_SERIAL_MAC(txmpdu),
#endif /* WSR_MINIMIZE_REPORT */
	{"", 0, 0}
};

static int
wsr_serial_wlc(char *buf, int *sub_idx, wsr_elem_t **list, int list_cnt)
{
	int bytes_written = 0;
	wsr_serial_info_count_t *info;
	char tmp_buf[WSR_REPORT_ELEM_PRINT_BUF] = {0, };
	int idx;
	uint32 *ptr;
	wl_cnt_info_t *cntinfo;
	char *cntptr;

	if (strlen(wsr_serial_info_wlc_tbl[*sub_idx].name) == 0) {
		/* Serialize normal is finished */
		return 0;
	}

	info = &wsr_serial_info_wlc_tbl[*sub_idx];

	for (idx = 0; idx < list_cnt; idx++) {
		if (bytes_written >= WSR_REPORT_ELEM_PRINT_BUF) {
			return -1;
		}
		if (list[idx] == NULL) {
			bytes_written += scnprintf(&tmp_buf[bytes_written],
				WSR_REPORT_ELEM_PRINT_BUF - bytes_written, " 0");
			continue;
		}

		/* find base PTR for WLC or MACSTAT */
		cntinfo = (wl_cnt_info_t *)list[idx]->cntinfo;
		if (info->type == WSR_CNT_WLC) {
			cntptr = (char *)bcm_get_data_from_xtlv_buf(cntinfo->data, cntinfo->datalen,
				WL_CNT_XTLV_WLC, NULL, BCM_XTLV_OPTION_ALIGN32);
		} else if (info->type == WSR_CNT_MAC) {
			cntptr =  (char *)bcm_get_data_from_xtlv_buf(cntinfo->data,
				cntinfo->datalen,
				WL_CNT_XTLV_GE40_UCODE_V1, NULL, BCM_XTLV_OPTION_ALIGN32);
		} else {
			WL_ERR(("INVALID TYPE\n"));
			return -1;
		}

		if (cntptr == NULL) {
			bytes_written += scnprintf(&tmp_buf[bytes_written],
				WSR_REPORT_ELEM_PRINT_BUF - bytes_written, " 0");
		} else {
			ptr = (uint32 *)(cntptr + info->offset);
			bytes_written += scnprintf(&tmp_buf[bytes_written],
				WSR_REPORT_ELEM_PRINT_BUF - bytes_written, " %u",
				(*ptr) % WSR_CNT_VALUE_MAX);
		}
	}

	memcpy(buf, tmp_buf, bytes_written);
	(*sub_idx)++;
	return bytes_written;
}

int
wl_android_stat_report_get_start(void *net, char *cmd, int tot_len)
{
	struct net_device *dev = (struct net_device *)net;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	wsr_info_t *wsr_info = cfg->stat_report_info;
	char *pcmd = cmd;
	char *str_cmd;
	char *str;
	int mode, max_len;
	int lock_count;
	int bytes_written = 0;

	/* SKIP command */
	str_cmd = bcmstrtok(&pcmd, " ", NULL);

	/* get MODE */
	str = bcmstrtok(&pcmd, " ", NULL);
	if (!str) {
		WL_ERR(("NO mode for %s\n", str_cmd));
		return BCME_ERROR;
	}
	mode = bcm_atoi(str);
	if (mode != WSR_SERIAL_MODE_RECORD_FIRST &&
		mode != WSR_SERIAL_MODE_ITEM_FIRST) {
		WL_ERR(("NOT SUPPORTED MODE \n"));
		return BCME_ERROR;
	}


	/* get max length */
	str = bcmstrtok(&pcmd, " ", NULL);
	if (!str) {
		WL_ERR(("NO length for %s\n", str_cmd));
		return BCME_ERROR;
	}
	max_len = bcm_atoi(str);
	if (max_len <= WSR_REPORT_ELEM_PRINT_BUF) {
		WL_ERR(("TO short buffer length (min=%d)\n", WSR_REPORT_ELEM_PRINT_BUF));
		return BCME_ERROR;
	}

	/* Set seiralize info and lock record till serialzation is finished */
	wsr_info->seral_mode = mode;
	wsr_info->trans_buf_len = max_len;
	wsr_set_lock_idx(wsr_info, WSR_REPORT_MAX_WPA_CNT);
	wsr_info->seral_state = WSR_REPORT_STATE_NORMAL;
	wsr_info->serial_sub_state = 0;

	if (mode == WSR_SERIAL_MODE_RECORD_FIRST) {
		lock_count = wsr_get_lock_cnt(wsr_info);
		WL_ERR(("Lock WSR_SERIAL_MODE_RECORD_FIRST is set count= %d\n",
			lock_count));
		bytes_written = scnprintf(cmd, tot_len, "%d", lock_count);
		return bytes_written;
	}

	WL_ERR(("Lock WSR_SERIAL_MODE_ITEM_FIRST is set\n"));
	return BCME_OK;
}

int
wl_android_stat_report_get_next_mode_record(void *net, char *cmd, int tot_len)
{
	struct net_device *dev = (struct net_device *)net;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	wsr_info_t *wsr_info = cfg->stat_report_info;
	wsr_elem_t *elem;
	int bytes_written = 0;
	int iter_written;
	int lock_idx;

	lock_idx = wsr_get_lock_idx(wsr_info);
	if (lock_idx == WSR_INVALID) {
		WL_ERR(("Lock pointer is not set\n"));
		return BCME_ERROR;
	}

	elem = &wsr_info->elem[lock_idx];

	memset(cmd, 0, wsr_info->trans_buf_len);
	wsr_info->seral_state = WSR_REPORT_STATE_NORMAL;
	wsr_info->serial_sub_state = 0;

	while (bytes_written + WSR_REPORT_ELEM_PRINT_BUF < wsr_info->trans_buf_len) {
		switch (wsr_info->seral_state) {
			case WSR_REPORT_STATE_NORMAL:
				iter_written = wsr_serial_normal(&cmd[bytes_written],
					&wsr_info->serial_sub_state, &elem, 1);
				break;
			case WSR_REPORT_STATE_WLC:
				iter_written = wsr_serial_wlc(&cmd[bytes_written],
					&wsr_info->serial_sub_state, &elem, 1);
				break;
			default:
				goto finished;
		}

		if (iter_written == 0) {
			wsr_info->seral_state++;
			wsr_info->serial_sub_state = 0;
			continue;
		}
		if (iter_written < 0) {
			WL_ERR(("At list one element is string lenght is too big(%d %d)\n",
				wsr_info->seral_state, wsr_info->serial_sub_state));
			wsr_free_lock_idx(wsr_info);
			return -1;
		}
		bytes_written += iter_written;
	}

	cmd[bytes_written] = '\0';
	WL_ERR(("(len=%d) %s\n", bytes_written, cmd));
	return bytes_written;

finished:
	cmd[bytes_written] = '\0';
	wsr_decr_lock_count(wsr_info);

	WL_ERR(("(len=%d) %s\n", bytes_written, cmd));
	return bytes_written;
}

int
wl_android_stat_report_get_next_mode_item(void *net, char *cmd, int tot_len)
{
	struct net_device *dev = (struct net_device *)net;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	wsr_info_t *wsr_info = cfg->stat_report_info;
	wsr_elem_t *elem[WSR_REPORT_MAX_WPA_CNT] = {NULL, };
	int idx;
	int next_idx;
	int bytes_written = 0;
	int iter_written;
	int lock_idx, lock_count;

	lock_idx = wsr_get_lock_idx(wsr_info);
	lock_count = wsr_get_lock_cnt(wsr_info);

	if (lock_idx == WSR_INVALID) {
		WL_ERR(("Lock pointer is not set\n"));
		return BCME_ERROR;
	}

/* Init seiralze records of WSR_REPORT_MAX_WPA_CNT
 * if locked record is less than WSR_REPORT_MAX_WPA_CNT
 * fill NULL from first to WSR_REPORT_MAX_WPA_CNT - count
 */
	next_idx = lock_idx;
	for (idx = WSR_REPORT_MAX_WPA_CNT - lock_count;
		idx < WSR_REPORT_MAX_WPA_CNT; idx++) {
		elem[idx] = &wsr_info->elem[next_idx];
		next_idx = wsr_get_next_idx(wsr_info, next_idx);
	}

	memset(cmd, 0, wsr_info->trans_buf_len);
	bytes_written = scnprintf(cmd, tot_len, "MORE");

	while (bytes_written + WSR_REPORT_ELEM_PRINT_BUF < wsr_info->trans_buf_len) {
		switch (wsr_info->seral_state) {
			case WSR_REPORT_STATE_NORMAL:
				iter_written = wsr_serial_normal(&cmd[bytes_written],
					&wsr_info->serial_sub_state, elem, WSR_REPORT_MAX_WPA_CNT);
				break;
			case WSR_REPORT_STATE_WLC:
				iter_written = wsr_serial_wlc(&cmd[bytes_written],
					&wsr_info->serial_sub_state, elem, WSR_REPORT_MAX_WPA_CNT);
				break;
			default:
				goto finished;
		}

		if (iter_written == 0) {
			wsr_info->seral_state++;
			wsr_info->serial_sub_state = 0;
			continue;
		}
		if (iter_written < 0) {
			WL_ERR(("At list one element is string lenght is too big(%d %d)\n",
				wsr_info->seral_state, wsr_info->serial_sub_state));
			goto finished;
		}
		bytes_written += iter_written;
	}

	cmd[bytes_written] = '\0';
	WL_ERR(("(len=%d) %s\n", bytes_written, cmd));
	return bytes_written;

finished:
	cmd[bytes_written] = '\0';
	memcpy(cmd, "LAST", strlen("LAST"));
	wsr_free_lock_idx(wsr_info);
	WL_ERR(("(len=%d) %s\n", bytes_written, cmd));
	return bytes_written;
}

int
wl_android_stat_report_get_next(void *net, char *cmd, int tot_len)
{
	struct net_device *dev = (struct net_device *)net;
	struct bcm_cfg80211 *cfg = wl_get_cfg(dev);
	wsr_info_t *wsr_info = cfg->stat_report_info;

	if (wsr_info == NULL) {
		WL_ERR(("REPORT STAT is NOT enabled\n"));
		return BCME_ERROR;
	}

	if (tot_len < wsr_info->trans_buf_len) {
		WL_ERR(("To short buffer length (config:%d buf len:%d\n",
			wsr_info->trans_buf_len, tot_len));
		wsr_free_lock_idx(wsr_info);
		return BCME_ERROR;
	}

	if (wsr_info->seral_mode == WSR_SERIAL_MODE_RECORD_FIRST) {
		return wl_android_stat_report_get_next_mode_record(net, cmd, tot_len);
	} else {
		return wl_android_stat_report_get_next_mode_item(net, cmd, tot_len);
	}

	return BCME_ERROR;
}