summaryrefslogtreecommitdiff
path: root/drivers/media/radio/CG2900/radio-cg2900.c
blob: 9ccb4e6b85d2a5f9c0db5a9902489380f4d2fddd (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
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Linux Wrapper for V4l2 FM Driver for CG2900.
 *
 * Author: Hemant Gupta <hemant.gupta@stericsson.com> for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2
 */

#include<linux/init.h>
#include<linux/videodev2.h>
#include<media/v4l2-ioctl.h>
#include<media/v4l2-common.h>
#include<linux/module.h>
#include <linux/platform_device.h>
#include<linux/string.h>
#include<linux/wait.h>
#include"cg2900.h"
#include"cg2900_fm_driver.h"

#define RADIO_CG2900_VERSION KERNEL_VERSION(1, 1, 0)
#define BANNER "ST-Ericsson FM Radio Card driver v1.1.0"

#define FMR_HZ_TO_MHZ_CONVERTER				1000000
#define FMR_EU_US_LOW_FREQ_IN_MHZ			87.5
#define FMR_EU_US_HIGH_FREQ_IN_MHZ			108
#define FMR_JAPAN_LOW_FREQ_IN_MHZ			76
#define FMR_JAPAN_HIGH_FREQ_IN_MHZ			90
#define FMR_CHINA_LOW_FREQ_IN_MHZ			70
#define FMR_CHINA_HIGH_FREQ_IN_MHZ			108
#define FMR_MAX_BLOCK_SCAN_CHANNELS			198
#define FMR_CHINA_GRID_IN_HZ				50000
#define FMR_EUROPE_GRID_IN_HZ				100000
#define FMR_USA_GRID_IN_HZ				200000
#define FMR_AF_SWITCH_DATA_SIZE				2
#define FMR_BLOCK_SCAN_DATA_SIZE			2
#define FMR_GET_INTERRUPT_DATA_SIZE			2
#define FMR_TEST_TONE_CONNECT_DATA_SIZE			2
#define FMR_TEST_TONE_SET_PARAMS_DATA_SIZE		6

/* freq in Hz to V4l2 freq (units of 62.5Hz) */
#define HZ_TO_V4L2(X)  (2*(X)/125)
/* V4l2 freq (units of 62.5Hz) to freq in Hz */
#define V4L2_TO_HZ(X)  (((X)*125)/(2))

static int cg2900_open(
			struct file *file
			);
static int cg2900_release(
			struct file *file
			);
static ssize_t cg2900_read(
			struct file *file,
			char __user *data,
			size_t count,
			loff_t *pos
			);
static unsigned int cg2900_poll(
			struct file *file,
			struct poll_table_struct *wait
			);
static int vidioc_querycap(
			struct file *file,
			void *priv,
			struct v4l2_capability *query_caps
			);
static int vidioc_get_tuner(
			struct file *file,
			void *priv,
			struct v4l2_tuner *tuner
			);
static int vidioc_set_tuner(
			struct file *file,
			void *priv,
			struct v4l2_tuner *tuner
			);
static int vidioc_get_modulator(
			struct file *file,
			void *priv,
			struct v4l2_modulator *modulator
			);
static int vidioc_set_modulator(
			struct file *file,
			void *priv,
			struct v4l2_modulator *modulator
			);
static int vidioc_get_frequency(
			struct file *file,
			void *priv,
			struct v4l2_frequency *freq
			);
static int vidioc_set_frequency(
			struct file *file,
			void *priv,
			struct v4l2_frequency *freq
			);
static int vidioc_query_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_queryctrl *query_ctrl
			);
static int vidioc_get_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_control *ctrl
			);
static int vidioc_set_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_control *ctrl
			);
static int vidioc_get_ext_ctrls(
			struct file *file,
			void *priv,
			struct v4l2_ext_controls *ext_ctrl
			);
static int vidioc_set_ext_ctrls(
			struct file *file,
			void *priv,
			struct v4l2_ext_controls *ext_ctrl
			);
static int vidioc_set_hw_freq_seek(
			struct file *file,
			void *priv,
			struct v4l2_hw_freq_seek *freq_seek
			);
static int vidioc_get_audio(
			struct file *file,
			void *priv,
			struct v4l2_audio *audio
			);
static int vidioc_set_audio(
			struct file *file,
			void *priv,
			struct v4l2_audio *audio
			);
static int vidioc_get_input(
			struct file *filp,
			void *priv,
			unsigned int *input
			);
static int vidioc_set_input(
			struct file *filp,
			void *priv,
			unsigned int input
			);
static void cg2900_convert_err_to_v4l2(
			char status_byte,
			char *out_byte
			);
static int cg2900_map_event_to_v4l2(
			u8 fm_event
			);

static u32 freq_low;
static u32 freq_high;

/* Module Parameters */
static int radio_nr = -1;
static int grid;
static int band;

/* cg2900_poll_queue - Main Wait Queue for polling (Scan/Seek) */
static wait_queue_head_t cg2900_poll_queue;

struct sk_buff_head		fm_interrupt_queue;

/**
 * enum fm_seek_status - Seek status of FM Radio.
 *
 * @FMR_SEEK_NONE: No seek in progress.
 * @FMR_SEEK_IN_PROGRESS: Seek is in progress.
 *
 * Seek status of FM Radio.
 */
enum fm_seek_status {
	FMR_SEEK_NONE,
	FMR_SEEK_IN_PROGRESS
};

/**
 * enum fm_power_state - Power states of FM Radio.
 *
 * @FMR_SWITCH_OFF: FM Radio is switched off.
 * @FMR_SWITCH_ON: FM Radio is switched on.
 * @FMR_STANDBY: FM Radio in standby state.
 *
 * Power states of FM Radio.
 */
enum fm_power_state {
	FMR_SWITCH_OFF,
	FMR_SWITCH_ON,
	FMR_STANDBY
};

/**
 * struct cg2900_device - Stores FM Device Info.
 *
 * @state: state of FM Radio
 * @muted: FM Radio Mute/Unmute status
 * @seekstatus: seek status
 * @rx_rds_enabled: Rds enable/disable status for FM Rx
 * @tx_rds_enabled: Rds enable/disable status for FM Tx
 * @rx_stereo_status: Stereo Mode status for FM Rx
 * @tx_stereo_status: Stereo Mode status for FM Tx
 * @volume: Analog Volume Gain of FM Radio
 * @rssi_threshold: rssi Thresold set on FM Radio
 * @frequency: Frequency tuned on FM Radio in V4L2 Format
 * @audiopath: Audio Balance
 * @wait_on_read_queue: Flag for waiting on read queue.
 * @fm_mode: Enum for storing the current FM Mode.
 *
 * FM Driver Information Structure.
 */
struct cg2900_device {
	u8 state;
	u8 muted;
	u8 seekstatus;
	bool rx_rds_enabled;
	bool tx_rds_enabled;
	bool rx_stereo_status;
	bool tx_stereo_status;
	int volume;
	u16 rssi_threshold;
	u32 frequency;
	u32 audiopath;
	bool wait_on_read_queue;
	enum cg2900_fm_mode fm_mode;
};

/* Global Structure to store the maintain FM Driver device info */
static struct cg2900_device cg2900_device;

/* V4l2 File Operation Structure */
static const struct v4l2_file_operations cg2900_fops = {
	.owner = THIS_MODULE,
	.open = cg2900_open,
	.release = cg2900_release,
	.read = cg2900_read,
	.poll = cg2900_poll,
	.ioctl = video_ioctl2,
};

/* V4L2 IOCTL Operation Structure */
static const struct v4l2_ioctl_ops cg2900_ioctl_ops = {
	.vidioc_querycap = vidioc_querycap,
	.vidioc_g_tuner = vidioc_get_tuner,
	.vidioc_s_tuner = vidioc_set_tuner,
	.vidioc_g_modulator = vidioc_get_modulator,
	.vidioc_s_modulator = vidioc_set_modulator,
	.vidioc_g_frequency = vidioc_get_frequency,
	.vidioc_s_frequency = vidioc_set_frequency,
	.vidioc_queryctrl = vidioc_query_ctrl,
	.vidioc_g_ctrl = vidioc_get_ctrl,
	.vidioc_s_ctrl = vidioc_set_ctrl,
	.vidioc_g_ext_ctrls = vidioc_get_ext_ctrls,
	.vidioc_s_ext_ctrls = vidioc_set_ext_ctrls,
	.vidioc_s_hw_freq_seek = vidioc_set_hw_freq_seek,
	.vidioc_g_audio = vidioc_get_audio,
	.vidioc_s_audio = vidioc_set_audio,
	.vidioc_g_input = vidioc_get_input,
	.vidioc_s_input = vidioc_set_input,
};

/* V4L2 Video Device Structure */
static struct video_device cg2900_video_device = {
	.name = "STE CG2900 FM Rx/Tx Radio",
	.fops = &cg2900_fops,
	.ioctl_ops = &cg2900_ioctl_ops,
	.release = video_device_release_empty,
};

static u16 no_of_scan_freq;
static u16 no_of_block_scan_freq;
static u32 scanfreq_rssi_level[MAX_CHANNELS_TO_SCAN];
static u16 block_scan_rssi_level[MAX_CHANNELS_FOR_BLOCK_SCAN];
static u32 scanfreq[MAX_CHANNELS_TO_SCAN];
static struct mutex fm_mutex;
static spinlock_t fm_spinlock;
static int users;

/**
 * vidioc_querycap()- Query FM Driver Capabilities.
 *
 * This function is used to query the capabilities of the
 * FM Driver. This function is called when the application issues the IOCTL
 * VIDIOC_QUERYCAP.
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @query_caps: v4l2_capability structure.
 *
 * Returns: 0
 */
static int vidioc_querycap(
			struct file *file,
			void *priv,
			struct v4l2_capability *query_caps
			)
{
	FM_INFO_REPORT("vidioc_querycap");
	memset(
			query_caps,
			0,
			sizeof(*query_caps)
			);
	strlcpy(
			query_caps->driver,
			"CG2900 Driver",
			sizeof(query_caps->driver)
			);
	strlcpy(
			query_caps->card,
			"CG2900 FM Radio",
			sizeof(query_caps->card)
			);
	strcpy(
			query_caps->bus_info,
			"platform"
			);
	query_caps->version = RADIO_CG2900_VERSION;
	query_caps->capabilities =
			V4L2_CAP_TUNER |
			V4L2_CAP_MODULATOR |
			V4L2_CAP_RADIO  |
			V4L2_CAP_READWRITE |
			V4L2_CAP_RDS_CAPTURE |
			V4L2_CAP_HW_FREQ_SEEK |
			V4L2_CAP_RDS_OUTPUT;
	FM_DEBUG_REPORT("vidioc_querycap returning 0");
	return 0;
}

