hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Dongle BUS interface
 * USB Linux Implementation
 *
 * Copyright (C) 1999-2016, 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: dbus_usb_linux.c 564663 2015-06-18 02:34:42Z $
 */
 
/**
 * @file @brief
 * This file contains DBUS code that is USB *and* OS (Linux) specific. DBUS is a Broadcom
 * proprietary host specific abstraction layer.
 */
 
#include <typedefs.h>
#include <osl.h>
 
/**
 * DBUS_LINUX_RXDPC is created for router platform performance tuning. A separate thread is created
 * to handle USB RX and avoid the call chain getting too long and enhance cache hit rate.
 *
 * DBUS_LINUX_RXDPC setting is in wlconfig file.
 */
 
/*
 * If DBUS_LINUX_RXDPC is off, spin_lock_bh() for CTFPOOL in
 * linux_osl.c has to be changed to spin_lock_irqsave() because
 * PKTGET/PKTFREE are no longer in bottom half.
 *
 * Right now we have another queue rpcq in wl_linux.c. Maybe we
 * can eliminate that one to reduce the overhead.
 *
 * Enabling 2nd EP and DBUS_LINUX_RXDPC causing traffic from
 * both EP's to be queued in the same rx queue. If we want
 * RXDPC to work with 2nd EP. The EP for RPC call return
 * should bypass the dpc and go directly up.
 */
 
/* #define DBUS_LINUX_RXDPC */
 
/* Dbus histogram for ntxq, nrxq, dpc parameter tuning */
/* #define DBUS_LINUX_HIST */
 
#include <usbrdl.h>
#include <bcmendian.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/random.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/uaccess.h>
#include <asm/unaligned.h>
#include <dbus.h>
#include <bcmutils.h>
#include <bcmdevs.h>
#include <linux/usb.h>
#include <usbrdl.h>
#include <linux/firmware.h>
#include <dngl_stats.h>
#include <dhd.h>
 
#if defined(USBOS_THREAD) || defined(USBOS_TX_THREAD)
 
/**
 * The usb-thread is designed to provide currency on multiprocessors and SMP linux kernels. On the
 * dual cores platform, the WLAN driver, without threads, executed only on CPU0. The driver consumed
 * almost of 100% on CPU0, while CPU1 remained idle. The behavior was observed on Broadcom's STB.
 *
 * The WLAN driver consumed most of CPU0 and not CPU1 because tasklets/queues, software irq, and
 * hardware irq are executing from CPU0, only. CPU0 became the system's bottle-neck. TPUT is lower
 * and system's responsiveness is slower.
 *
 * To improve system responsiveness and TPUT usb-thread was implemented. The system's threads could
 * be scheduled to run on any core. One core could be processing data in the usb-layer and the other
 * core could be processing data in the wl-layer.
 *
 * For further info see [WlThreadAndUsbThread] Twiki.
 */
 
#include <linux/kthread.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <asm/hardirq.h>
#include <linux/list.h>
#include <linux_osl.h>
#endif /* USBOS_THREAD || USBOS_TX_THREAD */
 
 
 
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
#define KERNEL26
#endif
 
/**
 * Starting with the 3.10 kernel release, dynamic PM support for USB is present whenever
 * the kernel was built with CONFIG_PM_RUNTIME enabled. The CONFIG_USB_SUSPEND option has
 * been eliminated.
 */
#if ((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21)) && defined(CONFIG_USB_SUSPEND)) \
   || ((LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) && defined(CONFIG_PM_RUNTIME))
/* For USB power management support, see Linux kernel: Documentation/usb/power-management.txt */
#define USB_SUSPEND_AVAILABLE
#endif
 
/* Define alternate fw/nvram paths used in Android */
#ifdef OEM_ANDROID
#define CONFIG_ANDROID_BCMDHD_FW_PATH "broadcom/dhd/firmware/fw.bin.trx"
#define CONFIG_ANDROID_BCMDHD_NVRAM_PATH "broadcom/dhd/nvrams/nvm.txt"
#endif /* OEM_ANDROID */
 
static inline int usb_submit_urb_linux(struct urb *urb)
{
 
#ifdef BCM_MAX_URB_LEN
   if (urb && (urb->transfer_buffer_length > BCM_MAX_URB_LEN)) {
       DBUSERR(("URB transfer length=%d exceeded %d ra=%p\n", urb->transfer_buffer_length,
       BCM_MAX_URB_LEN, __builtin_return_address(0)));
       return DBUS_ERR;
   }
#endif
 
#ifdef KERNEL26
   return usb_submit_urb(urb, GFP_ATOMIC);
#else
   return usb_submit_urb(urb);
#endif
 
}
 
#define USB_SUBMIT_URB(urb) usb_submit_urb_linux(urb)
 
#ifdef KERNEL26
 
#define USB_ALLOC_URB()                usb_alloc_urb(0, GFP_ATOMIC)
#define USB_UNLINK_URB(urb)            (usb_kill_urb(urb))
#define USB_FREE_URB(urb)            (usb_free_urb(urb))
#define USB_REGISTER()                usb_register(&dbus_usbdev)
#define USB_DEREGISTER()            usb_deregister(&dbus_usbdev)
 
#ifdef USB_SUSPEND_AVAILABLE
 
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
#define USB_AUTOPM_SET_INTERFACE(intf)        usb_autopm_set_interface(intf)
#else
#define USB_ENABLE_AUTOSUSPEND(udev)        usb_enable_autosuspend(udev)
#define USB_DISABLE_AUTOSUSPEND(udev)       usb_disable_autosuspend(udev)
#endif  /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))  */
 
#define USB_AUTOPM_GET_INTERFACE(intf)        usb_autopm_get_interface(intf)
#define USB_AUTOPM_PUT_INTERFACE(intf)        usb_autopm_put_interface(intf)
#define USB_AUTOPM_GET_INTERFACE_ASYNC(intf)    usb_autopm_get_interface_async(intf)
#define USB_AUTOPM_PUT_INTERFACE_ASYNC(intf)    usb_autopm_put_interface_async(intf)
#define USB_MARK_LAST_BUSY(dev)            usb_mark_last_busy(dev)
 
#else /* USB_SUSPEND_AVAILABLE */
 
#define USB_AUTOPM_GET_INTERFACE(intf)        do {} while (0)
#define USB_AUTOPM_PUT_INTERFACE(intf)        do {} while (0)
#define USB_AUTOPM_GET_INTERFACE_ASYNC(intf)    do {} while (0)
#define USB_AUTOPM_PUT_INTERFACE_ASYNC(intf)    do {} while (0)
#define USB_MARK_LAST_BUSY(dev)            do {} while (0)
#endif /* USB_SUSPEND_AVAILABLE */
 
#define USB_CONTROL_MSG(dev, pipe, request, requesttype, value, index, data, size, timeout) \
   usb_control_msg((dev), (pipe), (request), (requesttype), (value), (index), \
   (data), (size), (timeout))
#define USB_BULK_MSG(dev, pipe, data, len, actual_length, timeout) \
   usb_bulk_msg((dev), (pipe), (data), (len), (actual_length), (timeout))
#define USB_BUFFER_ALLOC(dev, size, mem, dma)    usb_buffer_alloc(dev, size, mem, dma)
#define USB_BUFFER_FREE(dev, size, data, dma)    usb_buffer_free(dev, size, data, dma)
 
#ifdef WL_URB_ZPKT
#define URB_QUEUE_BULK   URB_ZERO_PACKET
#else
#define URB_QUEUE_BULK   0
#endif /* WL_URB_ZPKT */
 
#define CALLBACK_ARGS        struct urb *urb, struct pt_regs *regs
#define CALLBACK_ARGS_DATA    urb, regs
#define CONFIGDESC(usb)        (&((usb)->actconfig)->desc)
#define IFPTR(usb, idx)        ((usb)->actconfig->interface[idx])
#define IFALTS(usb, idx)    (IFPTR((usb), (idx))->altsetting[0])
#define IFDESC(usb, idx)    IFALTS((usb), (idx)).desc
#define IFEPDESC(usb, idx, ep)    (IFALTS((usb), (idx)).endpoint[ep]).desc
 
#else /* KERNEL26 */
 
#define USB_ALLOC_URB()                usb_alloc_urb(0)
#define USB_UNLINK_URB(urb)            usb_unlink_urb(urb)
#define USB_FREE_URB(urb)            (usb_free_urb(urb))
#define USB_REGISTER()                usb_register(&dbus_usbdev)
#define USB_DEREGISTER()            usb_deregister(&dbus_usbdev)
#define USB_AUTOPM_GET_INTERFACE(intf)        do {} while (0)
#define USB_AUTOPM_GET_INTERFACE_ASYNC(intf)    do {} while (0)
#define USB_AUTOPM_PUT_INTERFACE_ASYNC(intf)    do {} while (0)
#define USB_MARK_LAST_BUSY(dev)            do {} while (0)
 
#define USB_CONTROL_MSG(dev, pipe, request, requesttype, value, index, data, size, timeout) \
   usb_control_msg((dev), (pipe), (request), (requesttype), (value), (index), \
   (data), (size), (timeout))
#define USB_BUFFER_ALLOC(dev, size, mem, dma)  kmalloc(size, mem)
#define USB_BUFFER_FREE(dev, size, data, dma)  kfree(data)
 
#ifdef WL_URB_ZPKT
#define URB_QUEUE_BULK   USB_QUEUE_BULK|URB_ZERO_PACKET
#else
#define URB_QUEUE_BULK   0
#endif /*  WL_URB_ZPKT */
 
#define CALLBACK_ARGS        struct urb *urb
#define CALLBACK_ARGS_DATA    urb
#define CONFIGDESC(usb)        ((usb)->actconfig)
#define IFPTR(usb, idx)        (&(usb)->actconfig->interface[idx])
#define IFALTS(usb, idx)    ((usb)->actconfig->interface[idx].altsetting[0])
#define IFDESC(usb, idx)    IFALTS((usb), (idx))
#define IFEPDESC(usb, idx, ep)    (IFALTS((usb), (idx)).endpoint[ep])
 
 
#endif /* KERNEL26 */
 
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31))
#define USB_SPEED_SUPER        5
#endif  /* #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)) */
 
#define CONTROL_IF   0
#define BULK_IF      0
 
#ifdef BCMUSBDEV_COMPOSITE
#define USB_COMPIF_MAX       4
 
#define USB_CLASS_WIRELESS    0xe0
#define USB_CLASS_MISC        0xef
#define USB_SUBCLASS_COMMON    0x02
#define USB_PROTO_IAD        0x01
#define USB_PROTO_VENDOR    0xff
 
#define USB_QUIRK_NO_SET_INTF   0x04 /* device does not support set_interface */
#endif /* BCMUSBDEV_COMPOSITE */
 
#define USB_SYNC_WAIT_TIMEOUT  300  /* ms */
 
/* Private data kept in skb */
#define SKB_PRIV(skb, idx)  (&((void **)skb->cb)[idx])
#define SKB_PRIV_URB(skb)   (*(struct urb **)SKB_PRIV(skb, 0))
 
#ifndef DBUS_USB_RXQUEUE_BATCH_ADD
/* items to add each time within limit */
#define DBUS_USB_RXQUEUE_BATCH_ADD            8
#endif
 
#ifndef DBUS_USB_RXQUEUE_LOWER_WATERMARK
/* add a new batch req to rx queue when waiting item count reduce to this number */
#define DBUS_USB_RXQUEUE_LOWER_WATERMARK      4
#endif
 
enum usbos_suspend_state {
   USBOS_SUSPEND_STATE_DEVICE_ACTIVE = 0, /* Device is busy, won't allow suspend */
   USBOS_SUSPEND_STATE_SUSPEND_PENDING,   /* Device is idle, can be suspended */
                                          /* Wating PM to suspend */
   USBOS_SUSPEND_STATE_SUSPENDED          /* Device suspended */
};
 
enum usbos_request_state {
   USBOS_REQUEST_STATE_UNSCHEDULED = 0,    /* USB TX request not scheduled */
   USBOS_REQUEST_STATE_SCHEDULED,        /* USB TX request given to TX thread */
   USBOS_REQUEST_STATE_SUBMITTED        /* USB TX request submitted */
};
 
typedef struct {
   uint32 notification;
   uint32 reserved;
} intr_t;
 
typedef struct {
   dbus_pub_t *pub;
 
   void *cbarg;
   dbus_intf_callbacks_t *cbs;
 
   /* Imported */
   struct usb_device *usb;    /* USB device pointer from OS */
   struct urb *intr_urb; /* URB for interrupt endpoint */
   struct list_head req_rxfreeq;
   struct list_head req_txfreeq;
   struct list_head req_rxpostedq;    /* Posted down to USB driver for RX */
   struct list_head req_txpostedq;    /* Posted down to USB driver for TX */
   spinlock_t rxfree_lock; /* Lock for rx free list */
   spinlock_t txfree_lock; /* Lock for tx free list */
   spinlock_t rxposted_lock; /* Lock for rx posted list */
   spinlock_t txposted_lock; /* Lock for tx posted list */
   uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2; /* Pipe numbers for USB I/O */
   uint rxbuf_len;
 
   struct list_head req_rxpendingq; /* RXDPC: Pending for dpc to send up */
   spinlock_t rxpending_lock;    /* RXDPC: Lock for rx pending list */
   long dpc_pid;
   struct semaphore dpc_sem;
   struct completion dpc_exited;
   int rxpending;
 
   struct urb               *ctl_urb;
   int                      ctl_in_pipe, ctl_out_pipe;
   struct usb_ctrlrequest   ctl_write;
   struct usb_ctrlrequest   ctl_read;
   struct semaphore         ctl_lock;     /* Lock for CTRL transfers via tx_thread */
#ifdef USBOS_TX_THREAD
   enum usbos_request_state ctl_state;
#endif /* USBOS_TX_THREAD */
 
   spinlock_t rxlock;      /* Lock for rxq management */
   spinlock_t txlock;      /* Lock for txq management */
 
   int intr_size;          /* Size of interrupt message */
   int interval;           /* Interrupt polling interval */
   intr_t intr;            /* Data buffer for interrupt endpoint */
 
   int maxps;
   atomic_t txposted;
   atomic_t rxposted;
   atomic_t txallocated;
   atomic_t rxallocated;
   bool rxctl_deferrespok;    /* Get a response for setup from dongle */
 
   wait_queue_head_t wait;
   bool waitdone;
   int sync_urb_status;
 
   struct urb *blk_urb; /* Used for downloading embedded image */
 
#ifdef USBOS_THREAD
   spinlock_t              ctrl_lock;
   spinlock_t              usbos_list_lock;
   struct list_head        usbos_list;
   struct list_head        usbos_free_list;
   atomic_t                usbos_list_cnt;
   wait_queue_head_t       usbos_queue_head;
   struct task_struct      *usbos_kt;
#endif /* USBOS_THREAD */
 
#ifdef USBOS_TX_THREAD
   spinlock_t              usbos_tx_list_lock;
   struct list_head    usbos_tx_list;
   wait_queue_head_t    usbos_tx_queue_head;
   struct task_struct      *usbos_tx_kt;
#endif /* USBOS_TX_THREAD */
 
   struct dma_pool *qtd_pool; /* QTD pool for USB optimization only */
   int tx_ep, rx_ep, rx2_ep;  /* EPs for USB optimization */
   struct usb_device *usb_device; /* USB device for optimization */
} usbos_info_t;
 
