aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/sdio_al.c
blob: bcfc556cad39dd1f591e1853e2f4094d0a507ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
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
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
/* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

/*
 * SDIO-Abstraction-Layer Module.
 *
 * To be used with Qualcomm's SDIO-Client connected to this host.
 */
#include "sdio_al_private.h"

#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/wakelock.h>
#include <linux/mmc/core.h>
#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
#include <linux/mmc/mmc.h>
#include <linux/mmc/sdio.h>
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>
#include <linux/gpio.h>
#include <linux/dma-mapping.h>
#include <linux/earlysuspend.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
#include <linux/time.h>
#include <linux/spinlock.h>

#include <mach/dma.h>
#include <mach/gpio.h>
#include <mach/subsystem_notif.h>

#include "../../../drivers/mmc/host/msm_sdcc.h"

/**
 *  Func#0 has SDIO standard registers
 *  Func#1 is for Mailbox.
 *  Functions 2..7 are for channels.
 *  Currently only functions 2..5 are active due to SDIO-Client
 *  number of pipes.
 *
 */
#define SDIO_AL_MAX_CHANNELS 6

/** Func 1..5 */
#define SDIO_AL_MAX_FUNCS    (SDIO_AL_MAX_CHANNELS+1)
#define SDIO_AL_WAKEUP_FUNC  6

/** Number of SDIO-Client pipes */
#define SDIO_AL_MAX_PIPES    16
#define SDIO_AL_ACTIVE_PIPES 8

/** CMD53/CMD54 Block size */
#define SDIO_AL_BLOCK_SIZE   256

/** Func#1 hardware Mailbox base address	 */
#define HW_MAILBOX_ADDR			0x1000

/** Func#1 peer sdioc software version.
 *  The header is duplicated also to the mailbox of the other
 *  functions. It can be used before other functions are enabled. */
#define SDIOC_SW_HEADER_ADDR		0x0400

/** Func#2..7 software Mailbox base address at 16K */
#define SDIOC_SW_MAILBOX_ADDR			0x4000

/** Some Mailbox registers address, written by host for
 control */
#define PIPES_THRESHOLD_ADDR		0x01000

#define PIPES_0_7_IRQ_MASK_ADDR 	0x01048

#define PIPES_8_15_IRQ_MASK_ADDR	0x0104C

#define FUNC_1_4_MASK_IRQ_ADDR		0x01040
#define FUNC_5_7_MASK_IRQ_ADDR		0x01044
#define FUNC_1_4_USER_IRQ_ADDR		0x01050
#define FUNC_5_7_USER_IRQ_ADDR		0x01054

#define EOT_PIPES_ENABLE		0x00

/** Maximum read/write data available is SDIO-Client limitation */
#define MAX_DATA_AVAILABLE   		(16*1024)
#define INVALID_DATA_AVAILABLE  	(0x8000)

/** SDIO-Client HW threshold to generate interrupt to the
 *  SDIO-Host on write available bytes.
 */
#define DEFAULT_WRITE_THRESHOLD 	(1024)

/** SDIO-Client HW threshold to generate interrupt to the
 *  SDIO-Host on read available bytes, for streaming (non
 *  packet) rx data.
 */
#define DEFAULT_READ_THRESHOLD  	(1024)
#define LOW_LATENCY_THRESHOLD		(1)

/* Extra bytes to ensure getting the rx threshold interrupt on stream channels
   when restoring the threshold after sleep */
#define THRESHOLD_CHANGE_EXTRA_BYTES (100)

/** SW threshold to trigger reading the mailbox. */
#define DEFAULT_MIN_WRITE_THRESHOLD 	(1024)
#define DEFAULT_MIN_WRITE_THRESHOLD_STREAMING	(1600)

#define THRESHOLD_DISABLE_VAL  		(0xFFFFFFFF)

/** Mailbox polling time for packet channels */
#define DEFAULT_POLL_DELAY_MSEC		10
/** Mailbox polling time for streaming channels */
#define DEFAULT_POLL_DELAY_NOPACKET_MSEC 30

/** The SDIO-Client prepares N buffers of size X per Tx pipe.
 *  Even when the transfer fills a partial buffer,
 *  that buffer becomes unusable for the next transfer. */
#define DEFAULT_PEER_TX_BUF_SIZE	(128)

#define ROUND_UP(x, n) (((x + n - 1) / n) * n)

/** Func#2..7 FIFOs are r/w via
 sdio_readsb() & sdio_writesb(),when inc_addr=0 */
#define PIPE_RX_FIFO_ADDR   0x00
#define PIPE_TX_FIFO_ADDR   0x00

/** Inactivity time to go to sleep in mseconds */
#define INACTIVITY_TIME_MSEC 30
#define INITIAL_INACTIVITY_TIME_MSEC 5000

/** Context validity check */
#define SDIO_AL_SIGNATURE 0xAABBCCDD

/* Vendor Specific Command */
#define SD_IO_RW_EXTENDED_QCOM 54

#define TIME_TO_WAIT_US 500
#define SDIO_CLOSE_FLUSH_TIMEOUT_MSEC   (10000)
#define RX_FLUSH_BUFFER_SIZE (16*1024)

#define SDIO_TEST_POSTFIX "_TEST"

#define DATA_DEBUG(x, y...)						\
	do {								\
		if (sdio_al->debug.debug_data_on)			\
			pr_info(y);					\
		sdio_al_log(x, y);					\
	} while (0)

#define LPM_DEBUG(x, y...)						\
	do {								\
		if (sdio_al->debug.debug_lpm_on)			\
			pr_info(y);					\
		sdio_al_log(x, y);					\
	} while (0)

#define sdio_al_loge(x, y...)						\
	do {								\
		pr_err(y);						\
		sdio_al_log(x, y);					\
	} while (0)

#define sdio_al_logi(x, y...)						\
	do {								\
		pr_info(y);						\
		sdio_al_log(x, y);					\
	} while (0)

#define CLOSE_DEBUG(x, y...)						\
	do {								\
		if (sdio_al->debug.debug_close_on)			\
			pr_info(y);					\
		sdio_al_log(x, y);					\
	} while (0)

/* The index of the SDIO card used for the sdio_al_dloader */
#define SDIO_BOOTLOADER_CARD_INDEX 1


/* SDIO card state machine */
enum sdio_al_device_state {
	CARD_INSERTED,
	CARD_REMOVED,
	MODEM_RESTART
};

struct sdio_al_debug {
	u8 debug_lpm_on;
	u8 debug_data_on;
	u8 debug_close_on;
	struct dentry *sdio_al_debug_root;
	struct dentry *sdio_al_debug_lpm_on;
	struct dentry *sdio_al_debug_data_on;
	struct dentry *sdio_al_debug_close_on;
	struct dentry *sdio_al_debug_info;
	struct dentry *sdio_al_debug_log_buffers[MAX_NUM_OF_SDIO_DEVICES + 1];
};

/* Polling time for the inactivity timer for devices that doesn't have
 * a streaming channel
 */
#define SDIO_AL_POLL_TIME_NO_STREAMING 30

#define CHAN_TO_FUNC(x) ((x) + 2 - 1)

/**
 *  Mailbox structure.
 *  The Mailbox is located on the SDIO-Client Function#1.
 *  The mailbox size is 128 bytes, which is one block.
 *  The mailbox allows the host ton:
 *  1. Get the number of available bytes on the pipes.
 *  2. Enable/Disable SDIO-Client interrupt, related to pipes.
 *  3. Set the Threshold for generating interrupt.
 *
 */
struct sdio_mailbox {
	u32 pipe_bytes_threshold[SDIO_AL_MAX_PIPES]; /* Addr 0x1000 */

	/* Mask USER interrupts generated towards host - Addr 0x1040 */
	u32 mask_irq_func_1:8; /* LSB */
	u32 mask_irq_func_2:8;
	u32 mask_irq_func_3:8;
	u32 mask_irq_func_4:8;

	u32 mask_irq_func_5:8;
	u32 mask_irq_func_6:8;
	u32 mask_irq_func_7:8;
	u32 mask_mutex_irq:8;

	/* Mask PIPE interrupts generated towards host - Addr 0x1048 */
	u32 mask_eot_pipe_0_7:8;
	u32 mask_thresh_above_limit_pipe_0_7:8;
	u32 mask_overflow_pipe_0_7:8;
	u32 mask_underflow_pipe_0_7:8;

	u32 mask_eot_pipe_8_15:8;
	u32 mask_thresh_above_limit_pipe_8_15:8;
	u32 mask_overflow_pipe_8_15:8;
	u32 mask_underflow_pipe_8_15:8;

	/* Status of User interrupts generated towards host - Addr 0x1050 */
	u32 user_irq_func_1:8;
	u32 user_irq_func_2:8;
	u32 user_irq_func_3:8;
	u32 user_irq_func_4:8;

	u32 user_irq_func_5:8;
	u32 user_irq_func_6:8;
	u32 user_irq_func_7:8;
	u32 user_mutex_irq:8;

	/* Status of PIPE interrupts generated towards host */
	/* Note: All sources are cleared once they read. - Addr 0x1058 */
	u32 eot_pipe_0_7:8;
	u32 thresh_above_limit_pipe_0_7:8;
	u32 overflow_pipe_0_7:8;
	u32 underflow_pipe_0_7:8;

	u32 eot_pipe_8_15:8;
	u32 thresh_above_limit_pipe_8_15:8;
	u32 overflow_pipe_8_15:8;
	u32 underflow_pipe_8_15:8;

	u16 pipe_bytes_avail[SDIO_AL_MAX_PIPES];
};

/** Track pending Rx Packet size */
struct rx_packet_size {
	u32 size; /* in bytes */
	struct list_head	list;
};

#define PEER_SDIOC_SW_MAILBOX_SIGNATURE 0xFACECAFE
#define PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE 0x5D107E57
#define PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE 0xDEADBEEF

/* Allow support in old sdio version */
#define PEER_SDIOC_OLD_VERSION_MAJOR	0x0002
#define INVALID_SDIO_CHAN		0xFF

/**
 * Peer SDIO-Client software header.
 */
struct peer_sdioc_sw_header {
	u32 signature;
	u32 version;
	u32 max_channels;
	char channel_names[SDIO_AL_MAX_CHANNELS][PEER_CHANNEL_NAME_SIZE];
	u32 reserved[23];
};

struct peer_sdioc_boot_sw_header {
	u32 signature;
	u32 version;
	u32 boot_ch_num;
	u32 reserved[29]; /* 32 - previous fields */
};

/**
 * Peer SDIO-Client software mailbox.
 */
struct peer_sdioc_sw_mailbox {
	struct peer_sdioc_sw_header sw_header;
	struct peer_sdioc_channel_config ch_config[SDIO_AL_MAX_CHANNELS];
};

#define SDIO_AL_DEBUG_LOG_SIZE 3000
struct sdio_al_local_log {
	char buffer[SDIO_AL_DEBUG_LOG_SIZE];
	unsigned int buf_cur_pos;
	spinlock_t log_lock;
};

#define SDIO_AL_DEBUG_TMP_LOG_SIZE 250
static int sdio_al_log(struct sdio_al_local_log *, const char *fmt, ...);

/**
 *  SDIO Abstraction Layer driver context.
 *
 *  @pdata -
 *  @debug -
 *  @devices - an array of the the devices claimed by sdio_al
 *  @unittest_mode - a flag to indicate if sdio_al is in
 *		   unittest mode
 *  @bootloader_dev - the device which is used for the
 *                 bootloader
 *  @subsys_notif_handle - handle for modem restart
 *                 notifications
 *
 */
struct sdio_al {
	struct sdio_al_local_log gen_log;
	struct sdio_al_local_log device_log[MAX_NUM_OF_SDIO_DEVICES];
	struct sdio_al_platform_data *pdata;
	struct sdio_al_debug debug;
	struct sdio_al_device *devices[MAX_NUM_OF_SDIO_DEVICES];
	int unittest_mode;
	struct sdio_al_device *bootloader_dev;
	void *subsys_notif_handle;
	int sdioc_major;
	int skip_print_info;
};

struct sdio_al_work {
	struct work_struct work;
	struct sdio_al_device *sdio_al_dev;
};


/**
 *  SDIO Abstraction Layer device context.
 *
 *  @card - card claimed.
 *
 *  @mailbox - A shadow of the SDIO-Client mailbox.
 *
 *  @channel - Channels context.
 *
 *  @workqueue - workqueue to read the mailbox and handle
 *     pending requests. Reading the mailbox should not happen
 *     in interrupt context.
 *
 *  @work - work to submit to workqueue.
 *
 *  @is_ready - driver is ready.
 *
 *  @ask_mbox - Flag to request reading the mailbox,
 *					  for different reasons.
 *
 *  @wake_lock - Lock when can't sleep.
 *
 *  @lpm_chan - Channel to use for LPM (low power mode)
 *            communication.
 *
 *  @is_ok_to_sleep - Mark if driver is OK with going to sleep
 * 			(no pending transactions).
 *
 *  @inactivity_time - time allowed to be in inactivity before
 * 		going to sleep
 *
 *  @timer - timer to use for polling the mailbox.
 *
 *  @poll_delay_msec - timer delay for polling the mailbox.
 *
 *  @is_err - error detected.
 *
 *  @signature - Context Validity Check.
 *
 *  @flashless_boot_on - flag to indicate if sdio_al is in
 *    flshless boot mode
 *
 */
struct sdio_al_device {
	struct sdio_al_local_log *dev_log;
	struct mmc_card *card;
	struct mmc_host *host;
	struct sdio_mailbox *mailbox;
	struct sdio_channel channel[SDIO_AL_MAX_CHANNELS];

	struct peer_sdioc_sw_header *sdioc_sw_header;
	struct peer_sdioc_boot_sw_header *sdioc_boot_sw_header;

	struct workqueue_struct *workqueue;
	struct sdio_al_work sdio_al_work;
	struct sdio_al_work boot_work;

	int is_ready;

	wait_queue_head_t   wait_mbox;
	int ask_mbox;
	int bootloader_done;

	struct wake_lock wake_lock;
	int lpm_chan;
	int is_ok_to_sleep;
	unsigned long inactivity_time;

	struct timer_list timer;
	u32 poll_delay_msec;
	int is_timer_initialized;

	int is_err;

	u32 signature;

	unsigned int is_suspended;

	int flashless_boot_on;
	int ch_close_supported;
	int state;
	int (*lpm_callback)(void *, int);

	int print_after_interrupt;

	u8 *rx_flush_buf;
};

/*
 * Host operation:
 *   lower 16bits are operation code
 *   upper 16bits are operation state
 */
#define PEER_OPERATION(op_code , op_state) ((op_code) | ((op_state) << 16))
#define GET_PEER_OPERATION_CODE(op) ((op) & 0xffff)
#define GET_PEER_OPERATION_STATE(op) ((op) >> 16)

enum peer_op_code {
	PEER_OP_CODE_CLOSE = 1
};

enum peer_op_state {
	PEER_OP_STATE_INIT = 0,
	PEER_OP_STATE_START = 1
};


/*
 * On the kernel command line specify
 * sdio_al.debug_lpm_on=1 to enable the LPM debug messages
 * By default the LPM debug messages are turned off
 */
static int debug_lpm_on;
module_param(debug_lpm_on, int, 0);

/*
 * On the kernel command line specify
 * sdio_al.debug_data_on=1 to enable the DATA debug messages
 * By default the DATA debug messages are turned off
 */
static int debug_data_on;
module_param(debug_data_on, int, 0);

/*
 * Enables / disables open close debug messages
 */
static int debug_close_on = 1;
module_param(debug_close_on, int, 0);

/** The driver context */
static struct sdio_al *sdio_al;

/* Static functions declaration */
static int enable_eot_interrupt(struct sdio_al_device *sdio_al_dev,
				int pipe_index, int enable);
static int enable_threshold_interrupt(struct sdio_al_device *sdio_al_dev,
				      int pipe_index, int enable);
static void sdio_func_irq(struct sdio_func *func);
static void sdio_al_timer_handler(unsigned long data);
static int get_min_poll_time_msec(struct sdio_al_device *sdio_al_dev);
static u32 check_pending_rx_packet(struct sdio_channel *ch, u32 eot);
static u32 remove_handled_rx_packet(struct sdio_channel *ch);
static int set_pipe_threshold(struct sdio_al_device *sdio_al_dev,
			      int pipe_index, int threshold);
static int sdio_al_wake_up(struct sdio_al_device *sdio_al_dev,
			   u32 not_from_int, struct sdio_channel *ch);
static int sdio_al_client_setup(struct sdio_al_device *sdio_al_dev);
static int enable_mask_irq(struct sdio_al_device *sdio_al_dev,
			   int func_num, int enable, u8 bit_offset);