/**
 * vidioc_get_tuner()- Get FM Tuner Features.
 *
 * This function is used to get the tuner features.
 * This function is called when the application issues the IOCTL
 * VIDIOC_G_TUNER
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @tuner: v4l2_tuner structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_tuner(
			struct file *file,
			void *priv,
			struct v4l2_tuner *tuner
			)
{
	int status = 0;
	u8 mode;
	bool rds_enabled;
	u16 rssi;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_get_tuner");

	if (tuner->index > 0) {
		FM_ERR_REPORT("vidioc_get_tuner: Only 1 tuner supported");
		goto error;
	}

	memset(tuner, 0, sizeof(*tuner));
	strcpy(tuner->name, "CG2900 FM Receiver");
	tuner->type = V4L2_TUNER_RADIO;
	tuner->rangelow = HZ_TO_V4L2(freq_low);
	tuner->rangehigh = HZ_TO_V4L2(freq_high);
	tuner->capability =
			V4L2_TUNER_CAP_LOW /* Frequency steps = 1/16 kHz */
			| V4L2_TUNER_CAP_STEREO	/* Can receive stereo */
			| V4L2_TUNER_CAP_RDS;	/* Supports RDS Capture  */

	if (cg2900_device.fm_mode == CG2900_FM_RX_MODE) {

		status = cg2900_fm_get_mode(&mode);

		FM_DEBUG_REPORT("vidioc_get_tuner: mode = %x, ", mode);

		if (0 != status) {
			/* Get mode API failed, set mode to mono */
			tuner->audmode = V4L2_TUNER_MODE_MONO;
			tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
			goto error;
		}

		switch (mode) {
		case CG2900_MODE_STEREO:
			tuner->audmode = V4L2_TUNER_MODE_STEREO;
			tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
			break;
		case CG2900_MODE_MONO:
		default:
			tuner->audmode = V4L2_TUNER_MODE_MONO;
			tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
			break;
		}

		status = cg2900_fm_get_rds_status(&rds_enabled);

		if (0 != status) {
			tuner->rxsubchans &= ~V4L2_TUNER_SUB_RDS;
			goto error;
		}

		if (rds_enabled)
			tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
		else
			tuner->rxsubchans &= ~V4L2_TUNER_SUB_RDS;
	} else {
		tuner->audmode = V4L2_TUNER_MODE_MONO;
		tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
	}

	if (cg2900_device.fm_mode == CG2900_FM_RX_MODE) {
		status = cg2900_fm_get_signal_strength(&rssi);

		if (0 != status) {
			tuner->signal = 0;
			goto error;
		}
		tuner->signal = rssi;
	} else {
		tuner->signal = 0;
	}

	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_get_tuner: returning %d", ret_val);
	return ret_val;
}

/**
 * vidioc_set_tuner()- Set FM Tuner Features.
 *
 * This function is used to set the tuner features.
 * It also sets the default FM Rx settings.
 * This function is called when the application issues the IOCTL
 * VIDIOC_S_TUNER
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @tuner: v4l2_tuner structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_tuner(
			struct file *file,
			void *priv,
			struct v4l2_tuner *tuner
			)
{
	bool rds_status = false;
	bool stereo_status = false;
	int status = 0;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_set_tuner");
	if (tuner->index != 0) {
		FM_ERR_REPORT("vidioc_set_tuner: Only 1 tuner supported");
		goto error;
	}

	if (cg2900_device.fm_mode != CG2900_FM_RX_MODE) {
		/*
		 * FM Rx mode should be configured
		 * as earlier mode was not FM Rx
		 */
		if (CG2900_FM_BAND_US_EU == band) {
			freq_low = FMR_EU_US_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_EU_US_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		} else if (CG2900_FM_BAND_JAPAN == band) {
			freq_low = FMR_JAPAN_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_JAPAN_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		} else if (CG2900_FM_BAND_CHINA == band) {
			freq_low = FMR_CHINA_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_CHINA_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		}
		cg2900_device.fm_mode = CG2900_FM_RX_MODE;
		cg2900_device.rx_rds_enabled =
		    (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) ?
		    true : false;
		if (tuner->rxsubchans & V4L2_TUNER_SUB_STEREO)
			stereo_status = true;
		else if (tuner->rxsubchans & V4L2_TUNER_SUB_MONO)
			stereo_status = false;
		cg2900_device.rx_stereo_status = stereo_status;
		status = cg2900_fm_set_rx_default_settings(freq_low,
				band,
				grid,
				cg2900_device.rx_rds_enabled,
				cg2900_device.rx_stereo_status);

		if (0 != status) {
			FM_ERR_REPORT("vidioc_set_tuner: "
				"cg2900_fm_set_rx_default_settings returned "
				" %d", status);
			goto error;
		}
		status = cg2900_fm_set_rssi_threshold(
				cg2900_device.rssi_threshold);
		if (0 != status) {
			FM_ERR_REPORT("vidioc_set_tuner: "
					"cg2900_fm_set_rssi_threshold returned "
					" %d", status);
			goto error;
		}
	} else {
		/*
		 * Mode was FM Rx only, change the RDS settings or stereo mode
		 * if they are changed by application
		 */
		rds_status = (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) ?
		    true : false;
		if (tuner->rxsubchans & V4L2_TUNER_SUB_STEREO)
			stereo_status = true;
		else if (tuner->rxsubchans & V4L2_TUNER_SUB_MONO)
			stereo_status = false;
		if (stereo_status != cg2900_device.rx_stereo_status) {
			cg2900_device.rx_stereo_status = stereo_status;
			if (stereo_status)
				status =
				    cg2900_fm_set_mode(
						FMD_STEREOMODE_BLENDING);
			else
				status = cg2900_fm_set_mode(
						FMD_STEREOMODE_MONO);

			if (0 != status) {
				FM_ERR_REPORT("vidioc_set_tuner: "
						"cg2900_fm_set_mode returned "
						" %d", status);
				goto error;
			}
		}
		if (rds_status != cg2900_device.rx_rds_enabled) {
			cg2900_device.rx_rds_enabled = rds_status;
			if (rds_status)
				status = cg2900_fm_rds_on();
			else
				status = cg2900_fm_rds_off();

			if (0 != status) {
				FM_ERR_REPORT("vidioc_set_tuner: "
						"cg2900_fm_rds returned "
						" %d", status);
				goto error;
			}
		}
	}

	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_set_tuner: returning %d", ret_val);
	return ret_val;
}