typedef struct urb_req {
   void         *pkt;
   int          buf_len;
   struct urb   *urb;
   void         *arg;
   usbos_info_t *usbinfo;
   struct list_head urb_list;
} urb_req_t;
 
#ifdef USBOS_THREAD
typedef struct usbos_list_entry {
   struct list_head    list;   /* must be first */
   void               *urb_context;
   int                 urb_length;
   int                 urb_status;
} usbos_list_entry_t;
 
static void* dbus_usbos_thread_init(usbos_info_t *usbos_info);
static void  dbus_usbos_thread_deinit(usbos_info_t *usbos_info);
static void  dbus_usbos_dispatch_schedule(CALLBACK_ARGS);
static int   dbus_usbos_thread_func(void *data);
#endif /* USBOS_THREAD */
 
#ifdef USBOS_TX_THREAD
void* dbus_usbos_tx_thread_init(usbos_info_t *usbos_info);
void  dbus_usbos_tx_thread_deinit(usbos_info_t *usbos_info);
int   dbus_usbos_tx_thread_func(void *data);
#endif /* USBOS_TX_THREAD */
 
/* Shared Function prototypes */
bool dbus_usbos_dl_cmd(usbos_info_t *usbinfo, uint8 cmd, void *buffer, int buflen);
int dbus_usbos_wait(usbos_info_t *usbinfo, uint16 ms);
bool dbus_usbos_dl_send_bulk(usbos_info_t *usbinfo, void *buffer, int len);
int dbus_write_membytes(usbos_info_t *usbinfo, bool set, uint32 address, uint8 *data, uint size);
 
/* Local function prototypes */
static void dbus_usbos_send_complete(CALLBACK_ARGS);
static void dbus_usbos_recv_complete(CALLBACK_ARGS);
static int  dbus_usbos_errhandler(void *bus, int err);
static int  dbus_usbos_state_change(void *bus, int state);
static void dbusos_stop(usbos_info_t *usbos_info);
 
#ifdef KERNEL26
static int dbus_usbos_probe(struct usb_interface *intf, const struct usb_device_id *id);
static void dbus_usbos_disconnect(struct usb_interface *intf);
#if defined(USB_SUSPEND_AVAILABLE)
static int dbus_usbos_resume(struct usb_interface *intf);
static int dbus_usbos_suspend(struct usb_interface *intf, pm_message_t message);
/* at the moment, used for full dongle host driver only */
static int dbus_usbos_reset_resume(struct usb_interface *intf);
#endif /* USB_SUSPEND_AVAILABLE */
#else /* KERNEL26 */
static void *dbus_usbos_probe(struct usb_device *usb, unsigned int ifnum,
   const struct usb_device_id *id);
static void dbus_usbos_disconnect(struct usb_device *usb, void *ptr);
#endif /* KERNEL26 */
 
 
/**
 * have to disable missing-field-initializers warning as last element {} triggers it
 * and different versions of kernel have different number of members so it is impossible
 * to specify the initializer. BTW issuing the warning here is bug og GCC as  universal
 * zero {0} specified in C99 standard as correct way of initialization of struct to all zeros
 */
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == \
   4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
 
static struct usb_device_id devid_table[] = {
   { USB_DEVICE(BCM_DNGL_VID, 0x0000) }, /* Configurable via register() */
#if defined(BCM_REQUEST_FW)
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_4328) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_4322) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_4319) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_43236) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_43143) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_43242) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_4360) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_4350) },
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BL_PID_43569) },
#endif
#ifdef EXTENDED_VID_PID
   EXTENDED_VID_PID,
#endif /* EXTENDED_VID_PID */
   { USB_DEVICE(BCM_DNGL_VID, BCM_DNGL_BDC_PID) }, /* Default BDC */
   { }
};
 
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == \
   4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop
#endif
 
MODULE_DEVICE_TABLE(usb, devid_table);
 
/** functions called by the Linux kernel USB subsystem */
static struct usb_driver dbus_usbdev = {
   name:           "dbus_usbdev",
   probe:          dbus_usbos_probe,
   disconnect:     dbus_usbos_disconnect,
   id_table:       devid_table,
#if defined(USB_SUSPEND_AVAILABLE)
   suspend:        dbus_usbos_suspend,
   resume:         dbus_usbos_resume,
   reset_resume:    dbus_usbos_reset_resume,
   /* Linux USB core will allow autosuspend for devices bound to this driver */
   supports_autosuspend: 1
#endif /* USB_SUSPEND_AVAILABLE */
};
 
/**
 * This stores USB info during Linux probe callback since attach() is not called yet at this point
 */
typedef struct {
   void    *usbos_info;
   struct usb_device *usb; /* USB device pointer from OS */
   uint    rx_pipe;   /* Pipe numbers for USB I/O */
   uint    tx_pipe;   /* Pipe numbers for USB I/O */
   uint    intr_pipe; /* Pipe numbers for USB I/O */
   uint    rx_pipe2;  /* Pipe numbers for USB I/O */
   int     intr_size; /* Size of interrupt message */
   int     interval;  /* Interrupt polling interval */
   bool    dldone;
   int     vid;
   int     pid;
   bool    dereged;
   bool    disc_cb_done;
   DEVICE_SPEED    device_speed;
   enum usbos_suspend_state suspend_state;
   struct usb_interface     *intf;
} probe_info_t;
 
/*
 * USB Linux dbus_intf_t
 */
static void *dbus_usbos_intf_attach(dbus_pub_t *pub, void *cbarg, dbus_intf_callbacks_t *cbs);
static void dbus_usbos_intf_detach(dbus_pub_t *pub, void *info);
static int  dbus_usbos_intf_send_irb(void *bus, dbus_irb_tx_t *txirb);
static int  dbus_usbos_intf_recv_irb(void *bus, dbus_irb_rx_t *rxirb);
static int  dbus_usbos_intf_recv_irb_from_ep(void *bus, dbus_irb_rx_t *rxirb, uint32 ep_idx);
static int  dbus_usbos_intf_cancel_irb(void *bus, dbus_irb_tx_t *txirb);
static int  dbus_usbos_intf_send_ctl(void *bus, uint8 *buf, int len);
static int  dbus_usbos_intf_recv_ctl(void *bus, uint8 *buf, int len);
static int  dbus_usbos_intf_get_attrib(void *bus, dbus_attrib_t *attrib);
static int  dbus_usbos_intf_up(void *bus);
static int  dbus_usbos_intf_down(void *bus);
static int  dbus_usbos_intf_stop(void *bus);
static int  dbus_usbos_readreg(void *bus, uint32 regaddr, int datalen, uint32 *value);
extern int dbus_usbos_loopback_tx(void *usbos_info_ptr, int cnt, int size);
int dbus_usbos_writereg(void *bus, uint32 regaddr, int datalen, uint32 data);
static int  dbus_usbos_intf_set_config(void *bus, dbus_config_t *config);
static bool dbus_usbos_intf_recv_needed(void *bus);
static void *dbus_usbos_intf_exec_rxlock(void *bus, exec_cb_t cb, struct exec_parms *args);
static void *dbus_usbos_intf_exec_txlock(void *bus, exec_cb_t cb, struct exec_parms *args);
#ifdef BCMUSBDEV_COMPOSITE
static int dbus_usbos_intf_wlan(struct usb_device *usb);
#endif /* BCMUSBDEV_COMPOSITE */
 
/** functions called by dbus_usb.c */
static dbus_intf_t dbus_usbos_intf = {
   .attach = dbus_usbos_intf_attach,
   .detach = dbus_usbos_intf_detach,
   .up = dbus_usbos_intf_up,
   .down = dbus_usbos_intf_down,
   .send_irb = dbus_usbos_intf_send_irb,
   .recv_irb = dbus_usbos_intf_recv_irb,
   .cancel_irb = dbus_usbos_intf_cancel_irb,
   .send_ctl = dbus_usbos_intf_send_ctl,
   .recv_ctl = dbus_usbos_intf_recv_ctl,
   .get_stats = NULL,
   .get_attrib = dbus_usbos_intf_get_attrib,
   .remove = NULL,
   .resume = NULL,
   .suspend = NULL,
   .stop = dbus_usbos_intf_stop,
   .reset = NULL,
   .pktget = NULL,
   .pktfree = NULL,
   .iovar_op = NULL,
   .dump = NULL,
   .set_config = dbus_usbos_intf_set_config,
   .get_config = NULL,
   .device_exists = NULL,
   .dlneeded = NULL,
   .dlstart = NULL,
   .dlrun = NULL,
   .recv_needed = dbus_usbos_intf_recv_needed,
   .exec_rxlock = dbus_usbos_intf_exec_rxlock,
   .exec_txlock = dbus_usbos_intf_exec_txlock,
 
   .tx_timer_init = NULL,
   .tx_timer_start = NULL,
   .tx_timer_stop = NULL,
 
   .sched_dpc = NULL,
   .lock = NULL,
   .unlock = NULL,
   .sched_probe_cb = NULL,
 
   .shutdown = NULL,
 
   .recv_stop = NULL,
   .recv_resume = NULL,
 
   .recv_irb_from_ep = dbus_usbos_intf_recv_irb_from_ep,
   .readreg = dbus_usbos_readreg
};
 
static probe_info_t    g_probe_info;
static probe_cb_t      probe_cb = NULL;
static disconnect_cb_t disconnect_cb = NULL;
static void            *probe_arg = NULL;
static void            *disc_arg = NULL;
 
 
 
static volatile int loopback_rx_cnt, loopback_tx_cnt;
int loopback_size;
bool is_loopback_pkt(void *buf);
int matches_loopback_pkt(void *buf);
 
/**
 * multiple code paths in this file dequeue a URB request, this function makes sure that it happens
 * in a concurrency save manner. Don't call this from a sleepable process context.
 */
static urb_req_t * BCMFASTPATH
dbus_usbos_qdeq(struct list_head *urbreq_q, spinlock_t *lock)
{
   unsigned long flags;
   urb_req_t *req;
 
   ASSERT(urbreq_q != NULL);
 
   spin_lock_irqsave(lock, flags);
 
   if (list_empty(urbreq_q)) {
       req = NULL;
   } else {
       ASSERT(urbreq_q->next != NULL);
       ASSERT(urbreq_q->next != urbreq_q);
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
       req = list_entry(urbreq_q->next, urb_req_t, urb_list);
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
       list_del_init(&req->urb_list);
   }
 
   spin_unlock_irqrestore(lock, flags);
 
   return req;
}
 
static void BCMFASTPATH
dbus_usbos_qenq(struct list_head *urbreq_q, urb_req_t *req, spinlock_t *lock)
{
   unsigned long flags;
 
   spin_lock_irqsave(lock, flags);
 
   list_add_tail(&req->urb_list, urbreq_q);
 
   spin_unlock_irqrestore(lock, flags);
}
 
/**
 * multiple code paths in this file remove a URB request from a list, this function makes sure that
 * it happens in a concurrency save manner. Don't call this from a sleepable process context.
 * Is quite similar to dbus_usbos_qdeq(), I wonder why this function is needed.
 */
static void
dbus_usbos_req_del(urb_req_t *req, spinlock_t *lock)
{
   unsigned long flags;
 
   spin_lock_irqsave(lock, flags);
 
   list_del_init(&req->urb_list);
 
   spin_unlock_irqrestore(lock, flags);
}
 
 
/**
 * Driver requires a pool of URBs to operate. This function is called during
 * initialization (attach phase), allocates a number of URBs, and puts them
 * on the free (req_rxfreeq and req_txfreeq) queue
 */
static int
dbus_usbos_urbreqs_alloc(usbos_info_t *usbos_info, uint32 count, bool is_rx)
{
   int i;
   int allocated = 0;
   int err = DBUS_OK;
 
   for (i = 0; i < count; i++) {
       urb_req_t *req;
 
       req = MALLOC(usbos_info->pub->osh, sizeof(urb_req_t));
       if (req == NULL) {
           DBUSERR(("%s: MALLOC req failed\n", __FUNCTION__));
           err = DBUS_ERR_NOMEM;
           goto fail;
       }
       bzero(req, sizeof(urb_req_t));
 
       req->urb = USB_ALLOC_URB();
       if (req->urb == NULL) {
           DBUSERR(("%s: USB_ALLOC_URB req->urb failed\n", __FUNCTION__));
           err = DBUS_ERR_NOMEM;
           goto fail;
       }
 
       INIT_LIST_HEAD(&req->urb_list);
 
       if (is_rx) {
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
           /* don't allocate now. Do it on demand */
           req->pkt = NULL;
#else
           /* pre-allocate  buffers never to be released */
           req->pkt = MALLOC(usbos_info->pub->osh, usbos_info->rxbuf_len);
           if (req->pkt == NULL) {
               DBUSERR(("%s: MALLOC req->pkt failed\n", __FUNCTION__));
               err = DBUS_ERR_NOMEM;
               goto fail;
           }
#endif
           req->buf_len = usbos_info->rxbuf_len;
           dbus_usbos_qenq(&usbos_info->req_rxfreeq, req, &usbos_info->rxfree_lock);
       } else {
           req->buf_len = 0;
           dbus_usbos_qenq(&usbos_info->req_txfreeq, req, &usbos_info->txfree_lock);
       }
       allocated++;
       continue;
 
fail:
       if (req) {
           if (is_rx && req->pkt) {
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
               /* req->pkt is NULL in "NOCOPY" mode */
#else
               MFREE(usbos_info->pub->osh, req->pkt, req->buf_len);
#endif
           }
           if (req->urb) {
               USB_FREE_URB(req->urb);
           }
           MFREE(usbos_info->pub->osh, req, sizeof(urb_req_t));
       }
       break;
   }
 
   atomic_add(allocated, is_rx ? &usbos_info->rxallocated : &usbos_info->txallocated);
 
   if (is_rx) {
       DBUSTRACE(("%s: add %d (total %d) rx buf, each has %d bytes\n", __FUNCTION__,
           allocated, atomic_read(&usbos_info->rxallocated), usbos_info->rxbuf_len));
   } else {
       DBUSTRACE(("%s: add %d (total %d) tx req\n", __FUNCTION__,
           allocated, atomic_read(&usbos_info->txallocated)));
   }
 
   return err;
} /* dbus_usbos_urbreqs_alloc */
 
/** Typically called during detach or when attach failed. Don't call until all URBs unlinked */
static int
dbus_usbos_urbreqs_free(usbos_info_t *usbos_info, bool is_rx)
{
   int rtn = 0;
   urb_req_t *req;
   struct list_head *req_q;
   spinlock_t *lock;
 
   if (is_rx) {
       req_q = &usbos_info->req_rxfreeq;
       lock = &usbos_info->rxfree_lock;
   } else {
       req_q = &usbos_info->req_txfreeq;
       lock = &usbos_info->txfree_lock;
   }
   while ((req = dbus_usbos_qdeq(req_q, lock)) != NULL) {
 
       if (is_rx) {
           if (req->pkt) {
               /* We do MFREE instead of PKTFREE because the pkt has been
                * converted to native already
                */
               MFREE(usbos_info->pub->osh, req->pkt, req->buf_len);
               req->pkt = NULL;
               req->buf_len = 0;
           }
       } else {
           /* sending req should not be assigned pkt buffer */
           ASSERT(req->pkt == NULL);
       }
 
       if (req->urb) {
           USB_FREE_URB(req->urb);
           req->urb = NULL;
       }
       MFREE(usbos_info->pub->osh, req, sizeof(urb_req_t));
 
       rtn++;
   }
   return rtn;
} /* dbus_usbos_urbreqs_free */
 
