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
|
/*
* viatel_cbp_sync.c
*
* VIA CBP driver for Linux
*
* Copyright (C) 2011 VIA TELECOM Corporation, Inc.
* Author: VIA TELECOM Corporation, Inc.
*
* This package is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/time.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/wakelock.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <mach/viatel.h>
#include "core.h"
//define in oem.c
extern int isviatelcom;
extern int ap_ready_always;
static int asc_debug = 0;
#define ASCDPRT(fmt, arg...) do{ \
if(asc_debug) \
printk("[ASC] " fmt, ##arg); \
}while(0)
#define ASCPRT(fmt, arg...) printk("[ASC] " fmt, ##arg)
/*mS*/
#define ASC_RX_WAIT_IDLE_TIME (1000)
#define ASC_TX_WAIT_READY_TIME (1000)
#define ASC_TX_WAIT_IDLE_TIME (2000)
#define ASC_TX_AUTO_DELAY_TIME (2000)
#define ASC_TX_WAIT_SLEEP_TIME (500)
#define ASC_TX_TRY_TIMES (3)
#define ASC_TX_DEBOUNCE_TIME (10)
#define ASC_TX_SYSFS_USER "AscApp"
#define ASC_TX_AUTO_USER "AscAuto"
/* asc_list contains all registered struct struct asc_handle*/
static DEFINE_SPINLOCK(hdlock);
static LIST_HEAD(asc_tx_handle_list);
static LIST_HEAD(asc_rx_handle_list);
static struct workqueue_struct *asc_work_queue;
static struct kobject *asc_kobj;
typedef enum{
ASC_TX_HD = 0,
ASC_RX_HD
}asc_handle_type;
#define ASC_EVENT_POOL_MAX (60)
typedef enum{
ASC_EVENT_UNUSE = 0,
ASC_EVENT_STATIC,
ASC_EVENT_DYNAMIC
}asc_event_type;
struct asc_event{
int id;
struct list_head list;
char usage;
};
static struct asc_event event_pool[ASC_EVENT_POOL_MAX];
struct asc_user{
struct asc_infor infor;
atomic_t count;
struct list_head node;
};
struct asc_state_dsp{
char name[ASC_NAME_LEN];
/*state callback handle for events*/
int (*handle)(void * hd, int event);
};
/* TX STATUS and TX EVENT*/
typedef enum{
AP_TX_EVENT_REQUEST = 0, /*internal*/
AP_TX_EVENT_CP_READY,
AP_TX_EVENT_CP_UNREADY,
AP_TX_EVENT_WAIT_TIMEOUT,
AP_TX_EVENT_IDLE_TIMEOUT,
AP_TX_EVENT_STOP,
AP_TX_EVENT_RESET,
AP_TX_EVENT_NUM
} ap_tx_event;
typedef enum{
AP_TX_ST_SLEEP = 0,
AP_TX_ST_WAIT_READY,
AP_TX_ST_READY, /*wait All Tx channel finished*/
AP_TX_ST_IDLE,
AP_TX_ST_NUM
} ap_tx_state;
struct asc_tx_handle{
struct asc_config cfg;
atomic_t state;
atomic_t count;
struct list_head user_list;
struct asc_state_dsp *table;
/*process the event to switch different states*/
struct task_struct *thread;
int ntf;
int wait_try;
int auto_delay;
spinlock_t slock;
wait_queue_head_t wait;
wait_queue_head_t wait_tx_state;
struct mutex mlock;
struct wake_lock wlock;
struct timer_list timer_wait_ready;
struct timer_list timer_wait_idle;
struct timer_list timer_wait_sleep;
struct work_struct ntf_work;
struct list_head event_q;
struct list_head node;
struct kobject *kobj;
};
static int asc_tx_handle_sleep(void *, int );
static int asc_tx_handle_wait_ready(void *, int );
static int asc_tx_handle_ready(void *, int );
static int asc_tx_handle_idle(void *, int );
/*the table used to discribe all tx states*/
static struct asc_state_dsp asc_tx_table[AP_TX_ST_NUM] = {
[AP_TX_ST_SLEEP] = {
.name = "AP_TX_ST_SLEEP",
.handle = asc_tx_handle_sleep,
},
[AP_TX_ST_WAIT_READY] = {
.name = "AP_TX_ST_WAIT_READY",
.handle = asc_tx_handle_wait_ready,
},
[AP_TX_ST_READY] = {
.name = "AP_TX_ST_READY",
.handle = asc_tx_handle_ready,
},
[AP_TX_ST_IDLE] = {
.name = "AP_TX_ST_IDLE",
.handle = asc_tx_handle_idle,
},
};
/* RX STATUS and RX EVENT*/
typedef enum{
AP_RX_EVENT_REQUEST = 0,
AP_RX_EVENT_AP_READY,
AP_RX_EVENT_AP_UNREADY,
AP_RX_EVENT_STOP,
AP_RX_EVENT_IDLE_TIMEOUT,
AP_RX_EVENT_RESET,
AP_RX_EVENT_NUM
} ap_rx_event;
typedef enum{
AP_RX_ST_SLEEP = 0,
AP_RX_ST_WAIT_READY,
AP_RX_ST_READY,
AP_RX_ST_IDLE,
AP_RX_ST_NUM
} ap_rx_state;
struct asc_rx_handle{
struct asc_config cfg;
atomic_t state;
struct list_head user_list;
struct asc_state_dsp *table;
int ntf;
/*process the event to switch different states*/
struct task_struct *thread;
spinlock_t slock;
wait_queue_head_t wait;
struct mutex mlock;
struct wake_lock wlock;
struct timer_list timer;
struct list_head event_q;
struct list_head node;
struct work_struct ntf_work;
};
static struct asc_config tx_cfg,rx_cfg;
static int asc_rx_handle_sleep(void *, int );
static int asc_rx_handle_wait_ready(void *, int );
static int asc_rx_handle_ready(void *, int );
static int asc_rx_handle_idle(void *, int );
/*the table used to discribe all rx states*/
static struct asc_state_dsp asc_rx_table[AP_RX_ST_NUM] = {
[AP_RX_ST_SLEEP] = {
.name = "AP_RX_ST_SLEEP",
.handle = asc_rx_handle_sleep,
},
[AP_RX_ST_WAIT_READY] = {
.name = "AP_RX_ST_WAIT_READY",
.handle = asc_rx_handle_wait_ready,
},
[AP_RX_ST_READY] = {
.name = "AP_RX_ST_READY",
.handle = asc_rx_handle_ready,
},
[AP_RX_ST_IDLE] = {
.name = "AP_RX_ST_IDLE",
.handle = asc_rx_handle_idle,
},
};
static int asc_tx_event_send(struct asc_tx_handle *tx, int id);
static void asc_tx_handle_reset(struct asc_tx_handle *tx);
static int asc_rx_event_send(struct asc_rx_handle *rx, int id);
static void asc_rx_handle_reset(struct asc_rx_handle *rx);
static struct asc_event * asc_event_malloc(void)
{
int i = 0;
unsigned long flags = 0;
struct asc_event *event = NULL;
spin_lock_irqsave(&hdlock, flags);
for(i = 0; i < ASC_EVENT_POOL_MAX; i++){
if(ASC_EVENT_UNUSE == event_pool[i].usage){
event = &(event_pool[i]);
event->usage = ASC_EVENT_STATIC;
}
}
if(NULL == event){
event = kmalloc(sizeof(struct asc_event), GFP_ATOMIC);
if(event){
event->usage = ASC_EVENT_DYNAMIC;
}
}
spin_unlock_irqrestore(&hdlock, flags);
return event;
}
static void asc_event_free(struct asc_event * event)
{
unsigned long flags = 0;
if(!event)
return ;
spin_lock_irqsave(&hdlock, flags);
if(ASC_EVENT_STATIC == event->usage){
memset(event, 0, sizeof(struct asc_event));
}else{
kfree(event);
}
spin_unlock_irqrestore(&hdlock, flags);
}
static irqreturn_t asc_irq_cp_indicate_state(int irq, void *data)
{
int level;
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
struct asc_config *cfg = &tx->cfg;
if (!oem_gpio_irq_isenable(cfg->gpio_ready) ||
!oem_gpio_irq_isint(cfg->gpio_ready)){
return IRQ_NONE;
}
oem_gpio_irq_clear(cfg->gpio_ready);
level = !!oem_gpio_get_value(cfg->gpio_ready);
oem_gpio_set_irq_type(cfg->gpio_ready, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING);
ASCDPRT("Irq %s cp_indicate_ap %s.\n", cfg->name, (level == cfg->polar)?"WAKEN":"SLEEP");
if(level == cfg->polar){
asc_tx_event_send(tx, AP_TX_EVENT_CP_READY);
}else{
/*do not care*/
//asc_tx_event_send(tx, AP_TX_EVENT_CP_UNREADY);
}
oem_gpio_irq_unmask(cfg->gpio_ready);
return IRQ_HANDLED;
}
static irqreturn_t asc_irq_cp_wake_ap(int irq, void *data)
{
int level;
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
struct asc_config *cfg = &rx->cfg;
if(!cfg||cfg->gpio_wake<=0){
return IRQ_NONE;
}
if (!oem_gpio_irq_isenable(cfg->gpio_wake) ||
!oem_gpio_irq_isint(cfg->gpio_wake)){
return IRQ_NONE;
}
oem_gpio_irq_clear(cfg->gpio_wake);
level = !!oem_gpio_get_value(cfg->gpio_wake);
oem_gpio_set_irq_type(cfg->gpio_wake, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING);
ASCDPRT("Irq %s cp_wake_ap, requset ap to be %s.\n", cfg->name, (level == cfg->polar)?"WAKEN":"SLEEP");
if(level == cfg->polar){
/*Cp requset Ap wake*/
wake_lock(&rx->wlock);
/*FIXME: jump to ready as soon as possible to avoid the AP_READY error indication to CBP */
if(AP_RX_ST_IDLE == atomic_read(&rx->state)){
ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", cfg->name, AP_RX_EVENT_REQUEST, rx->table[AP_RX_ST_IDLE].name);
asc_rx_handle_idle(rx, AP_RX_EVENT_REQUEST);
ASCDPRT("Rx(%s): go into state(%s).\n", cfg->name, rx->table[atomic_read(&rx->state)].name);
}else{
asc_rx_event_send(rx, AP_RX_EVENT_REQUEST);
}
}else{
/*Cp allow Ap sleep*/
asc_rx_event_send(rx, AP_RX_EVENT_STOP);
}
oem_gpio_irq_unmask(cfg->gpio_wake);
return IRQ_HANDLED;
}
static struct asc_tx_handle *asc_tx_handle_lookup(const char *name)
{
unsigned long flags;
struct asc_tx_handle *hd, *tmp, *t;
if (!name)
return NULL;
hd = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node) {
if (!strncmp(name, tmp->cfg.name, ASC_NAME_LEN - 1)) {
hd = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
return hd;
}
static struct asc_rx_handle *asc_rx_handle_lookup(const char *name)
{
unsigned long flags;
struct asc_rx_handle *hd, *tmp, *t;
if (!name)
return NULL;
hd = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_rx_handle_list, node) {
if (!strncmp(name, tmp->cfg.name, ASC_NAME_LEN - 1)) {
hd = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
return hd;
}
irqreturn_t viatelcom_irq_cp_wake_ap(int irq, void *data){
struct asc_rx_handle *rx;
if(!isviatelcom||ap_ready_always)
return IRQ_HANDLED;
rx = asc_rx_handle_lookup(USB_RX_HD_NAME);
return asc_irq_cp_wake_ap(irq,rx);
}
static struct asc_user *asc_tx_user_lookup(struct asc_tx_handle *tx, const char *name)
{
unsigned long flags = 0;
struct asc_user *user = NULL, *tmp = NULL, *t = NULL;
if (!name)
return NULL;
spin_lock_irqsave(&tx->slock, flags);
list_for_each_entry_safe(tmp, t, &tx->user_list, node) {
if (!strncmp(name, tmp->infor.name, ASC_NAME_LEN - 1)) {
user = tmp;
break;
}
}
spin_unlock_irqrestore(&tx->slock, flags);
return user;
}
static struct asc_user *asc_rx_user_lookup(struct asc_rx_handle *rx, const char *name)
{
unsigned long flags = 0;
struct asc_user *user = NULL, *tmp = NULL, *t = NULL;
if (!name)
return NULL;
spin_lock_irqsave(&rx->slock, flags);
list_for_each_entry_safe(tmp, t, &rx->user_list, node) {
if (!strncmp(name, tmp->infor.name, ASC_NAME_LEN - 1)) {
user = tmp;
break;
}
}
spin_unlock_irqrestore(&rx->slock, flags);
return user;
}
static inline void asc_rx_indicate_wake(struct asc_rx_handle *rx)
{
if(rx->cfg.gpio_ready >= 0)
oem_gpio_direction_output(rx->cfg.gpio_ready, rx->cfg.polar);
}
static inline void asc_rx_indicate_sleep(struct asc_rx_handle *rx)
{
if(rx->cfg.gpio_ready >= 0)
oem_gpio_direction_output(rx->cfg.gpio_ready, !rx->cfg.polar);
}
static int asc_rx_event_send(struct asc_rx_handle *rx, int id)
{
unsigned long flags = 0;
struct asc_event *event = NULL;
int ret = -ENODEV;
if(rx->thread == NULL){
ASCPRT("%s:no thread for event\n", __FUNCTION__);
return ret;
}
/*check whether the event is cared by current charge state*/
if(id >= 0){
event = asc_event_malloc();
if(!event){
ASCPRT("No memory to create new event.\n");
ret = -ENOMEM;
goto send_event_error;
}
/*insert a new event to the list tail and wakeup the process thread*/
//ASCDPRT("Rx(%s):send event(%d) to state(%s).\n", rx->name, id, rx->table[atomic_read(&rx->state)].name);
event->id = id;
spin_lock_irqsave(&rx->slock, flags);
if(AP_RX_EVENT_RESET == id){
list_add(&event->list, &rx->event_q);
}else{
list_add_tail(&event->list, &rx->event_q);
}
spin_unlock_irqrestore(&rx->slock, flags);
wake_up(&rx->wait);
}
ret = 0;
send_event_error:
return ret;
}
static int asc_rx_event_recv(struct asc_rx_handle *rx)
{
unsigned long flags = 0;
struct asc_event *event = NULL;
int ret = -ENODEV;
if(rx->thread == NULL){
ASCPRT("%s:no thread for event\n", __FUNCTION__);
return ret;
}
spin_lock_irqsave(&rx->slock, flags);
if(!list_empty(&rx->event_q)){
event = list_first_entry(&rx->event_q, struct asc_event, list);
list_del(&event->list);
}
spin_unlock_irqrestore(&rx->slock, flags);
if(event){
ret = event->id;
asc_event_free(event);
}
return ret;
}
static int asc_rx_event_thread(void *data)
{
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
int id = 0, index;
char name[ASC_NAME_LEN] = {0};
struct asc_state_dsp *dsp = NULL;
rx->thread = current;
snprintf(name, ASC_NAME_LEN, "asc_rx_%s", rx->cfg.name);
daemonize(name);
ASCDPRT("%s thread start now.\n", name);
while(1){
/*sleep until receive an evnet or thread exist*/
wait_event(rx->wait, ((id=asc_rx_event_recv(rx)) >= 0) || (!rx->thread));
/*thread is existed*/
if(!rx->thread){
break;
}
mutex_lock(&rx->mlock);
if(AP_RX_EVENT_RESET == id){
asc_rx_handle_reset(rx);
}else{
index = atomic_read(&rx->state);
dsp = rx->table + index;
if(dsp->handle){
ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", rx->cfg.name, id, dsp->name);
dsp->handle(rx, id);
ASCDPRT("Rx(%s): go into state(%s).\n", rx->cfg.name, rx->table[atomic_read(&rx->state)].name);
}
}
mutex_unlock(&rx->mlock);
}
ASCDPRT("%s thread exit.\n", name);
kfree(rx);
return 0;
}
static void asc_rx_event_timer(unsigned long data)
{
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
//ASCDPRT("%s timer is time out.\n", rx->name);
asc_rx_event_send(rx, AP_RX_EVENT_IDLE_TIMEOUT);
}
static void asc_tx_notifier_work(struct work_struct *work)
{
struct asc_infor *infor;
struct asc_user *user = NULL, *t = NULL;
struct asc_tx_handle *tx = container_of(work, struct asc_tx_handle,
ntf_work);
list_for_each_entry_safe(user, t, &tx->user_list, node){
infor = &user->infor;
if(infor->notifier){
infor->notifier(tx->ntf, infor->data);
}
}
}
static void asc_rx_notifier_work(struct work_struct *work)
{
struct asc_infor *infor;
struct asc_user *user = NULL, *t = NULL;
struct asc_rx_handle *rx = container_of(work, struct asc_rx_handle,
ntf_work);
list_for_each_entry_safe(user, t, &rx->user_list, node){
infor = &user->infor;
if(infor->notifier){
infor->notifier(rx->ntf, infor->data);
}
}
}
static void asc_tx_notifier(struct asc_tx_handle *tx, int ntf)
{
tx->ntf = ntf;
queue_work(asc_work_queue, &tx->ntf_work);
}
static void asc_rx_notifier(struct asc_rx_handle *rx, int ntf)
{
rx->ntf = ntf;
queue_work(asc_work_queue, &rx->ntf_work);
}
static int asc_rx_handle_init(struct asc_rx_handle *rx)
{
int ret = 0;
char *name = NULL;
struct asc_config *cfg = &rx->cfg;
if(cfg->gpio_ready >= 0){
ret = oem_gpio_request(cfg->gpio_ready, "ap_ready");
if(ret < 0){
ASCPRT("Fail to requset ap_ready gpio %d for %s.\n", cfg->gpio_ready, cfg->name);
goto err_request_gpio_ap_ready;
}
if(ap_ready_always)
oem_gpio_output(cfg->gpio_ready, cfg->polar);
else
asc_rx_indicate_sleep(rx);
}
if(cfg->gpio_wake >= 0){
ret = oem_gpio_request(cfg->gpio_wake, "cp_wake_ap");
if(ret < 0){
ASCPRT("Fail to requset cp_wake_ap gpio %d for %s.\n", cfg->gpio_wake, cfg->name);
goto err_request_gpio_cp_wake_ap;
}
oem_gpio_irq_mask(cfg->gpio_wake);
oem_gpio_direction_input_for_irq(cfg->gpio_wake);
oem_gpio_set_irq_type(cfg->gpio_wake, IRQF_TRIGGER_RISING);
ret = oem_gpio_request_irq(cfg->gpio_wake, asc_irq_cp_wake_ap, IRQF_SHARED | IRQF_NO_SUSPEND, "cp_wake_ap", rx);
printk("asc_rx_handle_init call oem_gpio_irq_unmask %d\n",cfg->gpio_wake);
oem_gpio_irq_unmask(cfg->gpio_wake);
if (ret < 0) {
ASCPRT("fail to request cp_wake_ap irq for %s\n", cfg->name);
goto err_req_irq_cp_wake_ap;
}
}
rx->table = asc_rx_table;
mutex_init(&rx->mlock);
INIT_LIST_HEAD(&rx->event_q);
INIT_LIST_HEAD(&rx->user_list);
spin_lock_init(&rx->slock);
setup_timer(&rx->timer, asc_rx_event_timer, (unsigned long)rx);
name = kzalloc(ASC_NAME_LEN, GFP_KERNEL);
if(!name){
ret = -ENOMEM;
ASCPRT("%s: no memory to malloc for wake lock name\n", __FUNCTION__);
goto err_malloc_name;
}
snprintf(name, ASC_NAME_LEN, "asc_rx_%s", rx->cfg.name);
wake_lock_init(&rx->wlock, WAKE_LOCK_SUSPEND, name);
init_waitqueue_head(&rx->wait);
INIT_WORK(&rx->ntf_work, asc_rx_notifier_work);
if(ap_ready_always)
atomic_set(&rx->state, AP_RX_ST_READY);
else
atomic_set(&rx->state, AP_RX_ST_SLEEP);
ret = kernel_thread(asc_rx_event_thread, rx, 0);
if(ret < 0){
ASCPRT("Fail to create %s rx thread.\n", rx->cfg.name);
goto err_create_rx_thread;
}
if(!!oem_gpio_get_value(cfg->gpio_wake) == cfg->polar){
asc_rx_event_send(rx, AP_RX_EVENT_REQUEST);
}
return 0;
err_create_rx_thread:
if(name)
kfree(name);
err_malloc_name:
if(cfg->gpio_wake >= 0)
free_irq(oem_gpio_to_irq(cfg->gpio_wake), rx);
err_req_irq_cp_wake_ap:
if(cfg->gpio_wake)
oem_gpio_free(cfg->gpio_wake);
err_request_gpio_cp_wake_ap:
if(cfg->gpio_ready >= 0)
oem_gpio_free(cfg->gpio_ready);
err_request_gpio_ap_ready:
return ret;
}
static int asc_rx_handle_sleep(void *data, int event)
{
int ret = 0;
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
//ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", rx->name, event, rx->table[atomic_read(&rx->state)].name);
if(AP_RX_ST_SLEEP != atomic_read(&rx->state)){
return 0;
}
switch(event){
case AP_RX_EVENT_REQUEST:
wake_lock(&rx->wlock);
atomic_set(&rx->state, AP_RX_ST_WAIT_READY);
asc_rx_notifier(rx, ASC_NTF_RX_PREPARE);
break;
default:
ASCDPRT("ignore the rx event %d in state(%s)", event, rx->table[atomic_read(&rx->state)].name);
}
//ASCDPRT("Rx(%s): go into state(%s).\n", rx->name, rx->table[atomic_read(&rx->state)].name);
return ret;
}
static int asc_rx_handle_wait_ready(void *data, int event)
{
int ret = 0;
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
//ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", rx->name, event, rx->table[atomic_read(&rx->state)].name);
if(AP_RX_ST_WAIT_READY != atomic_read(&rx->state)){
return 0;
}
switch(event){
case AP_RX_EVENT_AP_READY:
/*need ack ready to cp, do nothing if no gpio for ap_ready*/
asc_rx_indicate_wake(rx);
atomic_set(&rx->state, AP_RX_ST_READY);
break;
case AP_RX_EVENT_AP_UNREADY:
case AP_RX_EVENT_STOP:
atomic_set(&rx->state, AP_RX_ST_SLEEP);
asc_rx_notifier(rx, ASC_NTF_RX_POST);
/*need ack ready to cp, do nothing if no gpio for ap_ready*/
asc_rx_indicate_sleep(rx);
wake_unlock(&rx->wlock);
break;
default:
ASCDPRT("ignore the rx event %d in state(%s)", event, rx->table[atomic_read(&rx->state)].name);
}
//ASCDPRT("Rx(%s): go into state(%s).\n", rx->name, rx->table[atomic_read(&rx->state)].name);
return ret;
}
static int asc_rx_handle_ready(void *data, int event)
{
int ret = 0;
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
//ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", rx->name, event, rx->table[atomic_read(&rx->state)].name);
if(AP_RX_ST_READY != atomic_read(&rx->state)){
return 0;
}
switch(event){
case AP_RX_EVENT_STOP:
atomic_set(&rx->state, AP_RX_ST_IDLE);
mod_timer(&rx->timer, jiffies + msecs_to_jiffies(ASC_RX_WAIT_IDLE_TIME));
break;
default:
ASCDPRT("ignore the rx event %d in state(%s)", event, rx->table[atomic_read(&rx->state)].name);
}
//ASCDPRT("Rx(%s): go into state(%s).\n", rx->name, rx->table[atomic_read(&rx->state)].name);
return ret;
}
static int asc_rx_handle_idle(void *data, int event)
{
int ret = 0;
unsigned long flags = 0;
struct asc_rx_handle *rx = (struct asc_rx_handle *)data;
//ASCDPRT("Rx(%s): process event(%d) in state(%s).\n", rx->name, event, rx->table[atomic_read(&rx->state)].name);
if(AP_RX_ST_IDLE != atomic_read(&rx->state)){
return 0;
}
/*FIXME: prevent from scheduled and interrupted to avoid error indication to CBP*/
spin_lock_irqsave(&rx->slock, flags);
switch(event){
case AP_RX_EVENT_REQUEST:
del_timer(&rx->timer);
atomic_set(&rx->state, AP_RX_ST_READY);
break;
case AP_RX_EVENT_IDLE_TIMEOUT:
asc_rx_notifier(rx, ASC_NTF_RX_POST);
atomic_set(&rx->state, AP_RX_ST_SLEEP);
/*need ack ready to cp, do nothing if no gpio for ap_ready*/
asc_rx_indicate_sleep(rx);
wake_unlock(&rx->wlock);
break;
default:
ASCDPRT("ignore the rx event %d in state(%s)", event, rx->table[atomic_read(&rx->state)].name);
}
spin_unlock_irqrestore(&rx->slock, flags);
//ASCDPRT("Rx(%s): go into state(%s).\n", rx->name, rx->table[atomic_read(&rx->state)].name);
return ret;
}
static void asc_tx_trig_busy(struct asc_tx_handle *tx)
{
mod_timer(&tx->timer_wait_idle, jiffies + msecs_to_jiffies(tx->auto_delay));
}
static inline void asc_tx_wake_cp(struct asc_tx_handle *tx)
{
if(tx->cfg.gpio_wake >= 0)
oem_gpio_direction_output(tx->cfg.gpio_wake, tx->cfg.polar);
}
static inline void asc_tx_sleep_cp(struct asc_tx_handle *tx)
{
if(tx->cfg.gpio_wake >= 0)
oem_gpio_direction_output(tx->cfg.gpio_wake, !tx->cfg.polar);
}
static inline int asc_tx_cp_be_ready(struct asc_tx_handle *tx)
{
int ret = 0;
if(tx->cfg.gpio_ready >= 0)
ret = ((!!oem_gpio_get_value(tx->cfg.gpio_ready)) == (tx->cfg.polar));
return ret;
}
static int asc_tx_event_send(struct asc_tx_handle *tx, int id)
{
unsigned long flags = 0;
struct asc_event *event = NULL;
int ret = -ENODEV;
if(tx->thread == NULL){
ASCPRT("%s:no thread for event\n", __FUNCTION__);
return ret;
}
/*check whether the event is cared by current charge state*/
if(id >= 0){
event = asc_event_malloc();
if(!event){
ASCPRT("No memory to create new event.\n");
ret = -ENOMEM;
goto send_event_error;
}
/*insert a new event to the list tail and wakeup the process thread*/
//ASCDPRT("Send tx event(%d) to state(%s).\n", id, tx->table[atomic_read(&tx->state)].name);
event->id = id;
spin_lock_irqsave(&tx->slock, flags);
if(AP_TX_EVENT_RESET == id){
list_add(&event->list, &tx->event_q);
}else{
list_add_tail(&event->list, &tx->event_q);
}
spin_unlock_irqrestore(&tx->slock, flags);
wake_up(&tx->wait);
}
send_event_error:
return ret;
}
static int asc_tx_event_recv(struct asc_tx_handle *tx)
{
unsigned long flags = 0;
struct asc_event *event = NULL;
int ret = -ENODEV;
if(tx->thread == NULL){
ASCPRT("%s:no thread for event\n", __FUNCTION__);
return ret;
}
spin_lock_irqsave(&tx->slock, flags);
if(!list_empty(&tx->event_q)){
event = list_first_entry(&tx->event_q, struct asc_event, list);
list_del(&event->list);
}
spin_unlock_irqrestore(&tx->slock, flags);
if(event){
ret = event->id;
asc_event_free(event);
}
return ret;
}
static int asc_tx_get_user(struct asc_tx_handle *tx, const char *name)
{
int ret = 0;
struct asc_user *user = NULL;
user = asc_tx_user_lookup(tx, name);
if(user){
atomic_inc(&user->count);
}else{
ret = -ENODEV;
}
return ret;
}
static int asc_tx_put_user(struct asc_tx_handle *tx, const char *name)
{
struct asc_user *user = NULL;
int ret = 0;
user = asc_tx_user_lookup(tx, name);
if(user){
if(atomic_read(&user->count) >= 1){
atomic_dec(&user->count);
}
}else{
ret = -ENODEV;
}
return ret;
}
static int asc_tx_refer(struct asc_tx_handle *tx, const char *name)
{
unsigned long flags = 0;
struct asc_user *user = NULL, *t = NULL;
int count = 0;
if(name){
/*get the reference count of the user*/
user = asc_tx_user_lookup(tx, name);
if(user){
count = atomic_read(&user->count);
}
}else{
spin_lock_irqsave(&tx->slock, flags);
list_for_each_entry_safe(user, t, &tx->user_list, node) {
count += atomic_read(&user->count);
}
spin_unlock_irqrestore(&tx->slock, flags);
}
return count;
}
static int asc_rx_refer(struct asc_rx_handle *rx, const char *name)
{
unsigned long flags = 0;
struct asc_user *user = NULL, *t = NULL;
int count = 0;
if(name){
/*get the reference count of the user*/
user = asc_rx_user_lookup(rx, name);
if(user){
count = atomic_read(&user->count);
}
}else{
spin_lock_irqsave(&rx->slock, flags);
list_for_each_entry_safe(user, t, &rx->user_list, node) {
count += atomic_read(&user->count);
}
spin_unlock_irqrestore(&rx->slock, flags);
}
return count;
}
static void asc_tx_refer_clear(struct asc_tx_handle *tx)
{
unsigned long flags = 0;
struct asc_user *user = NULL, *t = NULL;
spin_lock_irqsave(&tx->slock, flags);
list_for_each_entry_safe(user, t, &tx->user_list, node) {
atomic_set(&user->count, 0);
}
spin_unlock_irqrestore(&tx->slock, flags);
}
static int asc_tx_event_thread(void *data)
{
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
int id = 0, index;
char name[ASC_NAME_LEN] = {0};
struct asc_state_dsp *dsp = NULL;
snprintf(name, ASC_NAME_LEN, "asc_tx_%s", tx->cfg.name);
tx->thread = current;
daemonize(name);
ASCDPRT("%s thread start now.\n", name);
while(1){
/*sleep until receive an evnet or thread exist*/
wait_event(tx->wait, ((id=asc_tx_event_recv(tx)) >= 0) || (!tx->thread) );
/*thread is existed*/
if(!tx->thread){
break;
}
mutex_lock(&tx->mlock);
if(AP_TX_EVENT_RESET == id){
asc_tx_handle_reset(tx);
}else{
index = atomic_read(&tx->state);
dsp = tx->table + index;
if(dsp->handle){
ASCDPRT("Tx(%s): process event(%d) in state(%s).\n", tx->cfg.name, id, dsp->name);
dsp->handle(tx, id);
ASCDPRT("Tx(%s): go into state(%s) .\n", tx->cfg.name, tx->table[atomic_read(&tx->state)].name);
}
}
mutex_unlock(&tx->mlock);
}
ASCDPRT("%s thread exit.\n", name);
kfree(tx);
return 0;
}
static void asc_tx_wait_ready_timer(unsigned long data)
{
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
//ASCDPRT("%s tx wait ready timer is timeout.\n", tx->name);
asc_tx_event_send(tx, AP_TX_EVENT_WAIT_TIMEOUT);
}
static void asc_tx_wait_idle_timer(unsigned long data)
{
char path[ASC_NAME_LEN] = {0};
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
//ASCDPRT("%s tx wait idle timer is timeout.\n", tx->name);
snprintf(path, ASC_NAME_LEN, "%s.%s", tx->cfg.name, ASC_TX_AUTO_USER);
asc_tx_put_ready(path, 0);
}
static void asc_tx_wait_sleep_timer(unsigned long data)
{
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
//ASCDPRT("%s tx wait sleep timer is timeout.\n", tx->name);
asc_tx_event_send(tx, AP_TX_EVENT_IDLE_TIMEOUT);
}
static int asc_tx_handle_init(struct asc_tx_handle *tx)
{
int ret = 0;
char *name = NULL;
struct asc_config *cfg = &tx->cfg;
ret = oem_gpio_request(cfg->gpio_wake, "ap_wake_cp");
if(ret < 0){
ASCPRT("Fail to requset ap_wake_cp gpio %d for %s.\n", cfg->gpio_wake, cfg->name);
goto err_request_gpio_ap_wake_cp;
}
if(cfg->gpio_ready >= 0){
ret = oem_gpio_request(cfg->gpio_ready, "cp_ready");
if(ret < 0){
ASCPRT("Fail to requset cp_ready gpio %d for %s.\n", cfg->gpio_ready, cfg->name);
goto err_request_gpio_cp_ready;
}
oem_gpio_irq_mask(cfg->gpio_ready);
oem_gpio_direction_input_for_irq(cfg->gpio_ready);
oem_gpio_set_irq_type(cfg->gpio_ready, IRQF_TRIGGER_RISING);
ret = oem_gpio_request_irq(cfg->gpio_ready, asc_irq_cp_indicate_state,
IRQF_SHARED, "cp_indicate_state", tx);
oem_gpio_irq_unmask(cfg->gpio_ready);
if (ret < 0) {
ASCPRT("fail to request irq for %s:cp_ready\n", cfg->name);
goto err_req_irq_cp_indicate_state;
}
}
asc_tx_sleep_cp(tx);
tx->auto_delay = ASC_TX_AUTO_DELAY_TIME;
tx->table = asc_tx_table;
mutex_init(&tx->mlock);
INIT_LIST_HEAD(&tx->event_q);
INIT_LIST_HEAD(&tx->user_list);
spin_lock_init(&tx->slock);
name = kzalloc(ASC_NAME_LEN, GFP_KERNEL);
if(!name){
ret = -ENOMEM;
ASCPRT("%s: no memory to malloc for wake lock name\n", __FUNCTION__);
goto err_malloc_name;
}
snprintf(name, ASC_NAME_LEN, "asc_tx_%s", tx->cfg.name);
wake_lock_init(&tx->wlock, WAKE_LOCK_SUSPEND, name);
init_waitqueue_head(&tx->wait);
init_waitqueue_head(&tx->wait_tx_state);
setup_timer(&tx->timer_wait_ready, asc_tx_wait_ready_timer, (unsigned long)tx);
setup_timer(&tx->timer_wait_idle, asc_tx_wait_idle_timer, (unsigned long)tx);
setup_timer(&tx->timer_wait_sleep, asc_tx_wait_sleep_timer, (unsigned long)tx);
atomic_set(&tx->state, AP_TX_ST_SLEEP);
atomic_set(&tx->count, 0);
INIT_WORK(&tx->ntf_work, asc_tx_notifier_work);
ret = kernel_thread(asc_tx_event_thread, tx, 0);
if(ret < 0){
ASCPRT("Fail to create %s tx thread.\n", tx->cfg.name);
goto err_create_tx_event_thread;
}
return 0;
err_create_tx_event_thread:
if(cfg->gpio_ready >= 0)
free_irq(oem_gpio_to_irq(cfg->gpio_ready), tx);
err_malloc_name:
if(name)
kfree(name);
err_req_irq_cp_indicate_state:
if(cfg->gpio_ready >= 0)
oem_gpio_free(cfg->gpio_ready);
err_request_gpio_cp_ready:
if(cfg->gpio_wake >= 0)
oem_gpio_free(cfg->gpio_wake);
err_request_gpio_ap_wake_cp:
return ret;
}
static int asc_tx_handle_sleep(void *data, int event)
{
int ret = 0;
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
//ASCDPRT("Tx(%s): process event(%d) in state(%s).\n", tx->name, event, tx->table[atomic_read(&tx->state)].name);
if(AP_TX_ST_SLEEP != atomic_read(&tx->state)){
return 0;
}
switch(event){
case AP_TX_EVENT_REQUEST:
wake_lock(&tx->wlock);
asc_tx_wake_cp(tx);
if(tx->cfg.gpio_ready >= 0){
mod_timer(&tx->timer_wait_ready, jiffies + msecs_to_jiffies(ASC_TX_WAIT_READY_TIME));
atomic_set(&tx->state, AP_TX_ST_WAIT_READY);
if(asc_tx_cp_be_ready(tx)){
mdelay(ASC_TX_DEBOUNCE_TIME);//debounce wait, make sure CBP has already be ready
if(asc_tx_cp_be_ready(tx)){
ASCDPRT("Tx:cp %s was ready now.\n", tx->cfg.name);
asc_tx_handle_wait_ready(tx, AP_TX_EVENT_CP_READY);
}
}
}else{
mdelay(ASC_TX_DEBOUNCE_TIME);
atomic_set(&tx->state, AP_TX_ST_WAIT_READY);
asc_tx_handle_wait_ready(tx, AP_TX_EVENT_CP_READY);
}
break;
default:
ASCDPRT("Tx: ignore event %d in state(%s)", event, tx->table[atomic_read(&tx->state)].name);
}
//ASCDPRT("Tx(%s): go into state(%s).\n", tx->name, tx->table[atomic_read(&tx->state)].name);
return ret;
}
static int asc_tx_handle_wait_ready(void *data, int event)
{
int ret = 0;
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
if(AP_TX_ST_WAIT_READY != atomic_read(&tx->state)){
return 0;
}
//ASCDPRT("Tx(%s): process event(%d) in state(%s).\n", tx->name, event, tx->table[atomic_read(&tx->state)].name);
switch(event){
case AP_TX_EVENT_CP_READY:
del_timer(&tx->timer_wait_ready);
tx->wait_try = 0;
atomic_set(&tx->state, AP_TX_ST_READY);
wake_up_interruptible_all(&tx->wait_tx_state);
if(asc_tx_refer(tx, ASC_TX_AUTO_USER) > 0){
asc_tx_trig_busy(tx);
}
asc_tx_notifier(tx, ASC_NTF_TX_READY);
break;
case AP_TX_EVENT_WAIT_TIMEOUT:
ASCPRT("Tx: %s wait cp ready timeout, try=%d.\n", tx->cfg.name, tx->wait_try);
{
//kevin add .i dont know who disable irq after resume,so i reeanble it
oem_gpio_direction_input_for_irq(GPIO_VIATEL_USB_MDM_RDY);
oem_gpio_set_irq_type(GPIO_VIATEL_USB_MDM_RDY,(IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING));
oem_gpio_irq_unmask(GPIO_VIATEL_USB_MDM_RDY);
}
asc_tx_sleep_cp(tx);
mdelay(ASC_TX_DEBOUNCE_TIME);//delay to create a implus
atomic_set(&tx->state, AP_TX_ST_SLEEP);
if(tx->wait_try++ <= ASC_TX_TRY_TIMES){
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
}else{
tx->wait_try = 0;
atomic_set(&tx->state, AP_TX_ST_SLEEP);
asc_tx_refer_clear(tx);
wake_up_interruptible_all(&tx->wait_tx_state);
wake_unlock(&tx->wlock);
asc_tx_notifier(tx, ASC_NTF_TX_UNREADY);
ASCPRT("try out to wake %s.\n", tx->cfg.name);
}
break;
case AP_TX_EVENT_STOP:
asc_tx_sleep_cp(tx);
del_timer(&tx->timer_wait_ready);
tx->wait_try = 0;
atomic_set(&tx->state, AP_TX_ST_SLEEP);
wake_unlock(&tx->wlock);
wake_up_interruptible_all(&tx->wait_tx_state);
break;
default:
ASCDPRT("Tx: ignore event %d in state(%s)", event, tx->table[atomic_read(&tx->state)].name);
}
//ASCDPRT("Tx(%s): go into state(%s).\n", tx->name, tx->table[atomic_read(&tx->state)].name);
return ret;
}
static int asc_tx_handle_ready(void *data, int event)
{
int ret = 0;
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
if(AP_TX_ST_READY != atomic_read(&tx->state)){
return 0;
}
//ASCDPRT("Tx(%s): process event(%d) in state(%s).\n", tx->name, event, tx->table[atomic_read(&tx->state)].name);
switch(event){
case AP_TX_EVENT_STOP:
del_timer(&tx->timer_wait_idle);
asc_tx_sleep_cp(tx);
atomic_set(&tx->state, AP_TX_ST_SLEEP);
wake_unlock(&tx->wlock);
//mod_timer(&tx->timer_wait_sleep, jiffies + msecs_to_jiffies(ASC_TX_WAIT_IDLE_TIME));
break;
default:
ASCDPRT("Tx: ignore event %d in state(%s)", event, tx->table[atomic_read(&tx->state)].name);
}
//ASCDPRT("Tx(%s): go into state(%s).\n", tx->name, tx->table[atomic_read(&tx->state)].name);
return ret;
}
/*Ignore the idle state, wait for a while to let CBP go to sleep*/
static int asc_tx_handle_idle(void *data, int event)
{
int ret = 0;
struct asc_tx_handle *tx = (struct asc_tx_handle *)data;
if(AP_TX_ST_IDLE != atomic_read(&tx->state)){
return 0;
}
//ASCDPRT("Tx(%s): process event(%d) in state(%s).\n", tx->name, event, tx->table[atomic_read(&tx->state)].name);
switch(event){
case AP_TX_EVENT_IDLE_TIMEOUT:
atomic_set(&tx->state, AP_TX_ST_SLEEP);
wake_unlock(&tx->wlock);
break;
case AP_TX_EVENT_REQUEST:
del_timer(&tx->timer_wait_sleep);
atomic_set(&tx->state, AP_TX_ST_SLEEP);
/*loop back to SLEEP handle*/
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
break;
default:
ASCDPRT("Tx: ignore event %d in state(%s)", event, tx->table[atomic_read(&tx->state)].name);
}
//ASCDPRT("Tx(%s): go into state(%s).\n", tx->name, tx->table[atomic_read(&tx->state)].name);
return ret;
}
static void asc_tx_handle_reset(struct asc_tx_handle *tx)
{
unsigned long flags;
ASCDPRT("%s %s\n", __FUNCTION__, tx->cfg.name);
del_timer(&tx->timer_wait_ready);
del_timer(&tx->timer_wait_idle);
del_timer(&tx->timer_wait_sleep);
spin_lock_irqsave(&tx->slock, flags);
INIT_LIST_HEAD(&tx->event_q);
spin_unlock_irqrestore(&tx->slock, flags);
asc_tx_sleep_cp(tx);
atomic_set(&tx->state, AP_TX_ST_SLEEP);
wake_unlock(&tx->wlock);
}
/**
* asc_tx_reset - reset the tx handle
* @name: the config name for the handle
*
* return 0 ok, others be error
*/
void asc_tx_reset(const char *name)
{
struct asc_tx_handle *tx = NULL;
tx = asc_tx_handle_lookup(name);
if(tx){
asc_tx_event_send(tx, AP_TX_EVENT_RESET);
}
}
EXPORT_SYMBOL(asc_tx_reset);
/**
* asc_tx_set_auto_delay - change the delay time for auto ready
* @name: the config name for the handle
* @delay: the time for auto ready which is valid while more than default value
* return 0 ok,others be error
*/
int asc_tx_set_auto_delay(const char *name, int delay)
{
int ret = 0;
unsigned long flags;
struct asc_tx_handle *tx;
tx = asc_tx_handle_lookup(name);
if(!tx){
ret = -ENODEV;
goto end;
}
if(delay > 0){
spin_lock_irqsave(&tx->slock, flags);
tx->auto_delay = delay;
spin_unlock_irqrestore(&tx->slock, flags);
}
end:
return ret;
}
EXPORT_SYMBOL(asc_tx_set_auto_delay);
/**
* asc_tx_check_ready - check whether tx tanslation has alreay be ready
* @name: the config name for the handle
*
* return 1 waken, 0 not, others be error
*/
int asc_tx_check_ready(const char *name)
{
int ret = 0;
struct asc_tx_handle *tx;
tx = asc_tx_handle_lookup(name);
if(NULL == tx)
return -ENODEV;
ret = atomic_read(&tx->state);
if(ret == AP_TX_ST_READY){
ret = 1;
}else{
ret = 0;
}
return ret;
}
EXPORT_SYMBOL(asc_tx_check_ready);
/**
* asc_tx_user_counts - get the refernce count of the user or the handle
* @path: (handle name).[user name]
* If user name is NULL, return the count of tx handle.
* others return the count of the tx user
*/
int asc_tx_user_count(const char *path)
{
const char *name;
char hname[ASC_NAME_LEN] = {0};
struct asc_tx_handle *tx = NULL;
name = strchr(path, '.');
if (name){
memcpy(hname, path, min(name - path, ASC_NAME_LEN - 1));
name++;
}else {
strncpy(hname, path, ASC_NAME_LEN - 1);
}
tx = asc_tx_handle_lookup(hname);
if(NULL == tx)
return -ENODEV;
return asc_tx_refer(tx, name);
}
EXPORT_SYMBOL(asc_tx_user_count);
/**
* asc_tx_add_user - add a user for tx handle
* @name: the config name for the handle
* @infor: the user information
*
* return 0, others be error
*/
int asc_tx_add_user(const char *name, struct asc_infor *infor)
{
int ret = 0;
unsigned long flags = 0;
struct asc_tx_handle *tx;
struct asc_user *user;
tx = asc_tx_handle_lookup(name);
if(NULL == tx)
return -ENODEV;
user = asc_tx_user_lookup(tx, infor->name);
if(NULL == user){
user = kzalloc(sizeof(*user), GFP_KERNEL);
if(!user){
ASCPRT("No memory to create new user reference.\n");
ret = -ENOMEM;
goto error;
}
user->infor.data = infor->data;
user->infor.notifier = infor->notifier;
strncpy(user->infor.name, infor->name, ASC_NAME_LEN - 1);
atomic_set(&user->count, 0);
spin_lock_irqsave(&tx->slock, flags);
list_add_tail(&user->node, &tx->user_list);
spin_unlock_irqrestore(&tx->slock, flags);
}else{
ASCPRT("%s error: user %s already exist!!\n", __FUNCTION__, infor->name);
ret = -EINVAL;
}
error:
return ret;
}
EXPORT_SYMBOL(asc_tx_add_user);
/**
* asc_tx_del_user - delete a user for tx handle
* @path: (handle name).(user name)
*
* no return
*/
void asc_tx_del_user(const char *path)
{
unsigned long flags = 0;
char hname[ASC_NAME_LEN] = {0};
const char *name;
struct asc_user *user = NULL;
struct asc_tx_handle *tx = NULL;
name = strchr(path, '.');
if (name) {
memcpy(hname, path, min(name - path, ASC_NAME_LEN - 1));
name++;
}else{
ASCPRT("%s: invalid path %s\n", __FUNCTION__, path);
return ;
}
/*if reserve user, do nothing*/
if (!strncmp(name, ASC_TX_SYSFS_USER, ASC_NAME_LEN - 1) || \
!strncmp(name, ASC_TX_AUTO_USER, ASC_NAME_LEN - 1) ) {
ASCPRT("Can not delete reserve user %s\n", path);
return ;
}
tx = asc_tx_handle_lookup(hname);
if(NULL == tx)
return ;
user = asc_tx_user_lookup(tx, name);
if(user){
/*put ready if the user had operated Tx handle*/
if(atomic_read(&user->count) > 0){
atomic_set(&user->count, 1);
asc_tx_put_ready(path, 0);
}
spin_lock_irqsave(&tx->slock, flags);
list_del(&user->node);
spin_unlock_irqrestore(&tx->slock, flags);
kfree(user);
}
return ;
}
EXPORT_SYMBOL(asc_tx_del_user);
/**
* asc_tx_get_ready - lock CBP to work
* @path: (handle name).(user name)
* @block: whether block wait for CBP has already waken
*
* This function try to wake the CBP and add the reference count.
* It will block wait for CBP has already be waken if set sync parameter,
* otherwise it just trig the action to wake CBP, which can not make sure
* that CBP has be waken after return.
* return 0 is ok, otherwise something error
*/
int asc_tx_get_ready(const char *path, int sync)
{
int ret = 0;
char hname[ASC_NAME_LEN] = {0};
const char *name;
struct asc_tx_handle *tx = NULL;
name = strchr(path, '.');
if (name) {
memcpy(hname, path, min(name - path, ASC_NAME_LEN - 1));
name++;
} else {
ASCPRT("Invalid path %s\n", path);
return -EINVAL;
}
tx = asc_tx_handle_lookup(hname);
if(NULL == tx)
return -ENODEV;
if(asc_tx_get_user(tx, name) < 0){
ASCPRT("%s:tx user name %s is unknow\n", __FUNCTION__, name);
return -ENODEV;
}
ASCDPRT("%s: %s=%d, %s=%d\n", __FUNCTION__,\
tx->cfg.name, asc_tx_refer(tx, NULL), path, asc_tx_refer(tx, name));
switch(atomic_read(&tx->state)){
case AP_TX_ST_SLEEP:
//To make CP wake ASAP,call the funtion directly
if(!list_empty(&tx->event_q)){
asc_tx_handle_sleep(tx, AP_TX_EVENT_REQUEST);
}else{
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
}
break;
case AP_TX_ST_IDLE:
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
break;
case AP_TX_ST_WAIT_READY:
case AP_TX_ST_READY:
if(!strncmp(name, ASC_TX_AUTO_USER, strlen(ASC_TX_AUTO_USER))){
asc_tx_trig_busy(tx);
}
break;
default:
ASCPRT("Unknow tx state %d\n", atomic_read(&tx->state));
return -EINVAL;
}
if(sync){
if(AP_TX_ST_READY != atomic_read(&tx->state)){
interruptible_sleep_on(&tx->wait_tx_state);
if(AP_TX_ST_READY != atomic_read(&tx->state)){
ret = -EBUSY;
}
}
}
return ret;
}
EXPORT_SYMBOL(asc_tx_get_ready);
/**
* asc_tx_put_ready - lock CBP to work if not set auto sleep
* @path: (config name).[user name]
* @block: whether block wait for CBP has already waken
*
* This function try to wake the CBP. It will block wait for CBP
* has already be sleep if set sync parameter, otherwise it just
* trig the action to sleep CBP, which can not make sure that
* CBP has be sleep after return. If the reference count is not 1. it
* do nothing but sub one.
* return 0 is ok, otherwise something error
*/
int asc_tx_put_ready(const char *path, int sync)
{
int ret = 0;
char hname[ASC_NAME_LEN] = {0};
const char *name;
struct asc_tx_handle *tx = NULL;
name = strchr(path, '.');
if (name) {
memcpy(hname, path, min(name - path, ASC_NAME_LEN - 1));
name++;
} else {
ASCPRT("Invalid path %s\n", path);
return -EINVAL;
}
tx = asc_tx_handle_lookup(hname);
if(NULL == tx)
return -ENODEV;
if(asc_tx_put_user(tx, name) < 0){
ASCPRT("%s:tx user name %s is unknow\n", __FUNCTION__, name);
return -ENODEV;
}
ASCDPRT("%s: %s=%d, %s=%d\n", __FUNCTION__,\
tx->cfg.name, asc_tx_refer(tx, NULL), path, asc_tx_refer(tx, name));
/*count is not 0, so do nothing*/
if(asc_tx_refer(tx, NULL) != 0){
return 0;
}
switch(atomic_read(&tx->state)){
case AP_TX_ST_SLEEP:
break;
case AP_TX_ST_WAIT_READY:
case AP_TX_ST_READY:
asc_tx_event_send(tx, AP_TX_EVENT_STOP);
break;
case AP_TX_ST_IDLE:
asc_tx_event_send(tx, AP_TX_EVENT_IDLE_TIMEOUT);
break;
default:
ASCPRT("Unknow tx state %d\n", atomic_read(&tx->state));
return -EINVAL;
}
if(sync){
if(AP_TX_ST_SLEEP != atomic_read(&tx->state)){
interruptible_sleep_on(&tx->wait_tx_state);
if(AP_TX_ST_SLEEP != atomic_read(&tx->state)){
ret = -EBUSY;
}
}
}
return ret;
}
EXPORT_SYMBOL(asc_tx_put_ready);
/**
* asc_tx_auto_ready - call each time before operate for CBP if set auto sleep
* @name: the cofnig name for the handle
* @sync: whether block wait for CBP has already waken
*
* This function try to wake the CBP and trig the tx state. It will
* block wait for CBP has already be waken if set sync parameter,
* otherwise it just trig the action to wake CBP, which can not make
* sure that CBP has be waken after return.
* return 0 is ok, otherwise something error
*/
int asc_tx_auto_ready(const char *name, int sync)
{
int ret = 0;
struct asc_user *user;
struct asc_tx_handle *tx;
if (!name) {
ASCPRT("%s:Invalid name\n", __FUNCTION__);
return -EINVAL;
}
tx = asc_tx_handle_lookup(name);
if(NULL == tx)
return -ENODEV;
user = asc_tx_user_lookup(tx, ASC_TX_AUTO_USER);
if(!user){
return -ENODEV;
}
if(atomic_read(&user->count) == 0){
ASCDPRT("%s: %s=%d, %s=%d\n", __FUNCTION__,\
tx->cfg.name, asc_tx_refer(tx, NULL), ASC_TX_AUTO_USER, asc_tx_refer(tx, ASC_TX_AUTO_USER));
atomic_inc(&user->count);
}
switch(atomic_read(&tx->state)){
case AP_TX_ST_SLEEP:
//To make CP wake ASAP,call the funtion directly
if(!list_empty(&tx->event_q)){
asc_tx_handle_sleep(tx, AP_TX_EVENT_REQUEST);
}else{
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
}
break;
case AP_TX_ST_IDLE:
asc_tx_event_send(tx, AP_TX_EVENT_REQUEST);
break;
case AP_TX_ST_WAIT_READY:
case AP_TX_ST_READY:
asc_tx_trig_busy(tx);
break;
default:
ASCPRT("Unknow tx state %d\n", atomic_read(&tx->state));
return -EINVAL;
}
if(sync){
if(AP_TX_ST_READY != atomic_read(&tx->state)){
interruptible_sleep_on(&tx->wait_tx_state);
if(AP_TX_ST_READY != atomic_read(&tx->state)){
ret = -EBUSY;
}
}
}
return ret;
}
EXPORT_SYMBOL(asc_tx_auto_ready);
static void asc_rx_handle_reset(struct asc_rx_handle *rx)
{
unsigned long flags;
ASCDPRT("%s %s\n", __FUNCTION__, rx->cfg.name);
del_timer(&rx->timer);
wake_unlock(&rx->wlock);
asc_rx_indicate_sleep(rx);
atomic_set(&rx->state, AP_RX_ST_SLEEP);
spin_lock_irqsave(&rx->slock, flags);
INIT_LIST_HEAD(&rx->event_q);
spin_unlock_irqrestore(&rx->slock, flags);
}
/**
* asc_rx_reset - reset the rx handle
* @name: the config name for the handle
*
* return 0 ok, others be error
*/
void asc_rx_reset(const char *name)
{
struct asc_rx_handle *rx = NULL;
rx = asc_rx_handle_lookup(name);
if(rx){
asc_rx_event_send(rx, AP_RX_EVENT_RESET);
}
}
EXPORT_SYMBOL(asc_rx_reset);
/**
* asc_rx_add_user - add a user for rx handle
* @name: the config name for the handle
* @infor: the user information
*
* return 0, others be error
*/
int asc_rx_add_user(const char *name, struct asc_infor *infor)
{
int ret = 0;
unsigned long flags = 0;
struct asc_rx_handle *rx;
struct asc_user *user;
rx = asc_rx_handle_lookup(name);
if(NULL == rx)
return -ENODEV;
user = asc_rx_user_lookup(rx, infor->name);
if(NULL == user){
user = kzalloc(sizeof(*user), GFP_KERNEL);
if(!user){
ASCPRT("No memory to create new user reference.\n");
ret = -ENOMEM;
goto error;
}
user->infor.data = infor->data;
user->infor.notifier = infor->notifier;
strncpy(user->infor.name, infor->name, ASC_NAME_LEN - 1);
atomic_set(&user->count, 0);
spin_lock_irqsave(&rx->slock, flags);
list_add_tail(&user->node, &rx->user_list);
spin_unlock_irqrestore(&rx->slock, flags);
if(AP_RX_ST_WAIT_READY == atomic_read(&rx->state)){
if(infor->notifier){
infor->notifier(ASC_NTF_RX_PREPARE, infor->data);
}
}
}else{
ASCPRT("%s error: user %s already exist!!\n", __FUNCTION__, infor->name);
ret = -EINVAL;
}
error:
return ret;
}
EXPORT_SYMBOL(asc_rx_add_user);
/**
* asc_rx_del_user - add a user for rx handle
* @path: (config name).[user name]
*
* no return
*/
void asc_rx_del_user(const char *path)
{
unsigned long flags = 0;
const char *name;
char hname[ASC_NAME_LEN] = {0};
struct asc_user *user;
struct asc_rx_handle *rx;
name = strchr(path, '.');
if (name) {
memcpy(hname, path, min(name - path, ASC_NAME_LEN - 1));
name++;
}else{
ASCPRT("%s: Invalid path %s\n",__FUNCTION__, path);
return ;
}
rx = asc_rx_handle_lookup(hname);
if(NULL == rx)
return ;
user = asc_rx_user_lookup(rx, name);
if(user){
atomic_set(&user->count, 0);
spin_lock_irqsave(&rx->slock, flags);
list_del(&user->node);
spin_unlock_irqrestore(&rx->slock, flags);
kfree(user);
if(list_empty(&rx->user_list)){
asc_rx_handle_reset(rx);
}
}
return ;
}
EXPORT_SYMBOL(asc_rx_del_user);
/**
* asc_rx_confirm_ready - echo AP state to rx CBP data
* @name: the config name to rx handle
* @ready: whether AP has been ready to rx data
*
* After CBP request AP to rx data, the function can be used to
* tell CBP whether AP has been ready to receive.
* return 0 is ok, otherwise something error
*/
int asc_rx_confirm_ready(const char *name, int ready)
{
struct asc_rx_handle *rx = NULL;
rx = asc_rx_handle_lookup(name);
if(!rx){
ASCDPRT("%s: name %s is unknow\n", __FUNCTION__, name);
return -ENODEV;
}
ASCDPRT("Rx(%s) cnofirm ready=%d\n", rx->cfg.name, ready);
return asc_rx_event_send(rx, ready ? AP_RX_EVENT_AP_READY : AP_RX_EVENT_AP_UNREADY);
}
EXPORT_SYMBOL(asc_rx_confirm_ready);
/**
* check_on_start - prevent from missing cp waking
* @name: the name of rx handle
*
* Before the rx user is registed,CP may wake up AP.Usually AP will ignore
* the waking.Because the interrupt don't be register at that time.So the
* signal will be missed.When opening the tty device, we should check whether CP
* has waken up AP or not.If CP did that,we should send the signal "AP READY"
* to CP.
*/
int asc_rx_check_on_start(const char *name)
{
int level;
struct asc_config *cfg = NULL;
struct asc_rx_handle *rx = NULL;
int ret = 1;
rx = asc_rx_handle_lookup(name);
if(!rx){
ASCPRT("config %s has not already exist.\n", name);
return -EINVAL;
}
cfg = &(rx->cfg);
level = !!oem_gpio_get_value(cfg->gpio_wake);
if(level == cfg->polar){
/*Cp has requested Ap wake*/
if(AP_RX_ST_SLEEP == atomic_read(&rx->state)){
ASCDPRT("Rx(%s):check_on_start--send event AP_RX_EVENT_REQUEST.\n", cfg->name);
ret = asc_rx_event_send(rx, AP_RX_EVENT_REQUEST);
}else{
ASCDPRT("Rx(%s): check_on_start--send event AP_RX_EVENT_AP_READY.\n", cfg->name);
ret = asc_rx_event_send(rx, AP_RX_EVENT_AP_READY);
}
}
return ret;
}
EXPORT_SYMBOL(asc_rx_check_on_start);
static ssize_t asc_debug_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
char *s = buf;
s += sprintf(s, "%d\n", asc_debug);
return (s - buf);
}
static ssize_t asc_debug_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
unsigned long val;
if (strict_strtoul(buf, 10, &val))
return -EINVAL;
if (val < 0)
return -EINVAL;
asc_debug = val;
return n;
}
static ssize_t asc_infor_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
char *s = buf;
int val1, val2;
struct asc_config *cfg;
struct asc_infor *infor;
struct asc_user *user = NULL, *tuser = NULL;
struct asc_tx_handle *tx = NULL, *ttmp = NULL;
struct asc_rx_handle *rx = NULL, *rtmp = NULL;
list_for_each_entry_safe(tx, ttmp, &asc_tx_handle_list, node){
cfg = &tx->cfg;
val1 = val2 = -1;
if(cfg->gpio_wake >= 0)
val1 = !!oem_gpio_get_value(cfg->gpio_wake);
if(cfg->gpio_ready >= 0)
val2 = !!oem_gpio_get_value(cfg->gpio_ready);
s += sprintf(s, "Tx %s: ref=%d, ap_wake_cp(%d)=%d, cp_ready(%d)=%d, polar=%d, auto_delay=%d mS\n",
cfg->name, asc_tx_refer(tx, NULL), cfg->gpio_wake, val1, cfg->gpio_ready, val2, cfg->polar, tx->auto_delay);
list_for_each_entry_safe(user, tuser, &tx->user_list, node) {
infor = &user->infor;
s += sprintf(s, " user %s: ref=%d\n", infor->name, atomic_read(&user->count));
}
}
s += sprintf(s, "\n");
list_for_each_entry_safe(rx, rtmp, &asc_rx_handle_list, node){
cfg = &rx->cfg;
val1 = val2 = -1;
if(cfg->gpio_wake >= 0)
val1 = !!oem_gpio_get_value(cfg->gpio_wake);
if(cfg->gpio_ready >= 0)
val2 = !!oem_gpio_get_value(cfg->gpio_ready);
s += sprintf(s, "Rx %s: ref=%d, cp_wake_ap(%d)=%d, ap_ready(%d)=%d, polar=%d\n", \
cfg->name, asc_rx_refer(rx, NULL), cfg->gpio_wake, val1, cfg->gpio_ready, val2, cfg->polar);
list_for_each_entry_safe(user, tuser, &rx->user_list, node) {
infor = &user->infor;
s += sprintf(s, " user %s: ref=%d\n", infor->name, atomic_read(&user->count));
}
}
return (s - buf);
}
static ssize_t asc_infor_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
return n;
}
static ssize_t asc_refer_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
unsigned long flags;
char *s = buf;
struct asc_tx_handle *tx, *tmp, *t;
tx = tmp = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node){
if(tmp->kobj == kobj){
tx = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
if(tx){
s += sprintf(s, "%d\n", asc_tx_refer(tx, ASC_TX_SYSFS_USER));
return s - buf;
}else{
ASCPRT("%s read error\n", __FUNCTION__);
return -EINVAL;
}
}
static ssize_t asc_setcpmode_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
char *s = buf;
s += sprintf(s, "nothing\n");
return (s - buf);
}
static ssize_t asc_setcpmode_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
unsigned long val;
if (strict_strtoul(buf, 10, &val))
return -EINVAL;
if (val < 0)
return -EINVAL;
if(val>0)
oem_gpio_output(GPIO_VIATEL_USB_AP_WAKE_MDM,1);
else
oem_gpio_output(GPIO_VIATEL_USB_AP_WAKE_MDM,0);
//asc_debug = val;
return n;
}
static ssize_t asc_refer_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
unsigned long flags;
char *p;
int error = 0, len;
char path[ASC_NAME_LEN] = {0};
struct asc_tx_handle *tx, *tmp, *t;
tx = tmp = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node){
if(tmp->kobj == kobj){
tx = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
if(tx){
p = memchr(buf, '\n', n);
len = p ? p - buf : n;
snprintf(path, ASC_NAME_LEN, "%s.%s", tx->cfg.name, ASC_TX_SYSFS_USER);
if (len == 3 && !strncmp(buf, "get", len)) {
error = asc_tx_get_ready(path, 1);
}else if (len == 3 && !strncmp(buf, "put", len)) {
error= asc_tx_put_ready(path, 1);
}
}
return error ? error : n;
}
static ssize_t asc_state_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
unsigned long flags;
char *s = buf;
struct asc_tx_handle *tx, *tmp, *t;
tx = tmp = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node){
if(tmp->kobj == kobj){
tx = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
if(tx){
s += sprintf(s, "%s\n", tx->table[atomic_read(&tx->state)].name);
return s - buf;
}else{
ASCPRT("%s read error\n", __FUNCTION__);
return -EINVAL;
}
}
static ssize_t asc_state_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
return n;
}
static ssize_t asc_auto_ready_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
unsigned long flags;
char *s = buf;
struct asc_tx_handle *tx, *tmp, *t;
tx = tmp = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node){
if(tmp->kobj == kobj){
tx = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
if(tx){
s += sprintf(s, "%d\n", tx->auto_delay);
return s - buf;
}else{
ASCPRT("%s read error\n", __FUNCTION__);
return -EINVAL;
}
}
static ssize_t asc_auto_ready_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
int error = 0;
long val;
unsigned long flags;
struct asc_tx_handle *tx, *tmp, *t;
tx = tmp = NULL;
spin_lock_irqsave(&hdlock, flags);
list_for_each_entry_safe(tmp, t, &asc_tx_handle_list, node){
if(tmp->kobj == kobj){
tx = tmp;
break;
}
}
spin_unlock_irqrestore(&hdlock, flags);
if(tx){
error = strict_strtol(buf, 10, &val);
if(error || (val < 0)){
error = -EINVAL;
goto end;
}
if(val > 0){
spin_lock_irqsave(&tx->slock, flags);
tx->auto_delay = val;
spin_unlock_irqrestore(&tx->slock, flags);
}
error = asc_tx_auto_ready(tx->cfg.name, 1);
}else{
ASCPRT("%s read error\n", __FUNCTION__);
error = -EINVAL;
}
end:
return error ? error : n;
}
#define asc_attr(_name) \
static struct kobj_attribute _name##_attr = { \
.attr = { \
.name = __stringify(_name), \
.mode = 0644, \
}, \
.show = asc_##_name##_show, \
.store = asc_##_name##_store, \
}
asc_attr(debug);
asc_attr(infor);
asc_attr(setcpmode);
static struct attribute * g_attr[] = {
&debug_attr.attr,
&infor_attr.attr,
&setcpmode_attr.attr,
NULL,
};
static struct attribute_group g_attr_group = {
.attrs = g_attr,
};
asc_attr(refer);
asc_attr(state);
asc_attr(auto_ready);
static struct attribute * tx_hd_attr[] = {
&refer_attr.attr,
&state_attr.attr,
&auto_ready_attr.attr,
NULL,
};
int asc_suspend(struct platform_device *pdev, pm_message_t state)
{
oem_gpio_irq_mask(tx_cfg.gpio_ready);
oem_gpio_irq_mask(rx_cfg.gpio_wake);
if(ap_ready_always)
oem_gpio_output(rx_cfg.gpio_ready,!rx_cfg.polar);
return 0;
}
int asc_resume(struct platform_device *pdev)
{
msleep(500);
printk("asc_resume\n");
oem_gpio_direction_output(tx_cfg.gpio_wake, tx_cfg.polar);
oem_gpio_direction_input_for_irq(tx_cfg.gpio_ready);
oem_gpio_set_irq_type(tx_cfg.gpio_ready, IRQF_TRIGGER_RISING);
oem_gpio_irq_unmask(tx_cfg.gpio_ready);
oem_gpio_output(rx_cfg.gpio_ready, rx_cfg.polar);
oem_gpio_direction_input_for_irq(rx_cfg.gpio_wake);
oem_gpio_set_irq_type(rx_cfg.gpio_wake, IRQF_TRIGGER_RISING);
oem_gpio_irq_unmask(rx_cfg.gpio_wake);
return 0;
}
static struct attribute_group tx_hd_attr_group = {
.attrs = tx_hd_attr,
};
static struct platform_driver asc_driver = {
.driver.name = "asc",
.suspend = asc_suspend,
.resume = asc_resume,
};
static struct platform_device asc_device = {
.name = "asc",
};
/**
* asc_rx_register_handle - register the rx handle
* @cfg: the config for the handle
*
* the device which receive data from CBP can register a notifier to
* listen the event according to the changes from CBP.
* ASC_PREPARE_RX_DATA event will be send when CBP start tx data
* to the device which must be ready to work;
* ASC_POST_RX_DATA event will be send when CBP stop tx data to
* the device which can go to sleep.
* The gpio for ap_ready can be -1 which be ignored when the device
* can receive the data from CBP correctly any time.
* return index according to the notifier in handle, otherwise something error
*/
int asc_rx_register_handle(struct asc_config *cfg)
{
int ret = 0;
unsigned long flags;
struct asc_rx_handle *rx = NULL;
if(NULL == asc_work_queue){
ASCPRT("%s: error Asc has not been init\n", __FUNCTION__);
return -EINVAL;
}
if(NULL == cfg){
return -EINVAL;
}
if(cfg->gpio_wake < 0){
ASCPRT("%s: config %s gpio is invalid.\n", __FUNCTION__, cfg->name);
return -EINVAL;
}
rx = asc_rx_handle_lookup(cfg->name);
if(rx){
ASCPRT("config %s has already exist.\n", cfg->name);
return -EINVAL;
}
rx = kzalloc(sizeof(struct asc_rx_handle), GFP_KERNEL);
if(NULL == rx){
ASCPRT("No memory to alloc rx handle.\n");
return -ENOMEM;
}
rx->cfg.gpio_ready = cfg->gpio_ready;
rx->cfg.gpio_wake = cfg->gpio_wake;
rx->cfg.polar = !!cfg->polar;
strncpy(rx->cfg.name, cfg->name, ASC_NAME_LEN - 1);
memcpy(&rx_cfg, &rx->cfg, sizeof(struct asc_config));
ret = asc_rx_handle_init(rx);
if(ret < 0){
kfree(rx);
ASCPRT("fail to init rx handle %s\n", rx->cfg.name);
return -EINVAL;
}
/* Add the handle to the asc list */
spin_lock_irqsave(&hdlock, flags);
list_add(&rx->node, &asc_rx_handle_list);
spin_unlock_irqrestore(&hdlock, flags);
ASCDPRT("Register rx handle %s\n", rx->cfg.name);
return ret;
}
EXPORT_SYMBOL(asc_rx_register_handle);
/**
* asc_tx_register_handle - register the tx handle for state change
* @cfg: the config for the handle
*
* the chip which exchanged data with CBP must create a handle.
* There is only one tx state handle between the AP and CBP because
* all devices in CBP will be ready to receive data after CBP has been
* waken. But servial rx state handles can be exist because different
* devices in AP maybe waken indivially. Each rx state handle must be
* registed a notifier to listen the evnets.
* return 0 is ok, otherwise something error
*/
int asc_tx_register_handle(struct asc_config *cfg)
{
int ret=0;
unsigned long flags;
struct asc_infor infor;
struct asc_tx_handle *tx = NULL;
if(NULL == asc_work_queue){
ASCPRT("%s: error Asc has not been init\n", __FUNCTION__);
return -EINVAL;
}
if(NULL == cfg){
return -EINVAL;
}
if(cfg->gpio_wake < 0){
ASCPRT("%s: config %s gpio is invalid.\n", __FUNCTION__, cfg->name);
return -EINVAL;
}
tx = asc_tx_handle_lookup(cfg->name);
if(tx){
ASCPRT("config %s has already exist.\n", cfg->name);
return -EINVAL;
}
tx = kzalloc(sizeof(struct asc_tx_handle), GFP_KERNEL);
if(NULL == tx){
ASCPRT("Fail to alloc memory for tx handle.\n");
return -ENOMEM;
}
tx->cfg.gpio_ready = cfg->gpio_ready;
tx->cfg.gpio_wake = cfg->gpio_wake;
tx->cfg.polar = !!cfg->polar;
strncpy(tx->cfg.name, cfg->name, ASC_NAME_LEN - 1);
memcpy(&tx_cfg, &tx->cfg, sizeof(struct asc_config));
ret = asc_tx_handle_init(tx);
if(ret < 0){
ASCPRT("Fail to init tx handle %s.\n", tx->cfg.name);
goto err_tx_handle_init;
}
/* Add the handle to the asc list */
spin_lock_irqsave(&hdlock, flags);
list_add(&tx->node, &asc_tx_handle_list);
spin_unlock_irqrestore(&hdlock, flags);
ASCDPRT("Register tx handle %s.\n", tx->cfg.name);
tx->kobj = kobject_create_and_add(cfg->name, asc_kobj);
if(!tx->kobj){
ret = -ENOMEM;
goto err_create_kobj;
}
/*add default user for application*/
memset(&infor, 0, sizeof(infor));
strncpy(infor.name, ASC_TX_SYSFS_USER, ASC_NAME_LEN);
asc_tx_add_user(tx->cfg.name, &infor);
memset(&infor, 0, sizeof(infor));
strncpy(infor.name, ASC_TX_AUTO_USER, ASC_NAME_LEN);
asc_tx_add_user(tx->cfg.name, &infor);
return sysfs_create_group(tx->kobj, &tx_hd_attr_group);
err_create_kobj:
list_del(&tx->node);
err_tx_handle_init:
if(tx){
kfree(tx);
}
return ret;
}
EXPORT_SYMBOL(asc_tx_register_handle);
static int __init asc_init(void)
{
int ret;
ret = oem_gpio_convert_init();
if(ret){
printk("Warnning:viatel gpio not set.\n");
return -EIO;
}
ret = platform_device_register(&asc_device);
if (ret) {
ASCPRT("platform_device_register failed\n");
goto err_platform_device_register;
}
ret = platform_driver_register(&asc_driver);
if (ret) {
ASCPRT("platform_driver_register failed\n");
goto err_platform_driver_register;
}
asc_work_queue = create_singlethread_workqueue("asc_work");
if (asc_work_queue == NULL) {
ret = -ENOMEM;
goto err_create_work_queue;
}
asc_kobj = viatel_kobject_add("asc");
if (!asc_kobj){
ret = -ENOMEM;
goto err_create_kobj;
}
return sysfs_create_group(asc_kobj, &g_attr_group);
err_create_kobj:
destroy_workqueue(asc_work_queue);
err_create_work_queue:
platform_driver_unregister(&asc_driver);
err_platform_driver_register:
platform_device_unregister(&asc_device);
err_platform_device_register:
return ret;
}
device_initcall(asc_init);
|