/**
 * vidioc_get_modulator()- Get FM Modulator Features.
 *
 * This function is used to get the modulator features.
 * This function is called when the application issues the IOCTL
 * VIDIOC_G_MODULATOR
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @modulator: v4l2_modulator structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_modulator(
			struct file *file,
			void *priv,
			struct v4l2_modulator *modulator
			)
{
	int status = 0;
	bool rds_enabled;
	u8 mode;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_get_modulator");

	if (modulator->index > 0) {
		FM_ERR_REPORT("vidioc_get_modulator: Only 1 "
			      "modulator supported");
		goto error;
	}

	memset(modulator, 0, sizeof(*modulator));
	strcpy(modulator->name, "CG2900 FM Transmitter");
	modulator->rangelow = freq_low;
	modulator->rangehigh = freq_high;
	modulator->capability = V4L2_TUNER_CAP_NORM /* Freq steps = 1/16 kHz */
	    | V4L2_TUNER_CAP_STEREO	/* Can receive stereo */
	    | V4L2_TUNER_CAP_RDS;	/* Supports RDS Capture  */

	if (cg2900_device.fm_mode == CG2900_FM_TX_MODE) {
		status = cg2900_fm_get_mode(&mode);
		FM_DEBUG_REPORT("vidioc_get_modulator: mode = %x", mode);
		if (0 != status) {
			/* Get mode API failed, set mode to mono */
			modulator->txsubchans = V4L2_TUNER_SUB_MONO;
			goto error;
		}
		switch (mode) {
			/* Stereo */
		case CG2900_MODE_STEREO:
			modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
			break;
			/* Mono */
		case CG2900_MODE_MONO:
			modulator->txsubchans = V4L2_TUNER_SUB_MONO;
			break;
			/* Switching or Blending, set mode as Stereo */
		default:
			modulator->txsubchans = V4L2_TUNER_SUB_STEREO;
		}
		status = cg2900_fm_get_rds_status(&rds_enabled);
		if (0 != status) {
			modulator->txsubchans &= ~V4L2_TUNER_SUB_RDS;
			goto error;
		}
		if (rds_enabled)
			modulator->txsubchans |= V4L2_TUNER_SUB_RDS;
		else
			modulator->txsubchans &= ~V4L2_TUNER_SUB_RDS;
	} else
		modulator->txsubchans = V4L2_TUNER_SUB_MONO;

	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_get_modulator: returning %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_set_modulator()- Set FM Modulator Features.
 *
 * This function is used to set the Modulaotr features.
 * It also sets the default FM Tx settings.
 * This function is called when the application issues the IOCTL
 * VIDIOC_S_MODULATOR
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @modulator: v4l2_modulator structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_modulator(
			struct file *file,
			void *priv,
			struct v4l2_modulator *modulator
			)
{
	bool rds_status = false;
	bool stereo_status = false;
	int status = 0;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_set_modulator");
	if (modulator->index != 0) {
		FM_ERR_REPORT("vidioc_set_modulator: Only 1 "
			      "modulator supported");
		goto error;
	}

	if (cg2900_device.fm_mode != CG2900_FM_TX_MODE) {
		/*
		 * FM Tx mode should be configured as
		 * earlier mode was not FM Tx
		 */
		if (band == CG2900_FM_BAND_US_EU) {
			freq_low = FMR_EU_US_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_EU_US_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		} else if (band == CG2900_FM_BAND_JAPAN) {
			freq_low = FMR_JAPAN_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_JAPAN_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		} else if (band == CG2900_FM_BAND_CHINA) {
			freq_low = FMR_CHINA_LOW_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
			freq_high = FMR_CHINA_HIGH_FREQ_IN_MHZ *
			    FMR_HZ_TO_MHZ_CONVERTER;
		}
		cg2900_device.fm_mode = CG2900_FM_TX_MODE;
		cg2900_device.rx_rds_enabled = false;
		cg2900_device.tx_rds_enabled =
		    (modulator->txsubchans & V4L2_TUNER_SUB_RDS) ?
		    true : false;
		if (modulator->txsubchans & V4L2_TUNER_SUB_STEREO)
			stereo_status = true;
		else if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
			stereo_status = false;
		cg2900_device.tx_stereo_status = stereo_status;

		status = cg2900_fm_set_tx_default_settings(freq_low,
					       band,
					       grid,
					       cg2900_device.tx_rds_enabled,
					       cg2900_device.
					       tx_stereo_status);

		if (0 != status) {
			FM_ERR_REPORT("vidioc_set_modulator: "
				"cg2900_fm_set_tx_default_settings returned "
				" %d", status);
			goto error;
		}
	} else {
		/*
		 * Mode was FM Tx only, change the RDS settings or stereo mode
		 * if they are changed by application
		 */
		rds_status = (modulator->txsubchans & V4L2_TUNER_SUB_RDS) ?
		    true : false;
		if (modulator->txsubchans & V4L2_TUNER_SUB_STEREO)
			stereo_status = true;
		else if (modulator->txsubchans & V4L2_TUNER_SUB_MONO)
			stereo_status = false;
		if (stereo_status != cg2900_device.tx_stereo_status) {
			cg2900_device.tx_stereo_status = stereo_status;
			status = cg2900_fm_set_mode(stereo_status);
			if (0 != status) {
				FM_ERR_REPORT("vidioc_set_modulator: "
						"cg2900_fm_set_mode returned "
						" %d", status);
				goto error;
			}
		}
		if (rds_status != cg2900_device.tx_rds_enabled) {
			cg2900_device.tx_rds_enabled = rds_status;
			status = cg2900_fm_tx_rds(rds_status);
			if (0 != status) {
				FM_ERR_REPORT("vidioc_set_modulator: "
						"cg2900_fm_tx_rds returned "
						" %d", status);
				goto error;
			}
		}
	}

	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_set_modulator: returning %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_get_frequency()- Get the Current FM Frequnecy.
 *
 * This function is used to get the currently tuned
 * frequency on FM Radio. This function is called when the application
 * issues the IOCTL VIDIOC_G_FREQUENCY
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @freq: v4l2_frequency structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_frequency(
			struct file *file,
			void *priv,
			struct v4l2_frequency *freq
			)
{
	int status;
	u32 frequency;
	int ret_val = -EINVAL;
	struct sk_buff *skb;

	FM_INFO_REPORT("vidioc_get_frequency: Status = %d",
			cg2900_device.seekstatus);

	status = cg2900_fm_get_frequency(&frequency);

	if (0 != status) {
		freq->frequency = cg2900_device.frequency;
		goto error;
	}

	if (cg2900_device.seekstatus == FMR_SEEK_IN_PROGRESS) {
		if (skb_queue_empty(&fm_interrupt_queue)) {
			/* No Interrupt, bad case */
			FM_ERR_REPORT("vidioc_get_frequency: "
				"No Interrupt to read");
			fm_event = CG2900_EVENT_NO_EVENT;
			goto error;
		}
		spin_lock(&fm_spinlock);
		skb = skb_dequeue(&fm_interrupt_queue);
		spin_unlock(&fm_spinlock);
		if (!skb) {
			/* No Interrupt, bad case */
			FM_ERR_REPORT("vidioc_get_frequency: "
				"No Interrupt to read");
			fm_event = CG2900_EVENT_NO_EVENT;
			goto error;
		}
		fm_event = (u8)skb->data[0];
		FM_DEBUG_REPORT("vidioc_get_frequency: Interrupt = %x",
				fm_event);
		/*  Check if seek is finished or not */
		if (CG2900_EVENT_SEARCH_CHANNEL_FOUND == fm_event) {
			/* seek is finished */
			spin_lock(&fm_spinlock);
			cg2900_device.frequency = HZ_TO_V4L2(frequency);
			freq->frequency = cg2900_device.frequency;
			cg2900_device.seekstatus = FMR_SEEK_NONE;
			fm_event = CG2900_EVENT_NO_EVENT;
			kfree_skb(skb);
			spin_unlock(&fm_spinlock);
		} else {
			/* Some other interrupt, queue it back */
			spin_lock(&fm_spinlock);
			skb_queue_head(&fm_interrupt_queue, skb);
			spin_unlock(&fm_spinlock);
		}
	} else {
		spin_lock(&fm_spinlock);
		cg2900_device.frequency = HZ_TO_V4L2(frequency);
		freq->frequency = cg2900_device.frequency;
		spin_unlock(&fm_spinlock);
	}
	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_get_frequency: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_set_frequency()- Set the FM Frequnecy.
 *
 * This function is used to set the frequency
 * on FM Radio. This function is called when the application
 * issues the IOCTL VIDIOC_S_FREQUENCY
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @freq: v4l2_frequency structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_frequency(
			struct file *file,
			void *priv,
			struct v4l2_frequency *freq
			)
{
	u32 frequency = freq->frequency;
	u32 freq_low, freq_high;
	int status;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_set_frequency: Frequency = "
			"%d ", V4L2_TO_HZ(frequency));

	/*  Check which band is set currently */
	switch (band) {
	case CG2900_FM_BAND_US_EU:
		freq_low = FMR_EU_US_LOW_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		freq_high = FMR_EU_US_HIGH_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		break;

	case CG2900_FM_BAND_CHINA:
		freq_low = FMR_CHINA_LOW_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		freq_high = FMR_CHINA_HIGH_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		break;

	case CG2900_FM_BAND_JAPAN:
		freq_low = FMR_JAPAN_LOW_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		freq_high = FMR_JAPAN_HIGH_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		break;

	default:
		/* Set to US_MAX and CHINA_MIN band */
		freq_low = FMR_CHINA_LOW_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
		freq_high = FMR_EU_US_HIGH_FREQ_IN_MHZ *
			FMR_HZ_TO_MHZ_CONVERTER;
	}

	/* Check if the frequency set is out of current band */
	if ((V4L2_TO_HZ(frequency) < freq_low) ||
		(V4L2_TO_HZ(frequency) > freq_high))
		goto error;

	spin_lock(&fm_spinlock);
	fm_event = CG2900_EVENT_NO_EVENT;
	no_of_scan_freq = 0;
	spin_unlock(&fm_spinlock);

	cg2900_device.seekstatus = FMR_SEEK_NONE;
	cg2900_device.frequency = frequency;
	status = cg2900_fm_set_frequency(V4L2_TO_HZ(frequency));

	if (0 != status)
		goto error;

	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_set_frequency: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_query_ctrl()- Query the FM Driver control features.
 *
 * This function is used to query the control features on FM Radio.
 * This function is called when the application
 * issues the IOCTL VIDIOC_QUERYCTRL
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @query_ctrl: v4l2_queryctrl structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_query_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_queryctrl *query_ctrl
			)
{
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_query_ctrl");
	/* Check which control is requested */
	switch (query_ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		FM_DEBUG_REPORT("vidioc_query_ctrl:  V4L2_CID_AUDIO_MUTE");
		query_ctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
		query_ctrl->minimum = 0;
		query_ctrl->maximum = 1;
		query_ctrl->step = 1;
		query_ctrl->default_value = 0;
		query_ctrl->flags = 0;
		strncpy(query_ctrl->name, "CG2900 Mute", 32);
		ret_val = 0;
		break;

	case V4L2_CID_AUDIO_VOLUME:
		FM_DEBUG_REPORT("vidioc_query_ctrl: V4L2_CID_AUDIO_VOLUME");

		strncpy(query_ctrl->name, "CG2900 Volume", 32);
		query_ctrl->minimum = MIN_ANALOG_VOLUME;
		query_ctrl->maximum = MAX_ANALOG_VOLUME;
		query_ctrl->step = 1;
		query_ctrl->default_value = MAX_ANALOG_VOLUME;
		query_ctrl->flags = 0;
		query_ctrl->type = V4L2_CTRL_TYPE_INTEGER;
		ret_val = 0;
		break;

	case V4L2_CID_AUDIO_BALANCE:
		FM_DEBUG_REPORT("vidioc_query_ctrl:   V4L2_CID_AUDIO_BALANCE ");
		strncpy(query_ctrl->name, "CG2900 Audio Balance", 32);
		query_ctrl->type = V4L2_CTRL_TYPE_INTEGER;
		query_ctrl->minimum = 0x0000;
		query_ctrl->maximum = 0xFFFF;
		query_ctrl->step = 0x0001;
		query_ctrl->default_value = 0x0000;
		query_ctrl->flags = 0;
		ret_val = 0;
		break;

	case V4L2_CID_AUDIO_BASS:
		FM_DEBUG_REPORT("vidioc_query_ctrl: "
				"V4L2_CID_AUDIO_BASS (unsupported)");
		break;

	case V4L2_CID_AUDIO_TREBLE:
		FM_DEBUG_REPORT("vidioc_query_ctrl: "
				"V4L2_CID_AUDIO_TREBLE (unsupported)");
		break;

	default:
		FM_DEBUG_REPORT("vidioc_query_ctrl: "
				"--> unsupported id = %x", query_ctrl->id);
		break;
	}

	FM_DEBUG_REPORT("vidioc_query_ctrl: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_get_ctrl()- Get the value of a particular Control.
 *
 * This function is used to get the value of a
 * particular control from the FM Driver. This function is called
 * when the application issues the IOCTL VIDIOC_G_CTRL
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @ctrl: v4l2_control structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_control *ctrl
			)
{
	int status;
	u8 value;
	u16 rssi;
	u8 antenna;
	u16 conclusion;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_get_ctrl");

	switch (ctrl->id) {
	case V4L2_CID_AUDIO_VOLUME:
		status = cg2900_fm_get_volume(&value);
		if (0 == status) {
			ctrl->value = value;
			cg2900_device.volume = value;
			ret_val = 0;
		}
		break;
	case V4L2_CID_AUDIO_MUTE:
		ctrl->value = cg2900_device.muted;
		ret_val = 0;
		break;
	case V4L2_CID_AUDIO_BALANCE:
		ctrl->value = cg2900_device.audiopath;
		ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_RSSI_THRESHOLD:
		ctrl->value = cg2900_device.rssi_threshold;
		ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_SELECT_ANTENNA:
		status = cg2900_fm_get_antenna(&antenna);
		FM_DEBUG_REPORT("vidioc_get_ctrl: Antenna = %x", antenna);
		if (0 == status) {
			ctrl->value = antenna;
			ret_val = 0;
		}
		break;
	case V4L2_CID_CG2900_RADIO_RDS_AF_UPDATE_GET_RESULT:
		status = cg2900_fm_af_update_get_result(&rssi);
		FM_DEBUG_REPORT("vidioc_get_ctrl: AF RSSI Level = %x", rssi);
		if (0 == status) {
			ctrl->value = rssi;
			ret_val = 0;
		}
		break;
	case V4L2_CID_CG2900_RADIO_RDS_AF_SWITCH_GET_RESULT:
		status = cg2900_fm_af_switch_get_result(&conclusion);
		FM_DEBUG_REPORT("vidioc_get_ctrl: AF Switch conclusion = %x",
				conclusion);
		if (0 != status)
			break;
		if (conclusion == 0) {
			ctrl->value = conclusion;
			FM_DEBUG_REPORT("vidioc_get_ctrl: "
					"AF Switch conclusion = %d",
					ctrl->value);
			ret_val = 0;
		} else {
			/*
			 * Convert positive error code returned by chip
			 * into negative error codes to be in line with linux.
			 */
			ctrl->value = -conclusion;
			FM_ERR_REPORT("vidioc_get_ctrl: "
			"AF-Switch failed with value %d", ctrl->value);
			ret_val = 0;
		}
		break;
	default:
		FM_DEBUG_REPORT("vidioc_get_ctrl: "
				"unsupported (id = %x)", (int)ctrl->id);
		ret_val = -EINVAL;
	}
	FM_DEBUG_REPORT("vidioc_get_ctrl: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_set_ctrl()- Set the value of a particular Control.
 *
 * This function is used to set the value of a
 * particular control from the FM Driver. This function is called when the
 * application issues the IOCTL VIDIOC_S_CTRL
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @ctrl: v4l2_control structure.
 *
 * Returns:
 *   0 when no error
 *   -ERANGE when the parameter is out of range.
 *   -EINVAL: otherwise
 */
static int vidioc_set_ctrl(
			struct file *file,
			void *priv,
			struct v4l2_control *ctrl
			)
{
	int status;
	int ret_val = -EINVAL;
	FM_INFO_REPORT("vidioc_set_ctrl");
	/* Check which control is requested */
	switch (ctrl->id) {
	case V4L2_CID_AUDIO_MUTE:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_AUDIO_MUTE, "
				"value = %d", ctrl->value);
		if (ctrl->value > 1 && ctrl->value < 0) {
			ret_val =  -ERANGE;
			break;
		}

		if (ctrl->value) {
			FM_DEBUG_REPORT("vidioc_set_ctrl: Ctrl_Id = "
					"V4L2_CID_AUDIO_MUTE, "
					"Muting the Radio");
			status = cg2900_fm_mute();
		} else {
			FM_DEBUG_REPORT("vidioc_set_ctrl: "
					"Ctrl_Id = V4L2_CID_AUDIO_MUTE, "
					"UnMuting the Radio");
			status = cg2900_fm_unmute();
		}
		if (0 == status) {
			cg2900_device.muted = ctrl->value;
			ret_val = 0;
		}
		break;
	case V4L2_CID_AUDIO_VOLUME:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_AUDIO_VOLUME, "
				"value = %d", ctrl->value);
		if (ctrl->value > MAX_ANALOG_VOLUME &&
				ctrl->value < MIN_ANALOG_VOLUME) {
			ret_val = -ERANGE;
			break;
		}
		status = cg2900_fm_set_volume(ctrl->value);
		if (0 == status) {
			cg2900_device.volume = ctrl->value;
			ret_val = 0;
		}
		break;
	case V4L2_CID_AUDIO_BALANCE:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_AUDIO_BALANCE, "
				"value = %d", ctrl->value);
		status = cg2900_fm_set_audio_balance(ctrl->value);
		if (0 == status) {
			cg2900_device.audiopath = ctrl->value;
			ret_val = 0;
		}
		break;
	case V4L2_CID_CG2900_RADIO_CHIP_STATE:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_CG2900_RADIO_CHIP_STATE, "
				"value = %d", ctrl->value);
		if (V4L2_CG2900_RADIO_STANDBY == ctrl->value)
			status = cg2900_fm_standby();
		else if (V4L2_CG2900_RADIO_POWERUP == ctrl->value)
			status = cg2900_fm_power_up_from_standby();
		else
			break;
		if (0 != status)
			break;
		if (V4L2_CG2900_RADIO_STANDBY == ctrl->value)
			cg2900_device.state = FMR_STANDBY;
		else if (V4L2_CG2900_RADIO_POWERUP == ctrl->value)
			cg2900_device.state = FMR_SWITCH_ON;
		ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_SELECT_ANTENNA:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_CG2900_RADIO_SELECT_ANTENNA, "
				"value = %d", ctrl->value);
		status = cg2900_fm_select_antenna(ctrl->value);
		if (0 == status)
			ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_BANDSCAN:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_CG2900_RADIO_BANDSCAN, "
				"value = %d", ctrl->value);
		if (V4L2_CG2900_RADIO_BANDSCAN_START == ctrl->value) {
			cg2900_device.seekstatus = FMR_SEEK_IN_PROGRESS;
			no_of_scan_freq = 0;
			status = cg2900_fm_start_band_scan();
		} else if (V4L2_CG2900_RADIO_BANDSCAN_STOP == ctrl->value) {
			status = cg2900_fm_stop_scan();
			cg2900_device.seekstatus = FMR_SEEK_NONE;
		} else
			break;
		if (0 == status)
			ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_RSSI_THRESHOLD:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_CG2900_RADIO_RSSI_THRESHOLD "
				"= %d", ctrl->value);
		status = cg2900_fm_set_rssi_threshold(ctrl->value);
		if (0 == status) {
			cg2900_device.rssi_threshold = ctrl->value;
			ret_val = 0;
		}
		break;
	case V4L2_CID_CG2900_RADIO_RDS_AF_UPDATE_START:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"V4L2_CID_CG2900_RADIO_RDS_AF_UPDATE_START "
				"freq = %d Hz", ctrl->value);
		status = cg2900_fm_af_update_start(ctrl->value);
		if (0 == status)
			ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_TEST_TONE_GENERATOR_SET_STATUS:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
			"V4L2_CID_CG2900_RADIO_TEST_TONE_GENERATOR_SET_STATUS "
			"state = %d ", ctrl->value);
		if (ctrl->value < V4L2_CG2900_RADIO_TEST_TONE_GEN_OFF ||
			ctrl->value >
			V4L2_CG2900_RADIO_TEST_TONE_GENERATOR_ON_WO_SRC) {
			FM_ERR_REPORT("Invalid parameter = %d", ctrl->value);
			break;
		}
		status = cg2900_fm_set_test_tone_generator(ctrl->value);
		if (0 == status)
			ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_TUNE_DEEMPHASIS:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
			"V4L2_CID_CG2900_RADIO_TUNE_DEEMPHASIS, "
			"Value = %d", ctrl->value);

		if ((V4L2_CG2900_RADIO_DEEMPHASIS_DISABLED >
			ctrl->value) ||
			(V4L2_CG2900_RADIO_DEEMPHASIS_75_uS <
			ctrl->value)) {
			FM_ERR_REPORT("Unsupported deemphasis = %d",
			ctrl->value);
			break;
		}

		switch (ctrl->value) {
		case V4L2_CG2900_RADIO_DEEMPHASIS_50_uS:
			ctrl->value = FMD_EMPHASIS_50US;
			break;
		case V4L2_CG2900_RADIO_DEEMPHASIS_75_uS:
			ctrl->value = FMD_EMPHASIS_75US;
			break;
		case V4L2_CG2900_RADIO_DEEMPHASIS_DISABLED:
			/* Drop Down */
		default:
			ctrl->value = FMD_EMPHASIS_NONE;
			break;
		}
		status = cg2900_fm_rx_set_deemphasis(ctrl->value);

		if (0 == status)
			ret_val = 0;
		break;
	default:
		FM_DEBUG_REPORT("vidioc_set_ctrl: "
				"unsupported (id = %x)", ctrl->id);
	}
	FM_DEBUG_REPORT("vidioc_set_ctrl: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_get_ext_ctrls()- Get the values of a particular control.
 *
 * This function is used to get the value of a
 * particular control from the FM Driver. This is used when the data to
 * be received is more than 1 paramter. This function is called when the
 * application issues the IOCTL VIDIOC_G_EXT_CTRLS
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @ext_ctrl: v4l2_ext_controls structure.
 *
 * Returns:
 *   0 when no error
 *  -ENOSPC: when there is no space to copy the data into the buffer provided
 * by application.
 *   -EINVAL: otherwise
 */
static int vidioc_get_ext_ctrls(
			struct file *file,
			void *priv,
			struct v4l2_ext_controls *ext_ctrl
			)
{
	u32 *dest_buffer;
	int index = 0;
	int count = 0;
	int ret_val = -EINVAL;
	int status;
	struct sk_buff *skb;
	u8 mode;
	s8 interrupt_success;
	int *fm_interrupt_buffer;

	FM_INFO_REPORT("vidioc_get_ext_ctrls: Id = %04x,"
			"ext_ctrl->ctrl_class = %04x",
			ext_ctrl->controls->id,
			ext_ctrl->ctrl_class);

	if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_FM_TX &&
	    ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
		FM_ERR_REPORT("vidioc_get_ext_ctrls: Unsupported "
			      "ctrl_class = %04x", ext_ctrl->ctrl_class);
		goto error;
	}

	switch (ext_ctrl->controls->id) {
	case V4L2_CID_CG2900_RADIO_BANDSCAN_GET_RESULTS:
		if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
			FM_ERR_REPORT("vidioc_get_ext_ctrls:  "
			"V4L2_CID_CG2900_RADIO_BANDSCAN_GET_RESULTS "
			"Unsupported ctrl_class = %04x",
			ext_ctrl->ctrl_class);
			break;
		}
		if (cg2900_device.seekstatus ==
			FMR_SEEK_IN_PROGRESS) {
			spin_lock(&fm_spinlock);
			skb = skb_dequeue(&fm_interrupt_queue);
			spin_unlock(&fm_spinlock);
			if (!skb) {
				/* No Interrupt, bad case */
				FM_ERR_REPORT("No Interrupt to read");
				fm_event = CG2900_EVENT_NO_EVENT;
				break;
			}
			fm_event = (u8)skb->data[0];
			FM_DEBUG_REPORT(
				"V4L2_CID_CG2900_RADIO"
				"_BANDSCAN_GET_RESULTS: "
				"fm_event = %x", fm_event);
			if (fm_event ==
				CG2900_EVENT_SCAN_CHANNELS_FOUND) {
				/* Check to get Scan Result */
				status =
					cg2900_fm_get_scan_result
					(&no_of_scan_freq, scanfreq,
					scanfreq_rssi_level);
				if (0 != status) {
					FM_ERR_REPORT
						("vidioc_get_ext_ctrls: "
						"cg2900_fm_get_scan_"
						"result: returned %d",
						status);
					kfree_skb(skb);
					break;
				}
				kfree_skb(skb);
			} else {
				/* Some other interrupt, Queue it back */
				spin_lock(&fm_spinlock);
				skb_queue_head(&fm_interrupt_queue, skb);
				spin_unlock(&fm_spinlock);
			}
		}
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"SeekStatus = %x, GlobalEvent = %x, "
				"numchannels = %x",
				cg2900_device.seekstatus,
				fm_event, no_of_scan_freq);

		if (ext_ctrl->controls->size == 0 &&
		    ext_ctrl->controls->string == NULL) {
			if (cg2900_device.seekstatus ==
			    FMR_SEEK_IN_PROGRESS &&
			    CG2900_EVENT_SCAN_CHANNELS_FOUND
			    == fm_event) {
				spin_lock(&fm_spinlock);
				ext_ctrl->controls->size =
				    no_of_scan_freq;
				cg2900_device.seekstatus
				    = FMR_SEEK_NONE;
				fm_event =
				    CG2900_EVENT_NO_EVENT;
				spin_unlock(&fm_spinlock);
				return -ENOSPC;
			}
		} else if (ext_ctrl->controls->string != NULL) {
			dest_buffer =
			    (u32 *) ext_ctrl->controls->string;
			while (index < no_of_scan_freq) {
				*(dest_buffer + count + 0) =
				    HZ_TO_V4L2(scanfreq[index]);
				*(dest_buffer + count + 1) =
				    scanfreq_rssi_level[index];
				count += 2;
				index++;
			}
			ret_val = 0;
		}
		break;
	case V4L2_CID_CG2900_RADIO_BLOCKSCAN_GET_RESULTS:
		if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
			FM_ERR_REPORT("vidioc_get_ext_ctrls:  "
			"V4L2_CID_CG2900_RADIO_BLOCKSCAN"
			"_GET_RESULTS "
			"Unsupported ctrl_class = %04x",
			ext_ctrl->ctrl_class);
			break;
		}
		if (cg2900_device.seekstatus == FMR_SEEK_IN_PROGRESS) {
			spin_lock(&fm_spinlock);
			skb = skb_dequeue(&fm_interrupt_queue);
			spin_unlock(&fm_spinlock);
			if (!skb) {
				/* No Interrupt, bad case */
				FM_ERR_REPORT("No Interrupt to read");
				fm_event = CG2900_EVENT_NO_EVENT;
				break;
			}
			fm_event = (u8)skb->data[0];
			FM_DEBUG_REPORT(
				"V4L2_CID_CG2900_RADIO_BLOCKSCAN"
				"GET_RESULTS: "
				"fm_event = %x", fm_event);
			if (fm_event ==
				CG2900_EVENT_BLOCK_SCAN_CHANNELS_FOUND) {
				/* Check for BlockScan Result */
				status =
					cg2900_fm_get_block_scan_result
					(&no_of_block_scan_freq,
					block_scan_rssi_level);
				if (0 != status) {
					FM_ERR_REPORT
						("vidioc_get_ext_ctrls: "
						"cg2900_fm_get_block_scan_"
						"result: returned %d",
						status);
					kfree_skb(skb);
					break;
				}
				kfree_skb(skb);
			} else {
				/* Some other interrupt,
				Queue it back */
				spin_lock(&fm_spinlock);
				skb_queue_head(&fm_interrupt_queue, skb);
				spin_unlock(&fm_spinlock);
			}
		}
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"SeekStatus = %x, GlobalEvent = %x, "
				"numchannels = %x",
				cg2900_device.seekstatus,
				fm_event, no_of_block_scan_freq);
		if (ext_ctrl->controls->size == 0 &&
		    ext_ctrl->controls->string == NULL) {
			if (cg2900_device.seekstatus ==
			    FMR_SEEK_IN_PROGRESS &&
			    CG2900_EVENT_BLOCK_SCAN_CHANNELS_FOUND
			    == fm_event) {
				spin_lock(&fm_spinlock);
				ext_ctrl->controls->size =
				    no_of_block_scan_freq;
				cg2900_device.seekstatus
				    = FMR_SEEK_NONE;
				fm_event =
				    CG2900_EVENT_NO_EVENT;
				spin_unlock(&fm_spinlock);
				return -ENOSPC;
			}
		} else if (ext_ctrl->controls->size >=
			   no_of_block_scan_freq &&
			   ext_ctrl->controls->string != NULL) {
			dest_buffer =
			    (u32 *) ext_ctrl->controls->string;
			while (index < no_of_block_scan_freq) {
				*(dest_buffer + index) =
				    block_scan_rssi_level
				    [index];
				index++;
			}
			ret_val = 0;
			return ret_val;
		}
		break;
	case V4L2_CID_RDS_TX_DEVIATION:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"V4L2_CID_RDS_TX_DEVIATION");
		if (V4L2_CTRL_CLASS_FM_TX != ext_ctrl->ctrl_class) {
			FM_ERR_REPORT("Invalid Ctrl Class = %x",
				      ext_ctrl->ctrl_class);
			break;
		}
		status = cg2900_fm_tx_get_rds_deviation((u16 *) &
						     ext_ctrl->
						     controls->value);
		if (status == 0)
			ret_val = 0;
		break;
	case V4L2_CID_PILOT_TONE_ENABLED:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"V4L2_CID_PILOT_TONE_ENABLED");
		if (V4L2_CTRL_CLASS_FM_TX != ext_ctrl->ctrl_class) {
			FM_ERR_REPORT("Invalid Ctrl Class = %x",
				      ext_ctrl->ctrl_class);
			break;
		}
		status = cg2900_fm_tx_get_pilot_tone_status(
				(bool *)&ext_ctrl->controls->value);
		if (status == 0)
			ret_val = 0;
		break;
	case V4L2_CID_PILOT_TONE_DEVIATION:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"V4L2_CID_PILOT_TONE_DEVIATION");
		if (V4L2_CTRL_CLASS_FM_TX != ext_ctrl->ctrl_class) {
			FM_ERR_REPORT("Invalid Ctrl Class = %x",
				      ext_ctrl->ctrl_class);
			break;
		}
		status = cg2900_fm_tx_get_pilot_deviation(
				(u16 *)&ext_ctrl->controls->value);
		if (status == 0)
			ret_val = 0;
		break;
	case V4L2_CID_TUNE_PREEMPHASIS:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"V4L2_CID_TUNE_PREEMPHASIS");
		if (V4L2_CTRL_CLASS_FM_TX != ext_ctrl->ctrl_class) {
			FM_ERR_REPORT("Invalid Ctrl Class = %x",
				      ext_ctrl->ctrl_class);
			break;
		}
		status = cg2900_fm_tx_get_preemphasis(
				(u8 *)&ext_ctrl->controls->value);
		if (status == 0)
			ret_val = 0;
		break;
	case V4L2_CID_TUNE_POWER_LEVEL:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"V4L2_CID_TUNE_POWER_LEVEL");
		if (V4L2_CTRL_CLASS_FM_TX != ext_ctrl->ctrl_class) {
			FM_ERR_REPORT("Invalid Ctrl Class = %x",
				      ext_ctrl->ctrl_class);
			break;
		}
		status = cg2900_fm_tx_get_power_level(
				(u16 *)&ext_ctrl->controls->value);
		if (status == 0)
			ret_val = 0;
		break;
	case V4L2_CID_CG2900_RADIO_GET_INTERRUPT:
		if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
			FM_ERR_REPORT("vidioc_get_ext_ctrls:  "
			"V4L2_CID_CG2900_RADIO_GET_INTERRUPT "
			"Unsupported ctrl_class = %04x",
			ext_ctrl->ctrl_class);
			break;
		}
		if (ext_ctrl->controls->size != FMR_GET_INTERRUPT_DATA_SIZE ||
			ext_ctrl->controls->string == NULL) {
			FM_ERR_REPORT("vidioc_get_ext_ctrls:  "
			"V4L2_CID_CG2900_RADIO_GET_INTERRUPT "
			"Invalid parameters, ext_ctrl->controls->size = %x "
			"ext_ctrl->controls->string = %08x",
			ext_ctrl->controls->size,
			(unsigned int)ext_ctrl->controls->string);
			ret_val = -ENOSPC;
			break;
		}
		spin_lock(&fm_spinlock);
		skb = skb_dequeue(&fm_interrupt_queue);
		spin_unlock(&fm_spinlock);
		if (!skb) {
			/* No Interrupt, bad case */
			FM_ERR_REPORT("V4L2_CID_CG2900_RADIO_GET_INTERRUPT: "
				"No Interrupt to read");
			fm_event = CG2900_EVENT_NO_EVENT;
			break;
		}
		fm_event = (u8)skb->data[0];
		interrupt_success = (s8)skb->data[1];
		FM_DEBUG_REPORT("vidioc_get_ctrl: Interrupt = %x "
				"interrupt_success = %x",
				fm_event, interrupt_success);
		fm_interrupt_buffer =
		    (int *) ext_ctrl->controls->string;
		/* Interrupt that has occurred */
		*fm_interrupt_buffer = cg2900_map_event_to_v4l2(fm_event);

		/* Interrupt success or failed */
		if (interrupt_success) {
			/* Interrupt Success, return 0 */
			*(fm_interrupt_buffer + 1) = 0;
		} else {
			spin_lock(&fm_spinlock);
			no_of_scan_freq = 0;
			no_of_block_scan_freq = 0;
			spin_unlock(&fm_spinlock);
			cg2900_device.seekstatus = FMR_SEEK_NONE;
			/* Clear the Interrupt flag */
			fm_event = CG2900_EVENT_NO_EVENT;
			kfree_skb(skb);
			/* Interrupt Success, return negative error */
			*(fm_interrupt_buffer + 1) = -1;
			FM_ERR_REPORT("vidioc_get_ext_ctrls: Interrupt = %d "
				"failed with reason = %d",
				(*fm_interrupt_buffer),
				(*(fm_interrupt_buffer + 1)));
			/*
			 * Update return value, so that application
			 * can read the event failure reason.
			 */
			ret_val = 0;
			break;
		}

		if (CG2900_EVENT_MONO_STEREO_TRANSITION
			== fm_event) {
			/*
			 * In case of Mono/Stereo Interrupt,
			 * get the current value from chip
			 */
			status = cg2900_fm_get_mode(&mode);
			cg2900_device.rx_stereo_status = (bool)mode;
			/* Clear the Interrupt flag */
			fm_event = CG2900_EVENT_NO_EVENT;
			kfree_skb(skb);
		} else if (CG2900_EVENT_SCAN_CANCELLED ==
			fm_event) {
			/* Scan/Search cancelled by User */
			spin_lock(&fm_spinlock);
			no_of_scan_freq = 0;
			no_of_block_scan_freq = 0;
			spin_unlock(&fm_spinlock);
			cg2900_device.seekstatus = FMR_SEEK_NONE;
			/* Clear the Interrupt flag */
			fm_event = CG2900_EVENT_NO_EVENT;
			kfree_skb(skb);
		} else {
				/* Queue the interrupt back
					for later dequeuing */
				FM_DEBUG_REPORT("V4L2_CID_CG2900"
					"_RADIO_GET_INTERRUPT: "
					"Queuing the interrupt"
					"again to head of list");
				spin_lock(&fm_spinlock);
				skb_queue_head(&fm_interrupt_queue, skb);
				spin_unlock(&fm_spinlock);
		}
		ret_val = 0;
		break;
	default:
		FM_DEBUG_REPORT("vidioc_get_ext_ctrls: "
				"unsupported (id = %x)",
				ext_ctrl->controls->id);
	}