static int sdio_al_enable_func_retry(struct sdio_func *func, const char *name);
static void sdio_al_print_info(void);
static int sdio_read_internal(struct sdio_channel *ch, void *data, int len);
static int sdio_read_from_closed_ch(struct sdio_channel *ch, int len);
static void stop_and_del_timer(struct sdio_al_device *sdio_al_dev);

#define SDIO_AL_ERR(func)					\
	do {							\
		printk_once(KERN_ERR MODULE_NAME		\
			":In Error state, ignore %s\n",		\
			func);					\
		sdio_al_print_info();				\
	} while (0)

#ifdef CONFIG_DEBUG_FS
static int debug_info_open(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static ssize_t debug_info_write(struct file *file,
		const char __user *buf, size_t count, loff_t *ppos)
{
	sdio_al_print_info();
	return 1;
}

const struct file_operations debug_info_ops = {
	.open = debug_info_open,
	.write = debug_info_write,
};

struct debugfs_blob_wrapper sdio_al_dbgfs_log[MAX_NUM_OF_SDIO_DEVICES + 1];

/*
*
* Trigger on/off for debug messages
* for trigger off the data messages debug level use:
* echo 0 > /sys/kernel/debugfs/sdio_al/debug_data_on
* for trigger on the data messages debug level use:
* echo 1 > /sys/kernel/debugfs/sdio_al/debug_data_on
* for trigger off the lpm messages debug level use:
* echo 0 > /sys/kernel/debugfs/sdio_al/debug_lpm_on
* for trigger on the lpm messages debug level use:
* echo 1 > /sys/kernel/debugfs/sdio_al/debug_lpm_on
*/
static int sdio_al_debugfs_init(void)
{
	int i, blob_errs = 0;

	sdio_al->debug.sdio_al_debug_root = debugfs_create_dir("sdio_al", NULL);
	if (!sdio_al->debug.sdio_al_debug_root)
		return -ENOENT;

	sdio_al->debug.sdio_al_debug_lpm_on = debugfs_create_u8("debug_lpm_on",
					S_IRUGO | S_IWUGO,
					sdio_al->debug.sdio_al_debug_root,
					&sdio_al->debug.debug_lpm_on);

	sdio_al->debug.sdio_al_debug_data_on = debugfs_create_u8(
					"debug_data_on",
					S_IRUGO | S_IWUGO,
					sdio_al->debug.sdio_al_debug_root,
					&sdio_al->debug.debug_data_on);

	sdio_al->debug.sdio_al_debug_close_on = debugfs_create_u8(
					"debug_close_on",
					S_IRUGO | S_IWUGO,
					sdio_al->debug.sdio_al_debug_root,
					&sdio_al->debug.debug_close_on);

	sdio_al->debug.sdio_al_debug_info = debugfs_create_file(
					"sdio_debug_info",
					S_IRUGO | S_IWUGO,
					sdio_al->debug.sdio_al_debug_root,
					NULL,
					&debug_info_ops);

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
		char temp[18];

		scnprintf(temp, 18, "sdio_al_log_dev_%d", i + 1);
		sdio_al->debug.sdio_al_debug_log_buffers[i] =
			debugfs_create_blob(temp,
					S_IRUGO | S_IWUGO,
					sdio_al->debug.sdio_al_debug_root,
					&sdio_al_dbgfs_log[i]);
	}

	sdio_al->debug.sdio_al_debug_log_buffers[MAX_NUM_OF_SDIO_DEVICES] =
			debugfs_create_blob("sdio_al_gen_log",
				S_IRUGO | S_IWUGO,
				sdio_al->debug.sdio_al_debug_root,
				&sdio_al_dbgfs_log[MAX_NUM_OF_SDIO_DEVICES]);

	for (i = 0; i < (MAX_NUM_OF_SDIO_DEVICES + 1); ++i) {
		if (!sdio_al->debug.sdio_al_debug_log_buffers[i]) {
			pr_err(MODULE_NAME ": Failed to create debugfs buffer"
				   " entry for "
				   "sdio_al->debug.sdio_al_debug_log_buffers[%d]",
				   i);
			blob_errs = 1;
		}
	}

	if (blob_errs) {
		for (i = 0; i < (MAX_NUM_OF_SDIO_DEVICES + 1); ++i)
			if (sdio_al->debug.sdio_al_debug_log_buffers[i])
				debugfs_remove(
					sdio_al->
					debug.sdio_al_debug_log_buffers[i]);
	}


	if ((!sdio_al->debug.sdio_al_debug_data_on) &&
	    (!sdio_al->debug.sdio_al_debug_lpm_on) &&
	    (!sdio_al->debug.sdio_al_debug_close_on) &&
	    (!sdio_al->debug.sdio_al_debug_info) &&
		blob_errs) {
		debugfs_remove(sdio_al->debug.sdio_al_debug_root);
		sdio_al->debug.sdio_al_debug_root = NULL;
		return -ENOENT;
	}

	sdio_al_dbgfs_log[MAX_NUM_OF_SDIO_DEVICES].data =
						sdio_al->gen_log.buffer;
	sdio_al_dbgfs_log[MAX_NUM_OF_SDIO_DEVICES].size =
						SDIO_AL_DEBUG_LOG_SIZE;

	return 0;
}

static void sdio_al_debugfs_cleanup(void)
{
	int i;

	debugfs_remove(sdio_al->debug.sdio_al_debug_lpm_on);
	debugfs_remove(sdio_al->debug.sdio_al_debug_data_on);
	debugfs_remove(sdio_al->debug.sdio_al_debug_close_on);
	debugfs_remove(sdio_al->debug.sdio_al_debug_info);

	for (i = 0; i < (MAX_NUM_OF_SDIO_DEVICES + 1); ++i)
		debugfs_remove(sdio_al->debug.sdio_al_debug_log_buffers[i]);

	debugfs_remove(sdio_al->debug.sdio_al_debug_root);
}
#endif

static int sdio_al_log(struct sdio_al_local_log *log, const char *fmt, ...)
{
	va_list args;
	int r;
	char *tp, *log_buf;
	unsigned int *log_cur_pos;
	struct timeval kt;
	unsigned long flags;
	static char sdio_al_log_tmp[SDIO_AL_DEBUG_TMP_LOG_SIZE];

	spin_lock_irqsave(&log->log_lock, flags);

	kt = ktime_to_timeval(ktime_get());
	r = scnprintf(sdio_al_log_tmp, SDIO_AL_DEBUG_TMP_LOG_SIZE,
			"[%8ld.%6ld] ", kt.tv_sec, kt.tv_usec);

	va_start(args, fmt);
	r += vscnprintf(&sdio_al_log_tmp[r], (SDIO_AL_DEBUG_TMP_LOG_SIZE - r),
			fmt, args);
	va_end(args);

	log_buf = log->buffer;
	log_cur_pos = &(log->buf_cur_pos);

	for (tp = sdio_al_log_tmp; tp < (sdio_al_log_tmp + r); tp++) {
		log_buf[(*log_cur_pos)++] = *tp;
		if ((*log_cur_pos) == SDIO_AL_DEBUG_LOG_SIZE)
			*log_cur_pos = 0;
	}

	spin_unlock_irqrestore(&log->log_lock, flags);

	return r;
}

static int sdio_al_verify_func1(struct sdio_al_device *sdio_al_dev,
				char const *func)
{
	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
				"sdio_al_dev\n", func);
		return -ENODEV;
	}

	if (sdio_al_dev->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: Invalid "
				"signature\n", func);
		return -ENODEV;
	}

	if (!sdio_al_dev->card) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: NULL "
				"card\n", func);
		return -ENODEV;
	}
	if (!sdio_al_dev->card->sdio_func[0]) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: NULL "
				"func1\n", func);
		return -ENODEV;
	}
	return 0;
}

static int sdio_al_claim_mutex(struct sdio_al_device *sdio_al_dev,
			       char const *func)
{
	if (!sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
					"device\n", func);
		return -ENODEV;
	}

	if (sdio_al_dev->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: Invalid "
					"device signature\n", func);
		return -ENODEV;
	}

	if (!sdio_al_dev->host) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: NULL "
					"host\n", func);
		return -ENODEV;
	}

	mmc_claim_host(sdio_al_dev->host);

	return 0;
}

static int sdio_al_release_mutex(struct sdio_al_device *sdio_al_dev,
			       char const *func)
{
	if (!sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
					"device\n", func);
		return -ENODEV;
	}

	if (sdio_al_dev->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: Invalid "
					"device signature\n", func);
		return -ENODEV;
	}

	if (!sdio_al_dev->host) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: NULL "
					"host\n", func);
		return -ENODEV;
	}

	mmc_release_host(sdio_al_dev->host);

	return 0;
}

static int sdio_al_claim_mutex_and_verify_dev(
	struct sdio_al_device *sdio_al_dev,
	char const *func)
{
	if (sdio_al_claim_mutex(sdio_al_dev, __func__))
		return -ENODEV;

	if (sdio_al_dev->state != CARD_INSERTED) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": %s: Invalid "
				"device state %d\n", func, sdio_al_dev->state);
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return -ENODEV;
	}

	return 0;
}

static void sdio_al_get_into_err_state(struct sdio_al_device *sdio_al_dev)
{
	if ((!sdio_al) || (!sdio_al_dev))
		return;

	sdio_al_dev->is_err = true;
	sdio_al->debug.debug_data_on = 0;
	sdio_al->debug.debug_lpm_on = 0;
	sdio_al_print_info();
}

void sdio_al_register_lpm_cb(void *device_handle,
				       int(*lpm_callback)(void *, int))
{
	struct sdio_al_device *sdio_al_dev =
		(struct sdio_al_device *) device_handle;

	if (!sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s - "
				"device_handle is NULL\n", __func__);
		return;
	}

	if (lpm_callback) {
		sdio_al_dev->lpm_callback = lpm_callback;
		lpm_callback((void *)sdio_al_dev,
					   sdio_al_dev->is_ok_to_sleep);
	}

	LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": %s - device %d "
			"registered for wakeup callback\n", __func__,
			sdio_al_dev->host->index);
}

void sdio_al_unregister_lpm_cb(void *device_handle)
{
	struct sdio_al_device *sdio_al_dev =
		(struct sdio_al_device *) device_handle;

	if (!sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s - "
				"device_handle is NULL\n", __func__);
		return;
	}

	sdio_al_dev->lpm_callback = NULL;
	LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": %s - device %d "
		"unregister for wakeup callback\n", __func__,
		sdio_al_dev->host->index);
}

static void sdio_al_vote_for_sleep(struct sdio_al_device *sdio_al_dev,
				   int is_vote_for_sleep)
{
	pr_debug(MODULE_NAME ": %s()", __func__);

	if (!sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s - sdio_al_dev"
				" is NULL\n", __func__);
		return;
	}

	if (is_vote_for_sleep) {
		pr_debug(MODULE_NAME ": %s - sdio vote for Sleep", __func__);
		wake_unlock(&sdio_al_dev->wake_lock);
	} else {
		pr_debug(MODULE_NAME ": %s - sdio vote against sleep",
			  __func__);
		wake_lock(&sdio_al_dev->wake_lock);
	}

	if (sdio_al_dev->lpm_callback != NULL) {
		LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": %s - "
				"is_vote_for_sleep=%d for card#%d, "
				"calling callback...", __func__,
				is_vote_for_sleep,
				sdio_al_dev->host->index);
		sdio_al_dev->lpm_callback((void *)sdio_al_dev,
					   is_vote_for_sleep);
	}
}

/**
 *  Write SDIO-Client lpm information
 *  Should only be called with host claimed.
 */
static int write_lpm_info(struct sdio_al_device *sdio_al_dev)
{
	struct sdio_func *lpm_func = NULL;
	int offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config)+
		sizeof(struct peer_sdioc_channel_config) *
		sdio_al_dev->lpm_chan+
		offsetof(struct peer_sdioc_channel_config, is_host_ok_to_sleep);
	int ret;

	if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Invalid "
				"lpm_chan for card %d\n",
				sdio_al_dev->host->index);
		return -EINVAL;
	}

	if (!sdio_al_dev->card ||
		!sdio_al_dev->card->sdio_func[sdio_al_dev->lpm_chan+1]) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": NULL card or lpm_func\n");
		return -ENODEV;
	}
	lpm_func = sdio_al_dev->card->sdio_func[sdio_al_dev->lpm_chan+1];

	pr_debug(MODULE_NAME ":write_lpm_info is_ok_to_sleep=%d, device %d\n",
		 sdio_al_dev->is_ok_to_sleep,
		 sdio_al_dev->host->index);

	ret = sdio_memcpy_toio(lpm_func, SDIOC_SW_MAILBOX_ADDR+offset,
				&sdio_al_dev->is_ok_to_sleep, sizeof(u32));
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":failed to "
				"write lpm info for card %d\n",
				sdio_al_dev->host->index);
		return ret;
	}

	return 0;
}

/* Set inactivity counter to intial value to allow clients come up */
static inline void start_inactive_time(struct sdio_al_device *sdio_al_dev)
{
	sdio_al_dev->inactivity_time = jiffies +
		msecs_to_jiffies(INITIAL_INACTIVITY_TIME_MSEC);
}

static inline void restart_inactive_time(struct sdio_al_device *sdio_al_dev)
{
	sdio_al_dev->inactivity_time = jiffies +
		msecs_to_jiffies(INACTIVITY_TIME_MSEC);
}

static inline int is_inactive_time_expired(struct sdio_al_device *sdio_al_dev)
{
	return time_after(jiffies, sdio_al_dev->inactivity_time);
}


static int is_user_irq_enabled(struct sdio_al_device *sdio_al_dev,
			       int func_num)
{
	int ret = 0;
	struct sdio_func *func1;
	u32 user_irq = 0;
	u32 addr = 0;
	u32 offset = 0;
	u32 masked_user_irq = 0;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return 0;
	func1 = sdio_al_dev->card->sdio_func[0];

	if (func_num < 4) {
		addr = FUNC_1_4_USER_IRQ_ADDR;
		offset = func_num * 8;
	} else {
		addr = FUNC_5_7_USER_IRQ_ADDR;
		offset = (func_num - 4) * 8;
	}

	user_irq = sdio_readl(func1, addr, &ret);
	if (ret) {
		pr_debug(MODULE_NAME ":read_user_irq fail\n");
		return 0;
	}

	masked_user_irq = (user_irq >> offset) && 0xFF;
	if (masked_user_irq == 0x1) {
		sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":user_irq "
				"enabled\n");
		return 1;
	}

	return 0;
}

static void sdio_al_sleep(struct sdio_al_device *sdio_al_dev,
			  struct mmc_host *host)
{
	int i;

	/* Go to sleep */
	pr_debug(MODULE_NAME  ":Inactivity timer expired."
		" Going to sleep\n");
	/* Stop mailbox timer */
	stop_and_del_timer(sdio_al_dev);
	/* Make sure we get interrupt for non-packet-mode right away */
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		struct sdio_channel *ch = &sdio_al_dev->channel[i];
		if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
		    (ch->state != SDIO_CHANNEL_STATE_CLOSED)) {
			pr_debug(MODULE_NAME  ":continue for channel %s in"
					" state %d\n", ch->name, ch->state);
			continue;
		}
		if (ch->is_packet_mode == false) {
			ch->read_threshold = LOW_LATENCY_THRESHOLD;
			set_pipe_threshold(sdio_al_dev,
					   ch->rx_pipe_index,
					   ch->read_threshold);
		}
	}
	/* Prevent modem to go to sleep until we get the PROG_DONE on
	   the dummy CMD52 */
	msmsdcc_set_pwrsave(sdio_al_dev->host, 0);
	/* Mark HOST_OK_TOSLEEP */
	sdio_al_dev->is_ok_to_sleep = 1;
	write_lpm_info(sdio_al_dev);

	msmsdcc_lpm_enable(host);
	LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":Finished sleep sequence"
			" for card %d. Sleep now.\n",
		sdio_al_dev->host->index);
	/* Release wakelock */
	sdio_al_vote_for_sleep(sdio_al_dev, 1);
}


/**
 *  Read SDIO-Client Mailbox from Function#1.thresh_pipe
 *
 *  The mailbox contain the bytes available per pipe,
 *  and the End-Of-Transfer indication per pipe (if available).
 *
 * WARNING: Each time the Mailbox is read from the client, the
 * read_bytes_avail is incremented with another pending
 * transfer. Therefore, a pending rx-packet should be added to a
 * list before the next read of the mailbox.
 *
 * This function should run from a workqueue context since it
 * notifies the clients.
 *
 * This function assumes that sdio_al_claim_mutex was called before
 * calling it.
 *
 */