/**
 * called by Linux kernel on URB completion. Upper DBUS layer (dbus_usb.c) has to be notified of
 * send completion.
 */
void
dbus_usbos_send_complete(CALLBACK_ARGS)
{
   urb_req_t *req = urb->context;
   dbus_irb_tx_t *txirb = req->arg;
   usbos_info_t *usbos_info = req->usbinfo;
   unsigned long flags;
   int status = DBUS_OK;
   int txposted;
 
   USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
 
   spin_lock_irqsave(&usbos_info->txlock, flags);
 
   dbus_usbos_req_del(req, &usbos_info->txposted_lock);
   txposted = atomic_dec_return(&usbos_info->txposted);
   if (unlikely (txposted < 0)) {
       DBUSERR(("%s ERROR: txposted is negative (%d)!!\n", __FUNCTION__, txposted));
   }
   spin_unlock_irqrestore(&usbos_info->txlock, flags);
 
   if (unlikely (urb->status)) {
       status = DBUS_ERR_TXFAIL;
       DBUSTRACE(("txfail status %d\n", urb->status));
   }
 
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
   /* sending req should not be assigned pkt buffer */
   ASSERT(req->pkt == NULL);
#endif
   /*  txirb should always be set, except for ZLP. ZLP is reusing this callback function. */
   if (txirb != NULL) {
       if (txirb->send_buf != NULL) {
           MFREE(usbos_info->pub->osh, txirb->send_buf, req->buf_len);
           txirb->send_buf = NULL;
           req->buf_len = 0;
       }
       if (likely (usbos_info->cbarg && usbos_info->cbs)) {
           if (likely (usbos_info->cbs->send_irb_complete != NULL))
               usbos_info->cbs->send_irb_complete(usbos_info->cbarg, txirb, status);
       }
   }
 
   dbus_usbos_qenq(&usbos_info->req_txfreeq, req, &usbos_info->txfree_lock);
} /* dbus_usbos_send_complete */
 
/**
 * In order to receive USB traffic from the dongle, we need to supply the Linux kernel with a free
 * URB that is going to contain received data.
 */
static int BCMFASTPATH
dbus_usbos_recv_urb_submit(usbos_info_t *usbos_info, dbus_irb_rx_t *rxirb, uint32 ep_idx)
{
   urb_req_t *req;
   int ret = DBUS_OK;
   unsigned long flags;
   void *p;
   uint rx_pipe;
   int rxposted;
 
   BCM_REFERENCE(rxposted);
 
   if (!(req = dbus_usbos_qdeq(&usbos_info->req_rxfreeq, &usbos_info->rxfree_lock))) {
       DBUSTRACE(("%s No free URB!\n", __FUNCTION__));
       return DBUS_ERR_RXDROP;
   }
 
   spin_lock_irqsave(&usbos_info->rxlock, flags);
 
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
   req->pkt = rxirb->pkt = PKTGET(usbos_info->pub->osh, req->buf_len, FALSE);
   if (!rxirb->pkt) {
       DBUSERR(("%s: PKTGET failed\n", __FUNCTION__));
       dbus_usbos_qenq(&usbos_info->req_rxfreeq, req, &usbos_info->rxfree_lock);
       ret = DBUS_ERR_RXDROP;
       goto fail;
   }
   /* consider the packet "native" so we don't count it as MALLOCED in the osl */
   PKTTONATIVE(usbos_info->pub->osh, req->pkt);
   rxirb->buf = NULL;
   p = PKTDATA(usbos_info->pub->osh, req->pkt);
#else
   if (req->buf_len != usbos_info->rxbuf_len) {
       ASSERT(req->pkt);
       MFREE(usbos_info->pub->osh, req->pkt, req->buf_len);
       DBUSTRACE(("%s: replace rx buff: old len %d, new len %d\n", __FUNCTION__,
           req->buf_len, usbos_info->rxbuf_len));
       req->buf_len = 0;
       req->pkt = MALLOC(usbos_info->pub->osh, usbos_info->rxbuf_len);
       if (req->pkt == NULL) {
           DBUSERR(("%s: MALLOC req->pkt failed\n", __FUNCTION__));
           ret = DBUS_ERR_NOMEM;
           goto fail;
       }
       req->buf_len = usbos_info->rxbuf_len;
   }
   rxirb->buf = req->pkt;
   p = rxirb->buf;
#endif /* defined(BCM_RPC_NOCOPY) */
   rxirb->buf_len = req->buf_len;
   req->usbinfo = usbos_info;
   req->arg = rxirb;
   if (ep_idx == 0) {
       rx_pipe = usbos_info->rx_pipe;
   } else {
       rx_pipe = usbos_info->rx_pipe2;
       ASSERT(usbos_info->rx_pipe2);
   }
   /* Prepare the URB */
   usb_fill_bulk_urb(req->urb, usbos_info->usb, rx_pipe,
       p,
       rxirb->buf_len,
       (usb_complete_t)dbus_usbos_recv_complete, req);
       req->urb->transfer_flags |= URB_QUEUE_BULK;
 
   if ((ret = USB_SUBMIT_URB(req->urb))) {
       DBUSERR(("%s USB_SUBMIT_URB failed. status %d\n", __FUNCTION__, ret));
       dbus_usbos_qenq(&usbos_info->req_rxfreeq, req, &usbos_info->rxfree_lock);
       ret = DBUS_ERR_RXFAIL;
       goto fail;
   }
   rxposted = atomic_inc_return(&usbos_info->rxposted);
 
   dbus_usbos_qenq(&usbos_info->req_rxpostedq, req, &usbos_info->rxposted_lock);
fail:
   spin_unlock_irqrestore(&usbos_info->rxlock, flags);
   return ret;
} /* dbus_usbos_recv_urb_submit */
 
 
/**
 * Called by worked thread when a 'receive URB' completed or Linux kernel when it returns a URB to
 * this driver.
 */
static void BCMFASTPATH
dbus_usbos_recv_complete_handle(urb_req_t *req, int len, int status)
{
   dbus_irb_rx_t *rxirb = req->arg;
   usbos_info_t *usbos_info = req->usbinfo;
   unsigned long flags;
   int rxallocated, rxposted;
   int dbus_status = DBUS_OK;
   bool killed = (g_probe_info.suspend_state == USBOS_SUSPEND_STATE_SUSPEND_PENDING) ? 1 : 0;
 
   spin_lock_irqsave(&usbos_info->rxlock, flags);
   dbus_usbos_req_del(req, &usbos_info->rxposted_lock);
   rxposted = atomic_dec_return(&usbos_info->rxposted);
   rxallocated = atomic_read(&usbos_info->rxallocated);
   spin_unlock_irqrestore(&usbos_info->rxlock, flags);
 
   if ((rxallocated < usbos_info->pub->nrxq) && (!status) &&
       (rxposted == DBUS_USB_RXQUEUE_LOWER_WATERMARK)) {
           DBUSTRACE(("%s: need more rx buf: rxallocated %d rxposted %d!\n",
               __FUNCTION__, rxallocated, rxposted));
           dbus_usbos_urbreqs_alloc(usbos_info,
               MIN(DBUS_USB_RXQUEUE_BATCH_ADD,
               usbos_info->pub->nrxq - rxallocated), TRUE);
   }
 
   /* Handle errors */
   if (status) {
       /*
        * Linux 2.4 disconnect: -ENOENT or -EILSEQ for CRC error; rmmod: -ENOENT
        * Linux 2.6 disconnect: -EPROTO, rmmod: -ESHUTDOWN
        */
       if ((status == -ENOENT && (!killed))|| status == -ESHUTDOWN) {
           /* NOTE: unlink() can not be called from URB callback().
            * Do not call dbusos_stop() here.
            */
           DBUSTRACE(("%s rx error %d\n", __FUNCTION__, status));
           dbus_usbos_state_change(usbos_info, DBUS_STATE_DOWN);
       } else if (status == -EPROTO) {
           DBUSTRACE(("%s rx error %d\n", __FUNCTION__, status));
       } else if (killed && (status == -EHOSTUNREACH || status == -ENOENT)) {
           /* Device is suspended */
       } else {
           DBUSTRACE(("%s rx error %d\n", __FUNCTION__, status));
           dbus_usbos_errhandler(usbos_info, DBUS_ERR_RXFAIL);
       }
 
       /* On error, don't submit more URBs yet */
       rxirb->buf = NULL;
       rxirb->actual_len = 0;
       dbus_status = DBUS_ERR_RXFAIL;
       goto fail;
   }
 
   /* Make the skb represent the received urb */
   rxirb->actual_len = len;
 
   if (rxirb->actual_len < sizeof(uint32)) {
       DBUSTRACE(("small pkt len %d, process as ZLP\n", rxirb->actual_len));
       dbus_status = DBUS_ERR_RXZLP;
   }
 
fail:
#if defined(BCM_RPC_NOCOPY) || defined(BCM_RPC_RXNOCOPY)
   /* detach the packet from the queue */
   req->pkt = NULL;
#endif /* BCM_RPC_NOCOPY || BCM_RPC_RXNOCOPY */
 
   if (usbos_info->cbarg && usbos_info->cbs) {
       if (usbos_info->cbs->recv_irb_complete) {
           usbos_info->cbs->recv_irb_complete(usbos_info->cbarg, rxirb, dbus_status);
       }
   }
 
   dbus_usbos_qenq(&usbos_info->req_rxfreeq, req, &usbos_info->rxfree_lock);
 
   /* Mark the interface as busy to reset USB autosuspend timer */
   USB_MARK_LAST_BUSY(usbos_info->usb);
} /* dbus_usbos_recv_complete_handle */
 
/** called by Linux kernel when it returns a URB to this driver */
static void
dbus_usbos_recv_complete(CALLBACK_ARGS)
{
#ifdef USBOS_THREAD
   dbus_usbos_dispatch_schedule(CALLBACK_ARGS_DATA);
#else /*  !USBOS_THREAD */
   dbus_usbos_recv_complete_handle(urb->context, urb->actual_length, urb->status);
#endif /*  USBOS_THREAD */
}
 
 
/**
 * If Linux notifies our driver that a control read or write URB has completed, we should notify
 * the DBUS layer above us (dbus_usb.c in this case).
 */
static void
dbus_usbos_ctl_complete(usbos_info_t *usbos_info, int type, int urbstatus)
{
   int status = DBUS_ERR;
 
   if (usbos_info == NULL)
       return;
 
   switch (urbstatus) {
       case 0:
           status = DBUS_OK;
       break;
       case -EINPROGRESS:
       case -ENOENT:
       default:
#ifdef INTR_EP_ENABLE
           DBUSERR(("%s:%d fail status %d bus:%d susp:%d intr:%d ctli:%d ctlo:%d\n",
               __FUNCTION__, type, urbstatus,
               usbos_info->pub->busstate, g_probe_info.suspend_state,
               usbos_info->intr_urb_submitted, usbos_info->ctlin_urb_submitted,
               usbos_info->ctlout_urb_submitted));
#else
           DBUSERR(("%s: failed with status %d\n", __FUNCTION__, urbstatus));
           status = DBUS_ERR;
       break;
#endif /* INTR_EP_ENABLE */
   }
 
   if (usbos_info->cbarg && usbos_info->cbs) {
       if (usbos_info->cbs->ctl_complete)
           usbos_info->cbs->ctl_complete(usbos_info->cbarg, type, status);
   }
}
 
/** called by Linux */
static void
dbus_usbos_ctlread_complete(CALLBACK_ARGS)
{
   usbos_info_t *usbos_info = (usbos_info_t *)urb->context;
 
   ASSERT(urb);
   usbos_info = (usbos_info_t *)urb->context;
 
   dbus_usbos_ctl_complete(usbos_info, DBUS_CBCTL_READ, urb->status);
 
#ifdef USBOS_THREAD
   if (usbos_info->rxctl_deferrespok) {
       usbos_info->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_CLASS |
       USB_RECIP_INTERFACE;
       usbos_info->ctl_read.bRequest = 1;
   }
#endif
 
   up(&usbos_info->ctl_lock);
 
   USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
}
 
/** called by Linux */
static void
dbus_usbos_ctlwrite_complete(CALLBACK_ARGS)
{
   usbos_info_t *usbos_info = (usbos_info_t *)urb->context;
 
   ASSERT(urb);
   usbos_info = (usbos_info_t *)urb->context;
 
   dbus_usbos_ctl_complete(usbos_info, DBUS_CBCTL_WRITE, urb->status);
 
#ifdef USBOS_TX_THREAD
   usbos_info->ctl_state = USBOS_REQUEST_STATE_UNSCHEDULED;
#endif /* USBOS_TX_THREAD */
 
   up(&usbos_info->ctl_lock);
 
   USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
}
 
#ifdef INTR_EP_ENABLE
/** called by Linux */
static void
dbus_usbos_intr_complete(CALLBACK_ARGS)
{
   usbos_info_t *usbos_info = (usbos_info_t *)urb->context;
   bool killed = (g_probe_info.suspend_state == USBOS_SUSPEND_STATE_SUSPEND_PENDING) ? 1 : 0;
 
   if (usbos_info == NULL || usbos_info->pub == NULL)
       return;
   if ((urb->status == -ENOENT && (!killed)) || urb->status == -ESHUTDOWN ||
       urb->status == -ENODEV) {
       dbus_usbos_state_change(usbos_info, DBUS_STATE_DOWN);
   }
 
   if (usbos_info->pub->busstate == DBUS_STATE_DOWN) {
       DBUSERR(("%s: intr cb when DBUS down, ignoring\n", __FUNCTION__));
       return;
   }
   dbus_usbos_ctl_complete(usbos_info, DBUS_CBINTR_POLL, urb->status);
}
#endif    /* INTR_EP_ENABLE */
 
/**
 * when the bus is going to sleep or halt, the Linux kernel requires us to take ownership of our
 * URBs again. Multiple code paths in this file require a list of URBs to be cancelled in a
 * concurrency save manner.
 */
static void
dbus_usbos_unlink(struct list_head *urbreq_q, spinlock_t *lock)
{
   urb_req_t *req;
 
   /* dbus_usbos_recv_complete() adds req back to req_freeq */
   while ((req = dbus_usbos_qdeq(urbreq_q, lock)) != NULL) {
       ASSERT(req->urb != NULL);
       USB_UNLINK_URB(req->urb);
   }
}
 
/** multiple code paths in this file require the bus to stop */
static void
dbus_usbos_cancel_all_urbs(usbos_info_t *usbos_info)
{
   int rxposted, txposted;
 
   DBUSTRACE(("%s: unlink all URBs\n", __FUNCTION__));
 
#ifdef USBOS_TX_THREAD
   usbos_info->ctl_state = USBOS_REQUEST_STATE_UNSCHEDULED;
 
   /* Yield the CPU to TX thread so all pending requests are submitted */
   while (!list_empty(&usbos_info->usbos_tx_list)) {
       wake_up_interruptible(&usbos_info->usbos_tx_queue_head);
       OSL_SLEEP(10);
   }
#endif /* USBOS_TX_THREAD */
 
   /* tell Linux kernel to cancel a single intr, ctl and blk URB */
   if (usbos_info->intr_urb)
       USB_UNLINK_URB(usbos_info->intr_urb);
   if (usbos_info->ctl_urb)
       USB_UNLINK_URB(usbos_info->ctl_urb);
   if (usbos_info->blk_urb)
       USB_UNLINK_URB(usbos_info->blk_urb);
 
   dbus_usbos_unlink(&usbos_info->req_txpostedq, &usbos_info->txposted_lock);
   dbus_usbos_unlink(&usbos_info->req_rxpostedq, &usbos_info->rxposted_lock);
 
   /* Wait until the callbacks for all submitted URBs have been called, because the
    * handler needs to know is an USB suspend is in progress.
    */
   SPINWAIT((atomic_read(&usbos_info->txposted) != 0 ||
       atomic_read(&usbos_info->rxposted) != 0), 10000);
 
   txposted = atomic_read(&usbos_info->txposted);
   rxposted = atomic_read(&usbos_info->rxposted);
   if (txposted != 0 || rxposted != 0) {
       DBUSERR(("%s ERROR: REQs posted, rx=%d tx=%d!\n",
           __FUNCTION__, rxposted, txposted));
   }
} /* dbus_usbos_cancel_all_urbs */
 