error:
	FM_DEBUG_REPORT("vidioc_get_ext_ctrls: returning = %d", ret_val);
	return ret_val;
}

/**
 * vidioc_set_ext_ctrls()- Set the values of a particular control.
 *
 * This function is used to set the value of a
 * particular control on the FM Driver. This is used when the data to
 * be set is more than 1 paramter. This function is called when the
 * application issues the IOCTL VIDIOC_S_EXT_CTRLS
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @ext_ctrl: v4l2_ext_controls structure.
 *
 * Returns:
 *   0 when no error
 *   -ENOSPC: when there is no space to copy the data into the buffer provided
 * by application.
 *   -EINVAL: otherwise
 */
static int vidioc_set_ext_ctrls(
			struct file *file,
			void *priv,
			struct v4l2_ext_controls *ext_ctrl
			)
{
	int ret_val = -EINVAL;
	int status;

	FM_INFO_REPORT("vidioc_set_ext_ctrls: Id = %04x, ctrl_class = %04x",
			ext_ctrl->controls->id, ext_ctrl->ctrl_class);

	if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_FM_TX &&
	    ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
		FM_ERR_REPORT("vidioc_set_ext_ctrls: Unsupported "
			      "ctrl_class = %04x", ext_ctrl->ctrl_class);
		goto error;
	}

	switch (ext_ctrl->controls->id) {
	case V4L2_CID_CG2900_RADIO_RDS_AF_SWITCH_START:
		{
			u32 af_switch_freq;
			u16 af_switch_pi;
			u32 *af_switch_buf;

			if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"V4L2_CID_CG2900_RADIO_RDS_AF_SWITCH_START "
				"Unsupported ctrl_class = %04x",
				ext_ctrl->ctrl_class);
				break;
			}

			if (ext_ctrl->controls->size !=
				FMR_AF_SWITCH_DATA_SIZE ||
				ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"V4L2_CID_CG2900_RADIO_RDS_AF_SWITCH_START "
				"Unsupported ctrl_class = %04x",
				ext_ctrl->ctrl_class);
				break;
			}

			af_switch_buf = (u32 *) ext_ctrl->controls->string;
			af_switch_freq = V4L2_TO_HZ(*af_switch_buf);
			af_switch_pi = *(af_switch_buf + 1);
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
				"V4L2_CID_CG2900_RADIO_RDS_AF_SWITCH_START: "
				"AF Switch Freq =%d Hz AF Switch PI = %04x",
				(int)af_switch_freq, af_switch_pi);

			if (af_switch_freq < (FMR_CHINA_LOW_FREQ_IN_MHZ
				* FMR_HZ_TO_MHZ_CONVERTER) ||
				af_switch_freq > (FMR_CHINA_HIGH_FREQ_IN_MHZ
				* FMR_HZ_TO_MHZ_CONVERTER)) {
				FM_ERR_REPORT("Invalid Freq = %04x",
					af_switch_freq);
				break;
			}

			status = cg2900_fm_af_switch_start(
					af_switch_freq,
					af_switch_pi);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_RDS_TX_DEVIATION:
		{
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_RDS_TX_DEVIATION, "
					"Value = %d",
					ext_ctrl->controls->value);

			if (ext_ctrl->controls->value <= MIN_RDS_DEVIATION &&
			    ext_ctrl->controls->value > MAX_RDS_DEVIATION) {
				FM_ERR_REPORT("Invalid RDS Deviation = %02x",
					ext_ctrl->controls->value);
				break;
			}

			status = cg2900_fm_tx_set_rds_deviation(
					ext_ctrl->controls->value);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_RDS_TX_PI:
		{
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_RDS_TX_PI, PI = %04x",
					ext_ctrl->controls->value);

			if (ext_ctrl->controls->value <= MIN_PI_VALUE &&
			    ext_ctrl->controls->value > MAX_PI_VALUE) {
				FM_ERR_REPORT("Invalid PI = %04x",
					ext_ctrl->controls->value);
				break;
			}

			status = cg2900_fm_tx_set_pi_code(
					ext_ctrl->controls->value);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_RDS_TX_PTY:
		{
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_RDS_TX_PTY, PTY = %d",
					ext_ctrl->controls->value);

			if (ext_ctrl->controls->value < MIN_PTY_VALUE &&
			    ext_ctrl->controls->value > MAX_PTY_VALUE) {
				FM_ERR_REPORT("Invalid PTY = %02x",
					      ext_ctrl->controls->value);
				break;
			}

			status = cg2900_fm_tx_set_pty_code(
					ext_ctrl->controls->value);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_RDS_TX_PS_NAME:
		{
			if (ext_ctrl->controls->size > MAX_PSN_SIZE
			    || ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("Invalid PSN");
				break;
			}

			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_RDS_TX_PS_NAME, "
					"PSN = %s, Len = %x",
					ext_ctrl->controls->string,
					ext_ctrl->controls->size);

			status = cg2900_fm_tx_set_program_station_name(
					ext_ctrl->controls->string,
					ext_ctrl->controls->size);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_RDS_TX_RADIO_TEXT:
		{
			if (ext_ctrl->controls->size >= MAX_RT_SIZE
			    || ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("Invalid RT");
				break;
			}

			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_RDS_TX_RADIO_TEXT, "
					"RT = %s, Len = %x",
					ext_ctrl->controls->string,
					ext_ctrl->controls->size);

			status = cg2900_fm_tx_set_radio_text(
					ext_ctrl->controls->string,
					ext_ctrl->controls->size);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_PILOT_TONE_ENABLED:
		{
			bool enable;
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_PILOT_TONE_ENABLED, "
					"Value = %d",
					ext_ctrl->controls->value);

			if (FMD_PILOT_TONE_ENABLED ==
				ext_ctrl->controls->value)
				enable = true;
			else if (FMD_PILOT_TONE_DISABLED ==
				ext_ctrl->controls->value)
				enable = false;
			else {
				FM_ERR_REPORT("Unsupported Value = %d",
					      ext_ctrl->controls->value);
				break;
			}
			status = cg2900_fm_tx_set_pilot_tone_status(enable);
			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_PILOT_TONE_DEVIATION:
		{
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_PILOT_TONE_DEVIATION, "
					"Value = %d",
					ext_ctrl->controls->value);

			if (ext_ctrl->controls->value <= MIN_PILOT_DEVIATION &&
			    ext_ctrl->controls->value > MAX_PILOT_DEVIATION) {
				FM_ERR_REPORT("Invalid Pilot Deviation = %02x",
					      ext_ctrl->controls->value);
				break;
			}

			status = cg2900_fm_tx_set_pilot_deviation(
					ext_ctrl->controls->value);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_TUNE_PREEMPHASIS:
		{
			u8 preemphasis;
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_TUNE_PREEMPHASIS, "
					"Value = %d",
					ext_ctrl->controls->value);

			if ((V4L2_PREEMPHASIS_50_uS >
					ext_ctrl->controls->value) ||
					(V4L2_PREEMPHASIS_75_uS <
					ext_ctrl->controls->value)) {
				FM_ERR_REPORT("Unsupported Preemphasis = %d",
					      ext_ctrl->controls->value);
				break;
			}

			if (V4L2_PREEMPHASIS_50_uS ==
				ext_ctrl->controls->value) {
				preemphasis = FMD_EMPHASIS_50US;
			} else if (V4L2_PREEMPHASIS_75_uS ==
				ext_ctrl->controls->value) {
				preemphasis = FMD_EMPHASIS_75US;
			}

			status = cg2900_fm_tx_set_preemphasis(preemphasis);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_TUNE_POWER_LEVEL:
		{
			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_TUNE_POWER_LEVEL, "
					"Value = %d",
					ext_ctrl->controls->value);
			if (ext_ctrl->controls->value < MIN_POWER_LEVEL &&
			    ext_ctrl->controls->value > MAX_POWER_LEVEL) {
				FM_ERR_REPORT("Invalid Power Level = %02x",
					      ext_ctrl->controls->value);
				break;
			}

			status = cg2900_fm_tx_set_power_level(
					ext_ctrl->controls->value);

			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_CG2900_RADIO_BLOCKSCAN_START:
		{
			u32 start_freq;
			u32 end_freq;
			u32 *block_scan_buf;
			u32 current_grid;
			u32 low_freq;
			u32 high_freq;
			u32 result_freq;
			u8  no_of_block_scan_channels;

			/* V4L2 Initial check */
			if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"V4L2_CID_CG2900_RADIO_BLOCKSCAN_START "
				"Unsupported ctrl_class = %04x",
				ext_ctrl->ctrl_class);
				break;
			}

			if (ext_ctrl->controls->size !=
				FMR_BLOCK_SCAN_DATA_SIZE ||
				ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"V4L2_CID_CG2900_RADIO_BLOCKSCAN_START "
				"Invalid Parameters");
				break;
			}

			/* Check for current grid */
			if (grid == CG2900_FM_GRID_50)
				current_grid = FMR_CHINA_GRID_IN_HZ;
			else if (grid == CG2900_FM_GRID_100)
				current_grid = FMR_EUROPE_GRID_IN_HZ;
			else
				current_grid = FMR_USA_GRID_IN_HZ;

			/* Check for current band */
			if (band == CG2900_FM_BAND_US_EU) {
				low_freq = FMR_EU_US_LOW_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;
				high_freq = FMR_EU_US_HIGH_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;

			} else if (band == CG2900_FM_BAND_JAPAN) {
				low_freq = FMR_JAPAN_LOW_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;
				high_freq = FMR_JAPAN_HIGH_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;

			} else {
				low_freq = FMR_CHINA_LOW_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;
				high_freq = FMR_CHINA_HIGH_FREQ_IN_MHZ *
					FMR_HZ_TO_MHZ_CONVERTER;
			}

			/* V4L2 Extended control */

			block_scan_buf = (u32 *)ext_ctrl->controls->string;
			start_freq = V4L2_TO_HZ(*block_scan_buf);
			end_freq = V4L2_TO_HZ(*(block_scan_buf + 1));

			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
					"V4L2_CID_CG2900_RADIO_"
					"BLOCKSCAN_START: "
					"Start Freq = %d Hz "
					"End Freq = %d Hz",
					(int)start_freq,
					(int)end_freq);

			result_freq = end_freq - start_freq;
			no_of_block_scan_channels =
				(u8)(result_freq / current_grid);

			/* Frequency Check */
			if (end_freq < start_freq) {
				FM_ERR_REPORT("Start Freq (%d Hz) "
					" > End Freq (%d Hz)",
					(int)start_freq,
					(int)end_freq);
				break;
			}

			if ((start_freq < low_freq) ||
				(start_freq > high_freq)) {
				FM_ERR_REPORT("Out of Band Freq: "
					"Start Freq = %d Hz",
					(int)start_freq);
				break;
			}

			if ((end_freq < low_freq) ||
				(end_freq > high_freq)) {
				FM_ERR_REPORT("Out of Band Freq: "
					"End Freq = %d Hz",
					(int)end_freq);
				break;
			}

			/* Maximum allowed block scan range */
			if (FMR_MAX_BLOCK_SCAN_CHANNELS <
				no_of_block_scan_channels) {
				FM_ERR_REPORT("No of channels (%x)"
					"exceeds Max Block Scan (%x)",
					no_of_block_scan_channels,
					FMR_MAX_BLOCK_SCAN_CHANNELS);
				break;
			}

			status = cg2900_fm_start_block_scan(
					start_freq,
					end_freq);
			if (0 == status) {
				cg2900_device.seekstatus =
					FMR_SEEK_IN_PROGRESS;
				ret_val = 0;
			}
			break;
		}
	case V4L2_CID_CG2900_RADIO_TEST_TONE_CONNECT:
		{
			u8 left_audio_mode;
			u8 right_audio_mode;
			u8 *test_tone_connect_buf;

			/* V4L2 Initial check */
			if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
					"V4L2_CID_CG2900_RADIO_"
					"TEST_TONE_CONNECT "
					"Unsupported ctrl_class = %04x",
					ext_ctrl->ctrl_class);
				break;
			}

			if (ext_ctrl->controls->size !=
				FMR_TEST_TONE_CONNECT_DATA_SIZE ||
				ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
					"V4L2_CID_CG2900_RADIO_TEST"
					"_TONE_CONNECT "
					"Invalid Parameters");
				break;
			}

			/* V4L2 Extended control */
			test_tone_connect_buf =
				(u8 *)ext_ctrl->controls->string;
			left_audio_mode = *test_tone_connect_buf;
			right_audio_mode = *(test_tone_connect_buf + 1);

			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
				"V4L2_CID_CG2900_RADIO_TEST_TONE_CONNECT"
				"left_audio_mode Freq = %02x"
				"right_audio_modeFreq = %02x",
				left_audio_mode,
				right_audio_mode);

			/* Range Check */
			if (left_audio_mode > \
			V4L2_CG2900_RADIO_TEST_TONE_TONE_SUM) {
				FM_ERR_REPORT("Invalid Value of "
					"left_audio_mode (%02x) ",
					left_audio_mode);
				break;
			}

			if (right_audio_mode > \
			V4L2_CG2900_RADIO_TEST_TONE_TONE_SUM) {
				FM_ERR_REPORT("Invalid Value of "
					"right_audio_mode (%02x) ",
					left_audio_mode);
				break;
			}

			status = cg2900_fm_test_tone_connect(
					left_audio_mode,
					right_audio_mode);
			if (0 == status)
				ret_val = 0;
			break;
		}
	case V4L2_CID_CG2900_RADIO_TEST_TONE_SET_PARAMS:
		{
			u8 tone_gen;
			u16 frequency;
			u16 volume;
			u16 phase_offset;
			u16 dc;
			u8 waveform;
			u16 *test_tone_set_params_buf;

			/* V4L2 Initial check */
			if (ext_ctrl->ctrl_class != V4L2_CTRL_CLASS_USER) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"V4L2_CID_CG2900_RADIO_TEST_TONE_SET_PARAMS "
				"Unsupported ctrl_class = %04x",
				ext_ctrl->ctrl_class);
				break;
			}

			if (ext_ctrl->controls->size !=
				FMR_TEST_TONE_SET_PARAMS_DATA_SIZE ||
				ext_ctrl->controls->string == NULL) {
				FM_ERR_REPORT("vidioc_set_ext_ctrls:  "
				"FMR_TEST_TONE_SET_PARAMS_DATA_SIZE "
				"Invalid Parameters");
				break;
			}

			/* V4L2 Extended control */
			test_tone_set_params_buf = \
			(u16 *)ext_ctrl->controls->string;

			tone_gen = (u8)(*test_tone_set_params_buf);
			frequency = *(test_tone_set_params_buf + 1);
			volume = *(test_tone_set_params_buf + 2);
			phase_offset = *(test_tone_set_params_buf + 3);
			dc = *(test_tone_set_params_buf + 4);
			waveform = (u8)(*(test_tone_set_params_buf + 5));

			FM_DEBUG_REPORT("vidioc_set_ext_ctrls: "
				"V4L2_CID_CG2900_RADIO_TEST_TONE_SET_PARAMS"
				"tone_gen = %02x frequency = %04x"
				"volume = %04x phase_offset = %04x"
				"dc = %04x waveform = %02x",
				tone_gen, frequency,
				volume, phase_offset,
				dc, waveform);

			/* Range Check */
			if (tone_gen > FMD_TST_TONE_2) {
				FM_ERR_REPORT("Invalid Value of "
					"tone_gen (%02x) ",
					tone_gen);
				break;
			}

			if (waveform > FMD_TST_TONE_PULSE) {
				FM_ERR_REPORT("Invalid Value of "
					"waveform (%02x) ",
					waveform);
				break;
			}

			if (frequency > 0x7FFF) {
				FM_ERR_REPORT("Invalid Value of "
					"frequency (%04x) ",
					frequency);
				break;
			}

			if (volume > 0x7FFF) {
				FM_ERR_REPORT("Invalid Value of "
					"volume (%04x) ",
					volume);
				break;
			}

			status = cg2900_fm_test_tone_set_params(
					tone_gen,
					frequency,
					volume,
					phase_offset,
					dc,
					waveform);

			if (0 == status)
				ret_val = 0;
			break;
		}
	default:
		{
			FM_ERR_REPORT("vidioc_set_ext_ctrls: "
				      "Unsupported Id = %04x",
				      ext_ctrl->controls->id);
		}
	}