static int read_mailbox(struct sdio_al_device *sdio_al_dev, int from_isr)
{
	int ret;
	struct sdio_func *func1 = NULL;
	struct sdio_mailbox *mailbox = sdio_al_dev->mailbox;
	struct mmc_host *host = sdio_al_dev->host;
	u32 new_write_avail = 0;
	u32 old_write_avail = 0;
	u32 any_read_avail = 0;
	u32 any_write_pending = 0;
	int i;
	u32 rx_notify_bitmask = 0;
	u32 tx_notify_bitmask = 0;
	u32 eot_pipe = 0;
	u32 thresh_pipe = 0;
	u32 overflow_pipe = 0;
	u32 underflow_pipe = 0;
	u32 thresh_intr_mask = 0;
	int is_closing = 0;

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		return 0;
	}

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = sdio_al_dev->card->sdio_func[0];

	pr_debug(MODULE_NAME ":start %s from_isr = %d for card %d.\n"
		 , __func__, from_isr, sdio_al_dev->host->index);

	pr_debug(MODULE_NAME ":before sdio_memcpy_fromio.\n");
	memset(mailbox, 0, sizeof(struct sdio_mailbox));
	ret = sdio_memcpy_fromio(func1, mailbox,
			HW_MAILBOX_ADDR, sizeof(*mailbox));
	pr_debug(MODULE_NAME ":after sdio_memcpy_fromio.\n");
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to read "
				"Mailbox for card %d, goto error state\n",
				sdio_al_dev->host->index);
		sdio_al_get_into_err_state(sdio_al_dev);
		goto exit_err;
	}

	eot_pipe =	(mailbox->eot_pipe_0_7) |
			(mailbox->eot_pipe_8_15<<8);
	thresh_pipe = 	(mailbox->thresh_above_limit_pipe_0_7) |
			(mailbox->thresh_above_limit_pipe_8_15<<8);

	overflow_pipe = (mailbox->overflow_pipe_0_7) |
			(mailbox->overflow_pipe_8_15<<8);
	underflow_pipe = mailbox->underflow_pipe_0_7 |
			(mailbox->underflow_pipe_8_15<<8);
	thresh_intr_mask =
		(mailbox->mask_thresh_above_limit_pipe_0_7) |
		(mailbox->mask_thresh_above_limit_pipe_8_15<<8);

	if (overflow_pipe || underflow_pipe)
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Mailbox ERROR "
				"overflow=0x%x, underflow=0x%x\n",
				overflow_pipe, underflow_pipe);

	/* In case of modem reset we would like to read the daya from the modem
	   to clear the interrupts but do not process it */
	if (sdio_al_dev->state != CARD_INSERTED) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":sdio_al_device"
				" (card %d) is in invalid state %d\n",
				sdio_al_dev->host->index,
				sdio_al_dev->state);
		return -ENODEV;
	}

	pr_debug(MODULE_NAME ":card %d: eot=0x%x, thresh=0x%x\n",
			sdio_al_dev->host->index,
			eot_pipe, thresh_pipe);

	/* Scan for Rx Packets available and update read available bytes */
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		struct sdio_channel *ch = &sdio_al_dev->channel[i];
		u32 old_read_avail;
		u32 read_avail;
		u32 new_packet_size = 0;

		if (ch->state == SDIO_CHANNEL_STATE_CLOSING)
			is_closing = true; /* used to prevent sleep */

		old_read_avail = ch->read_avail;
		read_avail = mailbox->pipe_bytes_avail[ch->rx_pipe_index];

		if ((ch->state == SDIO_CHANNEL_STATE_CLOSED) &&
			(read_avail > 0)) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				 ":%s: Invalid read_avail 0x%x, for CLOSED ch %s\n",
				 __func__, read_avail, ch->name);
			sdio_read_from_closed_ch(ch, read_avail);
		}
		if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
		    (ch->state != SDIO_CHANNEL_STATE_CLOSING))
			continue;

		if (read_avail > INVALID_DATA_AVAILABLE) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				 ":Invalid read_avail 0x%x for pipe %d\n",
				 read_avail, ch->rx_pipe_index);
			continue;
		}
		any_read_avail |= read_avail | old_read_avail;
		ch->statistics.last_any_read_avail = any_read_avail;
		ch->statistics.last_read_avail = read_avail;
		ch->statistics.last_old_read_avail = old_read_avail;

		if (ch->is_packet_mode) {
			if ((eot_pipe & (1<<ch->rx_pipe_index)) &&
			    sdio_al_dev->print_after_interrupt) {
				LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME
					":Interrupt on ch %s, "
					"card %d", ch->name,
					sdio_al_dev->host->index);
			}
			new_packet_size = check_pending_rx_packet(ch, eot_pipe);
		} else {
			if ((thresh_pipe & (1<<ch->rx_pipe_index)) &&
			    sdio_al_dev->print_after_interrupt) {
				LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME
					":Interrupt on ch %s, "
					"card %d", ch->name,
					sdio_al_dev->host->index);
			}
			ch->read_avail = read_avail;

			/*
			 * Restore default thresh for non packet channels.
			 * in case it IS low latency channel then read_threshold
			 * and def_read_threshold are both
			 * LOW_LATENCY_THRESHOLD
			 */
			if ((ch->read_threshold != ch->def_read_threshold) &&
			    (read_avail >= ch->threshold_change_cnt)) {
				if (!ch->is_low_latency_ch) {
					ch->read_threshold =
						ch->def_read_threshold;
					set_pipe_threshold(sdio_al_dev,
							   ch->rx_pipe_index,
							   ch->read_threshold);
				}
			}
		}

		if ((ch->is_packet_mode) && (new_packet_size > 0)) {
			rx_notify_bitmask |= (1<<ch->num);
			ch->statistics.total_notifs++;
		}

		if ((!ch->is_packet_mode) && (ch->read_avail > 0) &&
		    (old_read_avail == 0)) {
			rx_notify_bitmask |= (1<<ch->num);
			ch->statistics.total_notifs++;
		}
	}
	sdio_al_dev->print_after_interrupt = 0;

	/* Update Write available */
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		struct sdio_channel *ch = &sdio_al_dev->channel[i];

		if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
		    (ch->state != SDIO_CHANNEL_STATE_CLOSING))
			continue;

		new_write_avail = mailbox->pipe_bytes_avail[ch->tx_pipe_index];

		if (new_write_avail > INVALID_DATA_AVAILABLE) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				 ":Invalid write_avail 0x%x for pipe %d\n",
				 new_write_avail, ch->tx_pipe_index);
			continue;
		}

		old_write_avail = ch->write_avail;
		ch->write_avail = new_write_avail;

		if ((old_write_avail <= ch->min_write_avail) &&
			(new_write_avail >= ch->min_write_avail))
			tx_notify_bitmask |= (1<<ch->num);

		/* There is not enough write avail for this channel.
		   We need to keep reading mailbox to wait for the appropriate
		   write avail and cannot sleep. Ignore SMEM channel that has
		   only one direction. */
		if (strncmp(ch->name, "SDIO_SMEM", CHANNEL_NAME_SIZE))
			any_write_pending |=
			(new_write_avail < ch->ch_config.max_tx_threshold);
	}
	/* notify clients */
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		struct sdio_channel *ch = &sdio_al_dev->channel[i];

		if ((ch->state != SDIO_CHANNEL_STATE_OPEN) ||
				(ch->notify == NULL))
			continue;

		if (rx_notify_bitmask & (1<<ch->num))
			ch->notify(ch->priv,
					   SDIO_EVENT_DATA_READ_AVAIL);

		if (tx_notify_bitmask & (1<<ch->num))
			ch->notify(ch->priv,
					   SDIO_EVENT_DATA_WRITE_AVAIL);
	}


	if ((rx_notify_bitmask == 0) && (tx_notify_bitmask == 0) &&
	    !any_read_avail && !any_write_pending) {
		DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":Nothing to "
				"Notify for card %d, is_closing=%d\n",
				sdio_al_dev->host->index, is_closing);
		if (is_closing)
			restart_inactive_time(sdio_al_dev);
		else if (is_inactive_time_expired(sdio_al_dev))
			sdio_al_sleep(sdio_al_dev, host);
	} else {
		DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":Notify bitmask"
				" for card %d rx=0x%x, tx=0x%x.\n",
				sdio_al_dev->host->index,
				rx_notify_bitmask, tx_notify_bitmask);
		/* Restart inactivity timer if any activity on the channel */
		restart_inactive_time(sdio_al_dev);
	}

	pr_debug(MODULE_NAME ":end %s.\n", __func__);

exit_err:
	return ret;
}

/**
 *  Check pending rx packet when reading the mailbox.
 */
static u32 check_pending_rx_packet(struct sdio_channel *ch, u32 eot)
{
	u32 rx_pending;
	u32 rx_avail;
	u32 new_packet_size = 0;
	struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;


	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": NULL sdio_al_dev"
				" for channel %s\n", ch->name);
		return -EINVAL;
	}

	mutex_lock(&ch->ch_lock);

	rx_pending = ch->rx_pending_bytes;
	rx_avail = sdio_al_dev->mailbox->pipe_bytes_avail[ch->rx_pipe_index];

	pr_debug(MODULE_NAME ":pipe %d of card %d rx_avail=0x%x, "
			     "rx_pending=0x%x\n",
	   ch->rx_pipe_index, sdio_al_dev->host->index, rx_avail,
		 rx_pending);


	/* new packet detected */
	if (eot & (1<<ch->rx_pipe_index)) {
		struct rx_packet_size *p = NULL;
		new_packet_size = rx_avail - rx_pending;

		if ((rx_avail <= rx_pending)) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": Invalid new packet size."
					" rx_avail=%d.\n", rx_avail);
			new_packet_size = 0;
			goto exit_err;
		}

		p = kzalloc(sizeof(*p), GFP_KERNEL);
		if (p == NULL) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": failed to allocate item for "
					"rx_pending list. rx_avail=%d, "
					"rx_pending=%d.\n",
					rx_avail, rx_pending);
			new_packet_size = 0;
			goto exit_err;
		}
		p->size = new_packet_size;
		/* Add new packet as last */
		list_add_tail(&p->list, &ch->rx_size_list_head);
		ch->rx_pending_bytes += new_packet_size;

		if (ch->read_avail == 0)
			ch->read_avail = new_packet_size;
	}

exit_err:
	mutex_unlock(&ch->ch_lock);

	return new_packet_size;
}



/**
 *  Remove first pending packet from the list.
 */
static u32 remove_handled_rx_packet(struct sdio_channel *ch)
{
	struct rx_packet_size *p = NULL;

	mutex_lock(&ch->ch_lock);

	ch->rx_pending_bytes -= ch->read_avail;

	if (!list_empty(&ch->rx_size_list_head)) {
		p = list_first_entry(&ch->rx_size_list_head,
			struct rx_packet_size, list);
		list_del(&p->list);
		kfree(p);
	} else {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME ":%s: ch "
				"%s: unexpected empty list!!\n",
				__func__, ch->name);
	}

	if (list_empty(&ch->rx_size_list_head))	{
		ch->read_avail = 0;
	} else {
		p = list_first_entry(&ch->rx_size_list_head,
			struct rx_packet_size, list);
		ch->read_avail = p->size;
	}

	mutex_unlock(&ch->ch_lock);

	return ch->read_avail;
}


/**
 *  Bootloader worker function.
 *
 *  @note: clear the bootloader_done flag only after reading the
 *  mailbox, to ignore more requests while reading the mailbox.
 */
static void boot_worker(struct work_struct *work)
{
	int ret = 0;
	int func_num = 0;
	int i;
	struct sdio_al_device *sdio_al_dev = NULL;
	struct sdio_al_work *sdio_al_work = container_of(work,
							 struct sdio_al_work,
							 work);

	if (sdio_al_work == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
				"sdio_al_work\n", __func__);
		return;
	}

	sdio_al_dev = sdio_al_work->sdio_al_dev;
	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
				"sdio_al_dev\n", __func__);
		return;
	}
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":Bootloader Worker Started"
			", wait for bootloader_done event..\n");
	wait_event(sdio_al_dev->wait_mbox,
		   sdio_al_dev->bootloader_done);
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":Got bootloader_done "
			"event..\n");
	/* Do polling until MDM is up */
	for (i = 0; i < 5000; ++i) {
		if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
			return;
		if (is_user_irq_enabled(sdio_al_dev, func_num)) {
			sdio_al_release_mutex(sdio_al_dev, __func__);
			sdio_al_dev->bootloader_done = 0;
			ret = sdio_al_client_setup(sdio_al_dev);
			if (ret) {
				sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": sdio_al_client_setup failed, "
					"for card %d ret=%d\n",
					sdio_al_dev->host->index, ret);
				sdio_al_get_into_err_state(sdio_al_dev);
			}
			goto done;
		}
		sdio_al_release_mutex(sdio_al_dev, __func__);
		msleep(100);
	}
	sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Timeout waiting for "
			"user_irq for card %d\n",
			sdio_al_dev->host->index);
	sdio_al_get_into_err_state(sdio_al_dev);

done:
	pr_debug(MODULE_NAME ":Boot Worker for card %d Exit!\n",
		sdio_al_dev->host->index);
}

/**
 *  Worker function.
 *
 *  @note: clear the ask_mbox flag only after
 *  	 reading the mailbox, to ignore more requests while
 *  	 reading the mailbox.
 */
static void worker(struct work_struct *work)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = NULL;
	struct sdio_al_work *sdio_al_work = container_of(work,
							 struct sdio_al_work,
							 work);
	if (sdio_al_work == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": worker: NULL "
				"sdio_al_work\n");
		return;
	}

	sdio_al_dev = sdio_al_work->sdio_al_dev;
	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": worker: NULL "
				"sdio_al_dev\n");
		return;
	}
	pr_debug(MODULE_NAME ":Worker Started..\n");
	while ((sdio_al_dev->is_ready) && (ret == 0)) {
		pr_debug(MODULE_NAME ":Wait for read mailbox request..\n");
		wait_event(sdio_al_dev->wait_mbox, sdio_al_dev->ask_mbox);
		if (!sdio_al_dev->is_ready)
			break;
		if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
			break;
		if (sdio_al_dev->is_ok_to_sleep) {
			ret = sdio_al_wake_up(sdio_al_dev, 1, NULL);
			if (ret) {
				sdio_al_release_mutex(sdio_al_dev, __func__);
				return;
			}
		}
		ret = read_mailbox(sdio_al_dev, false);
		sdio_al_release_mutex(sdio_al_dev, __func__);
		sdio_al_dev->ask_mbox = false;
	}

	pr_debug(MODULE_NAME ":Worker Exit!\n");
}

/**
 *  Write command using CMD54 rather than CMD53.
 *  Writing with CMD54 generate EOT interrupt at the
 *  SDIO-Client.
 *  Based on mmc_io_rw_extended()
 */
static int sdio_write_cmd54(struct mmc_card *card, unsigned fn,
	unsigned addr, const u8 *buf,
	unsigned blocks, unsigned blksz)
{
	struct mmc_request mrq;
	struct mmc_command cmd;
	struct mmc_data data;
	struct scatterlist sg;
	int incr_addr = 1; /* MUST */
	int write = 1;

	BUG_ON(!card);
	BUG_ON(fn > 7);
	BUG_ON(blocks == 1 && blksz > 512);
	WARN_ON(blocks == 0);
	WARN_ON(blksz == 0);

	write = true;
	pr_debug(MODULE_NAME ":sdio_write_cmd54()"
		"fn=%d,buf=0x%x,blocks=%d,blksz=%d\n",
		fn, (u32) buf, blocks, blksz);

	memset(&mrq, 0, sizeof(struct mmc_request));
	memset(&cmd, 0, sizeof(struct mmc_command));
	memset(&data, 0, sizeof(struct mmc_data));

	mrq.cmd = &cmd;
	mrq.data = &data;

	cmd.opcode = SD_IO_RW_EXTENDED_QCOM;

	cmd.arg = write ? 0x80000000 : 0x00000000;
	cmd.arg |= fn << 28;
	cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
	cmd.arg |= addr << 9;
	if (blocks == 1 && blksz <= 512)
		cmd.arg |= (blksz == 512) ? 0 : blksz;  /* byte mode */
	else
		cmd.arg |= 0x08000000 | blocks; 	/* block mode */
	cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;

	data.blksz = blksz;
	data.blocks = blocks;
	data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
	data.sg = &sg;
	data.sg_len = 1;

	sg_init_one(&sg, buf, blksz * blocks);

	mmc_set_data_timeout(&data, card);

	mmc_wait_for_req(card->host, &mrq);

	if (cmd.error)
		return cmd.error;
	if (data.error)
		return data.error;

	if (mmc_host_is_spi(card->host)) {
		/* host driver already reported errors */
	} else {
		if (cmd.resp[0] & R5_ERROR) {
			sdio_al_loge(&sdio_al->gen_log, MODULE_NAME
						":%s: R5_ERROR for card %d",
						__func__, card->host->index);
			return -EIO;
		}
		if (cmd.resp[0] & R5_FUNCTION_NUMBER) {
			sdio_al_loge(&sdio_al->gen_log, MODULE_NAME
						":%s: R5_FUNCTION_NUMBER for card %d",
						__func__, card->host->index);
			return -EINVAL;
		}
		if (cmd.resp[0] & R5_OUT_OF_RANGE) {
			sdio_al_loge(&sdio_al->gen_log, MODULE_NAME
						":%s: R5_OUT_OF_RANGE for card %d",
						__func__, card->host->index);
			return -ERANGE;
		}
	}

	return 0;
}