/** multiple code paths require the bus to stop */
static void
dbusos_stop(usbos_info_t *usbos_info)
{
   urb_req_t *req;
   int rxposted;
   req = NULL;
   BCM_REFERENCE(req);
 
   ASSERT(usbos_info);
 
   dbus_usbos_state_change(usbos_info, DBUS_STATE_DOWN);
 
   dbus_usbos_cancel_all_urbs(usbos_info);
 
#ifdef USBOS_THREAD
   /* yield the CPU to rx packet thread */
   while (1) {
       if (atomic_read(&usbos_info->usbos_list_cnt) <= 0)    break;
       wake_up_interruptible(&usbos_info->usbos_queue_head);
       OSL_SLEEP(3);
   }
#endif /* USBOS_THREAD */
 
   rxposted = atomic_read(&usbos_info->rxposted);
   if (rxposted > 0) {
       DBUSERR(("%s ERROR: rx REQs posted=%d in stop!\n", __FUNCTION__,
           rxposted));
   }
 
   ASSERT(atomic_read(&usbos_info->txposted) == 0 && rxposted == 0);
 
} /* dbusos_stop */
 
#if defined(USB_SUSPEND_AVAILABLE)
 
/**
 * Linux kernel sports a 'USB auto suspend' feature. See: http://lwn.net/Articles/373550/
 * The suspend method is called by the Linux kernel to warn the driver that the device is going to
 * be suspended.  If the driver returns a negative error code, the suspend will be aborted. If the
 * driver returns 0, it must cancel all outstanding URBs (usb_kill_urb()) and not submit any more.
 */
static int
dbus_usbos_suspend(struct usb_interface *intf,
            pm_message_t message)
{
   DBUSERR(("%s suspend state: %d\n", __FUNCTION__, g_probe_info.suspend_state));
   /* DHD for full dongle model */
   g_probe_info.suspend_state = USBOS_SUSPEND_STATE_SUSPEND_PENDING;
   dbus_usbos_state_change((usbos_info_t*)g_probe_info.usbos_info, DBUS_STATE_SLEEP);
   dbus_usbos_cancel_all_urbs((usbos_info_t*)g_probe_info.usbos_info);
   g_probe_info.suspend_state = USBOS_SUSPEND_STATE_SUSPENDED;
 
   return 0;
}
 
/**
 * The resume method is called to tell the driver that the device has been resumed and the driver
 * can return to normal operation.  URBs may once more be submitted.
 */
static int dbus_usbos_resume(struct usb_interface *intf)
{
   DBUSERR(("%s Device resumed\n", __FUNCTION__));
 
   dbus_usbos_state_change((usbos_info_t*)g_probe_info.usbos_info, DBUS_STATE_UP);
   g_probe_info.suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE;
   return 0;
}
 
/**
* This function is directly called by the Linux kernel, when the suspended device has been reset
* instead of being resumed
*/
static int dbus_usbos_reset_resume(struct usb_interface *intf)
{
   DBUSERR(("%s Device reset resumed\n", __FUNCTION__));
 
   /* The device may have lost power, so a firmware download may be required */
   dbus_usbos_state_change((usbos_info_t*)g_probe_info.usbos_info, DBUS_STATE_DL_NEEDED);
   g_probe_info.suspend_state = USBOS_SUSPEND_STATE_DEVICE_ACTIVE;
   return 0;
}
 
#endif /* USB_SUSPEND_AVAILABLE */
 
/**
 * Called by Linux kernel at initialization time, kernel wants to know if our driver will accept the
 * caller supplied USB interface. Note that USB drivers are bound to interfaces, and not to USB
 * devices.
 */
#ifdef KERNEL26
#define DBUS_USBOS_PROBE() static int dbus_usbos_probe(struct usb_interface *intf, const struct usb_device_id *id)
#define DBUS_USBOS_DISCONNECT() static void dbus_usbos_disconnect(struct usb_interface *intf)
#else
#define DBUS_USBOS_PROBE() static void * dbus_usbos_probe(struct usb_device *usb, unsigned int ifnum, const struct usb_device_id *id)
#define DBUS_USBOS_DISCONNECT() static void dbus_usbos_disconnect(struct usb_device *usb, void *ptr)
#endif /* KERNEL26 */
 
DBUS_USBOS_PROBE()
{
   int ep;
   struct usb_endpoint_descriptor *endpoint;
   int ret = 0;
#ifdef KERNEL26
   struct usb_device *usb = interface_to_usbdev(intf);
#else
   int claimed = 0;
#endif
   int num_of_eps;
#ifdef BCMUSBDEV_COMPOSITE
   int wlan_if = -1;
   bool intr_ep = FALSE;
#endif /* BCMUSBDEV_COMPOSITE */
   wifi_adapter_info_t *adapter;
 
   DHD_MUTEX_LOCK();
 
   DBUSERR(("%s: bus num(busnum)=%d, slot num (portnum)=%d\n", __FUNCTION__,
       usb->bus->busnum, usb->portnum));
   adapter = dhd_wifi_platform_attach_adapter(USB_BUS, usb->bus->busnum,
       usb->portnum, WIFI_STATUS_POWER_ON);
   if (adapter == NULL) {
       DBUSERR(("%s: can't find adapter info for this chip\n", __FUNCTION__));
       goto fail;
   }
 
#ifdef BCMUSBDEV_COMPOSITE
   wlan_if = dbus_usbos_intf_wlan(usb);
#ifdef KERNEL26
   if ((wlan_if >= 0) && (IFPTR(usb, wlan_if) == intf))
#else
   if (wlan_if == ifnum)
#endif /* KERNEL26 */
   {
#endif /* BCMUSBDEV_COMPOSITE */
       g_probe_info.usb = usb;
       g_probe_info.dldone = TRUE;
#ifdef BCMUSBDEV_COMPOSITE
   } else {
       DBUSTRACE(("dbus_usbos_probe: skip probe for non WLAN interface\n"));
       ret = BCME_UNSUPPORTED;
       goto fail;
   }
#endif /* BCMUSBDEV_COMPOSITE */
 
#ifdef KERNEL26
   g_probe_info.intf = intf;
#endif /* KERNEL26 */
 
#ifdef BCMUSBDEV_COMPOSITE
   if (IFDESC(usb, wlan_if).bInterfaceNumber > USB_COMPIF_MAX)
#else
   if (IFDESC(usb, CONTROL_IF).bInterfaceNumber)
#endif /* BCMUSBDEV_COMPOSITE */
   {
       ret = -1;
       goto fail;
   }
   if (id != NULL) {
       g_probe_info.vid = id->idVendor;
       g_probe_info.pid = id->idProduct;
   }
 
#ifdef KERNEL26
   usb_set_intfdata(intf, &g_probe_info);
#endif
 
   /* Check that the device supports only one configuration */
   if (usb->descriptor.bNumConfigurations != 1) {
       ret = -1;
       goto fail;
   }
 
   if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
#ifdef BCMUSBDEV_COMPOSITE
       if ((usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
           (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS)) {
#endif /* BCMUSBDEV_COMPOSITE */
           ret = -1;
           goto fail;
#ifdef BCMUSBDEV_COMPOSITE
       }
#endif /* BCMUSBDEV_COMPOSITE */
   }
 
   /*
    * Only the BDC interface configuration is supported:
    *    Device class: USB_CLASS_VENDOR_SPEC
    *    if0 class: USB_CLASS_VENDOR_SPEC
    *    if0/ep0: control
    *    if0/ep1: bulk in
    *    if0/ep2: bulk out (ok if swapped with bulk in)
    */
   if (CONFIGDESC(usb)->bNumInterfaces != 1) {
#ifdef BCMUSBDEV_COMPOSITE
       if (CONFIGDESC(usb)->bNumInterfaces > USB_COMPIF_MAX) {
#endif /* BCMUSBDEV_COMPOSITE */
           ret = -1;
           goto fail;
#ifdef BCMUSBDEV_COMPOSITE
       }
#endif /* BCMUSBDEV_COMPOSITE */
   }
 
   /* Check interface */
#ifndef KERNEL26
#ifdef BCMUSBDEV_COMPOSITE
   if (usb_interface_claimed(IFPTR(usb, wlan_if)))
#else
   if (usb_interface_claimed(IFPTR(usb, CONTROL_IF)))
#endif /* BCMUSBDEV_COMPOSITE */
   {
       ret = -1;
       goto fail;
   }
#endif /* !KERNEL26 */
 
#ifdef BCMUSBDEV_COMPOSITE
   if ((IFDESC(usb, wlan_if).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
       IFDESC(usb, wlan_if).bInterfaceSubClass != 2 ||
       IFDESC(usb, wlan_if).bInterfaceProtocol != 0xff) &&
       (IFDESC(usb, wlan_if).bInterfaceClass != USB_CLASS_MISC ||
       IFDESC(usb, wlan_if).bInterfaceSubClass != USB_SUBCLASS_COMMON ||
       IFDESC(usb, wlan_if).bInterfaceProtocol != USB_PROTO_IAD))
#else
   if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
       IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
       IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff)
#endif /* BCMUSBDEV_COMPOSITE */
   {
#ifdef BCMUSBDEV_COMPOSITE
           DBUSERR(("%s: invalid control interface: class %d, subclass %d, proto %d\n",
               __FUNCTION__,
               IFDESC(usb, wlan_if).bInterfaceClass,
               IFDESC(usb, wlan_if).bInterfaceSubClass,
               IFDESC(usb, wlan_if).bInterfaceProtocol));
#else
           DBUSERR(("%s: invalid control interface: class %d, subclass %d, proto %d\n",
               __FUNCTION__,
               IFDESC(usb, CONTROL_IF).bInterfaceClass,
               IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
               IFDESC(usb, CONTROL_IF).bInterfaceProtocol));
#endif /* BCMUSBDEV_COMPOSITE */
           ret = -1;
           goto fail;
   }
 
   /* Check control endpoint */
#ifdef BCMUSBDEV_COMPOSITE
   endpoint = &IFEPDESC(usb, wlan_if, 0);
#else
   endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
#endif /* BCMUSBDEV_COMPOSITE */
   if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
#ifdef BCMUSBDEV_COMPOSITE
       if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
           USB_ENDPOINT_XFER_BULK) {
#endif /* BCMUSBDEV_COMPOSITE */
           DBUSERR(("%s: invalid control endpoint %d\n",
               __FUNCTION__, endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK));
           ret = -1;
           goto fail;
#ifdef BCMUSBDEV_COMPOSITE
       }
#endif /* BCMUSBDEV_COMPOSITE */
   }
 
#ifdef BCMUSBDEV_COMPOSITE
   if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
#endif /* BCMUSBDEV_COMPOSITE */
       g_probe_info.intr_pipe =
           usb_rcvintpipe(usb, endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
#ifdef BCMUSBDEV_COMPOSITE
       intr_ep = TRUE;
   }
#endif /* BCMUSBDEV_COMPOSITE */
 
#ifndef KERNEL26
   /* Claim interface */
#ifdef BCMUSBDEV_COMPOSITE
   usb_driver_claim_interface(&dbus_usbdev, IFPTR(usb, wlan_if), &g_probe_info);
#else
   usb_driver_claim_interface(&dbus_usbdev, IFPTR(usb, CONTROL_IF), &g_probe_info);
#endif /* BCMUSBDEV_COMPOSITE */
   claimed = 1;
#endif /* !KERNEL26 */
   g_probe_info.rx_pipe = 0;
   g_probe_info.rx_pipe2 = 0;
   g_probe_info.tx_pipe = 0;
#ifdef BCMUSBDEV_COMPOSITE
   if (intr_ep)
       ep = 1;
   else
       ep = 0;
   num_of_eps = IFDESC(usb, wlan_if).bNumEndpoints - 1;
#else
   num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
#endif /* BCMUSBDEV_COMPOSITE */
 
   if ((num_of_eps != 2) && (num_of_eps != 3)) {
#ifdef BCMUSBDEV_COMPOSITE
       if (num_of_eps > 7)
#endif /* BCMUSBDEV_COMPOSITE */
           ASSERT(0);
   }
   /* Check data endpoints and get pipes */
#ifdef BCMUSBDEV_COMPOSITE
   for (; ep <= num_of_eps; ep++)
#else
   for (ep = 1; ep <= num_of_eps; ep++)
#endif /* BCMUSBDEV_COMPOSITE */
   {
#ifdef BCMUSBDEV_COMPOSITE
       endpoint = &IFEPDESC(usb, wlan_if, ep);
#else
       endpoint = &IFEPDESC(usb, BULK_IF, ep);
#endif /* BCMUSBDEV_COMPOSITE */
       if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
           USB_ENDPOINT_XFER_BULK) {
           DBUSERR(("%s: invalid data endpoint %d\n",
                      __FUNCTION__, ep));
           ret = -1;
           goto fail;
       }
 
       if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
           /* direction: dongle->host */
           if (!g_probe_info.rx_pipe) {
               g_probe_info.rx_pipe = usb_rcvbulkpipe(usb,
                   (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK));
           } else {
               g_probe_info.rx_pipe2 = usb_rcvbulkpipe(usb,
                   (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK));
           }
 
       } else
           g_probe_info.tx_pipe = usb_sndbulkpipe(usb, (endpoint->bEndpointAddress &
                USB_ENDPOINT_NUMBER_MASK));
   }
 
   /* Allocate interrupt URB and data buffer */
   /* RNDIS says 8-byte intr, our old drivers used 4-byte */
#ifdef BCMUSBDEV_COMPOSITE
   g_probe_info.intr_size = (IFEPDESC(usb, wlan_if, 0).wMaxPacketSize == 16) ? 8 : 4;
   g_probe_info.interval = IFEPDESC(usb, wlan_if, 0).bInterval;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21))
   usb->quirks |= USB_QUIRK_NO_SET_INTF;
#endif
#else
   g_probe_info.intr_size = (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == 16) ? 8 : 4;
   g_probe_info.interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
#endif /* BCMUSBDEV_COMPOSITE */
 
#ifndef KERNEL26
   /* usb_fill_int_urb does the interval decoding in 2.6 */
   if (usb->speed == USB_SPEED_HIGH)
       g_probe_info.interval = 1 << (g_probe_info.interval - 1);
#endif
   if (usb->speed == USB_SPEED_SUPER) {
       g_probe_info.device_speed = SUPER_SPEED;
       DBUSERR(("super speed device detected\n"));
   } else if (usb->speed == USB_SPEED_HIGH) {
       g_probe_info.device_speed = HIGH_SPEED;
       DBUSERR(("high speed device detected\n"));
   } else {
       g_probe_info.device_speed = FULL_SPEED;
       DBUSERR(("full speed device detected\n"));
   }
   if (g_probe_info.dereged == FALSE && probe_cb) {
       disc_arg = probe_cb(probe_arg, "", USB_BUS, usb->bus->busnum, usb->portnum, 0);
   }
 
   g_probe_info.disc_cb_done = FALSE;
 