error:
	return ret_val;
}

/**
 * vidioc_set_hw_freq_seek()- seek Up/Down Frequency.
 *
 * This function is used to start seek
 * on the FM Radio. Direction if seek is as inicated by the parameter
 * inside the v4l2_hw_freq_seek structure. This function is called when the
 * application issues the IOCTL VIDIOC_S_HW_FREQ_SEEK
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @freq_seek: v4l2_hw_freq_seek structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_hw_freq_seek(
			struct file *file,
			void *priv,
			struct v4l2_hw_freq_seek *freq_seek
			)
{
	int status;
	int ret_val = -EINVAL;

	FM_INFO_REPORT("vidioc_set_hw_freq_seek");

	FM_DEBUG_REPORT("vidioc_set_hw_freq_seek: Status = %x, "
			"Upwards = %x, Wrap Around = %x",
			cg2900_device.seekstatus,
			freq_seek->seek_upward, freq_seek->wrap_around);

	if (cg2900_device.seekstatus == FMR_SEEK_IN_PROGRESS) {
		FM_ERR_REPORT("vidioc_set_hw_freq_seek: "
				"VIDIOC_S_HW_FREQ_SEEK, "
				"freq_seek in progress");
		goto error;
	}

	spin_lock(&fm_spinlock);
	fm_event = CG2900_EVENT_NO_EVENT;
	no_of_scan_freq = 0;
	spin_unlock(&fm_spinlock);

	if (CG2900_DIR_UP == freq_seek->seek_upward)
		status = cg2900_fm_search_up_freq();
	else if (CG2900_DIR_DOWN == freq_seek->seek_upward)
		status = cg2900_fm_search_down_freq();
	else
		goto error;

	if (0 != status)
		goto error;

	cg2900_device.seekstatus = FMR_SEEK_IN_PROGRESS;
	ret_val = 0;

error:
	FM_DEBUG_REPORT("vidioc_set_hw_freq_seek: returning = %d",
			ret_val);
	return ret_val;
}

/**
 * vidioc_get_audio()- Get Audio features of FM Driver.
 *
 * This function is used to get the audio features of FM Driver.
 * This function is imlemented as a dumy function.
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @audio: (out) v4l2_audio structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_audio(
			struct file *file,
			void *priv,
			struct v4l2_audio *audio
			)
{
	FM_INFO_REPORT("vidioc_get_audio");
	strcpy(audio->name, "");
	audio->capability = 0;
	audio->mode = 0;
	return 0;
}

/**
 * vidioc_set_audio()- Set Audio features of FM Driver.
 *
 * This function is used to set the audio features of FM Driver.
 * This function is imlemented as a dumy function.
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @audio: v4l2_audio structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_audio(
			struct file *file,
			void *priv,
			struct v4l2_audio *audio
			)
{
	FM_INFO_REPORT("vidioc_set_audio");
	if (audio->index != 0)
		return -EINVAL;
	return 0;
}

/**
 * vidioc_get_input()- Get the Input Value
 *
 * This function is used to get the Input.
 * This function is imlemented as a dumy function.
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @input: (out) Value to be stored.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_get_input(
			struct file *file,
			void *priv,
			unsigned int *input
			)
{
	FM_INFO_REPORT("vidioc_get_input");
	*input = 0;
	return 0;
}

/**
 * vidioc_set_input()- Set the input value.
 *
 * This function is used to set input.
 * This function is imlemented as a dumy function.
 *
 * @file: File structure.
 * @priv: Previous data of file structure.
 * @input: Value to set
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int vidioc_set_input(
			struct file *file,
			void *priv,
			unsigned int input
			)
{
	FM_INFO_REPORT("vidioc_set_input");
	if (input != 0)
		return -EINVAL;
	return 0;
}

/**
 * cg2900_convert_err_to_v4l2()- Convert Error Bits to V4L2 RDS format.
 *
 * This function converts the error bits in RDS Block
 * as received from Chip into V4L2 RDS data specification.
 *
 * @status_byte: The status byte as received in RDS Group for
 * particular RDS Block
 * @out_byte: byte to store the modified byte with the err bits
 * alligned as per V4L2 RDS Specifications.
 */