/**
 *  Write data to channel.
 *  Handle different data size types.
 *
 */
static int sdio_ch_write(struct sdio_channel *ch, const u8 *buf, u32 len)
{
	int ret = 0;
	unsigned blksz = ch->func->cur_blksize;
	int blocks = len / blksz;
	int remain_bytes = len % blksz;
	struct mmc_card *card = NULL;
	u32 fn = ch->func->num;

	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
				"channel\n", __func__);
		return -ENODEV;
	}

	if (!ch->sdio_al_dev) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
				"sdio_al_dev\n", __func__);
		return -ENODEV;
	}

	if (len == 0) {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME ":channel "
				"%s trying to write 0 bytes\n", ch->name);
		return -EINVAL;
	}

	card = ch->func->card;

	if (remain_bytes) {
		/* CMD53 */
		if (blocks) {
			ret = sdio_memcpy_toio(ch->func, PIPE_TX_FIFO_ADDR,
					       (void *) buf, blocks*blksz);
			if (ret != 0) {
				sdio_al_loge(ch->sdio_al_dev->dev_log,
					MODULE_NAME ":%s: sdio_memcpy_toio "
					"failed for channel %s\n",
					__func__, ch->name);
				sdio_al_get_into_err_state(ch->sdio_al_dev);
				return ret;
			}
		}

		buf += (blocks*blksz);

		ret = sdio_write_cmd54(card, fn, PIPE_TX_FIFO_ADDR,
				buf, 1, remain_bytes);
	} else {
		ret = sdio_write_cmd54(card, fn, PIPE_TX_FIFO_ADDR,
				buf, blocks, blksz);
	}

	if (ret != 0) {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME ":%s: "
				"sdio_write_cmd54 failed for channel %s\n",
				__func__, ch->name);
		ch->sdio_al_dev->is_err = true;
		return ret;
	}

	return ret;
}

static int sdio_al_bootloader_completed(void)
{
	int i;

	pr_debug(MODULE_NAME ":sdio_al_bootloader_completed was called\n");

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
		struct sdio_al_device *dev = NULL;
		if (sdio_al->devices[i] == NULL)
			continue;
		dev = sdio_al->devices[i];
		dev->bootloader_done = 1;
		wake_up(&dev->wait_mbox);
	}

	return 0;
}

static int sdio_al_wait_for_bootloader_comp(struct sdio_al_device *sdio_al_dev)
{
	int ret = 0;

	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	/*
	 * Enable function 0 interrupt mask to allow 9k to raise this interrupt
	 * in power-up. When sdio_downloader will notify its completion
	 * we will poll on this interrupt to wait for 9k power-up
	 */
	ret = enable_mask_irq(sdio_al_dev, 0, 1, 0);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": Enable_mask_irq for card %d failed, "
				"ret=%d\n",
				sdio_al_dev->host->index, ret);
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return ret;
	}

	sdio_al_release_mutex(sdio_al_dev, __func__);

	/*
	 * Start bootloader worker that will wait for the bootloader
	 * completion
	 */
	sdio_al_dev->boot_work.sdio_al_dev = sdio_al_dev;
	INIT_WORK(&sdio_al_dev->boot_work.work, boot_worker);
	sdio_al_dev->bootloader_done = 0;
	queue_work(sdio_al_dev->workqueue, &sdio_al_dev->boot_work.work);

	return 0;
}

static int sdio_al_bootloader_setup(void)
{
	int ret = 0;
	struct sdio_al_device *bootloader_dev = sdio_al->bootloader_dev;
	struct sdio_func *func1 = NULL;

	if (sdio_al_claim_mutex_and_verify_dev(bootloader_dev, __func__))
		return -ENODEV;

	if (bootloader_dev->flashless_boot_on) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME ":Already "
			"in boot process.\n");
		sdio_al_release_mutex(bootloader_dev, __func__);
		return 0;
	}

	bootloader_dev->sdioc_boot_sw_header
		= kzalloc(sizeof(*bootloader_dev->sdioc_boot_sw_header),
			  GFP_KERNEL);
	if (bootloader_dev->sdioc_boot_sw_header == NULL) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME ":fail to "
			"allocate sdioc boot sw header.\n");
		sdio_al_release_mutex(bootloader_dev, __func__);
		return -ENOMEM;
	}

	if (sdio_al_verify_func1(bootloader_dev, __func__)) {
		sdio_al_release_mutex(bootloader_dev, __func__);
		goto exit_err;
	}
	func1 = bootloader_dev->card->sdio_func[0];

	ret = sdio_memcpy_fromio(func1,
				 bootloader_dev->sdioc_boot_sw_header,
				 SDIOC_SW_HEADER_ADDR,
				 sizeof(struct peer_sdioc_boot_sw_header));
	if (ret) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME ":fail to "
			"read sdioc boot sw header.\n");
		sdio_al_release_mutex(bootloader_dev, __func__);
		goto exit_err;
	}

	if (bootloader_dev->sdioc_boot_sw_header->signature !=
	    (u32) PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME ":invalid "
			"mailbox signature 0x%x.\n",
			bootloader_dev->sdioc_boot_sw_header->signature);
		sdio_al_release_mutex(bootloader_dev, __func__);
		ret = -EINVAL;
		goto exit_err;
	}

	/* Upper byte has to be equal - no backward compatibility for unequal */
	if ((bootloader_dev->sdioc_boot_sw_header->version >> 16) !=
	    (sdio_al->pdata->peer_sdioc_boot_version_major)) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME ": HOST(0x%x)"
			" and CLIENT(0x%x) SDIO_AL BOOT VERSION don't match\n",
			((sdio_al->pdata->peer_sdioc_boot_version_major<<16)+
			sdio_al->pdata->peer_sdioc_boot_version_minor),
			bootloader_dev->sdioc_boot_sw_header->version);
		sdio_al_release_mutex(bootloader_dev, __func__);
		ret = -EIO;
		goto exit_err;
	}

	sdio_al_logi(bootloader_dev->dev_log, MODULE_NAME ": SDIOC BOOT SW "
			"version 0x%x\n",
			bootloader_dev->sdioc_boot_sw_header->version);

	bootloader_dev->flashless_boot_on = true;

	sdio_al_release_mutex(bootloader_dev, __func__);

	ret = sdio_al_wait_for_bootloader_comp(bootloader_dev);
	if (ret) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME
				": sdio_al_wait_for_bootloader_comp failed, "
				"err=%d\n", ret);
		goto exit_err;
	}

	ret = sdio_downloader_setup(bootloader_dev->card, 1,
			bootloader_dev->sdioc_boot_sw_header->boot_ch_num,
			sdio_al_bootloader_completed);

	if (ret) {
		sdio_al_loge(bootloader_dev->dev_log, MODULE_NAME
			": sdio_downloader_setup failed, err=%d\n", ret);
		goto exit_err;
	}

	sdio_al_logi(bootloader_dev->dev_log, MODULE_NAME ":In Flashless boot,"
		" waiting for its completion\n");


exit_err:
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":free "
			"sdioc_boot_sw_header.\n");
	kfree(bootloader_dev->sdioc_boot_sw_header);
	bootloader_dev->sdioc_boot_sw_header = NULL;
	bootloader_dev = NULL;

	return ret;
}


/**
 *  Read SDIO-Client software header
 *
 */
static int read_sdioc_software_header(struct sdio_al_device *sdio_al_dev,
				      struct peer_sdioc_sw_header *header)
{
	int ret;
	int i;
	int test_version = 0;
	int sdioc_test_version = 0;
	struct sdio_func *func1 = NULL;

	pr_debug(MODULE_NAME ":reading sdioc sw header.\n");

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;

	func1 = sdio_al_dev->card->sdio_func[0];

	ret = sdio_memcpy_fromio(func1, header,
			SDIOC_SW_HEADER_ADDR, sizeof(*header));
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":fail to read "
				"sdioc sw header.\n");
		goto exit_err;
	}

	if (header->signature == (u32)PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE) {
		sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":SDIOC SW "
				"unittest signature. 0x%x\n",
				header->signature);
		sdio_al->unittest_mode = true;
		/* Verify test code compatibility with the modem */
		sdioc_test_version = (header->version & 0xFF00) >> 8;
		test_version = sdio_al->pdata->peer_sdioc_version_minor >> 8;
		if (test_version != sdioc_test_version) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": HOST(0x%x) and CLIENT(0x%x) "
				"testing VERSION don't match\n",
				test_version,
				sdioc_test_version);
			msleep(500);
			BUG();
		}
	}

	if ((header->signature != (u32) PEER_SDIOC_SW_MAILBOX_SIGNATURE) &&
	    (header->signature != (u32) PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE)) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":SDIOC SW "
				"invalid signature. 0x%x\n", header->signature);
		goto exit_err;
	}
	/* Upper byte has to be equal - no backward compatibility for unequal */
	sdio_al->sdioc_major = header->version >> 16;
	if (sdio_al->pdata->allow_sdioc_version_major_2) {
		if ((sdio_al->sdioc_major !=
		    sdio_al->pdata->peer_sdioc_version_major) &&
		    (sdio_al->sdioc_major != PEER_SDIOC_OLD_VERSION_MAJOR)) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": HOST(0x%x) and CLIENT(0x%x) "
				"SDIO_AL VERSION don't match\n",
				((sdio_al->pdata->peer_sdioc_version_major<<16)+
				sdio_al->pdata->peer_sdioc_version_minor),
				header->version);
			goto exit_err;
		}
	} else {
		if (sdio_al->sdioc_major !=
		    sdio_al->pdata->peer_sdioc_version_major) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": HOST(0x%x) and CLIENT(0x%x) "
				"SDIO_AL VERSION don't match\n",
				((sdio_al->pdata->peer_sdioc_version_major<<16)+
				sdio_al->pdata->peer_sdioc_version_minor),
				header->version);
			goto exit_err;
		}
	}
	sdio_al_dev->ch_close_supported = (header->version & 0x000F) >=
		(sdio_al->pdata->peer_sdioc_version_minor & 0xF);

	sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":SDIOC SW version 0x%x,"
			" sdio_al major 0x%x minor 0x%x\n", header->version,
			sdio_al->sdioc_major,
			sdio_al->pdata->peer_sdioc_version_minor);

	sdio_al_dev->flashless_boot_on = false;
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		struct sdio_channel *ch = &sdio_al_dev->channel[i];

		/* Set default values */
		ch->read_threshold  = DEFAULT_READ_THRESHOLD;
		ch->write_threshold = DEFAULT_WRITE_THRESHOLD;
		ch->min_write_avail = DEFAULT_MIN_WRITE_THRESHOLD;
		ch->is_packet_mode = true;
		ch->peer_tx_buf_size = DEFAULT_PEER_TX_BUF_SIZE;
		ch->poll_delay_msec = 0;

		ch->num = i;
		ch->func = NULL;
		ch->rx_pipe_index = ch->num*2;
		ch->tx_pipe_index = ch->num*2+1;

		memset(ch->name, 0, sizeof(ch->name));

		if (header->channel_names[i][0]) {
			memcpy(ch->name, SDIO_PREFIX,
			       strlen(SDIO_PREFIX));
			memcpy(ch->name + strlen(SDIO_PREFIX),
			       header->channel_names[i],
			       PEER_CHANNEL_NAME_SIZE);

			ch->state = SDIO_CHANNEL_STATE_IDLE;
			ch->sdio_al_dev = sdio_al_dev;
			if (sdio_al_dev->card->sdio_func[ch->num+1]) {
				ch->func =
				sdio_al_dev->card->sdio_func[ch->num+1];
			} else {
				sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": NULL func for channel %s\n",
					ch->name);
				goto exit_err;
			}
		} else {
			ch->state = SDIO_CHANNEL_STATE_INVALID;
		}

		sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":Channel=%s, "
				"state=%d\n", ch->name,	ch->state);
	}

	return 0;

exit_err:
	sdio_al_get_into_err_state(sdio_al_dev);
	memset(header, 0, sizeof(*header));

	return -EIO;
}

/**
 *  Read SDIO-Client channel configuration
 *
 */
static int read_sdioc_channel_config(struct sdio_channel *ch)
{
	int ret;
	struct peer_sdioc_sw_mailbox *sw_mailbox = NULL;
	struct peer_sdioc_channel_config *ch_config = NULL;
	struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;

	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": NULL sdio_al_dev"
				" for channel %s\n", ch->name);
		return -EINVAL;
	}

	if (sdio_al_dev->sdioc_sw_header->version == 0)
		return -1;

	pr_debug(MODULE_NAME ":reading sw mailbox %s channel.\n", ch->name);

	sw_mailbox = kzalloc(sizeof(*sw_mailbox), GFP_KERNEL);
	if (sw_mailbox == NULL)
		return -ENOMEM;

	ret = sdio_memcpy_fromio(ch->func, sw_mailbox,
			SDIOC_SW_MAILBOX_ADDR, sizeof(*sw_mailbox));
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":fail to read "
				"sw mailbox.\n");
		goto exit_err;
	}

	ch_config = &sw_mailbox->ch_config[ch->num];
	memcpy(&ch->ch_config, ch_config,
		sizeof(struct peer_sdioc_channel_config));

	if (!ch_config->is_ready) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":sw mailbox "
				"channel not ready.\n");
		goto exit_err;
	}

	ch->read_threshold = LOW_LATENCY_THRESHOLD;
	ch->is_low_latency_ch = ch_config->is_low_latency_ch;
	/* Threshold on 50% of the maximum size , sdioc uses double-buffer */
	ch->write_threshold = (ch_config->max_tx_threshold * 5) / 10;
	ch->threshold_change_cnt = ch->ch_config.max_rx_threshold -
			ch->read_threshold + THRESHOLD_CHANGE_EXTRA_BYTES;

	if (ch->is_low_latency_ch)
		ch->def_read_threshold = LOW_LATENCY_THRESHOLD;
	else /* Aggregation up to 90% of the maximum size */
		ch->def_read_threshold = (ch_config->max_rx_threshold * 9) / 10;

	ch->is_packet_mode = ch_config->is_packet_mode;
	if (!ch->is_packet_mode) {
		ch->poll_delay_msec = DEFAULT_POLL_DELAY_NOPACKET_MSEC;
		ch->min_write_avail = DEFAULT_MIN_WRITE_THRESHOLD_STREAMING;
	}
	/* The max_packet_size is set by the modem in version 3 and on */
	if (sdio_al->sdioc_major > PEER_SDIOC_OLD_VERSION_MAJOR)
		ch->min_write_avail = ch_config->max_packet_size;

	if (ch->min_write_avail > ch->write_threshold)
		ch->min_write_avail = ch->write_threshold;

	CLOSE_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":ch %s "
			"read_threshold=%d, write_threshold=%d,"
			" min_write_avail=%d, max_rx_threshold=%d,"
			" max_tx_threshold=%d\n", ch->name, ch->read_threshold,
			ch->write_threshold, ch->min_write_avail,
			ch_config->max_rx_threshold,
			ch_config->max_tx_threshold);

	ch->peer_tx_buf_size = ch_config->tx_buf_size;

	kfree(sw_mailbox);

	return 0;

exit_err:
	sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":Reading SW Mailbox "
			"error.\n");
	kfree(sw_mailbox);

	return -1;
}


/**
 *  Enable/Disable EOT interrupt of a pipe.
 *
 */
static int enable_eot_interrupt(struct sdio_al_device *sdio_al_dev,
				int pipe_index, int enable)
{
	int ret = 0;
	struct sdio_func *func1;
	u32 mask;
	u32 pipe_mask;
	u32 addr;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = sdio_al_dev->card->sdio_func[0];

	if (pipe_index < 8) {
		addr = PIPES_0_7_IRQ_MASK_ADDR;
		pipe_mask = (1<<pipe_index);
	} else {
		addr = PIPES_8_15_IRQ_MASK_ADDR;
		pipe_mask = (1<<(pipe_index-8));
	}

	mask = sdio_readl(func1, addr, &ret);
	if (ret) {
		pr_debug(MODULE_NAME ":enable_eot_interrupt fail\n");
		goto exit_err;
	}

	if (enable)
		mask &= (~pipe_mask); /* 0 = enable */
	else
		mask |= (pipe_mask);  /* 1 = disable */

	sdio_writel(func1, mask, addr, &ret);

exit_err:
	return ret;
}


/**
 *  Enable/Disable mask interrupt of a function.
 *
 */