#ifdef KERNEL26
   intf->needs_remote_wakeup = 1;
#endif /* KERNEL26 */
   DHD_MUTEX_UNLOCK();
 
   /* Success */
#ifdef KERNEL26
   return DBUS_OK;
#else
   usb_inc_dev_use(usb);
   return &g_probe_info;
#endif
 
fail:
   printf("%s: Exit ret=%d\n", __FUNCTION__, ret);
#ifdef BCMUSBDEV_COMPOSITE
   if (ret != BCME_UNSUPPORTED)
#endif /* BCMUSBDEV_COMPOSITE */
       DBUSERR(("%s: failed with errno %d\n", __FUNCTION__, ret));
#ifndef KERNEL26
   if (claimed)
#ifdef BCMUSBDEV_COMPOSITE
       usb_driver_release_interface(&dbus_usbdev, IFPTR(usb, wlan_if));
#else
       usb_driver_release_interface(&dbus_usbdev, IFPTR(usb, CONTROL_IF));
#endif /* BCMUSBDEV_COMPOSITE */
#endif /* !KERNEL26 */
 
   DHD_MUTEX_UNLOCK();
#ifdef KERNEL26
   usb_set_intfdata(intf, NULL);
   return ret;
#else
   return NULL;
#endif
} /* dbus_usbos_probe */
 
/** Called by Linux kernel, is the counter part of dbus_usbos_probe() */
DBUS_USBOS_DISCONNECT()
{
#ifdef KERNEL26
   struct usb_device *usb = interface_to_usbdev(intf);
   probe_info_t *probe_usb_init_data = usb_get_intfdata(intf);
#else
   probe_info_t *probe_usb_init_data = (probe_info_t *) ptr;
#endif
   usbos_info_t *usbos_info;
 
   DHD_MUTEX_LOCK();
 
   DBUSERR(("%s: bus num(busnum)=%d, slot num (portnum)=%d\n", __FUNCTION__,
       usb->bus->busnum, usb->portnum));
 
   if (probe_usb_init_data) {
       usbos_info = (usbos_info_t *) probe_usb_init_data->usbos_info;
       if (usbos_info) {
           if ((probe_usb_init_data->dereged == FALSE) && disconnect_cb && disc_arg) {
               disconnect_cb(disc_arg);
               disc_arg = NULL;
               probe_usb_init_data->disc_cb_done = TRUE;
           }
       }
   }
 
   if (usb) {
#ifndef KERNEL26
#ifdef BCMUSBDEV_COMPOSITE
       usb_driver_release_interface(&dbus_usbdev, IFPTR(usb, wlan_if));
#else
       usb_driver_release_interface(&dbus_usbdev, IFPTR(usb, CONTROL_IF));
#endif /* BCMUSBDEV_COMPOSITE */
       usb_dec_dev_use(usb);
#endif /* !KERNEL26 */
   }
   DHD_MUTEX_UNLOCK();
} /* dbus_usbos_disconnect */
 
#define LOOPBACK_PKT_START 0xBABE1234
 
bool is_loopback_pkt(void *buf)
{
 
   uint32 *buf_ptr = (uint32 *) buf;
 
   if (*buf_ptr == LOOPBACK_PKT_START)
       return TRUE;
   return FALSE;
 
}
 
int matches_loopback_pkt(void *buf)
{
   int i, j;
   unsigned char *cbuf = (unsigned char *) buf;
 
   for (i = 4; i < loopback_size; i++) {
       if (cbuf[i] != (i % 256)) {
           printf("%s: mismatch at i=%d %d : ", __FUNCTION__, i, cbuf[i]);
           for (j = i; ((j < i+ 16) && (j < loopback_size)); j++) {
               printf("%d ", cbuf[j]);
           }
           printf("\n");
           return 0;
       }
   }
   loopback_rx_cnt++;
   return 1;
}
 
int dbus_usbos_loopback_tx(void *usbos_info_ptr, int cnt, int size)
{
   usbos_info_t *usbos_info = (usbos_info_t *) usbos_info_ptr;
   unsigned char *buf;
   int j;
   void* p = NULL;
   int rc, last_rx_cnt;
   int tx_failed_cnt;
   int max_size = 1650;
   int usb_packet_size = 512;
   int min_packet_size = 10;
 
   if (size % usb_packet_size == 0) {
       size = size - 1;
       DBUSERR(("%s: overriding size=%d \n", __FUNCTION__, size));
   }
 
   if (size < min_packet_size) {
       size = min_packet_size;
       DBUSERR(("%s: overriding size=%d\n", __FUNCTION__, min_packet_size));
   }
   if (size > max_size) {
       size = max_size;
       DBUSERR(("%s: overriding size=%d\n", __FUNCTION__, max_size));
   }
 
   loopback_tx_cnt = 0;
   loopback_rx_cnt = 0;
   tx_failed_cnt = 0;
   loopback_size   = size;
 
   while (loopback_tx_cnt < cnt) {
       uint32 *x;
       int pkt_size = loopback_size;
 
       p = PKTGET(usbos_info->pub->osh, pkt_size, TRUE);
       if (p == NULL) {
           DBUSERR(("%s:%d Failed to allocate packet sz=%d\n",
                  __FUNCTION__, __LINE__, pkt_size));
           return BCME_ERROR;
       }
       x = (uint32*) PKTDATA(usbos_info->pub->osh, p);
       *x = LOOPBACK_PKT_START;
       buf = (unsigned char*) x;
       for (j = 4; j < pkt_size; j++) {
           buf[j] = j % 256;
       }
       rc = dbus_send_buf(usbos_info->pub, buf, pkt_size, p);
       if (rc != BCME_OK) {
           DBUSERR(("%s:%d Freeing packet \n", __FUNCTION__, __LINE__));
           PKTFREE(usbos_info->pub->osh, p, TRUE);
           dbus_usbos_wait(usbos_info, 1);
           tx_failed_cnt++;
       } else {
           loopback_tx_cnt++;
           tx_failed_cnt = 0;
       }
       if (tx_failed_cnt == 5) {
           DBUSERR(("%s : Failed to send loopback packets cnt=%d loopback_tx_cnt=%d\n",
            __FUNCTION__, cnt, loopback_tx_cnt));
           break;
       }
   }
   printf("Transmitted %d loopback packets of size %d\n", loopback_tx_cnt, loopback_size);
 
   last_rx_cnt = loopback_rx_cnt;
   while (loopback_rx_cnt < loopback_tx_cnt) {
       dbus_usbos_wait(usbos_info, 1);
       if (loopback_rx_cnt <= last_rx_cnt) {
           DBUSERR(("%s: Matched rx cnt stuck at %d \n", __FUNCTION__, last_rx_cnt));
           return BCME_ERROR;
       }
       last_rx_cnt = loopback_rx_cnt;
   }
   printf("Received %d loopback packets of size %d\n", loopback_tx_cnt, loopback_size);
 
   return BCME_OK;
} /* dbus_usbos_loopback_tx */
 
/**
 * Higher layer (dbus_usb.c) wants to transmit an I/O Request Block
 *     @param[in] txirb txirb->pkt, if non-zero, contains a single or a chain of packets
 */
static int
dbus_usbos_intf_send_irb(void *bus, dbus_irb_tx_t *txirb)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   urb_req_t *req, *req_zlp = NULL;
   int ret = DBUS_OK;
   unsigned long flags;
   void *pkt;
   uint32 buffer_length;
   uint8 *buf;
 
   if ((usbos_info == NULL) || !usbos_info->tx_pipe) {
       return DBUS_ERR;
   }
 
   if (txirb->pkt != NULL) {
       buffer_length = pkttotlen(usbos_info->pub->osh, txirb->pkt);
       /* In case of multiple packets the values below may be overwritten */
       txirb->send_buf = NULL;
       buf = PKTDATA(usbos_info->pub->osh, txirb->pkt);
   } else { /* txirb->buf != NULL */
       ASSERT(txirb->buf != NULL);
       ASSERT(txirb->send_buf == NULL);
       buffer_length = txirb->len;
       buf = txirb->buf;
   }
 
   if (!(req = dbus_usbos_qdeq(&usbos_info->req_txfreeq, &usbos_info->txfree_lock))) {
       DBUSERR(("%s No free URB!\n", __FUNCTION__));
       return DBUS_ERR_TXDROP;
   }
 
   /* If not using standard Linux kernel functionality for handling Zero Length Packet(ZLP),
    * the dbus needs to generate ZLP when length is multiple of MaxPacketSize.
    */
#ifndef WL_URB_ZPKT
   if (!(buffer_length % usbos_info->maxps)) {
       if (!(req_zlp =
           dbus_usbos_qdeq(&usbos_info->req_txfreeq, &usbos_info->txfree_lock))) {
           DBUSERR(("%s No free URB for ZLP!\n", __FUNCTION__));
           dbus_usbos_qenq(&usbos_info->req_txfreeq, req, &usbos_info->txfree_lock);
           return DBUS_ERR_TXDROP;
       }
 
       /* No txirb, so that dbus_usbos_send_complete can differentiate between
        * DATA and ZLP.
        */
       req_zlp->arg = NULL;
       req_zlp->usbinfo = usbos_info;
       req_zlp->buf_len = 0;
 
       usb_fill_bulk_urb(req_zlp->urb, usbos_info->usb, usbos_info->tx_pipe, NULL,
           0, (usb_complete_t)dbus_usbos_send_complete, req_zlp);
 
       req_zlp->urb->transfer_flags |= URB_QUEUE_BULK;
   }
#endif /* !WL_URB_ZPKT */
 
#ifndef USBOS_TX_THREAD
   /* Disable USB autosuspend until this request completes, request USB resume if needed.
    * Because this call runs asynchronously, there is no guarantee the bus is resumed before
    * the URB is submitted, and the URB might be dropped. Use USBOS_TX_THREAD to avoid
    * this.
    */
   USB_AUTOPM_GET_INTERFACE_ASYNC(g_probe_info.intf);
#endif /* !USBOS_TX_THREAD */
 
   spin_lock_irqsave(&usbos_info->txlock, flags);
 
   req->arg = txirb;
   req->usbinfo = usbos_info;
   req->buf_len = 0;
 
   /* Prepare the URB */
   if (txirb->pkt != NULL) {
       uint32 pktlen;
       uint8 *transfer_buf;
 
       /* For multiple packets, allocate contiguous buffer and copy packet data to it */
       if (PKTNEXT(usbos_info->pub->osh, txirb->pkt)) {
           transfer_buf = MALLOC(usbos_info->pub->osh, buffer_length);
           if (!transfer_buf) {
               ret = DBUS_ERR_TXDROP;
               DBUSERR(("fail to alloc to usb buffer\n"));
               goto fail;
           }
 
           pkt = txirb->pkt;
           txirb->send_buf = transfer_buf;
           req->buf_len = buffer_length;
 
           while (pkt) {
               pktlen = PKTLEN(usbos_info->pub->osh, pkt);
               bcopy(PKTDATA(usbos_info->pub->osh, pkt), transfer_buf, pktlen);
               transfer_buf += pktlen;
               pkt = PKTNEXT(usbos_info->pub->osh, pkt);
           }
 
           ASSERT(((uint8 *) txirb->send_buf + buffer_length) == transfer_buf);
 
           /* Overwrite buf pointer with pointer to allocated contiguous transfer_buf
            */
           buf = txirb->send_buf;
       }
   }
 
   usb_fill_bulk_urb(req->urb, usbos_info->usb, usbos_info->tx_pipe, buf,
       buffer_length, (usb_complete_t)dbus_usbos_send_complete, req);
 
   req->urb->transfer_flags |= URB_QUEUE_BULK;
 
#ifdef USBOS_TX_THREAD
   /* Enqueue TX request, the TX thread will resume the bus if needed and submit
    * it asynchronously
    */
   dbus_usbos_qenq(&usbos_info->usbos_tx_list, req, &usbos_info->usbos_tx_list_lock);
   if (req_zlp != NULL) {
       dbus_usbos_qenq(&usbos_info->usbos_tx_list, req_zlp,
           &usbos_info->usbos_tx_list_lock);
   }
   spin_unlock_irqrestore(&usbos_info->txlock, flags);
 
   wake_up_interruptible(&usbos_info->usbos_tx_queue_head);
   return DBUS_OK;
#else
   if ((ret = USB_SUBMIT_URB(req->urb))) {
       ret = DBUS_ERR_TXDROP;
       goto fail;
   }
 
   dbus_usbos_qenq(&usbos_info->req_txpostedq, req, &usbos_info->txposted_lock);
   atomic_inc(&usbos_info->txposted);
 
   if (req_zlp != NULL) {
       if ((ret = USB_SUBMIT_URB(req_zlp->urb))) {
           DBUSERR(("failed to submit ZLP URB!\n"));
           ASSERT(0);
           ret = DBUS_ERR_TXDROP;
           goto fail2;
       }
 
       dbus_usbos_qenq(&usbos_info->req_txpostedq, req_zlp, &usbos_info->txposted_lock);
       /* Also increment txposted for zlp packet, as it will be decremented in
        * dbus_usbos_send_complete()
        */
       atomic_inc(&usbos_info->txposted);
   }
 
   spin_unlock_irqrestore(&usbos_info->txlock, flags);
   return DBUS_OK;
#endif /* USBOS_TX_THREAD */
 
fail:
   if (txirb->send_buf != NULL) {
       MFREE(usbos_info->pub->osh, txirb->send_buf, req->buf_len);
       txirb->send_buf = NULL;
       req->buf_len = 0;
   }
   dbus_usbos_qenq(&usbos_info->req_txfreeq, req, &usbos_info->txfree_lock);
#ifndef USBOS_TX_THREAD
fail2:
#endif
   if (req_zlp != NULL) {
       dbus_usbos_qenq(&usbos_info->req_txfreeq, req_zlp, &usbos_info->txfree_lock);
   }
 
   spin_unlock_irqrestore(&usbos_info->txlock, flags);
 
#ifndef USBOS_TX_THREAD
   USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
#endif /* !USBOS_TX_THREAD */
 
   return ret;
} /* dbus_usbos_intf_send_irb */
 
/** Higher layer (dbus_usb.c) recycles a received (and used) packet. */
static int
dbus_usbos_intf_recv_irb(void *bus, dbus_irb_rx_t *rxirb)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   int ret = DBUS_OK;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   ret = dbus_usbos_recv_urb_submit(usbos_info, rxirb, 0);
   return ret;
}
 
static int
dbus_usbos_intf_recv_irb_from_ep(void *bus, dbus_irb_rx_t *rxirb, uint32 ep_idx)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   int ret = DBUS_OK;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
#ifdef INTR_EP_ENABLE
       /* By specifying the ep_idx value of 0xff, the cdc layer is asking to
       * submit an interrupt URB
       */
       if (rxirb == NULL && ep_idx == 0xff) {
           /* submit intr URB */
           if ((ret = USB_SUBMIT_URB(usbos_info->intr_urb)) < 0) {
               DBUSERR(("%s intr USB_SUBMIT_URB failed, status %d\n",
                   __FUNCTION__, ret));
           }
           return ret;
       }
#else
       if (rxirb == NULL) {
           return DBUS_ERR;
       }