static void cg2900_convert_err_to_v4l2(
			char status_byte,
			char *out_byte
			)
{
	if ((status_byte & RDS_ERROR_STATUS_MASK) == RDS_ERROR_STATUS_MASK) {
		/* Uncorrectable Block */
		*out_byte = (*out_byte | V4L2_RDS_BLOCK_ERROR);
	} else if (((status_byte & RDS_UPTO_TWO_BITS_CORRECTED)
		    == RDS_UPTO_TWO_BITS_CORRECTED) ||
		   ((status_byte & RDS_UPTO_FIVE_BITS_CORRECTED)
		    == RDS_UPTO_FIVE_BITS_CORRECTED)) {
		/* Corrected Bits in Block */
		*out_byte = (*out_byte | V4L2_RDS_BLOCK_CORRECTED);
	}
}

/**
 * cg2900_map_event_to_v4l2()- Maps cg2900 event to v4l2 events .
 *
 * This function maps cg2900 events to corresponding v4l2 events.
 *
 * @event: This contains the cg2900 event to be converted.
 *
 * Returns: Corresponding V4L2 events.
 */
static int cg2900_map_event_to_v4l2(
			u8 event
			)
{
	switch (event) {
	case CG2900_EVENT_MONO_STEREO_TRANSITION:
		return V4L2_CG2900_RADIO_INTERRUPT_MONO_STEREO_TRANSITION;
	case CG2900_EVENT_SEARCH_CHANNEL_FOUND:
		return V4L2_CG2900_RADIO_INTERRUPT_SEARCH_COMPLETED;
	case CG2900_EVENT_SCAN_CHANNELS_FOUND:
		return V4L2_CG2900_RADIO_INTERRUPT_BAND_SCAN_COMPLETED;
	case CG2900_EVENT_BLOCK_SCAN_CHANNELS_FOUND:
		return V4L2_CG2900_RADIO_INTERRUPT_BLOCK_SCAN_COMPLETED;
	case CG2900_EVENT_SCAN_CANCELLED:
		return V4L2_CG2900_RADIO_INTERRUPT_SCAN_CANCELLED;
	case CG2900_EVENT_DEVICE_RESET:
		return V4L2_CG2900_RADIO_INTERRUPT_DEVICE_RESET;
	case CG2900_EVENT_RDS_EVENT:
		return V4L2_CG2900_RADIO_INTERRUPT_RDS_RECEIVED;
	default:
		return V4L2_CG2900_RADIO_INTERRUPT_UNKNOWN;
	}
}