static int enable_mask_irq(struct sdio_al_device *sdio_al_dev,
			   int func_num, int enable, u8 bit_offset)
{
	int ret = 0;
	struct sdio_func *func1 = NULL;
	u32 mask = 0;
	u32 func_mask = 0;
	u32 addr = 0;
	u32 offset = 0;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = sdio_al_dev->card->sdio_func[0];

	if (func_num < 4) {
		addr = FUNC_1_4_MASK_IRQ_ADDR;
		offset = func_num * 8 + bit_offset;
	} else {
		addr = FUNC_5_7_MASK_IRQ_ADDR;
		offset = (func_num - 4) * 8 + bit_offset;
	}

	func_mask = 1<<offset;

	mask = sdio_readl(func1, addr, &ret);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"enable_mask_irq fail\n");
		goto exit_err;
	}

	if (enable)
		mask &= (~func_mask); /* 0 = enable */
	else
		mask |= (func_mask);  /* 1 = disable */

	pr_debug(MODULE_NAME ":enable_mask_irq,  writing mask = 0x%x\n", mask);

	sdio_writel(func1, mask, addr, &ret);

exit_err:
	return ret;
}

/**
 *  Enable/Disable Threshold interrupt of a pipe.
 *
 */
static int enable_threshold_interrupt(struct sdio_al_device *sdio_al_dev,
				      int pipe_index, int enable)
{
	int ret = 0;
	struct sdio_func *func1;
	u32 mask;
	u32 pipe_mask;
	u32 addr;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = sdio_al_dev->card->sdio_func[0];

	if (pipe_index < 8) {
		addr = PIPES_0_7_IRQ_MASK_ADDR;
		pipe_mask = (1<<pipe_index);
	} else {
		addr = PIPES_8_15_IRQ_MASK_ADDR;
		pipe_mask = (1<<(pipe_index-8));
	}

	mask = sdio_readl(func1, addr, &ret);
	if (ret) {
		pr_debug(MODULE_NAME ":enable_threshold_interrupt fail\n");
		goto exit_err;
	}

	pipe_mask = pipe_mask<<8; /* Threshold bits 8..15 */
	if (enable)
		mask &= (~pipe_mask); /* 0 = enable */
	else
		mask |= (pipe_mask);  /* 1 = disable */

	sdio_writel(func1, mask, addr, &ret);

exit_err:
	return ret;
}

/**
 *  Set the threshold to trigger interrupt from SDIO-Card on
 *  pipe available bytes.
 *
 */
static int set_pipe_threshold(struct sdio_al_device *sdio_al_dev,
			      int pipe_index, int threshold)
{
	int ret = 0;
	struct sdio_func *func1;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = sdio_al_dev->card->sdio_func[0];

	sdio_writel(func1, threshold,
			PIPES_THRESHOLD_ADDR+pipe_index*4, &ret);
	if (ret)
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"set_pipe_threshold err=%d\n", -ret);

	return ret;
}

/**
 *  Enable func w/ retries
 *
 */
static int sdio_al_enable_func_retry(struct sdio_func *func, const char *name)
{
	int ret, i;
	for (i = 0; i < 200; i++) {
		ret = sdio_enable_func(func);
		if (ret) {
			pr_debug(MODULE_NAME ":retry enable %s func#%d "
					     "ret=%d\n",
					 name, func->num, ret);
			msleep(10);
		} else
			break;
	}

	return ret;
}

/**
 *  Open Channel
 *
 *  1. Init Channel Context.
 *  2. Init the Channel SDIO-Function.
 *  3. Init the Channel Pipes on Mailbox.
 */
static int open_channel(struct sdio_channel *ch)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = ch->sdio_al_dev;

	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": NULL "
				"sdio_al_dev for channel %s\n", ch->name);
		return -EINVAL;
	}

	/* Init channel Context */
	/** Func#1 is reserved for mailbox */
	ch->func = sdio_al_dev->card->sdio_func[ch->num+1];
	ch->rx_pipe_index = ch->num*2;
	ch->tx_pipe_index = ch->num*2+1;
	ch->signature = SDIO_AL_SIGNATURE;

	ch->total_rx_bytes = 0;
	ch->total_tx_bytes = 0;

	ch->write_avail = 0;
	ch->read_avail = 0;
	ch->rx_pending_bytes = 0;

	mutex_init(&ch->ch_lock);

	pr_debug(MODULE_NAME ":open_channel %s func#%d\n",
			 ch->name, ch->func->num);

	INIT_LIST_HEAD(&(ch->rx_size_list_head));

	/* Init SDIO Function */
	ret = sdio_al_enable_func_retry(ch->func, ch->name);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"sdio_enable_func() err=%d\n", -ret);
		goto exit_err;
	}

	/* Note: Patch Func CIS tuple issue */
	ret = sdio_set_block_size(ch->func, SDIO_AL_BLOCK_SIZE);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"sdio_set_block_size()failed, err=%d\n", -ret);
		goto exit_err;
	}

	ch->func->max_blksize = SDIO_AL_BLOCK_SIZE;

	sdio_set_drvdata(ch->func, ch);

	/* Get channel parameters from the peer SDIO-Client */
	read_sdioc_channel_config(ch);

	/* Set Pipes Threshold on Mailbox */
	ret = set_pipe_threshold(sdio_al_dev,
				 ch->rx_pipe_index, ch->read_threshold);
	if (ret)
		goto exit_err;
	ret = set_pipe_threshold(sdio_al_dev,
				 ch->tx_pipe_index, ch->write_threshold);
	if (ret)
		goto exit_err;

	/* Set flag before interrupts are enabled to allow notify */
	ch->state = SDIO_CHANNEL_STATE_OPEN;
	pr_debug(MODULE_NAME ":channel %s is in OPEN state now\n", ch->name);

	sdio_al_dev->poll_delay_msec = get_min_poll_time_msec(sdio_al_dev);

	/* lpm mechanism lives under the assumption there is always a timer */
	/* Check if need to start the timer */
	if  ((sdio_al_dev->poll_delay_msec) &&
	     (sdio_al_dev->is_timer_initialized == false)) {

		init_timer(&sdio_al_dev->timer);
		sdio_al_dev->timer.data = (unsigned long) sdio_al_dev;
		sdio_al_dev->timer.function = sdio_al_timer_handler;
		sdio_al_dev->timer.expires = jiffies +
			msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
		add_timer(&sdio_al_dev->timer);
		sdio_al_dev->is_timer_initialized = true;
	}

	/* Enable Pipes Interrupts */
	enable_eot_interrupt(sdio_al_dev, ch->rx_pipe_index, true);
	enable_eot_interrupt(sdio_al_dev, ch->tx_pipe_index, true);

	enable_threshold_interrupt(sdio_al_dev, ch->rx_pipe_index, true);
	enable_threshold_interrupt(sdio_al_dev, ch->tx_pipe_index, true);

exit_err:

	return ret;
}

/**
 *  Ask the worker to read the mailbox.
 */
static void ask_reading_mailbox(struct sdio_al_device *sdio_al_dev)
{
	if (!sdio_al_dev->ask_mbox) {
		pr_debug(MODULE_NAME ":ask_reading_mailbox for card %d\n",
			 sdio_al_dev->host->index);
		sdio_al_dev->ask_mbox = true;
		wake_up(&sdio_al_dev->wait_mbox);
	}
}

/**
 *  Start the timer
 */
static void start_timer(struct sdio_al_device *sdio_al_dev)
{
	if ((sdio_al_dev->poll_delay_msec)  &&
		(sdio_al_dev->state == CARD_INSERTED)) {
		sdio_al_dev->timer.expires = jiffies +
			msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
		add_timer(&sdio_al_dev->timer);
	}
}

/**
 *  Restart(postpone) the already working timer
 */
static void restart_timer(struct sdio_al_device *sdio_al_dev)
{
	if ((sdio_al_dev->poll_delay_msec) &&
		(sdio_al_dev->state == CARD_INSERTED)) {
		ulong expires =	jiffies +
			msecs_to_jiffies(sdio_al_dev->poll_delay_msec);
		mod_timer(&sdio_al_dev->timer, expires);
	}
}

/**
 *  Stop and delete the timer
 */
static void stop_and_del_timer(struct sdio_al_device *sdio_al_dev)
{
	if (sdio_al_dev->is_timer_initialized) {
		sdio_al_dev->poll_delay_msec = 0;
		del_timer_sync(&sdio_al_dev->timer);
	}
}

/**
 *  Do the wakup sequence.
 *  This function should be called after claiming the host!
 *  The caller is responsible for releasing the host.
 *
 *  Wake up sequence
 *  1. Get lock
 *  2. Enable wake up function if needed
 *  3. Mark NOT OK to sleep and write it
 *  4. Restore default thresholds
 *  5. Start the mailbox and inactivity timer again
 */
static int sdio_al_wake_up(struct sdio_al_device *sdio_al_dev,
			   u32 not_from_int, struct sdio_channel *ch)
{
	int ret = 0;
	struct sdio_func *wk_func = NULL;
	unsigned long time_to_wait;
	struct mmc_host *host = sdio_al_dev->host;

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		return -ENODEV;
	}

	if (!sdio_al_dev->is_ok_to_sleep) {
		LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":card %d "
				"already awake, no need to wake up\n",
				sdio_al_dev->host->index);
		return 0;
	}

	/* Wake up sequence */
	if (not_from_int) {
		if (ch) {
			LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": Wake up"
					" card %d (not by interrupt), ch %s",
					sdio_al_dev->host->index,
					ch->name);
		} else {
			LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": Wake up"
					  " card %d (not	by interrupt)",
					  sdio_al_dev->host->index);
		}
	} else {
		LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": Wake up card "
				"%d by interrupt",
				sdio_al_dev->host->index);
		sdio_al_dev->print_after_interrupt = 1;
	}

	sdio_al_vote_for_sleep(sdio_al_dev, 0);

	msmsdcc_lpm_disable(host);
	msmsdcc_set_pwrsave(host, 0);
	/* Poll the GPIO */
	time_to_wait = jiffies + msecs_to_jiffies(1000);
	while (time_before(jiffies, time_to_wait)) {
		if (sdio_al->pdata->get_mdm2ap_status())
			break;
		udelay(TIME_TO_WAIT_US);
	}

	pr_debug(MODULE_NAME ":GPIO mdm2ap_status=%d\n",
		       sdio_al->pdata->get_mdm2ap_status());

	/* Here get_mdm2ap_status() returning 0 is not an error condition */
	if (sdio_al->pdata->get_mdm2ap_status() == 0)
		LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ": "
				"get_mdm2ap_status() is 0\n");

	/* Enable Wake up Function */
	if (!sdio_al_dev->card ||
	    !sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1]) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": NULL card or wk_func\n");
		return -ENODEV;
	}
	wk_func = sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1];
	ret = sdio_al_enable_func_retry(wk_func, "wakeup func");
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"sdio_enable_func() err=%d\n", -ret);
		goto error_exit;
	}
	/* Mark NOT OK_TOSLEEP */
	sdio_al_dev->is_ok_to_sleep = 0;
	ret = write_lpm_info(sdio_al_dev);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": "
				"write_lpm_info() failed, err=%d\n", -ret);
		sdio_al_dev->is_ok_to_sleep = 1;
		sdio_disable_func(wk_func);
		goto error_exit;
	}
	sdio_disable_func(wk_func);

	/* Start the timer again*/
	restart_inactive_time(sdio_al_dev);
	sdio_al_dev->poll_delay_msec = get_min_poll_time_msec(sdio_al_dev);
	start_timer(sdio_al_dev);

	LPM_DEBUG(sdio_al_dev->dev_log, MODULE_NAME "Finished Wake up sequence"
			" for card %d", sdio_al_dev->host->index);

	msmsdcc_set_pwrsave(host, 1);
	pr_debug(MODULE_NAME ":Turn clock off\n");

	return ret;
error_exit:
	sdio_al_vote_for_sleep(sdio_al_dev, 1);
	msmsdcc_set_pwrsave(host, 1);
	WARN_ON(ret);
	sdio_al_get_into_err_state(sdio_al_dev);
	return ret;
}


/**
 *  SDIO Function Interrupt handler.
 *
 *  Interrupt shall be triggered by SDIO-Client when:
 *  1. End-Of-Transfer (EOT) detected in packet mode.
 *  2. Bytes-available reached the threshold.
 *
 *  Reading the mailbox clears the EOT/Threshold interrupt
 *  source.
 *  The interrupt source should be cleared before this ISR
 *  returns. This ISR is called from IRQ Thread and not
 *  interrupt, so it may sleep.
 *
 */
static void sdio_func_irq(struct sdio_func *func)
{
	struct sdio_al_device *sdio_al_dev = sdio_get_drvdata(func);

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

	if (sdio_al_dev == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": NULL device");
		return;
	}

	if (sdio_al_dev->is_ok_to_sleep)
		sdio_al_wake_up(sdio_al_dev, 0, NULL);
	else
		restart_timer(sdio_al_dev);

	read_mailbox(sdio_al_dev, true);

	pr_debug(MODULE_NAME ":end %s.\n", __func__);
}

/**
 *  Timer Expire Handler
 *
 */
static void sdio_al_timer_handler(unsigned long data)
{
	struct sdio_al_device *sdio_al_dev = (struct sdio_al_device *)data;
	if (sdio_al_dev == NULL) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": NULL "
				"sdio_al_dev for data %lu\n", data);
		return;
	}
	if (sdio_al_dev->state != CARD_INSERTED) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": sdio_al_dev "
				"is in invalid state %d\n", sdio_al_dev->state);
		return;
	}
	pr_debug(MODULE_NAME " Timer Expired\n");

	ask_reading_mailbox(sdio_al_dev);

	restart_timer(sdio_al_dev);
}

/**
 *  Driver Setup.
 *
 */
static int sdio_al_setup(struct sdio_al_device *sdio_al_dev)
{
	int ret = 0;
	struct mmc_card *card = sdio_al_dev->card;
	struct sdio_func *func1 = NULL;
	int i = 0;
	int fn = 0;

	if (sdio_al_verify_func1(sdio_al_dev, __func__))
		return -ENODEV;
	func1 = card->sdio_func[0];

	sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":sdio_al_setup for "
			"card %d\n", sdio_al_dev->host->index);

	ret = sdio_al->pdata->config_mdm2ap_status(1);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME "Could not "
				"request GPIO\n");
		return ret;
	}

	INIT_WORK(&sdio_al_dev->sdio_al_work.work, worker);
	/* disable all pipes interrupts before claim irq.
	   since all are enabled by default. */
	for (i = 0 ; i < SDIO_AL_MAX_PIPES; i++) {
		enable_eot_interrupt(sdio_al_dev, i, false);
		enable_threshold_interrupt(sdio_al_dev, i, false);
	}

	/* Disable all SDIO Functions before claim irq. */
	for (fn = 1 ; fn <= card->sdio_funcs; fn++)
		sdio_disable_func(card->sdio_func[fn-1]);

	sdio_set_drvdata(func1, sdio_al_dev);
	sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":claim IRQ for card "
			"%d\n",	sdio_al_dev->host->index);

	ret = sdio_claim_irq(func1, sdio_func_irq);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to claim"
				" IRQ for card %d\n",
				sdio_al_dev->host->index);
		return ret;
	}

	sdio_al_dev->is_ready = true;

	/* Start worker before interrupt might happen */
	queue_work(sdio_al_dev->workqueue, &sdio_al_dev->sdio_al_work.work);

	start_inactive_time(sdio_al_dev);

	pr_debug(MODULE_NAME ":Ready.\n");

	return 0;
}

/**
 *  Driver Tear-Down.
 *
 */
static void sdio_al_tear_down(void)
{
	int i, j;
	struct sdio_al_device *sdio_al_dev = NULL;
	struct sdio_func *func1;

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
		if (sdio_al->devices[i] == NULL)
			continue;
		sdio_al_dev = sdio_al->devices[i];

		if (sdio_al_dev->is_ready) {
			sdio_al_dev->is_ready = false; /* Flag worker to exit */
			sdio_al_dev->ask_mbox = false;
			ask_reading_mailbox(sdio_al_dev); /* Wakeup worker */
			/* allow gracefully exit of the worker thread */
			msleep(100);

			flush_workqueue(sdio_al_dev->workqueue);
			destroy_workqueue(sdio_al_dev->workqueue);

			sdio_al_vote_for_sleep(sdio_al_dev, 1);

			if (!sdio_al_claim_mutex_and_verify_dev(sdio_al_dev,
								__func__)) {
				if (!sdio_al_dev->card ||
				    !sdio_al_dev->card->sdio_func[0]) {
					sdio_al_loge(sdio_al_dev->dev_log,
						     MODULE_NAME
							": %s: Invalid func1",
							__func__);
					return;
				}
				func1 = sdio_al_dev->card->sdio_func[0];
				sdio_release_irq(func1);
				sdio_disable_func(func1);
				sdio_al_release_mutex(sdio_al_dev, __func__);
			}
		}

		for (j = 0; j < SDIO_AL_MAX_CHANNELS; j++)
			sdio_al_dev->channel[j].signature = 0x0;
		sdio_al_dev->signature = 0;

		kfree(sdio_al_dev->sdioc_sw_header);
		kfree(sdio_al_dev->mailbox);
		kfree(sdio_al_dev->rx_flush_buf);
		kfree(sdio_al_dev);
	}

	sdio_al->pdata->config_mdm2ap_status(0);
}