#endif /* INTR_EP_ENABLE */
 
   ret = dbus_usbos_recv_urb_submit(usbos_info, rxirb, ep_idx);
   return ret;
}
 
/** Higher layer (dbus_usb.c) want to cancel an IRB */
static int
dbus_usbos_intf_cancel_irb(void *bus, dbus_irb_tx_t *txirb)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   return DBUS_ERR;
}
 
/** Only one CTL transfer can be pending at any time. This function may block. */
static int
dbus_usbos_intf_send_ctl(void *bus, uint8 *buf, int len)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   uint16 size;
#ifndef USBOS_TX_THREAD
   int status;
#endif /* USBOS_TX_THREAD */
 
   if ((usbos_info == NULL) || (buf == NULL) || (len == 0))
       return DBUS_ERR;
 
   if (usbos_info->ctl_urb == NULL)
       return DBUS_ERR;
 
   /* Block until a pending CTL transfer has completed */
   if (down_interruptible(&usbos_info->ctl_lock) != 0) {
       return DBUS_ERR_TXCTLFAIL;
   }
 
#ifdef USBOS_TX_THREAD
   ASSERT(usbos_info->ctl_state == USBOS_REQUEST_STATE_UNSCHEDULED);
#else
   /* Disable USB autosuspend until this request completes, request USB resume if needed.
    * Because this call runs asynchronously, there is no guarantee the bus is resumed before
    * the URB is submitted, and the URB might be dropped. Use USBOS_TX_THREAD to avoid
    * this.
    */
   USB_AUTOPM_GET_INTERFACE_ASYNC(g_probe_info.intf);
#endif /* USBOS_TX_THREAD */
 
   size = len;
   usbos_info->ctl_write.wLength = cpu_to_le16p(&size);
   usbos_info->ctl_urb->transfer_buffer_length = size;
 
   usb_fill_control_urb(usbos_info->ctl_urb,
       usbos_info->usb,
       usb_sndctrlpipe(usbos_info->usb, 0),
       (unsigned char *) &usbos_info->ctl_write,
       buf, size, (usb_complete_t)dbus_usbos_ctlwrite_complete, usbos_info);
 
#ifdef USBOS_TX_THREAD
   /* Enqueue CTRL request for transmission by the TX thread. The
    * USB bus will first be resumed if needed.
    */
   usbos_info->ctl_state = USBOS_REQUEST_STATE_SCHEDULED;
   wake_up_interruptible(&usbos_info->usbos_tx_queue_head);
#else
   status = USB_SUBMIT_URB(usbos_info->ctl_urb);
   if (status < 0) {
       DBUSERR(("%s: usb_submit_urb failed %d\n", __FUNCTION__, status));
       up(&usbos_info->ctl_lock);
 
       USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
 
       return DBUS_ERR_TXCTLFAIL;
   }
#endif /* USBOS_TX_THREAD */
 
   return DBUS_OK;
} /* dbus_usbos_intf_send_ctl */
 
/** This function does not seem to be called by anyone, including dbus_usb.c */
static int
dbus_usbos_intf_recv_ctl(void *bus, uint8 *buf, int len)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   int status;
   uint16 size;
 
   if ((usbos_info == NULL) || (buf == NULL) || (len == 0))
       return DBUS_ERR;
 
   if (usbos_info->ctl_urb == NULL)
       return DBUS_ERR;
 
   /* Block until a pending CTRL transfer has completed */
   if (down_interruptible(&usbos_info->ctl_lock) != 0) {
       return DBUS_ERR_TXCTLFAIL;
   }
 
   /* Disable USB autosuspend until this request completes, request USB resume if needed. */
   USB_AUTOPM_GET_INTERFACE_ASYNC(g_probe_info.intf);
 
   size = len;
   usbos_info->ctl_read.wLength = cpu_to_le16p(&size);
   usbos_info->ctl_urb->transfer_buffer_length = size;
 
   if (usbos_info->rxctl_deferrespok) {
       /* BMAC model */
       usbos_info->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
           USB_RECIP_INTERFACE;
       usbos_info->ctl_read.bRequest = DL_DEFER_RESP_OK;
   } else {
       /* full dongle model */
       usbos_info->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_CLASS |
           USB_RECIP_INTERFACE;
       usbos_info->ctl_read.bRequest = 1;
   }
 
   usb_fill_control_urb(usbos_info->ctl_urb,
       usbos_info->usb,
       usb_rcvctrlpipe(usbos_info->usb, 0),
       (unsigned char *) &usbos_info->ctl_read,
       buf, size, (usb_complete_t)dbus_usbos_ctlread_complete, usbos_info);
 
   status = USB_SUBMIT_URB(usbos_info->ctl_urb);
   if (status < 0) {
       DBUSERR(("%s: usb_submit_urb failed %d\n", __FUNCTION__, status));
       up(&usbos_info->ctl_lock);
 
       USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
 
       return DBUS_ERR_RXCTLFAIL;
   }
 
   return DBUS_OK;
}
 
static int
dbus_usbos_intf_get_attrib(void *bus, dbus_attrib_t *attrib)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if ((usbos_info == NULL) || (attrib == NULL))
       return DBUS_ERR;
 
   attrib->bustype = DBUS_USB;
   attrib->vid = g_probe_info.vid;
   attrib->pid = g_probe_info.pid;
   attrib->devid = 0x4322;
 
   attrib->nchan = 1;
 
   /* MaxPacketSize for USB hi-speed bulk out is 512 bytes
    * and 64-bytes for full-speed.
    * When sending pkt > MaxPacketSize, Host SW breaks it
    * up into multiple packets.
    */
   attrib->mtu = usbos_info->maxps;
 
   return DBUS_OK;
}
 
/** Called by higher layer (dbus_usb.c) when it wants to 'up' the USB interface to the dongle */
static int
dbus_usbos_intf_up(void *bus)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   uint16 ifnum;
#ifdef BCMUSBDEV_COMPOSITE
   int wlan_if = 0;
#endif
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   if (usbos_info->usb == NULL)
       return DBUS_ERR;
 
#if defined(INTR_EP_ENABLE)
   /* full dongle use intr EP, bmac doesn't use it */
   if (usbos_info->intr_urb) {
       int ret;
 
       usb_fill_int_urb(usbos_info->intr_urb, usbos_info->usb,
           usbos_info->intr_pipe, &usbos_info->intr,
           usbos_info->intr_size, (usb_complete_t)dbus_usbos_intr_complete,
           usbos_info, usbos_info->interval);
 
       if ((ret = USB_SUBMIT_URB(usbos_info->intr_urb))) {
           DBUSERR(("%s USB_SUBMIT_URB failed with status %d\n", __FUNCTION__, ret));
           return DBUS_ERR;
       }
   }
#endif    
 
   if (usbos_info->ctl_urb) {
       usbos_info->ctl_in_pipe = usb_rcvctrlpipe(usbos_info->usb, 0);
       usbos_info->ctl_out_pipe = usb_sndctrlpipe(usbos_info->usb, 0);
 
#ifdef BCMUSBDEV_COMPOSITE
       wlan_if = dbus_usbos_intf_wlan(usbos_info->usb);
       ifnum = cpu_to_le16(IFDESC(usbos_info->usb, wlan_if).bInterfaceNumber);
#else
       ifnum = cpu_to_le16(IFDESC(usbos_info->usb, CONTROL_IF).bInterfaceNumber);
#endif /* BCMUSBDEV_COMPOSITE */
       /* CTL Write */
       usbos_info->ctl_write.bRequestType =
           USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
       usbos_info->ctl_write.bRequest = 0;
       usbos_info->ctl_write.wValue = cpu_to_le16(0);
       usbos_info->ctl_write.wIndex = cpu_to_le16p(&ifnum);
 
       /* CTL Read */
       usbos_info->ctl_read.bRequestType =
           USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
       usbos_info->ctl_read.bRequest = 1;
       usbos_info->ctl_read.wValue = cpu_to_le16(0);
       usbos_info->ctl_read.wIndex = cpu_to_le16p(&ifnum);
   }
 
   /* Success, indicate usbos_info is fully up */
   dbus_usbos_state_change(usbos_info, DBUS_STATE_UP);
 
   return DBUS_OK;
} /* dbus_usbos_intf_up */
 
static int
dbus_usbos_intf_down(void *bus)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   dbusos_stop(usbos_info);
   return DBUS_OK;
}
 
static int
dbus_usbos_intf_stop(void *bus)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   dbusos_stop(usbos_info);
   return DBUS_OK;
}
 
 
/** Called by higher layer (dbus_usb.c) */
static int
dbus_usbos_intf_set_config(void *bus, dbus_config_t *config)
{
   int err = DBUS_ERR;
   usbos_info_t* usbos_info = bus;
 
   if (config->config_id == DBUS_CONFIG_ID_RXCTL_DEFERRES) {
       usbos_info->rxctl_deferrespok = config->rxctl_deferrespok;
       err = DBUS_OK;
   } else if (config->config_id == DBUS_CONFIG_ID_AGGR_LIMIT) {
       /* DBUS_CONFIG_ID_AGGR_LIMIT shouldn't be called after probe stage */
       ASSERT(disc_arg == NULL);
       ASSERT(config->aggr_param.maxrxsf > 0);
       ASSERT(config->aggr_param.maxrxsize > 0);
       if (config->aggr_param.maxrxsize > usbos_info->rxbuf_len) {
           int state = usbos_info->pub->busstate;
           dbus_usbos_unlink(&usbos_info->req_rxpostedq, &usbos_info->rxposted_lock);
           while (atomic_read(&usbos_info->rxposted)) {
               DBUSTRACE(("%s rxposted is %d, delay 1 ms\n", __FUNCTION__,
                   atomic_read(&usbos_info->rxposted)));
               dbus_usbos_wait(usbos_info, 1);
           }
           usbos_info->rxbuf_len = config->aggr_param.maxrxsize;
           dbus_usbos_state_change(usbos_info, state);
       }
       err = DBUS_OK;
   }
 
   return err;
}
 
 
/** Called by dbus_usb.c when it wants to download firmware into the dongle */
bool
dbus_usbos_dl_cmd(usbos_info_t *usbinfo, uint8 cmd, void *buffer, int buflen)
{
   int transferred;
   int index = 0;
   char *tmpbuf;
 
   if ((usbinfo == NULL) || (buffer == NULL) || (buflen == 0))
       return FALSE;
 
   tmpbuf = (char *) MALLOC(usbinfo->pub->osh, buflen);
   if (!tmpbuf) {
       DBUSERR(("%s: Unable to allocate memory \n", __FUNCTION__));
       return FALSE;
   }
 
#ifdef BCM_REQUEST_FW
   if (cmd == DL_GO) {
       index = 1;
   }
#endif
 
   /* Disable USB autosuspend until this request completes, request USB resume if needed. */
   USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
   transferred = USB_CONTROL_MSG(usbinfo->usb, usb_rcvctrlpipe(usbinfo->usb, 0),
       cmd, (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE),
       0, index,
       (void*) tmpbuf, buflen, USB_CTRL_EP_TIMEOUT);
   if (transferred == buflen) {
       memcpy(buffer, tmpbuf, buflen);
   } else {
       DBUSERR(("%s: usb_control_msg failed %d\n", __FUNCTION__, transferred));
   }
 
   USB_AUTOPM_PUT_INTERFACE(g_probe_info.intf);
 
   MFREE(usbinfo->pub->osh, tmpbuf, buflen);
   return (transferred == buflen);
}
 
/**
 * Called by dbus_usb.c when it wants to download a buffer into the dongle (e.g. as part of the
 * download process, when writing nvram variables).
 */
int
dbus_write_membytes(usbos_info_t* usbinfo, bool set, uint32 address, uint8 *data, uint size)
{
   hwacc_t hwacc;
   int write_bytes = 4;
   int status;
   int retval = 0;
 
   DBUSTRACE(("Enter:%s\n", __FUNCTION__));
 
   /* Read is not supported */
   if (set == 0) {
       DBUSERR(("Currently read is not supported!!\n"));
       return -1;
   }
 
   USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
   hwacc.cmd = DL_CMD_WRHW;
   hwacc.addr = address;
 
   DBUSTRACE(("Address:%x size:%d", hwacc.addr, size));
   do {
       if (size >= 4) {
           write_bytes = 4;
       } else if (size >= 2) {
           write_bytes = 2;
       } else {
           write_bytes = 1;
       }
 
       hwacc.len = write_bytes;
 
       while (size >= write_bytes) {
           hwacc.data = *((unsigned int*)data);
 
           status = USB_CONTROL_MSG(usbinfo->usb, usb_sndctrlpipe(usbinfo->usb, 0),
               DL_WRHW, (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE),
               1, 0, (char *)&hwacc, sizeof(hwacc_t), USB_CTRL_EP_TIMEOUT);
 
           if (status < 0) {
               retval = -1;
               DBUSERR((" Ctrl write hwacc failed w/status %d @ address:%x \n",
                   status, hwacc.addr));
               goto err;
           }
 
           hwacc.addr += write_bytes;
           data += write_bytes;
           size -= write_bytes;
       }
   } while (size > 0);
 
err:
   USB_AUTOPM_PUT_INTERFACE(g_probe_info.intf);
 
   return retval;
}
 
int
dbus_usbos_readreg(void *bus, uint32 regaddr, int datalen, uint32 *value)
{
   usbos_info_t *usbinfo = (usbos_info_t *) bus;
   int ret = DBUS_OK;
   int transferred;
   uint32 cmd;
   hwacc_t    hwacc;
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   if (datalen == 1)
       cmd = DL_RDHW8;
   else if (datalen == 2)
       cmd = DL_RDHW16;
   else
       cmd = DL_RDHW32;
 
   USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
   transferred = USB_CONTROL_MSG(usbinfo->usb, usb_rcvctrlpipe(usbinfo->usb, 0),
       cmd, (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE),
       (uint16)(regaddr), (uint16)(regaddr >> 16),
       (void *) &hwacc, sizeof(hwacc_t), USB_CTRL_EP_TIMEOUT);
 
   if (transferred >= sizeof(hwacc_t)) {
       *value = hwacc.data;
   } else {
       DBUSERR(("%s: usb_control_msg failed %d\n", __FUNCTION__, transferred));
       ret = DBUS_ERR;
   }
 
   USB_AUTOPM_PUT_INTERFACE(g_probe_info.intf);
 
   return ret;
}
 
int
dbus_usbos_writereg(void *bus, uint32 regaddr, int datalen, uint32 data)
{
   usbos_info_t *usbinfo = (usbos_info_t *) bus;
   int ret = DBUS_OK;
   int transferred;
   uint32 cmd = DL_WRHW;
   hwacc_t    hwacc;
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
   hwacc.cmd = DL_WRHW;
   hwacc.addr = regaddr;
   hwacc.data = data;
   hwacc.len = datalen;
 
   transferred = USB_CONTROL_MSG(usbinfo->usb, usb_sndctrlpipe(usbinfo->usb, 0),
       cmd, (USB_DIR_OUT| USB_TYPE_VENDOR | USB_RECIP_INTERFACE),
       1, 0,
       (void *) &hwacc, sizeof(hwacc_t), USB_CTRL_EP_TIMEOUT);
 
   if (transferred != sizeof(hwacc_t)) {
       DBUSERR(("%s: usb_control_msg failed %d\n", __FUNCTION__, transferred));
       ret = DBUS_ERR;
   }
 
   USB_AUTOPM_PUT_INTERFACE(g_probe_info.intf);
 
   return ret;
}
 