/**
 * cg2900_open()- This function nitializes and switches on FM.
 *
 * This is called when the application opens the character device.
 *
 * @file: File structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int cg2900_open(
			struct file *file
			)
{
	int status;
	int ret_val = -EINVAL;
	struct video_device *vdev = video_devdata(file);

	mutex_lock(&fm_mutex);
	users++;
	FM_INFO_REPORT("cg2900_open: users = %d", users);

	if (users > 1) {
		FM_INFO_REPORT("cg2900_open: FM already switched on!!!");
		ret_val = 0;
		/*
		 * No need to perform the initialization and switch on FM
		 * since it is already done during the first open call to
		 * this driver.
		 */
		goto done;
	}

	status = cg2900_fm_init();
	if (0 != status)
		goto init_error;

	FM_DEBUG_REPORT("cg2900_open: Switching on FM");
	status = cg2900_fm_switch_on(&(vdev->dev));
	if (0 != status)
		goto switch_on_error;

	cg2900_device.state = FMR_SWITCH_ON;
	cg2900_device.frequency = HZ_TO_V4L2(freq_low);
	cg2900_device.rx_rds_enabled = false;
	cg2900_device.muted = false;
	cg2900_device.audiopath = 0;
	cg2900_device.seekstatus = FMR_SEEK_NONE;
	cg2900_device.rssi_threshold = CG2900_FM_DEFAULT_RSSI_THRESHOLD;
	fm_event = CG2900_EVENT_NO_EVENT;
	no_of_scan_freq = 0;
	cg2900_device.fm_mode = CG2900_FM_IDLE_MODE;
	ret_val = 0;
	goto done;

switch_on_error:
	cg2900_fm_deinit();
init_error:
	users--;
done:
	mutex_unlock(&fm_mutex);
	FM_DEBUG_REPORT("cg2900_open: returning %d", ret_val);
	return ret_val;
}

/**
 * cg2900_release()- This function switches off FM.
 *
 * This function switches off FM and releases the resources.
 * This is called when the application closes the character
 * device.
 *
 * @file: File structure.
 *
 * Returns:
 *   0 when no error
 *   -EINVAL: otherwise
 */
static int cg2900_release(
			struct file *file
			)
{
	int status;
	int ret_val = -EINVAL;

	mutex_lock(&fm_mutex);

	FM_INFO_REPORT("cg2900_release");
	if (users <= 0) {
		FM_ERR_REPORT("cg2900_release: No users registered "
			      "with FM Driver");
		goto done;
	}

	users--;
	FM_INFO_REPORT("cg2900_release: users = %d", users);

	if (0 == users) {
		FM_DEBUG_REPORT("cg2900_release: Switching Off FM");
		status = cg2900_fm_switch_off();
		status = cg2900_fm_deinit();
		if (0 != status)
			goto done;

		cg2900_device.state = FMR_SWITCH_OFF;
		cg2900_device.frequency = 0;
		cg2900_device.rx_rds_enabled = false;
		cg2900_device.muted = false;
		cg2900_device.seekstatus = FMR_SEEK_NONE;
		fm_event = CG2900_EVENT_NO_EVENT;
		no_of_scan_freq = 0;
	}
	ret_val = 0;

done:
	mutex_unlock(&fm_mutex);
	FM_DEBUG_REPORT("cg2900_release: returning %d", ret_val);
	return ret_val;
}

/**
 * cg2900_read()- This function is invoked when the application
 * calls read() to receive RDS Data.
 *
 * @file: File structure.
 * @data: buffer provided by application for receving the data.
 * @count: Number of bytes that application wants to read from driver
 * @pos: offset
 *
 * Returns:
 *   Number of bytes copied to the user buffer
 *   -EFAULT: If there is problem in copying data to buffer supplied
 *    by application
 *   -EIO: If the number of bytes to be read are not a multiple of
 *    struct  v4l2_rds_data.
 *   -EAGAIN: More than 22 blocks requested to be read or read
 *    was called in non blocking mode and no data was available for reading.
 *   -EINTR: If read was interrupted by a signal before data was avaialble.
 *   0 when no data available for reading.
 */