/**
 *  Find channel by name.
 *
 */
static struct sdio_channel *find_channel_by_name(const char *name)
{
	struct sdio_channel *ch = NULL;
	int i, j;
	struct sdio_al_device *sdio_al_dev = NULL;

	for (j = 0; j < MAX_NUM_OF_SDIO_DEVICES; ++j) {
		if (sdio_al->devices[j] == NULL)
			continue;
		sdio_al_dev = sdio_al->devices[j];
		for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
			if (sdio_al_dev->channel[i].state ==
					SDIO_CHANNEL_STATE_INVALID)
				continue;
			if (strncmp(sdio_al_dev->channel[i].name, name,
					CHANNEL_NAME_SIZE) == 0) {
				ch = &sdio_al_dev->channel[i];
				break;
			}
		}
		if (ch != NULL)
			break;
	}

	return ch;
}

/**
 *  Find the minimal poll time.
 *
 */
static int get_min_poll_time_msec(struct sdio_al_device *sdio_sl_dev)
{
	int i;
	int poll_delay_msec = 0x0FFFFFFF;

	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++)
		if ((sdio_sl_dev->channel[i].state ==
					SDIO_CHANNEL_STATE_OPEN) &&
		(sdio_sl_dev->channel[i].poll_delay_msec > 0) &&
		(sdio_sl_dev->channel[i].poll_delay_msec < poll_delay_msec))
			poll_delay_msec =
				sdio_sl_dev->channel[i].poll_delay_msec;

	if (poll_delay_msec == 0x0FFFFFFF)
		poll_delay_msec = SDIO_AL_POLL_TIME_NO_STREAMING;

	pr_debug(MODULE_NAME ":poll delay time is %d msec\n", poll_delay_msec);

	return poll_delay_msec;
}

/**
 *  Open SDIO Channel.
 *
 *  Enable the channel.
 *  Set the channel context.
 *  Trigger reading the mailbox to check available bytes.
 *
 */
int sdio_open(const char *name, struct sdio_channel **ret_ch, void *priv,
		 void (*notify)(void *priv, unsigned ch_event))
{
	int ret = 0;
	struct sdio_channel *ch = NULL;
	struct sdio_al_device *sdio_al_dev = NULL;

	*ret_ch = NULL; /* default */

	ch = find_channel_by_name(name);
	if (ch == NULL) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":Can't find "
			"channel name %s\n", name);
		return -EINVAL;
	}

	sdio_al_dev = ch->sdio_al_dev;
	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	if ((ch->state != SDIO_CHANNEL_STATE_IDLE) &&
		(ch->state != SDIO_CHANNEL_STATE_CLOSED)) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Wrong ch %s "
				"state %d\n", name, ch->state);
		ret = -EPERM;
		goto exit_err;
	}

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		ret = -ENODEV;
		goto exit_err;
	}

	ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
	if (ret)
		goto exit_err;

	ch->notify = notify;
	ch->priv = priv;

	/* Note: Set caller returned context before interrupts are enabled */
	*ret_ch = ch;

	ret = open_channel(ch);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":sdio_open %s "
				"err=%d\n", name, -ret);
		goto exit_err;
	}

	CLOSE_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":sdio_open %s "
							"completed OK\n", name);
	if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
		if (sdio_al->sdioc_major == PEER_SDIOC_OLD_VERSION_MAJOR) {
			if (!ch->is_packet_mode) {
				sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME
						":setting channel %s as "
						"lpm_chan\n", name);
				sdio_al_dev->lpm_chan = ch->num;
			}
		} else {
			sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ": "
					"setting channel %s as lpm_chan\n",
					name);
			sdio_al_dev->lpm_chan = ch->num;
		}
	}

exit_err:
	sdio_al_release_mutex(sdio_al_dev, __func__);
	return ret;
}
EXPORT_SYMBOL(sdio_open);

/**
 *  Request peer operation
 *  note: sanity checks of parameters done by caller
 *        called under bus locked
 */
static int peer_set_operation(u32 opcode,
		struct sdio_al_device *sdio_al_dev,
		struct sdio_channel *ch)
{
	int ret;
	int offset;
	struct sdio_func *wk_func = NULL;
	u32 peer_operation;
	int loop_count = 0;

	if (!sdio_al_dev->card ||
	    !sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1]) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": NULL card or wk_func\n");
		ret = -ENODEV;
		goto exit;
	}
	wk_func = sdio_al_dev->card->sdio_func[SDIO_AL_WAKEUP_FUNC-1];

	/* calculate offset of peer_operation field in sw mailbox struct */
	offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config) +
		sizeof(struct peer_sdioc_channel_config) * ch->num +
		offsetof(struct peer_sdioc_channel_config, peer_operation);

	ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to "
				"wake up\n");
		goto exit;
	}
	/* request operation from MDM peer */
	peer_operation = PEER_OPERATION(opcode, PEER_OP_STATE_INIT);
	ret = sdio_memcpy_toio(ch->func, SDIOC_SW_MAILBOX_ADDR+offset,
			&peer_operation, sizeof(u32));
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":failed to "
				"request close operation\n");
		goto exit;
	}
	ret = sdio_al_enable_func_retry(wk_func, "wk_func");
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to enable"
				" Func#%d\n", wk_func->num);
		goto exit;
	}
	pr_debug(MODULE_NAME ":%s: wk_func enabled on ch %s\n",
			__func__, ch->name);
	/* send "start" operation to MDM */
	peer_operation = PEER_OPERATION(opcode, PEER_OP_STATE_START);
	ret  =  sdio_memcpy_toio(ch->func, SDIOC_SW_MAILBOX_ADDR+offset,
			&peer_operation, sizeof(u32));
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":failed to "
				"send start close operation\n");
		goto exit;
	}
	ret = sdio_disable_func(wk_func);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to "
				"disable Func#%d\n", wk_func->num);
		goto exit;
	}
	/* poll for peer operation ack */
	while (peer_operation != 0) {
		ret  =  sdio_memcpy_fromio(ch->func,
				&peer_operation,
				SDIOC_SW_MAILBOX_ADDR+offset,
				sizeof(u32));
		if (ret) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					":failed to request ack on close"
					" operation, loop_count = %d\n",
					loop_count);
			goto exit;
		}
		loop_count++;
		if (loop_count > 10) {
			sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":%s: "
					"peer_operation=0x%x wait loop"
					" %d on ch %s\n", __func__,
					peer_operation, loop_count, ch->name);
		}
	}
exit:
	return ret;
}

static int channel_close(struct sdio_channel *ch, int flush_flag)
{
	int ret;
	struct sdio_al_device *sdio_al_dev = NULL;
	int flush_len;
	ulong flush_expires;

	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n",  __func__);
		return -ENODEV;
	}

	if (!ch->func) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":%s: NULL func"
				" on channel:%d\n", __func__, ch->num);
		return -ENODEV;
	}

	sdio_al_dev = ch->sdio_al_dev;
	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	if (!sdio_al_dev->ch_close_supported) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":%s: Not "
			"supported by mdm, ch %s\n",
			__func__, ch->name);
		ret = -ENOTSUPP;
		goto error_exit;
	}

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		ret = -ENODEV;
		goto error_exit;
	}
	if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
		sdio_al_loge(sdio_al_dev->dev_log,
				MODULE_NAME ":%s: ch %s is not in "
				"open state (%d)\n",
				__func__, ch->name, ch->state);
		ret = -ENODEV;
		goto error_exit;
	}
	ch->state = SDIO_CHANNEL_STATE_CLOSING;
	ret = peer_set_operation(PEER_OP_CODE_CLOSE, sdio_al_dev, ch);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":%s: "
				"peer_set_operation() failed: %d\n",
				__func__, ret);
		ret = -ENODEV;
		goto error_exit;
	}
	/* udate poll time for opened channels */
	if  (ch->poll_delay_msec > 0) {
		sdio_al_dev->poll_delay_msec =
			get_min_poll_time_msec(sdio_al_dev);
	}
	sdio_al_release_mutex(ch->sdio_al_dev, __func__);

	flush_expires = jiffies +
		msecs_to_jiffies(SDIO_CLOSE_FLUSH_TIMEOUT_MSEC);
	/* flush rx packets of the channel */
	if (flush_flag) {
		do {
			while (ch->read_avail > 0) {
				flush_len = ch->read_avail;
				ret = sdio_read_internal(ch,
						sdio_al_dev->rx_flush_buf,
						flush_len);
				if (ret) {
					sdio_al_loge(&sdio_al->gen_log,
						MODULE_NAME ":%s sdio_read"
						" failed: %d, ch %s\n",
						__func__, ret,
						ch->name);
					return ret;
				}

				if (time_after(jiffies, flush_expires) != 0) {
					sdio_al_loge(&sdio_al->gen_log,
						MODULE_NAME ":%s flush rx "
						"packets timeout: ch %s\n",
						__func__, ch->name);
					sdio_al_get_into_err_state(sdio_al_dev);
					return -EBUSY;
				}
			}
			msleep(100);
			if (ch->signature != SDIO_AL_SIGNATURE) {
					sdio_al_loge(&sdio_al->gen_log,
						MODULE_NAME ":%s: after sleep,"
						" invalid signature"
						" 0x%x\n", __func__,
						ch->signature);
				return -ENODEV;
			}
			if (sdio_al_claim_mutex_and_verify_dev(ch->sdio_al_dev,
							       __func__))
				return -ENODEV;

			ret = read_mailbox(sdio_al_dev, false);
			if (ret) {
				sdio_al_loge(&sdio_al->gen_log,
						MODULE_NAME ":%s: failed to"
						" read mailbox", __func__);
				goto error_exit;
			}
			sdio_al_release_mutex(ch->sdio_al_dev, __func__);
		} while (ch->read_avail > 0);
	}
	if (sdio_al_claim_mutex_and_verify_dev(ch->sdio_al_dev,
					       __func__))
		return -ENODEV;
	/* disable function to be able to open the channel again */
	ret = sdio_disable_func(ch->func);
	if (ret) {
		sdio_al_loge(&sdio_al->gen_log,
			MODULE_NAME ":Fail to disable Func#%d\n",
			ch->func->num);
		goto error_exit;
	}
	ch->state = SDIO_CHANNEL_STATE_CLOSED;
	CLOSE_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":%s: Ch %s closed "
				"successfully\n", __func__, ch->name);

error_exit:
	sdio_al_release_mutex(ch->sdio_al_dev, __func__);

	return ret;
}

/**
 *  Close SDIO Channel.
 *
 */
int sdio_close(struct sdio_channel *ch)
{
	return channel_close(ch, true);
}
EXPORT_SYMBOL(sdio_close);

/**
 *  Get the number of available bytes to write.
 *
 */
int sdio_write_avail(struct sdio_channel *ch)
{
	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n", __func__);
		return -ENODEV;
	}
	if (ch->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: "
				"Invalid signature 0x%x\n",  __func__,
				ch->signature);
		return -ENODEV;
	}
	if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME ":%s: "
				"channel %s state is not open (%d)\n",
				__func__, ch->name, ch->state);
		return -ENODEV;
	}
	pr_debug(MODULE_NAME ":sdio_write_avail %s 0x%x\n",
			 ch->name, ch->write_avail);

	return ch->write_avail;
}
EXPORT_SYMBOL(sdio_write_avail);

/**
 *  Get the number of available bytes to read.
 *
 */
int sdio_read_avail(struct sdio_channel *ch)
{
	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n", __func__);
		return -ENODEV;
	}
	if (ch->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: "
				"Invalid signature 0x%x\n",  __func__,
				ch->signature);
		return -ENODEV;
	}
	if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME ":%s: "
				"channel %s state is not open (%d)\n",
				__func__, ch->name, ch->state);
		return -ENODEV;
	}
	pr_debug(MODULE_NAME ":sdio_read_avail %s 0x%x\n",
			 ch->name, ch->read_avail);

	return ch->read_avail;
}
EXPORT_SYMBOL(sdio_read_avail);

static int sdio_read_from_closed_ch(struct sdio_channel *ch, int len)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = NULL;

	if (!ch) {
		sdio_al_loge(ch->sdio_al_dev->dev_log,
			MODULE_NAME ":%s: NULL channel\n",  __func__);
		return -ENODEV;
	}

	sdio_al_dev = ch->sdio_al_dev;
	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	ret = sdio_memcpy_fromio(ch->func, sdio_al_dev->rx_flush_buf,
				 PIPE_RX_FIFO_ADDR, len);

	if (ret) {
		sdio_al_loge(ch->sdio_al_dev->dev_log,
				MODULE_NAME ":ch %s: %s err=%d, len=%d\n",
				ch->name, __func__, -ret, len);
		sdio_al_dev->is_err = true;
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return ret;
	}

	restart_inactive_time(sdio_al_dev);

	sdio_al_release_mutex(sdio_al_dev, __func__);

	return 0;
}

/**
 *  Internal read from SDIO Channel.
 *
 *  Reading from the pipe will trigger interrupt if there are
 *  other pending packets on the SDIO-Client.
 *
 */
static int sdio_read_internal(struct sdio_channel *ch, void *data, int len)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = NULL;

	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n",  __func__);
		return -ENODEV;
	}
	if (!data) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL data\n",
				__func__);
		return -ENODEV;
	}
	if (len == 0) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":channel %s trying"
				" to read 0 bytes\n", ch->name);
		return -EINVAL;
	}

	if (ch->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: Invalid "
				"signature 0x%x\n",  __func__, ch->signature);
		return -ENODEV;
	}

	sdio_al_dev = ch->sdio_al_dev;
	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		ret = -ENODEV;
		goto exit;
	}

	/* lpm policy says we can't go to sleep when we have pending rx data,
	   so either we had rx interrupt and woken up, or we never went to
	   sleep */
	if (sdio_al_dev->is_ok_to_sleep) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":%s: called "
				"when is_ok_to_sleep is set for ch %s, len=%d,"
				" last_any_read_avail=%d, last_read_avail=%d, "
				"last_old_read_avail=%d", __func__, ch->name,
				len, ch->statistics.last_any_read_avail,
				ch->statistics.last_read_avail,
				ch->statistics.last_old_read_avail);
	}
	BUG_ON(sdio_al_dev->is_ok_to_sleep);

	if ((ch->state != SDIO_CHANNEL_STATE_OPEN) &&
			(ch->state != SDIO_CHANNEL_STATE_CLOSING)) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":%s wrong "
				"channel %s state %d\n",
				__func__, ch->name, ch->state);
		ret = -EINVAL;
		goto exit;
	}

	DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":start ch %s read %d "
			"avail %d.\n", ch->name, len, ch->read_avail);

	restart_inactive_time(sdio_al_dev);

	if ((ch->is_packet_mode) && (len != ch->read_avail)) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":sdio_read ch "
				"%s len != read_avail\n", ch->name);
		ret = -EINVAL;
		goto exit;
	}

	if (len > ch->read_avail) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":ERR ch %s: "
				"reading more bytes (%d) than the avail(%d).\n",
				ch->name, len, ch->read_avail);
		ret = -ENOMEM;
		goto exit;
	}

	ret = sdio_memcpy_fromio(ch->func, data, PIPE_RX_FIFO_ADDR, len);

	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":ch %s: "
				"sdio_read err=%d, len=%d, read_avail=%d, "
				"last_read_avail=%d, last_old_read_avail=%d\n",
				ch->name, -ret, len, ch->read_avail,
				ch->statistics.last_read_avail,
				ch->statistics.last_old_read_avail);
		sdio_al_get_into_err_state(sdio_al_dev);
		goto exit;
	}

	ch->statistics.total_read_times++;

	/* Remove handled packet from the list regardless if ret is ok */
	if (ch->is_packet_mode)
		remove_handled_rx_packet(ch);
	else
		ch->read_avail -= len;

	ch->total_rx_bytes += len;
	DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":end ch %s read %d "
			"avail %d total %d.\n", ch->name, len,
			ch->read_avail, ch->total_rx_bytes);

	if ((ch->read_avail == 0) && !(ch->is_packet_mode))
		ask_reading_mailbox(sdio_al_dev);

exit:
	sdio_al_release_mutex(sdio_al_dev, __func__);

	return ret;
}

/**
 *  Read from SDIO Channel.
 *
 *  Reading from the pipe will trigger interrupt if there are
 *  other pending packets on the SDIO-Client.
 *
 */