int
dbus_usbos_wait(usbos_info_t *usbinfo, uint16 ms)
{
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
   if (in_interrupt())
       mdelay(ms);
   else
       msleep_interruptible(ms);
#else
   wait_ms(ms);
#endif
   return DBUS_OK;
}
 
/** Called by dbus_usb.c as part of the firmware download process */
bool
dbus_usbos_dl_send_bulk(usbos_info_t *usbinfo, void *buffer, int len)
{
   bool ret = TRUE;
   int status;
   int transferred = 0;
 
   if (usbinfo == NULL)
       return DBUS_ERR;
 
   USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
   status = USB_BULK_MSG(usbinfo->usb, usbinfo->tx_pipe,
       buffer, len,
       &transferred, USB_BULK_EP_TIMEOUT);
 
   if (status < 0) {
       DBUSERR(("%s: usb_bulk_msg failed %d\n", __FUNCTION__, status));
       ret = FALSE;
   }
 
   USB_AUTOPM_PUT_INTERFACE(g_probe_info.intf);
 
   return ret;
}
 
static bool
dbus_usbos_intf_recv_needed(void *bus)
{
   return FALSE;
}
 
/**
 * Higher layer (dbus_usb.c) wants to execute a function on the condition that the rx spin lock has
 * been acquired.
 */
static void*
dbus_usbos_intf_exec_rxlock(void *bus, exec_cb_t cb, struct exec_parms *args)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   void *ret;
   unsigned long flags;
 
   if (usbos_info == NULL)
       return NULL;
 
   spin_lock_irqsave(&usbos_info->rxlock, flags);
   ret = cb(args);
   spin_unlock_irqrestore(&usbos_info->rxlock, flags);
 
   return ret;
}
 
static void*
dbus_usbos_intf_exec_txlock(void *bus, exec_cb_t cb, struct exec_parms *args)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
   void *ret;
   unsigned long flags;
 
   if (usbos_info == NULL)
       return NULL;
 
   spin_lock_irqsave(&usbos_info->txlock, flags);
   ret = cb(args);
   spin_unlock_irqrestore(&usbos_info->txlock, flags);
 
   return ret;
}
 
/**
 * if an error condition was detected in this module, the higher DBUS layer (dbus_usb.c) has to
 * be notified.
 */
int
dbus_usbos_errhandler(void *bus, int err)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   if (usbos_info->cbarg && usbos_info->cbs) {
       if (usbos_info->cbs->errhandler)
           usbos_info->cbs->errhandler(usbos_info->cbarg, err);
   }
 
   return DBUS_OK;
}
 
/**
 * if a change in bus state was detected in this module, the higher DBUS layer (dbus_usb.c) has to
 * be notified.
 */
int
dbus_usbos_state_change(void *bus, int state)
{
   usbos_info_t *usbos_info = (usbos_info_t *) bus;
 
   if (usbos_info == NULL)
       return DBUS_ERR;
 
   if (usbos_info->cbarg && usbos_info->cbs) {
       if (usbos_info->cbs->state_change)
           usbos_info->cbs->state_change(usbos_info->cbarg, state);
   }
 
   usbos_info->pub->busstate = state;
   return DBUS_OK;
}
 
int
dbus_bus_osl_register(int vid, int pid, probe_cb_t prcb,
   disconnect_cb_t discb, void *prarg, dbus_intf_t **intf, void *param1, void *param2)
{
   bzero(&g_probe_info, sizeof(probe_info_t));
 
   probe_cb = prcb;
   disconnect_cb = discb;
   probe_arg = prarg;
 
   devid_table[0].idVendor = vid;
   devid_table[0].idProduct = pid;
 
   *intf = &dbus_usbos_intf;
 
   USB_REGISTER();
 
   return DBUS_ERR_NODEVICE;
}
 
int
dbus_bus_osl_deregister()
{
   g_probe_info.dereged = TRUE;
 
   DHD_MUTEX_LOCK();
   if (disconnect_cb && disc_arg && (g_probe_info.disc_cb_done == FALSE)) {
       disconnect_cb(disc_arg);
       disc_arg = NULL;
   }
   DHD_MUTEX_UNLOCK();
 
   USB_DEREGISTER();
 
   return DBUS_OK;
}
 
void *
dbus_usbos_intf_attach(dbus_pub_t *pub, void *cbarg, dbus_intf_callbacks_t *cbs)
{
   usbos_info_t *usbos_info;
 
   if (g_probe_info.dldone == FALSE) {
       DBUSERR(("%s: err device not downloaded!\n", __FUNCTION__));
       return NULL;
   }
 
   /* Sanity check for BUS_INFO() */
   ASSERT(OFFSETOF(usbos_info_t, pub) == 0);
 
   usbos_info = MALLOC(pub->osh, sizeof(usbos_info_t));
   if (usbos_info == NULL)
       return NULL;
 
   bzero(usbos_info, sizeof(usbos_info_t));
 
   usbos_info->pub = pub;
   usbos_info->cbarg = cbarg;
   usbos_info->cbs = cbs;
 
   /* Needed for disconnect() */
   g_probe_info.usbos_info = usbos_info;
 
   /* Update USB Info */
   usbos_info->usb = g_probe_info.usb;
   usbos_info->rx_pipe = g_probe_info.rx_pipe;
   usbos_info->rx_pipe2 = g_probe_info.rx_pipe2;
   usbos_info->tx_pipe = g_probe_info.tx_pipe;
   usbos_info->intr_pipe = g_probe_info.intr_pipe;
   usbos_info->intr_size = g_probe_info.intr_size;
   usbos_info->interval = g_probe_info.interval;
   usbos_info->pub->device_speed = g_probe_info.device_speed;
   if (usbos_info->rx_pipe2) {
       usbos_info->pub->attrib.has_2nd_bulk_in_ep = 1;
   } else {
       usbos_info->pub->attrib.has_2nd_bulk_in_ep = 0;
   }
 
   if (usbos_info->tx_pipe)
       usbos_info->maxps = usb_maxpacket(usbos_info->usb,
           usbos_info->tx_pipe, usb_pipeout(usbos_info->tx_pipe));
 
   INIT_LIST_HEAD(&usbos_info->req_rxfreeq);
   INIT_LIST_HEAD(&usbos_info->req_txfreeq);
   INIT_LIST_HEAD(&usbos_info->req_rxpostedq);
   INIT_LIST_HEAD(&usbos_info->req_txpostedq);
   spin_lock_init(&usbos_info->rxfree_lock);
   spin_lock_init(&usbos_info->txfree_lock);
   spin_lock_init(&usbos_info->rxposted_lock);
   spin_lock_init(&usbos_info->txposted_lock);
   spin_lock_init(&usbos_info->rxlock);
   spin_lock_init(&usbos_info->txlock);
 
   atomic_set(&usbos_info->rxposted, 0);
   atomic_set(&usbos_info->txposted, 0);
 
 
#ifdef USB_DISABLE_INT_EP
   usbos_info->intr_urb = NULL;
#else
   if (!(usbos_info->intr_urb = USB_ALLOC_URB())) {
       DBUSERR(("%s: usb_alloc_urb (tx) failed\n", __FUNCTION__));
       goto fail;
   }
#endif
 
   if (!(usbos_info->ctl_urb = USB_ALLOC_URB())) {
       DBUSERR(("%s: usb_alloc_urb (tx) failed\n", __FUNCTION__));
       goto fail;
   }
 
   init_waitqueue_head(&usbos_info->wait);
 
   if (!(usbos_info->blk_urb = USB_ALLOC_URB())) {    /* for embedded image downloading */
       DBUSERR(("%s: usb_alloc_urb (tx) failed\n", __FUNCTION__));
       goto fail;
   }
 
   usbos_info->rxbuf_len = (uint)usbos_info->pub->rxsize;
 
 
 
   atomic_set(&usbos_info->txallocated, 0);
   if (DBUS_OK != dbus_usbos_urbreqs_alloc(usbos_info,
       usbos_info->pub->ntxq, FALSE)) {
       goto fail;
   }
 
   atomic_set(&usbos_info->rxallocated, 0);
   if (DBUS_OK != dbus_usbos_urbreqs_alloc(usbos_info,
       MIN(DBUS_USB_RXQUEUE_BATCH_ADD, usbos_info->pub->nrxq),
       TRUE)) {
       goto fail;
   }
 
   sema_init(&usbos_info->ctl_lock, 1);
 
#ifdef USBOS_THREAD
   if (dbus_usbos_thread_init(usbos_info) == NULL)
       goto fail;
#endif /* USBOS_THREAD */
 
#ifdef USBOS_TX_THREAD
   if (dbus_usbos_tx_thread_init(usbos_info) == NULL)
       goto fail;
#endif /* USBOS_TX_THREAD */
 
   pub->dev_info = g_probe_info.usb;
 
 
   return (void *) usbos_info;
fail:
   if (usbos_info->intr_urb) {
       USB_FREE_URB(usbos_info->intr_urb);
       usbos_info->intr_urb = NULL;
   }
 
   if (usbos_info->ctl_urb) {
       USB_FREE_URB(usbos_info->ctl_urb);
       usbos_info->ctl_urb = NULL;
   }
 
#if defined(BCM_REQUEST_FW)
   if (usbos_info->blk_urb) {
       USB_FREE_URB(usbos_info->blk_urb);
       usbos_info->blk_urb = NULL;
   }
#endif
 
   dbus_usbos_urbreqs_free(usbos_info, TRUE);
   atomic_set(&usbos_info->rxallocated, 0);
   dbus_usbos_urbreqs_free(usbos_info, FALSE);
   atomic_set(&usbos_info->txallocated, 0);
 
   g_probe_info.usbos_info = NULL;
 
   MFREE(pub->osh, usbos_info, sizeof(usbos_info_t));
   return NULL;
} /* dbus_usbos_intf_attach */
 
void
dbus_usbos_intf_detach(dbus_pub_t *pub, void *info)
{
   usbos_info_t *usbos_info = (usbos_info_t *) info;
   osl_t *osh = pub->osh;
 
   if (usbos_info == NULL) {
       return;
   }
 
#ifdef USBOS_TX_THREAD
   dbus_usbos_tx_thread_deinit(usbos_info);
#endif /* USBOS_TX_THREAD */
 
   /* Must unlink all URBs prior to driver unload;
    * otherwise an URB callback can occur after driver
    * has been de-allocated and rmmod'd
    */
   dbusos_stop(usbos_info);
 
   if (usbos_info->intr_urb) {
       USB_FREE_URB(usbos_info->intr_urb);
       usbos_info->intr_urb = NULL;
   }
 
   if (usbos_info->ctl_urb) {
       USB_FREE_URB(usbos_info->ctl_urb);
       usbos_info->ctl_urb = NULL;
   }
 
   if (usbos_info->blk_urb) {
       USB_FREE_URB(usbos_info->blk_urb);
       usbos_info->blk_urb = NULL;
   }
 
   dbus_usbos_urbreqs_free(usbos_info, TRUE);
   atomic_set(&usbos_info->rxallocated, 0);
   dbus_usbos_urbreqs_free(usbos_info, FALSE);
   atomic_set(&usbos_info->txallocated, 0);
 
#ifdef USBOS_THREAD
   dbus_usbos_thread_deinit(usbos_info);
#endif /* USBOS_THREAD */
 
   g_probe_info.usbos_info = NULL;
   MFREE(osh, usbos_info, sizeof(usbos_info_t));
} /* dbus_usbos_intf_detach */
 
 
#ifdef USBOS_TX_THREAD
 
void*
dbus_usbos_tx_thread_init(usbos_info_t *usbos_info)
{
   spin_lock_init(&usbos_info->usbos_tx_list_lock);
   INIT_LIST_HEAD(&usbos_info->usbos_tx_list);
   init_waitqueue_head(&usbos_info->usbos_tx_queue_head);
 
   usbos_info->usbos_tx_kt = kthread_create(dbus_usbos_tx_thread_func,
       usbos_info, "usb-tx-thread");
 
   if (IS_ERR(usbos_info->usbos_tx_kt)) {
       DBUSERR(("Thread Creation failed\n"));
       return (NULL);
   }
 
   usbos_info->ctl_state = USBOS_REQUEST_STATE_UNSCHEDULED;
   wake_up_process(usbos_info->usbos_tx_kt);
 
   return (usbos_info->usbos_tx_kt);
}
 
void
dbus_usbos_tx_thread_deinit(usbos_info_t *usbos_info)
{
   urb_req_t *req;
 
   if (usbos_info->usbos_tx_kt) {
       wake_up_interruptible(&usbos_info->usbos_tx_queue_head);
       kthread_stop(usbos_info->usbos_tx_kt);
   }
 
   /* Move pending requests to free queue so they can be freed */
   while ((req = dbus_usbos_qdeq(
       &usbos_info->usbos_tx_list, &usbos_info->usbos_tx_list_lock)) != NULL) {
       dbus_usbos_qenq(&usbos_info->req_txfreeq, req, &usbos_info->txfree_lock);
   }
}
 
/**
 * Allow USB in-band resume to block by submitting CTRL and DATA URBs on a separate thread.
 */
int
dbus_usbos_tx_thread_func(void *data)
{
   usbos_info_t  *usbos_info = (usbos_info_t *)data;
   urb_req_t     *req;
   dbus_irb_tx_t *txirb;
   int           ret;
   unsigned long flags;
 
#ifdef WL_THREADNICE
   set_user_nice(current, WL_THREADNICE);
#endif
 
   while (1) {
       /* Wait until there are URBs to submit */
       wait_event_interruptible_timeout(
           usbos_info->usbos_tx_queue_head,
           !list_empty(&usbos_info->usbos_tx_list) ||
           usbos_info->ctl_state == USBOS_REQUEST_STATE_SCHEDULED,
           100);
 
       if (kthread_should_stop())
           break;
 
       /* Submit CTRL URB if needed */
       if (usbos_info->ctl_state == USBOS_REQUEST_STATE_SCHEDULED) {
 
           /* Disable USB autosuspend until this request completes. If the
            * interface was suspended, this call blocks until it has been resumed.
            */
           USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
           usbos_info->ctl_state = USBOS_REQUEST_STATE_SUBMITTED;
 
           ret = USB_SUBMIT_URB(usbos_info->ctl_urb);
           if (ret != 0) {
               DBUSERR(("%s CTRL USB_SUBMIT_URB failed, status %d\n",
                   __FUNCTION__, ret));
 
               usbos_info->ctl_state = USBOS_REQUEST_STATE_UNSCHEDULED;
               up(&usbos_info->ctl_lock);
 
               USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
           }
       }
 
       /* Submit all available TX URBs */
       while ((req = dbus_usbos_qdeq(&usbos_info->usbos_tx_list,
           &usbos_info->usbos_tx_list_lock)) != NULL) {
 
           /* Disable USB autosuspend until this request completes. If the
            * interface was suspended, this call blocks until it has been resumed.
            */
           USB_AUTOPM_GET_INTERFACE(g_probe_info.intf);
 
           spin_lock_irqsave(&usbos_info->txlock, flags);
 
           ret = USB_SUBMIT_URB(req->urb);
           if (ret == 0) {
               /* URB submitted successfully */
               dbus_usbos_qenq(&usbos_info->req_txpostedq, req,
                   &usbos_info->txposted_lock);
               atomic_inc(&usbos_info->txposted);
           } else {
               /* Submitting the URB failed. */
               DBUSERR(("%s TX USB_SUBMIT_URB failed, status %d\n",
                   __FUNCTION__, ret));
 
               USB_AUTOPM_PUT_INTERFACE_ASYNC(g_probe_info.intf);
           }
 
           spin_unlock_irqrestore(&usbos_info->txlock, flags);
 
           if (ret != 0) {
               /* Cleanup and notify higher layers */
               dbus_usbos_qenq(&usbos_info->req_txfreeq, req,
                   &usbos_info->txfree_lock);
 
               txirb = req->arg;
               if (txirb->send_buf) {
                   MFREE(usbos_info->pub->osh, txirb->send_buf, req->buf_len);
                   txirb->send_buf = NULL;
                   req->buf_len = 0;
               }
 
               if (likely (usbos_info->cbarg && usbos_info->cbs)) {
                   if (likely (usbos_info->cbs->send_irb_complete != NULL))
                       usbos_info->cbs->send_irb_complete(
                           usbos_info->cbarg, txirb, DBUS_ERR_TXDROP);
               }
           }
       }
   }
 
   return 0;
} /* dbus_usbos_tx_thread_func */
 