static ssize_t cg2900_read(
			struct file *file,
			char __user *data,
			size_t count, loff_t *pos
			)
{
	int current_rds_grp;
	int index = 0;
	int blocks_to_read;
	struct v4l2_rds_data rdsbuf[MAX_RDS_GROUPS * NUM_OF_RDS_BLOCKS];
	struct v4l2_rds_data *rdslocalbuf = rdsbuf;
	struct sk_buff *skb;

	FM_INFO_REPORT("cg2900_read");

	blocks_to_read = (count / sizeof(struct v4l2_rds_data));

	if (!cg2900_device.rx_rds_enabled) {
		/* Remove all Interrupts from the queue */
		skb_queue_purge(&fm_interrupt_queue);
		FM_INFO_REPORT("cg2900_read: returning 0");
		return 0;
	}

	if (count % sizeof(struct v4l2_rds_data) != 0) {
		FM_ERR_REPORT("cg2900_read: Invalid Number of bytes %x "
			      "requested to read", count);
		return -EIO;
	}

	if (blocks_to_read > MAX_RDS_GROUPS * NUM_OF_RDS_BLOCKS) {
		FM_ERR_REPORT("cg2900_read: Too many blocks(%d) "
			      "requested to be read", blocks_to_read);
		return -EAGAIN;
	}

	current_rds_grp = fm_rds_info.rds_group_sent;

	if ((fm_rds_info.rds_head == fm_rds_info.rds_tail) ||
	    (fm_rds_buf[fm_rds_info.rds_tail]
	     [current_rds_grp].block1 == 0x0000)) {
		/* Remove all Interrupts from the queue */
		skb_queue_purge(&fm_interrupt_queue);
		FM_INFO_REPORT("cg2900_read: returning 0");
		return 0;
	}

	spin_lock(&fm_spinlock);
	while (index < blocks_to_read) {
		/* Check which Block needs to be transferred next */
		switch (fm_rds_info.rds_block_sent % NUM_OF_RDS_BLOCKS) {
		case 0:
			(rdslocalbuf + index)->lsb =
			fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block1;
			(rdslocalbuf + index)->msb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block1 >> 8;
			(rdslocalbuf + index)->block =
			    (fm_rds_buf[fm_rds_info.rds_tail]
			     [current_rds_grp].status1
			     & RDS_BLOCK_MASK) >> 2;
			cg2900_convert_err_to_v4l2(
				fm_rds_buf[fm_rds_info.rds_tail]
				[current_rds_grp].status1,
				&(rdslocalbuf + index)->block);
			break;
		case 1:
			(rdslocalbuf + index)->lsb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block2;
			(rdslocalbuf + index)->msb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block2 >> 8;
			(rdslocalbuf + index)->block =
			    (fm_rds_buf[fm_rds_info.rds_tail]
			     [current_rds_grp].status2
			     & RDS_BLOCK_MASK) >> 2;
			cg2900_convert_err_to_v4l2(
				fm_rds_buf[fm_rds_info.rds_tail]
				[current_rds_grp].status2,
				&(rdslocalbuf + index)->block);
			break;
		case 2:
			(rdslocalbuf + index)->lsb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block3;
			(rdslocalbuf + index)->msb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block3 >> 8;
			(rdslocalbuf + index)->block =
			    (fm_rds_buf[fm_rds_info.rds_tail]
			     [current_rds_grp].status3
			     & RDS_BLOCK_MASK) >> 2;
			cg2900_convert_err_to_v4l2(
				fm_rds_buf[fm_rds_info.rds_tail]
				[current_rds_grp].status3,
				&(rdslocalbuf + index)->block);
			break;
		case 3:
			(rdslocalbuf + index)->lsb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block4;
			(rdslocalbuf + index)->msb =
			    fm_rds_buf[fm_rds_info.rds_tail]
			    [current_rds_grp].block4 >> 8;
			(rdslocalbuf + index)->block =
			    (fm_rds_buf[fm_rds_info.rds_tail]
			     [current_rds_grp].status4
			     & RDS_BLOCK_MASK) >> 2;
			cg2900_convert_err_to_v4l2(
				fm_rds_buf[fm_rds_info.rds_tail]
				[current_rds_grp].status4,
				&(rdslocalbuf + index)->block);
			current_rds_grp++;
			if (current_rds_grp == MAX_RDS_GROUPS) {
				fm_rds_info.rds_tail++;
				current_rds_grp = 0;
				/* Dequeue Rds Interrupt here */
				skb = skb_dequeue(&fm_interrupt_queue);
				if (!skb) {
					/* No Interrupt, bad case */
					FM_ERR_REPORT("cg2900_read: "
					"skb is NULL. Major error");
					spin_unlock(&fm_spinlock);
					return 0;
				}
				fm_event = (u8)skb->data[0];
				if (fm_event != CG2900_EVENT_RDS_EVENT) {
					/* RDS interrupt not found */
					FM_ERR_REPORT("cg2900_read:"
					"RDS interrupt not found"
					"for de-queuing."
					"fm_event = %x", fm_event);
					/* Queue the event back */
					skb_queue_head(&fm_interrupt_queue,
						skb);
					spin_unlock(&fm_spinlock);
					return 0;
				}
				kfree_skb(skb);
			}
			break;
		default:
			FM_ERR_REPORT("Invalid RDS Group!!!");
			spin_unlock(&fm_spinlock);
			return 0;
		}
		index++;
		fm_rds_info.rds_block_sent++;
		if (fm_rds_info.rds_block_sent == NUM_OF_RDS_BLOCKS)
			fm_rds_info.rds_block_sent = 0;

		if (!cg2900_device.rx_rds_enabled) {
			/* Remove all Interrupts from the queue */
			skb_queue_purge(&fm_interrupt_queue);
			FM_INFO_REPORT("cg2900_read: returning 0");
			spin_unlock(&fm_spinlock);
			return 0;
		}
	}
	/* Update the RDS Group Count Sent to Application */
	fm_rds_info.rds_group_sent = current_rds_grp;
	if (fm_rds_info.rds_tail == MAX_RDS_BUFFER)
		fm_rds_info.rds_tail = 0;

	spin_unlock(&fm_spinlock);
	if (copy_to_user(data, rdslocalbuf, count)) {
		FM_ERR_REPORT("cg2900_read: Error "
			      "in copying, returning");
		return -EFAULT;
	}
	return count;
}

/**
 * cg2900_poll()- Check if the operation is complete or not.
 *
 * This function is invoked by application on calling poll() and is used to
 * wait till the any FM interrupt is received from the chip.
 * The application decides to read the corresponding data depending on FM
 * interrupt.
 *
 * @file: File structure.
 * @wait: poll table
 *
 * Returns:
 *   POLLRDNORM|POLLIN	whenever FM interrupt has occurred.
 *   0			whenever the call times out.
 */
static unsigned int cg2900_poll(
			struct file *file,
			struct poll_table_struct *wait
			)
{
	int ret_val = 0;

	FM_INFO_REPORT("cg2900_poll");

	/* Check if we have some data in queue already */
	if (skb_queue_empty(&fm_interrupt_queue)) {
		FM_DEBUG_REPORT("cg2900_poll: Interrupt Queue Empty, waiting");
		/* No Interrupt, wait for it to occur */
		poll_wait(file, &cg2900_poll_queue, wait);
	}
	/* Check if we now have interrupt to read in queue */
	if (skb_queue_empty(&fm_interrupt_queue))
		goto done;

	ret_val = POLLIN | POLLRDNORM;

done:
	FM_DEBUG_REPORT("poll_wait returning %d", ret_val);
	return ret_val;
}

/**
 * radio_cg2900_probe()- This function registers FM Driver with V4L2 Driver.
 *
 * This function is called whenever the driver is probed by the device system,
 * i.e. when a CG2900 controller has connected. It registers the FM Driver with
 * Video4Linux as a character device.
 *
 * @pdev: Platform device.
 *
 * Returns:
 *   0 on success
 * -EINVAL on error
 */
static int __devinit radio_cg2900_probe(
			struct platform_device *pdev
			)
{
	int err;

	FM_INFO_REPORT(BANNER);

	err = fmd_set_dev(&pdev->dev);
	if (err) {
		FM_ERR_REPORT("Could not set device %s", pdev->name);
		return err;
	}

	radio_nr = 0;
	grid = CG2900_FM_GRID_100;
	band = CG2900_FM_BAND_US_EU;
	FM_INFO_REPORT("radio_cg2900_probe: radio_nr= %d.", radio_nr);

	/* Initialize the parameters */
	if (video_register_device(
				&cg2900_video_device,
				VFL_TYPE_RADIO,
				radio_nr) == -1) {
		FM_ERR_REPORT("radio_cg2900_probe: video_register_device err");
		return -EINVAL;
	}
	mutex_init(&fm_mutex);
	spin_lock_init(&fm_spinlock);
	init_waitqueue_head(&cg2900_poll_queue);
	skb_queue_head_init(&fm_interrupt_queue);
	users = 0;
	return 0;
}

/**
 * radio_cg2900_remove()- This function removes the FM Driver.
 *
 * This function is called whenever the driver is removed by the device system,
 * i.e. when a CG2900 controller has disconnected. It unregisters the FM Driver
 * from Video4Linux.
 *
 * @pdev: Platform device.
 *
 * Returns: 0 on success
 */
static int __devexit radio_cg2900_remove(
			struct platform_device *pdev
			)
{
	FM_INFO_REPORT("radio_cg2900_remove");
	/* Wake up the poll queue since we are now exiting */
	wake_up_poll_queue();
	/* Give some time for application to exit the poll thread */
	schedule_timeout_interruptible(msecs_to_jiffies(500));

	/* Try to Switch Off FM in case it is still switched on */
	cg2900_fm_switch_off();
	cg2900_fm_deinit();
	skb_queue_purge(&fm_interrupt_queue);
	mutex_destroy(&fm_mutex);
	video_unregister_device(&cg2900_video_device);
	fmd_set_dev(NULL);
	return 0;
}

static struct platform_driver radio_cg2900_driver = {
	.driver = {
		.name	= "cg2900-fm",
		.owner	= THIS_MODULE,
	},
	.probe	= radio_cg2900_probe,
	.remove	= __devexit_p(radio_cg2900_remove),
};

/**
 * radio_cg2900_init() - Initialize module.
 *
 * Registers platform driver.
 */
static int __init radio_cg2900_init(void)
{
	FM_INFO_REPORT("radio_cg2900_init");
	return platform_driver_register(&radio_cg2900_driver);
}

/**
 * radio_cg2900_exit() - Remove module.
 *
 * Unregisters platform driver.
 */
static void __exit radio_cg2900_exit(void)
{
	FM_INFO_REPORT("radio_cg2900_exit");
	platform_driver_unregister(&radio_cg2900_driver);
}

void wake_up_poll_queue(void)
{
	FM_INFO_REPORT("wake_up_poll_queue");
	wake_up_interruptible(&cg2900_poll_queue);
}

void cg2900_handle_device_reset(void)
{
	struct sk_buff *skb;
	FM_INFO_REPORT("cg2900_handle_device_reset");
	skb = alloc_skb(SKB_FM_INTERRUPT_DATA, GFP_KERNEL);
	if (!skb) {
		FM_ERR_REPORT("cg2900_handle_device_reset: "
			"Unable to Allocate Memory");
		return;
	}
	skb->data[0] = CG2900_EVENT_DEVICE_RESET;
	skb->data[1] = true;
	skb_queue_tail(&fm_interrupt_queue, skb);
	wake_up_poll_queue();
}

module_init(radio_cg2900_init);
module_exit(radio_cg2900_exit);
MODULE_AUTHOR("Hemant Gupta");
MODULE_LICENSE("GPL v2");

module_param(radio_nr, int, S_IRUGO);

module_param(grid, int, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(grid, "Grid:"
			"0=50 kHz"
			"*1=100 kHz*"
			"2=200 kHz");

module_param(band, int, S_IRUGO | S_IWUSR | S_IWGRP);
MODULE_PARM_DESC(band, "Band:"
			"*0=87.5-108 MHz*"
			"1=76-90 MHz"
			"2=70-108 MHz");