int sdio_read(struct sdio_channel *ch, void *data, int len)
{
	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n", __func__);
		return -ENODEV;
	}
	if (ch->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: "
			"Invalid signature 0x%x\n",  __func__, ch->signature);
		return -ENODEV;
	}
	if (ch->state == SDIO_CHANNEL_STATE_OPEN) {
		return sdio_read_internal(ch, data, len);
	} else {
		sdio_al_loge(ch->sdio_al_dev->dev_log, MODULE_NAME
				":%s: Invalid channel %s state %d\n",
				__func__, ch->name, ch->state);
	}
	return -ENODEV;
}
EXPORT_SYMBOL(sdio_read);

/**
 *  Write to SDIO Channel.
 *
 */
int sdio_write(struct sdio_channel *ch, const void *data, int len)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = NULL;

	if (!ch) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL "
				"channel\n",  __func__);
		return -ENODEV;
	}
	if (!data) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: NULL data\n",
				__func__);
		return -ENODEV;
	}
	if (len == 0) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":channel %s trying"
				" to write 0 bytes\n", ch->name);
		return -EINVAL;
	}

	if (ch->signature != SDIO_AL_SIGNATURE) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":%s: Invalid "
				"signature 0x%x\n",  __func__, ch->signature);
		return -ENODEV;
	}

	sdio_al_dev = ch->sdio_al_dev;
	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	WARN_ON(len > ch->write_avail);

	if (sdio_al_dev->is_err) {
		SDIO_AL_ERR(__func__);
		ret = -ENODEV;
		goto exit;
	}

	if (ch->state != SDIO_CHANNEL_STATE_OPEN) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":writing to "
				"closed channel %s\n", ch->name);
		ret = -EINVAL;
		goto exit;
	}

	if (sdio_al_dev->is_ok_to_sleep) {
		ret = sdio_al_wake_up(sdio_al_dev, 1, ch);
		if (ret)
			goto exit;
	} else {
		restart_inactive_time(sdio_al_dev);
	}

	DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":start ch %s write %d "
			"avail %d.\n", ch->name, len, ch->write_avail);

	if (len > ch->write_avail) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":ERR ch %s: "
				"write more bytes (%d) than  available %d.\n",
				ch->name, len, ch->write_avail);
		ret = -ENOMEM;
		goto exit;
	}

	ret = sdio_ch_write(ch, data, len);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":sdio_write "
				"on channel %s err=%d\n", ch->name, -ret);
		goto exit;
	}

	ch->total_tx_bytes += len;
	DATA_DEBUG(sdio_al_dev->dev_log, MODULE_NAME ":end ch %s write %d "
			"avail %d total %d.\n", ch->name, len,
			ch->write_avail, ch->total_tx_bytes);

	/* Round up to whole buffer size */
	len = ROUND_UP(len, ch->peer_tx_buf_size);
	/* Protect from wraparound */
	len = min(len, (int) ch->write_avail);
	ch->write_avail -= len;

	if (ch->write_avail < ch->min_write_avail)
		ask_reading_mailbox(sdio_al_dev);

exit:
	sdio_al_release_mutex(sdio_al_dev, __func__);

	return ret;
}
EXPORT_SYMBOL(sdio_write);

static int __devinit msm_sdio_al_probe(struct platform_device *pdev)
{
	if (!sdio_al) {
		pr_err(MODULE_NAME ": %s: NULL sdio_al\n", __func__);
		return -ENODEV;
	}

	sdio_al->pdata = pdev->dev.platform_data;
	return 0;
}

static int __devexit msm_sdio_al_remove(struct platform_device *pdev)
{
	return 0;
}

static void sdio_al_close_all_channels(struct sdio_al_device *sdio_al_dev)
{
	int j;
	int ret;
	struct sdio_channel *ch = NULL;

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s", __func__);

	if (!sdio_al_dev) {
		sdio_al_loge(sdio_al_dev->dev_log,
			MODULE_NAME ": %s: NULL device", __func__);
		return;
	}
	for (j = 0; j < SDIO_AL_MAX_CHANNELS; j++) {
		ch = &sdio_al_dev->channel[j];

		if (ch->state == SDIO_CHANNEL_STATE_OPEN) {
			sdio_al_loge(sdio_al_dev->dev_log,
				MODULE_NAME ": %s: Call to sdio_close() for"
				" ch %s\n", __func__, ch->name);
			ret = channel_close(ch, false);
			if (ret) {
				sdio_al_loge(sdio_al_dev->dev_log,
					MODULE_NAME ": %s: failed sdio_close()"
					" for ch %s (%d)\n",
					__func__, ch->name, ret);
			}
		} else {
			pr_debug(MODULE_NAME ": %s: skip sdio_close() ch %s"
					" (state=%d)\n", __func__,
					ch->name, ch->state);
		}
	}
}

static void sdio_al_invalidate_sdio_clients(struct sdio_al_device *sdio_al_dev,
					    struct platform_device **pdev_arr)
{
	int j;

	pr_debug(MODULE_NAME ": %s: Notifying SDIO clients for card %d",
			__func__, sdio_al_dev->host->index);
	for (j = 0; j < SDIO_AL_MAX_CHANNELS; ++j) {
		if (sdio_al_dev->channel[j].state ==
			SDIO_CHANNEL_STATE_INVALID)
			continue;
		pdev_arr[j] = sdio_al_dev->channel[j].pdev;
		sdio_al_dev->channel[j].signature = 0x0;
		sdio_al_dev->channel[j].state =
			SDIO_CHANNEL_STATE_INVALID;
	}
}

static void sdio_al_modem_reset_operations(struct sdio_al_device
							*sdio_al_dev)
{
	int ret = 0;
	struct platform_device *pdev_arr[SDIO_AL_MAX_CHANNELS];
	int j;

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s", __func__);

	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return;

	if (sdio_al_dev->state == CARD_REMOVED) {
		sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: "
			"card %d is already removed", __func__,
			sdio_al_dev->host->index);
		goto exit_err;
	}

	if (sdio_al_dev->state == MODEM_RESTART) {
		sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ": %s: "
			"card %d was already notified for modem reset",
			__func__, sdio_al_dev->host->index);
		goto exit_err;
	}

	sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ": %s: Set the "
		"state to MODEM_RESTART for card %d",
		__func__, sdio_al_dev->host->index);
	sdio_al_dev->state = MODEM_RESTART;
	sdio_al_dev->is_ready = false;

	/* Stop mailbox timer */
	stop_and_del_timer(sdio_al_dev);

	if ((sdio_al_dev->is_ok_to_sleep) &&
	    (!sdio_al_dev->is_err)) {
		pr_debug(MODULE_NAME ": %s: wakeup modem for "
				    "card %d", __func__,
			sdio_al_dev->host->index);
		ret = sdio_al_wake_up(sdio_al_dev, 1, NULL);
	}

	if (!ret && (!sdio_al_dev->is_err) && sdio_al_dev->card &&
		sdio_al_dev->card->sdio_func[0]) {
			sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME
			": %s: sdio_release_irq for card %d",
			__func__,
			sdio_al_dev->host->index);
			sdio_release_irq(sdio_al_dev->card->sdio_func[0]);
	}

	memset(pdev_arr, 0, sizeof(pdev_arr));
	sdio_al_invalidate_sdio_clients(sdio_al_dev, pdev_arr);

	sdio_al_release_mutex(sdio_al_dev, __func__);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: Notifying SDIO "
						    "clients for card %d",
			__func__, sdio_al_dev->host->index);
	for (j = 0; j < SDIO_AL_MAX_CHANNELS; j++) {
		if (!pdev_arr[j])
			continue;
		platform_device_unregister(pdev_arr[j]);
	}
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: Finished Notifying "
						    "SDIO clients for card %d",
			__func__, sdio_al_dev->host->index);

	return;

exit_err:
	sdio_al_release_mutex(sdio_al_dev, __func__);
	return;
}

#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
static void sdio_al_reset(void)
{
	int i;
	struct sdio_al_device *sdio_al_dev;

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s", __func__);

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; i++) {
		if (sdio_al->devices[i] == NULL) {
			pr_debug(MODULE_NAME ": %s: NULL device in index %d",
					__func__, i);
			continue;
		}
		sdio_al_dev = sdio_al->devices[i];
		sdio_al_modem_reset_operations(sdio_al->devices[i]);
	}

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s completed", __func__);
}
#endif

static void msm_sdio_al_shutdown(struct platform_device *pdev)
{
	int i;
	struct sdio_al_device *sdio_al_dev;

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME
			"Initiating msm_sdio_al_shutdown...");

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; i++) {
		if (sdio_al->devices[i] == NULL) {
			pr_debug(MODULE_NAME ": %s: NULL device in index %d",
					__func__, i);
			continue;
		}
		sdio_al_dev = sdio_al->devices[i];

		if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
			return;

		if (sdio_al_dev->ch_close_supported)
			sdio_al_close_all_channels(sdio_al_dev);

		sdio_al_release_mutex(sdio_al_dev, __func__);

		sdio_al_modem_reset_operations(sdio_al_dev);
	}
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: "
		"msm_sdio_al_shutdown complete.", __func__);
}

static struct platform_driver msm_sdio_al_driver = {
	.probe          = msm_sdio_al_probe,
	.remove         = __exit_p(msm_sdio_al_remove),
	.shutdown	= msm_sdio_al_shutdown,
	.driver         = {
		.name   = "msm_sdio_al",
	},
};

/**
 *  Initialize SDIO_AL channels.
 *
 */
static int init_channels(struct sdio_al_device *sdio_al_dev)
{
	int ret = 0;
	int i;

	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	ret = read_sdioc_software_header(sdio_al_dev,
					 sdio_al_dev->sdioc_sw_header);
	if (ret)
		goto exit;

	ret = sdio_al_setup(sdio_al_dev);
	if (ret)
		goto exit;

	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		int ch_name_size;
		if (sdio_al_dev->channel[i].state == SDIO_CHANNEL_STATE_INVALID)
			continue;
		if (sdio_al->unittest_mode) {
			memset(sdio_al_dev->channel[i].ch_test_name, 0,
				sizeof(sdio_al_dev->channel[i].ch_test_name));
			ch_name_size = strnlen(sdio_al_dev->channel[i].name,
				       CHANNEL_NAME_SIZE);
			strncpy(sdio_al_dev->channel[i].ch_test_name,
			       sdio_al_dev->channel[i].name,
			       ch_name_size);
			strncat(sdio_al_dev->channel[i].ch_test_name +
			       ch_name_size,
			       SDIO_TEST_POSTFIX,
			       SDIO_TEST_POSTFIX_SIZE);
			pr_debug(MODULE_NAME ":pdev.name = %s\n",
				sdio_al_dev->channel[i].ch_test_name);
			sdio_al_dev->channel[i].pdev = platform_device_alloc(
				sdio_al_dev->channel[i].ch_test_name, -1);
		} else {
			pr_debug(MODULE_NAME ":pdev.name = %s\n",
				sdio_al_dev->channel[i].name);
			sdio_al_dev->channel[i].pdev = platform_device_alloc(
				sdio_al_dev->channel[i].name, -1);
		}
		if (!sdio_al_dev->channel[i].pdev) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					":NULL platform device for ch %s",
					sdio_al_dev->channel[i].name);
			sdio_al_dev->channel[i].state =
				SDIO_CHANNEL_STATE_INVALID;
			continue;
		}
		ret = platform_device_add(sdio_al_dev->channel[i].pdev);
		if (ret) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					":platform_device_add failed, "
					"ret=%d\n", ret);
			sdio_al_dev->channel[i].state =
				SDIO_CHANNEL_STATE_INVALID;
		}
	}

exit:
	sdio_al_release_mutex(sdio_al_dev, __func__);
	return ret;
}

/**
 *  Initialize SDIO_AL channels according to the client setup.
 *  This function also check if the client is in boot mode and
 *  flashless boot is required to be activated or the client is
 *  up and running.
 *
 */
static int sdio_al_client_setup(struct sdio_al_device *sdio_al_dev)
{
	int ret = 0;
	struct sdio_func *func1;
	int signature = 0;

	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	if (!sdio_al_dev->card || !sdio_al_dev->card->sdio_func[0]) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":NULL card or "
							       "func1\n");
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return -ENODEV;
	}
	func1 = sdio_al_dev->card->sdio_func[0];

	/* Read the header signature to determine the status of the MDM
	 * SDIO Client
	 */
	signature = sdio_readl(func1, SDIOC_SW_HEADER_ADDR, &ret);
	sdio_al_release_mutex(sdio_al_dev, __func__);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":fail to read "
				"signature from sw header.\n");
		return ret;
	}

	switch (signature) {
	case PEER_SDIOC_SW_MAILBOX_BOOT_SIGNATURE:
		if (sdio_al_dev == sdio_al->bootloader_dev) {
			sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":setup "
					"bootloader on card %d\n",
					sdio_al_dev->host->index);
			return sdio_al_bootloader_setup();
		} else {
			sdio_al_logi(sdio_al_dev->dev_log, MODULE_NAME ":wait "
					"for bootloader completion "
					"on card %d\n",
					sdio_al_dev->host->index);
			return sdio_al_wait_for_bootloader_comp(sdio_al_dev);
		}
	case PEER_SDIOC_SW_MAILBOX_SIGNATURE:
	case PEER_SDIOC_SW_MAILBOX_UT_SIGNATURE:
		return init_channels(sdio_al_dev);
	default:
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Invalid "
				"signature 0x%x\n", signature);
		return -EINVAL;
	}

	return 0;
}

static void clean_sdio_al_device_data(struct sdio_al_device *sdio_al_dev)
{
	sdio_al_dev->is_ready = 0;
	sdio_al_dev->bootloader_done = 0;
	sdio_al_dev->lpm_chan = 0;
	sdio_al_dev->is_ok_to_sleep = 0;
	sdio_al_dev->inactivity_time = 0;
	sdio_al_dev->poll_delay_msec = 0;
	sdio_al_dev->is_timer_initialized = 0;
	sdio_al_dev->is_err = 0;
	sdio_al_dev->is_suspended = 0;
	sdio_al_dev->flashless_boot_on = 0;
	sdio_al_dev->ch_close_supported = 0;
	sdio_al_dev->print_after_interrupt = 0;
	memset(sdio_al_dev->sdioc_sw_header, 0,
	       sizeof(*sdio_al_dev->sdioc_sw_header));
	memset(sdio_al_dev->mailbox, 0, sizeof(*sdio_al_dev->mailbox));
	memset(sdio_al_dev->rx_flush_buf, 0,
	       sizeof(*sdio_al_dev->rx_flush_buf));
}

/*
 * SDIO driver functions
 */