#endif /* USBOS_TX_THREAD */
 
#ifdef USBOS_THREAD
 
/**
 * Increase system performance by creating a USB thread that runs parallel to other system
 * activity.
 */
static void*
dbus_usbos_thread_init(usbos_info_t *usbos_info)
{
   usbos_list_entry_t  *entry;
   unsigned long       flags, ii;
 
   spin_lock_init(&usbos_info->usbos_list_lock);
   spin_lock_init(&usbos_info->ctrl_lock);
   INIT_LIST_HEAD(&usbos_info->usbos_list);
   INIT_LIST_HEAD(&usbos_info->usbos_free_list);
   init_waitqueue_head(&usbos_info->usbos_queue_head);
   atomic_set(&usbos_info->usbos_list_cnt, 0);
 
 
   for (ii = 0; ii < (usbos_info->pub->nrxq + usbos_info->pub->ntxq); ii++) {
       entry = MALLOC(usbos_info->pub->osh, sizeof(usbos_list_entry_t));
       if (entry) {
           spin_lock_irqsave(&usbos_info->usbos_list_lock, flags);
           list_add_tail((struct list_head*) entry, &usbos_info->usbos_free_list);
           spin_unlock_irqrestore(&usbos_info->usbos_list_lock, flags);
       } else {
           DBUSERR(("Failed to create list\n"));
       }
   }
 
   usbos_info->usbos_kt = kthread_create(dbus_usbos_thread_func,
       usbos_info, "usb-thread");
 
   if (IS_ERR(usbos_info->usbos_kt)) {
       DBUSERR(("Thread Creation failed\n"));
       return (NULL);
   }
 
   wake_up_process(usbos_info->usbos_kt);
 
   return (usbos_info->usbos_kt);
}
 
static void
dbus_usbos_thread_deinit(usbos_info_t *usbos_info)
{
   struct list_head    *cur, *next;
   usbos_list_entry_t  *entry;
   unsigned long       flags;
 
   if (usbos_info->usbos_kt) {
       wake_up_interruptible(&usbos_info->usbos_queue_head);
       kthread_stop(usbos_info->usbos_kt);
   }
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
   list_for_each_safe(cur, next, &usbos_info->usbos_list)
   {
       entry = list_entry(cur, struct usbos_list_entry, list);
       /* detach this entry from the list and then free the entry */
       spin_lock_irqsave(&usbos_info->usbos_list_lock, flags);
       list_del(cur);
       MFREE(usbos_info->pub->osh, entry, sizeof(usbos_list_entry_t));
       spin_unlock_irqrestore(&usbos_info->usbos_list_lock, flags);
   }
 
   list_for_each_safe(cur, next, &usbos_info->usbos_free_list)
   {
       entry = list_entry(cur, struct usbos_list_entry, list);
       /* detach this entry from the list and then free the entry */
       spin_lock_irqsave(&usbos_info->usbos_list_lock, flags);
       list_del(cur);
       MFREE(usbos_info->pub->osh, entry, sizeof(usbos_list_entry_t));
       spin_unlock_irqrestore(&usbos_info->usbos_list_lock, flags);
   }
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
}
 
/** Process completed URBs in a worker thread */
static int
dbus_usbos_thread_func(void *data)
{
   usbos_info_t        *usbos_info = (usbos_info_t *)data;
   usbos_list_entry_t  *entry;
   struct list_head    *cur, *next;
   unsigned long       flags;
 
#ifdef WL_THREADNICE
   set_user_nice(current, WL_THREADNICE);
#endif
 
   while (1) {
       /* If the list is empty, then go to sleep */
       wait_event_interruptible_timeout
       (usbos_info->usbos_queue_head,
           atomic_read(&usbos_info->usbos_list_cnt) > 0,
           100);
 
       if (kthread_should_stop())
           break;
 
       spin_lock_irqsave(&usbos_info->usbos_list_lock, flags);
 
       /* For each entry on the list, process it.  Remove the entry from
       * the list when done.
       */
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
       list_for_each_safe(cur, next, &usbos_info->usbos_list)
       {
           urb_req_t           *req;
           int                 len;
           int                 stat;
           usbos_info_t        *usbos_info_local;
 
           entry = list_entry(cur, struct usbos_list_entry, list);
           if (entry == NULL)
               break;
 
           req = entry->urb_context;
           len = entry->urb_length;
           stat = entry->urb_status;
           usbos_info_local = req->usbinfo;
 
           /* detach this entry from the list and attach it to the free list */
           list_del_init(cur);
           spin_unlock_irqrestore(&usbos_info_local->usbos_list_lock, flags);
 
           dbus_usbos_recv_complete_handle(req, len, stat);
 
           spin_lock_irqsave(&usbos_info_local->usbos_list_lock, flags);
 
           list_add_tail(cur, &usbos_info_local->usbos_free_list);
 
           atomic_dec(&usbos_info_local->usbos_list_cnt);
       }
 
       spin_unlock_irqrestore(&usbos_info->usbos_list_lock, flags);
 
   }
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
 
   return 0;
} /* dbus_usbos_thread_func */
 
/** Called on Linux calling URB callback, see dbus_usbos_recv_complete() */
static void
dbus_usbos_dispatch_schedule(CALLBACK_ARGS)
{
   urb_req_t           *req = urb->context;
   usbos_info_t        *usbos_info = req->usbinfo;
   usbos_list_entry_t  *entry;
   unsigned long       flags;
   struct list_head    *cur;
 
   spin_lock_irqsave(&usbos_info->usbos_list_lock, flags);
 
   cur   = usbos_info->usbos_free_list.next;
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
   entry = list_entry(cur, struct usbos_list_entry, list);
#if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
 
   /* detach this entry from the free list and prepare it insert it to use list */
   list_del_init(cur);
 
   if (entry) {
       entry->urb_context = urb->context;
       entry->urb_length  = urb->actual_length;
       entry->urb_status  = urb->status;
 
       atomic_inc(&usbos_info->usbos_list_cnt);
       list_add_tail(cur, &usbos_info->usbos_list);
   } else {
       DBUSERR(("!!!!!!OUT OF MEMORY!!!!!!!\n"));
   }
 
   spin_unlock_irqrestore(&usbos_info->usbos_list_lock, flags);
 
   /* thread */
   wake_up_interruptible(&usbos_info->usbos_queue_head);
} /* dbus_usbos_dispatch_schedule */
 
#endif /* USBOS_THREAD */
 
 
 
 
#ifdef BCM_REQUEST_FW
 
struct request_fw_context {
   const struct firmware *firmware;
   struct semaphore lock;
};
 
/*
 * Callback for dbus_request_firmware().
 */
static void
dbus_request_firmware_done(const struct firmware *firmware, void *ctx)
{
   struct request_fw_context *context = (struct request_fw_context*)ctx;
 
   /* Store the received firmware handle in the context and wake requester */
   context->firmware = firmware;
   up(&context->lock);
}
 
/*
 * Send a firmware request and wait for completion.
 *
 * The use of the asynchronous version of request_firmware() is needed to avoid
 * kernel oopses when we just come out of system hibernate.
 */
static int
dbus_request_firmware(const char *name, const struct firmware **firmware)
{
   struct request_fw_context *context;
   int ret;
 
   context = kzalloc(sizeof(*context), GFP_KERNEL);
   if (!context)
       return -ENOMEM;
 
   sema_init(&context->lock, 0);
 
   ret = request_firmware_nowait(THIS_MODULE, true, name, &g_probe_info.usb->dev,
                                 GFP_KERNEL, context, dbus_request_firmware_done);
   if (ret) {
       kfree(context);
       return ret;
   }
 
   /* Wait for completion */
   if (down_interruptible(&context->lock) != 0) {
       kfree(context);
       return -ERESTARTSYS;
   }
 
   *firmware = context->firmware;
   kfree(context);
 
   return *firmware != NULL ? 0 : -ENOENT;
}
 
static void *
dbus_get_fwfile(int devid, int chiprev, uint8 **fw, int *fwlen, uint16 boardtype, uint16 boardrev)
{
   const struct firmware *firmware = NULL;
#ifndef OEM_ANDROID
   s8 *device_id = NULL;
   s8 *chip_rev = "";
#endif /* OEM_ANDROID */
   s8 file_name[64];
   int ret;
 
#ifndef OEM_ANDROID
   switch (devid) {
       case BCM4350_CHIP_ID:
       case BCM4354_CHIP_ID:
       case BCM43556_CHIP_ID:
       case BCM43558_CHIP_ID:
       case BCM43566_CHIP_ID:
       case BCM43568_CHIP_ID:
       case BCM43570_CHIP_ID:
       case BCM4358_CHIP_ID:
           device_id = "4350";
           break;
       case BCM43143_CHIP_ID:
           device_id = "43143";
           break;
       case BCM43234_CHIP_ID:
       case BCM43235_CHIP_ID:
       case BCM43236_CHIP_ID:
           device_id = "43236";
           break;
       case BCM43242_CHIP_ID:
           device_id = "43242";
           break;
       case BCM43238_CHIP_ID:
           device_id = "43238";
           break;
       case BCM43526_CHIP_ID:
           device_id = "43526";
           break;
       case BCM43569_CHIP_ID:
           device_id = "43569";
           switch (chiprev) {
               case 0:
                   chip_rev = "a0";
                   break;
               case 2:
                   chip_rev = "a2";
                   break;
               default:
                   break;
           }
           break;
       default:
           DBUSERR(("unsupported device %x\n", devid));
           return NULL;
   }
 
   /* Load firmware */
   snprintf(file_name, sizeof(file_name), "brcm/bcm%s%s-firmware.bin", device_id, chip_rev);
#else
   snprintf(file_name, sizeof(file_name), "%s", CONFIG_ANDROID_BCMDHD_FW_PATH);
#endif /* OEM_ANDROID */
 
   ret = dbus_request_firmware(file_name, &firmware);
   if (ret) {
       DBUSERR(("fail to request firmware %s\n", file_name));
       return NULL;
   }
 
   *fwlen = firmware->size;
   *fw = (uint8 *)firmware->data;
   return (void *)firmware;
 
}
 
static void *
dbus_get_nvfile(int devid, int chiprev, uint8 **fw, int *fwlen, uint16 boardtype, uint16 boardrev)
{
   const struct firmware *firmware = NULL;
#ifndef OEM_ANDROID
   s8 *device_id = NULL;
   s8 *chip_rev = "";
#endif /* OEM_ANDROID */
   s8 file_name[64];
   int ret;
 
#ifndef OEM_ANDROID
   switch (devid) {
       case BCM4350_CHIP_ID:
       case BCM4354_CHIP_ID:
       case BCM43556_CHIP_ID:
       case BCM43558_CHIP_ID:
       case BCM43566_CHIP_ID:
       case BCM43568_CHIP_ID:
       case BCM43570_CHIP_ID:
       case BCM4358_CHIP_ID:
           device_id = "4350";
           break;
       case BCM43143_CHIP_ID:
           device_id = "43143";
           break;
       case BCM43234_CHIP_ID:
           device_id = "43234";
           break;
       case BCM43235_CHIP_ID:
           device_id = "43235";
           break;
       case BCM43236_CHIP_ID:
           device_id = "43236";
           break;
       case BCM43238_CHIP_ID:
           device_id = "43238";
           break;
       case BCM43242_CHIP_ID:
           device_id = "43242";
           break;
       case BCM43526_CHIP_ID:
           device_id = "43526";
           break;
       case BCM43569_CHIP_ID:
           device_id = "43569";
           switch (chiprev) {
               case 0:
                   chip_rev = "a0";
                   break;
               case 2:
                   chip_rev = "a2";
                   break;
               default:
                   break;
           }
           break;
       default:
           DBUSERR(("unsupported device %x\n", devid));
           return NULL;
   }
 
   /* Load board specific nvram file */
   snprintf(file_name, sizeof(file_name), "brcm/bcm%s%s-%2x-%2x.nvm",
            device_id, chip_rev, boardtype, boardrev);
#else
   snprintf(file_name, sizeof(file_name), "%s", CONFIG_ANDROID_BCMDHD_NVRAM_PATH);
#endif /* OEM_ANDROID */
 
   ret = dbus_request_firmware(file_name, &firmware);
   if (ret) {
       DBUSERR(("fail to request nvram %s\n", file_name));
 
#ifndef OEM_ANDROID
       /* Load generic nvram file */
       snprintf(file_name, sizeof(file_name), "brcm/bcm%s%s.nvm",
                device_id, chip_rev);
 
       ret = dbus_request_firmware(file_name, &firmware);
#endif /* OEM_ANDROID */
 
       if (ret) {
           DBUSERR(("fail to request nvram %s\n", file_name));
           return NULL;
       }
   }
 
   *fwlen = firmware->size;
   *fw = (uint8 *)firmware->data;
   return (void *)firmware;
}
 
void *
dbus_get_fw_nvfile(int devid, int chiprev, uint8 **fw, int *fwlen, int type, uint16 boardtype,
   uint16 boardrev)
{
   switch (type) {
       case DBUS_FIRMWARE:
           return dbus_get_fwfile(devid, chiprev, fw, fwlen, boardtype, boardrev);
       case DBUS_NVFILE:
           return dbus_get_nvfile(devid, chiprev, fw, fwlen, boardtype, boardrev);
       default:
           return NULL;
   }
}
 
void
dbus_release_fw_nvfile(void *firmware)
{
   release_firmware((struct firmware *)firmware);
}
#endif /* BCM_REQUEST_FW */
 
#ifdef BCMUSBDEV_COMPOSITE
/**
 * For a composite device the interface order is not guaranteed, scan the device struct for the WLAN
 * interface.
 */
static int
dbus_usbos_intf_wlan(struct usb_device *usb)
{
   int i, num_of_eps, ep, intf_wlan = -1;
   int num_intf = CONFIGDESC(usb)->bNumInterfaces;
   struct usb_endpoint_descriptor *endpoint;
 
   for (i = 0; i < num_intf; i++) {
       if (IFDESC(usb, i).bInterfaceClass != USB_CLASS_VENDOR_SPEC)
           continue;
       num_of_eps = IFDESC(usb, i).bNumEndpoints;
 
       for (ep = 0; ep < num_of_eps; ep++) {
           endpoint = &IFEPDESC(usb, i, ep);
           if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
               USB_ENDPOINT_XFER_BULK) {
               intf_wlan = i;
               break;
           }
       }
       if (ep < num_of_eps)
           break;
   }
 
   return intf_wlan;
}
#endif /* BCMUSBDEV_COMPOSITE */