static int sdio_al_sdio_probe(struct sdio_func *func,
		const struct sdio_device_id *sdio_dev_id)
{
	int ret = 0;
	struct sdio_al_device *sdio_al_dev = NULL;
	int i;
	struct mmc_card *card = NULL;

	if (!func) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL func\n",
				__func__);
		return -ENODEV;
	}
	card = func->card;

	if (!card) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL card\n",
				__func__);
		return -ENODEV;
	}

	if (!card->sdio_func[0]) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
							    "func1\n",
				__func__);
		return -ENODEV;
	}

	if (card->sdio_funcs < SDIO_AL_MAX_FUNCS) {
		dev_info(&card->dev,
			 "SDIO-functions# %d less than expected.\n",
			 card->sdio_funcs);
		return -ENODEV;
	}

	/* Check if there is already a device for this card */
	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
		if (sdio_al->devices[i] == NULL)
			continue;
		if (sdio_al->devices[i]->host == card->host) {
			sdio_al_dev = sdio_al->devices[i];
			if (sdio_al_dev->state == CARD_INSERTED)
				return 0;
			clean_sdio_al_device_data(sdio_al_dev);
			break;
		}
	}

	if (!sdio_al_dev) {
		sdio_al_dev = kzalloc(sizeof(struct sdio_al_device),
				      GFP_KERNEL);
		if (sdio_al_dev == NULL)
			return -ENOMEM;

		for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES ; ++i)
			if (sdio_al->devices[i] == NULL) {
				sdio_al->devices[i] = sdio_al_dev;
				sdio_al_dev->dev_log = &sdio_al->device_log[i];
				spin_lock_init(&sdio_al_dev->dev_log->log_lock);
	#ifdef CONFIG_DEBUG_FS
				sdio_al_dbgfs_log[i].data =
						sdio_al_dev->dev_log->buffer;
				sdio_al_dbgfs_log[i].size =
					SDIO_AL_DEBUG_LOG_SIZE;
	#endif
				break;
			}
		if (i == MAX_NUM_OF_SDIO_DEVICES) {
			sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ":No space "
					"in devices array for the device\n");
			return -ENOMEM;
		}
	}

	dev_info(&card->dev, "SDIO Card claimed.\n");
	sdio_al->skip_print_info = 0;

	sdio_al_dev->state = CARD_INSERTED;

	if (card->host->index == SDIO_BOOTLOADER_CARD_INDEX)
		sdio_al->bootloader_dev = sdio_al_dev;

	sdio_al_dev->is_ready = false;

	sdio_al_dev->signature = SDIO_AL_SIGNATURE;

	sdio_al_dev->is_suspended = 0;
	sdio_al_dev->is_timer_initialized = false;

	sdio_al_dev->lpm_chan = INVALID_SDIO_CHAN;

	sdio_al_dev->card = card;
	sdio_al_dev->host = card->host;

	if (!sdio_al_dev->mailbox) {
		sdio_al_dev->mailbox = kzalloc(sizeof(struct sdio_mailbox),
					       GFP_KERNEL);
		if (sdio_al_dev->mailbox == NULL)
			return -ENOMEM;
	}

	if (!sdio_al_dev->sdioc_sw_header) {
		sdio_al_dev->sdioc_sw_header
			= kzalloc(sizeof(*sdio_al_dev->sdioc_sw_header),
				  GFP_KERNEL);
		if (sdio_al_dev->sdioc_sw_header == NULL)
			return -ENOMEM;
	}

	if (!sdio_al_dev->rx_flush_buf) {
		sdio_al_dev->rx_flush_buf = kzalloc(RX_FLUSH_BUFFER_SIZE,
						    GFP_KERNEL);
		if (sdio_al_dev->rx_flush_buf == NULL) {
			sdio_al_loge(&sdio_al->gen_log,
					MODULE_NAME ":Fail to allocate "
					   "rx_flush_buf for card %d\n",
			       card->host->index);
			return -ENOMEM;
		}
	}

	sdio_al_dev->timer.data = (unsigned long)sdio_al_dev;

	wake_lock_init(&sdio_al_dev->wake_lock, WAKE_LOCK_SUSPEND, MODULE_NAME);
	/* Don't allow sleep until all required clients register */
	sdio_al_vote_for_sleep(sdio_al_dev, 0);

	if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
		return -ENODEV;

	/* Init Func#1 */
	ret = sdio_al_enable_func_retry(card->sdio_func[0], "Init Func#1");
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to "
				"enable Func#%d\n", card->sdio_func[0]->num);
		goto exit;
	}

	/* Patch Func CIS tuple issue */
	ret = sdio_set_block_size(card->sdio_func[0], SDIO_AL_BLOCK_SIZE);
	if (ret) {
		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ":Fail to set "
			"block size, Func#%d\n", card->sdio_func[0]->num);
		goto exit;
	}
	sdio_al_dev->card->sdio_func[0]->max_blksize = SDIO_AL_BLOCK_SIZE;

	sdio_al_dev->workqueue = create_singlethread_workqueue("sdio_al_wq");
	sdio_al_dev->sdio_al_work.sdio_al_dev = sdio_al_dev;
	init_waitqueue_head(&sdio_al_dev->wait_mbox);

	ret = sdio_al_client_setup(sdio_al_dev);

exit:
	sdio_al_release_mutex(sdio_al_dev, __func__);
	return ret;
}

static void sdio_al_sdio_remove(struct sdio_func *func)
{
	struct sdio_al_device *sdio_al_dev = NULL;
	int i;
	struct mmc_card *card = NULL;
	struct platform_device *pdev_arr[SDIO_AL_MAX_CHANNELS];

	if (!func) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL func\n",
				__func__);
		return;
	}
	card = func->card;

	if (!card) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL card\n",
				__func__);
		return;
	}

	/* Find the sdio_al_device of this card */
	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES; ++i) {
		if (sdio_al->devices[i] == NULL)
			continue;
		if (sdio_al->devices[i]->card == card) {
			sdio_al_dev = sdio_al->devices[i];
			break;
		}
	}
	if (sdio_al_dev == NULL) {
		pr_debug(MODULE_NAME ":%s :NULL sdio_al_dev for card %d\n",
				 __func__, card->host->index);
		return;
	}

	if (sdio_al_claim_mutex(sdio_al_dev, __func__))
		return;

	if (sdio_al_dev->state == CARD_REMOVED) {
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return;
	}

	if (!card->sdio_func[0]) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: NULL "
						"func1\n", __func__);
		sdio_al_release_mutex(sdio_al_dev, __func__);
		return;
	}

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":%s for card %d\n",
			 __func__, card->host->index);

	sdio_al_dev->state = CARD_REMOVED;

	memset(pdev_arr, 0, sizeof(pdev_arr));
	sdio_al_invalidate_sdio_clients(sdio_al_dev, pdev_arr);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":%s: ask_reading_mailbox "
			"for card %d\n", __func__, card->host->index);
	sdio_al_dev->is_ready = false; /* Flag worker to exit */
	sdio_al_dev->ask_mbox = false;
	ask_reading_mailbox(sdio_al_dev); /* Wakeup worker */

	stop_and_del_timer(sdio_al_dev);

	sdio_al_release_mutex(sdio_al_dev, __func__);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: Notifying SDIO "
						    "clients for card %d",
			__func__, sdio_al_dev->host->index);
	for (i = 0; i < SDIO_AL_MAX_CHANNELS; i++) {
		if (!pdev_arr[i])
			continue;
		platform_device_unregister(pdev_arr[i]);
	}
	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: Finished Notifying "
						    "SDIO clients for card %d",
			__func__, sdio_al_dev->host->index);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":%s: vote for sleep for "
			"card %d\n", __func__, card->host->index);
	sdio_al_vote_for_sleep(sdio_al_dev, 1);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":%s: flush_workqueue for "
			"card %d\n", __func__, card->host->index);
	flush_workqueue(sdio_al_dev->workqueue);
	destroy_workqueue(sdio_al_dev->workqueue);
	wake_lock_destroy(&sdio_al_dev->wake_lock);

	sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ":%s: sdio card %d removed."
			"\n", __func__,	card->host->index);
}

static void sdio_print_mailbox(char *prefix_str, struct sdio_mailbox *mailbox)
{
	int k = 0;
	char buf[256];
	char buf1[10];

	if (!mailbox) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": mailbox is "
				"NULL\n");
		return;
	}

	sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s: pipes 0_7: eot=0x%x,"
		" thresh=0x%x, overflow=0x%x, "
		"underflow=0x%x, mask_thresh=0x%x\n",
		 prefix_str, mailbox->eot_pipe_0_7,
		 mailbox->thresh_above_limit_pipe_0_7,
		 mailbox->overflow_pipe_0_7,
		 mailbox->underflow_pipe_0_7,
		 mailbox->mask_thresh_above_limit_pipe_0_7);

	memset(buf, 0, sizeof(buf));
	strncat(buf, ": bytes_avail:", sizeof(buf));

	for (k = 0 ; k < SDIO_AL_ACTIVE_PIPES ; ++k) {
		snprintf(buf1, sizeof(buf1), "%d, ",
			 mailbox->pipe_bytes_avail[k]);
		strncat(buf, buf1, sizeof(buf));
	}

	sdio_al_loge(&sdio_al->gen_log, MODULE_NAME "%s", buf);
}

static void sdio_al_print_info(void)
{
	int i = 0;
	int j = 0;
	int ret = 0;
	struct sdio_mailbox *mailbox = NULL;
	struct sdio_mailbox *hw_mailbox = NULL;
	struct peer_sdioc_channel_config *ch_config = NULL;
	struct sdio_func *func1 = NULL;
	struct sdio_func *lpm_func = NULL;
	int offset = 0;
	int is_ok_to_sleep = 0;
	char buf[50];

	if (sdio_al->skip_print_info == 1)
		return;

	sdio_al->skip_print_info = 1;

	sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s - SDIO DEBUG INFO\n",
			__func__);

	if (!sdio_al) {
		sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": %s - ERROR - "
				"sdio_al is NULL\n",  __func__);
		return;
	}

	sdio_al_loge(&sdio_al->gen_log, MODULE_NAME ": GPIO mdm2ap_status=%d\n",
				sdio_al->pdata->get_mdm2ap_status());

	for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
		struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];

		if (sdio_al_dev == NULL) {
			continue;
		}

		if (!sdio_al_dev->host) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": Host"
					" is NULL\n);");
			continue;
		}

		snprintf(buf, sizeof(buf), "Card#%d: Shadow HW MB",
		       sdio_al_dev->host->index);

		/* printing Shadowing HW Mailbox*/
		mailbox = sdio_al_dev->mailbox;
		sdio_print_mailbox(buf, mailbox);

		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": Card#%d: "
			"is_ok_to_sleep=%d\n",
			sdio_al_dev->host->index,
			sdio_al_dev->is_ok_to_sleep);


		sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME ": Card#%d: "
				   "Shadow channels SW MB:",
		       sdio_al_dev->host->index);

		/* printing Shadowing SW Mailbox per channel*/
		for (i = 0 ; i < SDIO_AL_MAX_CHANNELS ; ++i) {
			struct sdio_channel *ch = &sdio_al_dev->channel[i];

			if (ch == NULL) {
				continue;
			}

			if (ch->state == SDIO_CHANNEL_STATE_INVALID)
				continue;

			ch_config = &sdio_al_dev->channel[i].ch_config;

			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": Ch %s: max_rx_thres=0x%x, "
				"max_tx_thres=0x%x, tx_buf=0x%x, "
				"is_packet_mode=%d, "
				"max_packet=0x%x, min_write=0x%x",
				ch->name, ch_config->max_rx_threshold,
				ch_config->max_tx_threshold,
				ch_config->tx_buf_size,
				ch_config->is_packet_mode,
				ch_config->max_packet_size,
				ch->min_write_avail);

			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": total_rx=0x%x, total_tx=0x%x, "
				"read_avail=0x%x, write_avail=0x%x, "
				"rx_pending=0x%x, num_reads=0x%x, "
				"num_notifs=0x%x", ch->total_rx_bytes,
				ch->total_tx_bytes, ch->read_avail,
				ch->write_avail, ch->rx_pending_bytes,
				ch->statistics.total_read_times,
				ch->statistics.total_notifs);
		} /* end loop over all channels */

	} /* end loop over all devices */

	/* reading from client and printing is_host_ok_to_sleep per device */
	for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
		struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];

		if (sdio_al_verify_func1(sdio_al_dev, __func__))
			continue;

		if (!sdio_al_dev->host) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": Host is NULL");
			continue;
		}

		if (sdio_al_dev->lpm_chan == INVALID_SDIO_CHAN) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
				": %s - for Card#%d, is lpm_chan=="
				"INVALID_SDIO_CHAN. continuing...",
				__func__, sdio_al_dev->host->index);
			continue;
		}

		offset = offsetof(struct peer_sdioc_sw_mailbox, ch_config)+
		sizeof(struct peer_sdioc_channel_config) *
		sdio_al_dev->lpm_chan+
		offsetof(struct peer_sdioc_channel_config, is_host_ok_to_sleep);

		lpm_func = sdio_al_dev->card->sdio_func[sdio_al_dev->
								lpm_chan+1];
		if (!lpm_func) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": %s - lpm_func is NULL for card#%d"
					" continuing...\n", __func__,
					sdio_al_dev->host->index);
			continue;
		}

		if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
			return;
		ret  =  sdio_memcpy_fromio(lpm_func,
					    &is_ok_to_sleep,
					    SDIOC_SW_MAILBOX_ADDR+offset,
					    sizeof(int));
		sdio_al_release_mutex(sdio_al_dev, __func__);

		if (ret)
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": %s - fail to read "
				"is_HOST_ok_to_sleep from mailbox for card %d",
				__func__, sdio_al_dev->host->index);
		else
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": Card#%d: "
				"is_HOST_ok_to_sleep=%d\n",
				sdio_al_dev->host->index,
				is_ok_to_sleep);
	}

	for (j = 0 ; j < MAX_NUM_OF_SDIO_DEVICES ; ++j) {
		struct sdio_al_device *sdio_al_dev = sdio_al->devices[j];

		if (!sdio_al_dev)
			continue;

		/* Reading HW Mailbox */
		hw_mailbox = sdio_al_dev->mailbox;

		if (sdio_al_claim_mutex_and_verify_dev(sdio_al_dev, __func__))
			return;

		if (!sdio_al_dev->card || !sdio_al_dev->card->sdio_func[0]) {
			sdio_al_release_mutex(sdio_al_dev, __func__);
			return;
		}
		func1 = sdio_al_dev->card->sdio_func[0];
		ret = sdio_memcpy_fromio(func1, hw_mailbox,
			HW_MAILBOX_ADDR, sizeof(*hw_mailbox));
		sdio_al_release_mutex(sdio_al_dev, __func__);

		if (ret) {
			sdio_al_loge(sdio_al_dev->dev_log, MODULE_NAME
					": fail to read "
			       "mailbox for card#%d. "
			       "continuing...\n",
			       sdio_al_dev->host->index);
			continue;
		}

		snprintf(buf, sizeof(buf), "Card#%d: Current HW MB",
		       sdio_al_dev->host->index);

		/* Printing HW Mailbox */
		sdio_print_mailbox(buf, hw_mailbox);
	}
}

static struct sdio_device_id sdio_al_sdioid[] = {
    {.class = 0, .vendor = 0x70, .device = 0x2460},
    {.class = 0, .vendor = 0x70, .device = 0x0460},
    {.class = 0, .vendor = 0x70, .device = 0x23F1},
    {.class = 0, .vendor = 0x70, .device = 0x23F0},
    {}
};

static struct sdio_driver sdio_al_sdiofn_driver = {
    .name      = "sdio_al_sdiofn",
    .id_table  = sdio_al_sdioid,
    .probe     = sdio_al_sdio_probe,
    .remove    = sdio_al_sdio_remove,
};

#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
/*
 *  Callback for notifications from restart mudule.
 *  This function handles only the BEFORE_RESTART notification.
 *  Stop all the activity on the card and notify our clients.
 */
static int sdio_al_subsys_notifier_cb(struct notifier_block *this,
				  unsigned long notif_type,
				  void *data)
{
	if (notif_type != SUBSYS_BEFORE_SHUTDOWN) {
		sdio_al_logi(&sdio_al->gen_log, MODULE_NAME ": %s: got "
				"notification %ld", __func__, notif_type);
		return NOTIFY_DONE;
	}

	sdio_al_reset();
	return NOTIFY_OK;
}

static struct notifier_block sdio_al_nb = {
	.notifier_call = sdio_al_subsys_notifier_cb,
};
#endif

/**
 *  Module Init.
 *
 *  @warn: allocate sdio_al context before registering driver.
 *
 */
static int __init sdio_al_init(void)
{
	int ret = 0;
	int i;

	pr_debug(MODULE_NAME ":sdio_al_init\n");

	pr_info(MODULE_NAME ":SDIO-AL SW version %s\n",
		DRV_VERSION);

	sdio_al = kzalloc(sizeof(struct sdio_al), GFP_KERNEL);
	if (sdio_al == NULL)
		return -ENOMEM;

	for (i = 0; i < MAX_NUM_OF_SDIO_DEVICES ; ++i)
		sdio_al->devices[i] = NULL;

	sdio_al->unittest_mode = false;

	sdio_al->debug.debug_lpm_on = debug_lpm_on;
	sdio_al->debug.debug_data_on = debug_data_on;
	sdio_al->debug.debug_close_on = debug_close_on;

#ifdef CONFIG_DEBUG_FS
	sdio_al_debugfs_init();
#endif


#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
	sdio_al->subsys_notif_handle = subsys_notif_register_notifier(
		"external_modem", &sdio_al_nb);
#endif

	ret = platform_driver_register(&msm_sdio_al_driver);
	if (ret) {
		pr_err(MODULE_NAME ": platform_driver_register failed: %d\n",
		       ret);
		goto exit;
	}

	sdio_register_driver(&sdio_al_sdiofn_driver);

	spin_lock_init(&sdio_al->gen_log.log_lock);

exit:
	if (ret)
		kfree(sdio_al);
	return ret;
}

/**
 *  Module Exit.
 *
 *  Free allocated memory.
 *  Disable SDIO-Card.
 *  Unregister driver.
 *
 */
static void __exit sdio_al_exit(void)
{
	if (sdio_al == NULL)
		return;

	pr_debug(MODULE_NAME ":sdio_al_exit\n");

#ifdef CONFIG_MSM_SUBSYSTEM_RESTART
	subsys_notif_unregister_notifier(
		sdio_al->subsys_notif_handle, &sdio_al_nb);
#endif

	sdio_al_tear_down();

	sdio_unregister_driver(&sdio_al_sdiofn_driver);

	kfree(sdio_al);

#ifdef CONFIG_DEBUG_FS
	sdio_al_debugfs_cleanup();
#endif

	platform_driver_unregister(&msm_sdio_al_driver);

	pr_debug(MODULE_NAME ":sdio_al_exit complete\n");
}

module_init(sdio_al_init);
module_exit(sdio_al_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("SDIO Abstraction Layer");
MODULE_AUTHOR("Amir Samuelov <amirs@codeaurora.org>");
MODULE_VERSION(DRV_VERSION);