summaryrefslogtreecommitdiff
path: root/drivers/media/radio/stfm1000/stfm1000-rds.c
blob: 5f63052d00c2903b53754ebc3f52fcff73fd440b (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
/*
 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/init.h>

#include "stfm1000.h"

#include "stfm1000-rds.h"

#define bitstream_to_rds_state(b)	\
	container_of(b, struct stfm1000_rds_state, bitstream)
#define demod_to_rds_state(d)		\
	container_of(d, struct stfm1000_rds_state, demod)
#define pkt_to_rds_state(p)		\
	container_of(p, struct stfm1000_rds_state, pkt)
#define text_to_rds_state(t)		\
	container_of(t, struct stfm1000_rds_state, text)
#define rds_state_to_stfm1000(r)	\
	container_of(r, struct stfm1000, rds_state)

#define TADJSH 8 /*Shifts used in bitslice loop filter */

/* Reverse of Matlab's Fquant (see MatchedFilterDecomposition.m), so that */
/* mixandsum code is easy;   Used by rds_bitstream_stfmdemod.arm */
const s16 u16_rds_basis[2*RDS_BASISLENGTH+8] = {
	14, 24, 34, 43, 50, 56, 60, 62, 62,
	60, 55, 49, 41, 32, 22, 11, 14, 24,
	34, 43, 50, 56, 60, 62, 62, 60, 55,
	49, 41, 32, 22, 11, 14, 24, 34, 43,
	50, 56, 60, 62
};

static int bits_free(struct stfm1000_rds_bitstream *rdsb)
{
	/* Do not show the last one word free. */
	int FreeSpace = rdsb->TailBitCount - rdsb->HeadBitCount - 32;

	if (FreeSpace < 0)
		FreeSpace = (RDS_BITBUFSIZE * 32) + FreeSpace;
	return FreeSpace;
}

static void put1bit(struct stfm1000_rds_bitstream *rdsb, int bit)
{
	int index = (rdsb->HeadBitCount >> 5);
	u32 CurBit = (rdsb->HeadBitCount & 0x1f);
	u32 CurWord = rdsb->buf[index];

	if (CurBit == 0)
		CurWord = 0;

	CurWord = CurWord | (((u32)bit & 1) << CurBit);
	rdsb->buf[index] = CurWord;
	rdsb->HeadBitCount++;
	if (rdsb->HeadBitCount >= RDS_BITBUFSIZE * 32)
		rdsb->HeadBitCount = 0;
}

static int get1bit(struct stfm1000_rds_bitstream *rdsb)
{
	int Bit = 0;
	int index = (rdsb->TailBitCount >> 5);
	int CurBit = (rdsb->TailBitCount & 0x1f);
	u32 CurWord = rdsb->buf[index];

	Bit = (CurWord >> CurBit) & 1;
	rdsb->TailBitCount++;
	if (rdsb->TailBitCount == RDS_BITBUFSIZE*32)
		rdsb->TailBitCount = 0;

	return Bit;
}

static int bits_filled(struct stfm1000_rds_bitstream *rdsb)
{
	int FilledSpace = rdsb->HeadBitCount - rdsb->TailBitCount;

	if (FilledSpace < 0)
		FilledSpace = (RDS_BITBUFSIZE * 32) + FilledSpace;
	return FilledSpace;
}

static void rds_mix_msg(struct stfm1000_rds_demod *rdsd, u8 MixSetting)
{
	if (rdsd->mix_msg_pending)
		rdsd->mix_msg_overrun++;
	rdsd->mix_msg = MixSetting;
	rdsd->mix_msg_pending = 1;

	/* signal monitor thread */
	stfm1000_monitor_signal(
		rds_state_to_stfm1000(demod_to_rds_state(rdsd)),
		EVENT_RDS_MIXFILT);
}

/* call with interrupts disabled please */
int stfm1000_rds_mix_msg_get(struct stfm1000_rds_state *rds)
{
	struct stfm1000_rds_demod *rdsd = &rds->demod;

	if (!rdsd->mix_msg_pending)
		return -1;

	return rdsd->mix_msg;
}

/* call with interrupts disabled please */
int stfm1000_rds_mix_msg_processed(struct stfm1000_rds_state *rds, int mix_msg)
{
	struct stfm1000_rds_demod *rdsd = &rds->demod;

	if (!rdsd->mix_msg_pending)
		return -1;

	rdsd->mix_msg_pending = 0;

	/* update the completion indication bit */
	if ((mix_msg & 0x8) == 0)
		rdsd->MixPopDone = 1;

	/* this is reflected off the hardware register */
	rdsd->rds_mix_offset = mix_msg & 1;

	if (rdsd->mix_msg != mix_msg) {
		rdsd->mix_msg_processed_changed++;
		return -1;
	}
	return 0;
}

static void rds_sdnominal_msg(struct stfm1000_rds_demod *rdsd, int sdnominal)
{
	if (rdsd->sdnominal_msg_pending)
		rdsd->sdnominal_msg_overrun++;
	rdsd->sdnominal_msg = sdnominal;
	rdsd->sdnominal_msg_pending = 1;

	/* signal monitor thread */
	stfm1000_monitor_signal(
		rds_state_to_stfm1000(demod_to_rds_state(rdsd)),
		EVENT_RDS_SDNOMINAL);
}

/* call with interrupts disabled please */
int stfm1000_rds_sdnominal_msg_get(struct stfm1000_rds_state *rds)
{
	struct stfm1000_rds_demod *rdsd = &rds->demod;

	if (!rdsd->sdnominal_msg_pending)
		return 0;

	return rdsd->sdnominal_msg;
}

/* call with interrupts disabled please */
int stfm1000_rds_sdnominal_msg_processed(struct stfm1000_rds_state *rds,
	int sdnominal_msg)
{
	struct stfm1000_rds_demod *rdsd = &rds->demod;

	if (!rdsd->sdnominal_msg_pending)
		return -1;

	rdsd->sdnominal_msg_pending = 0;
	return 0;
}

void demod_loop(struct stfm1000_rds_bitstream *rdsb,
	struct stfm1000_rds_demod *rdsd)
{
	s32 filter_out;
	u32 freeSpace;
	s32 decomp_hist_pp;
	u8 phase;

	/* Check if we're at a half-basis point */
	if ((rdsd->i & (RDS_BASISLENGTH/2 - 1)) != 0)
		return;	/* Nope, return */

	/* Yes, time to do our work */
	/* Rotate the length 3 history buffer */
	decomp_hist_pp = rdsd->decomp_hist_p;
	rdsd->decomp_hist_p = rdsd->decomp_hist;
	if ((rdsd->i & (RDS_BASISLENGTH-1)) == 0) {
		rdsd->decomp_hist = rdsd->mixandsum1>>9; /* Grab output of
							  * mixandsum1/512 */
		rdsd->mixandsum1 = 0;	 /* Reset mixandsum #1 */
	} else {
		rdsd->decomp_hist = rdsd->mixandsum2>>9; /*Grab output of
							  * mixandsum2/512 */
		rdsd->mixandsum2 = 0;	/* Reset mixandsum #2 */
	}

	/* Form correlator/decimator output by convolving with the
	 * decomposition coefficients, DecompQuant from Matlab work. */
	filter_out = (-58*rdsd->decomp_hist + 59*decomp_hist_pp)>>7;

	/*Figure out which half-basis we are in (out of a bit-length cycle) */
	phase = rdsd->i*2/RDS_BASISLENGTH;
	/*Now what we do depends on the phase variable */
	/*Phase 0:      Bitslice and do timing alignment */
	/*others (1-3): Keep value for timing alignment */

	if (phase == 0) {   /*Main processing (bitslice) */
		u32 Ph;
		u8 OldBit = rdsd->sliced_data;	/* Save the previous value */

		rdsd->return_num = 1;
		if (filter_out >= 0) { /*This bit is "1" */
			/*return value is XOR of previous bit (still in
			 * sliced_data) w/ this */
			/*  bit (1), which equals (NOT of the previous bit) */
			rdsd->return_rdsdemod = !OldBit;
			rdsd->sliced_data = 1;	/*Newest bit value is 1 */
		} else { /*This bit is "0" */
			/*return value is XOR of previous bit (still in
			 * sliced_data) w/ this */
			/*  bit (0), which equals the previous bit */
			rdsd->return_rdsdemod = OldBit;
			rdsd->sliced_data = 0;	/*Newest bit value is 0 */
		}

		freeSpace = bits_free(rdsb);

		if (freeSpace > 0)
			put1bit(rdsb, rdsd->return_rdsdemod);
		else
			rdsd->RdsDemodSkippedBitCnt++;

		/*Increment bits received counter */
		rdsd->BitAlignmentCounter++;
		/*If mixer phase determination hasn't been done, start it */
		if ((rdsd->MixPhaseState == 0) && (!rdsd->MixPhaseDetInProg)) {
			rdsd->MixPhaseDetInProg = 1;
			/*Go to first mixer setting (0) */
			rds_mix_msg(rdsd, 0);
		}

		/* Do bit-slicing time adaption after the mixer phase
		 * determination */
		if (!(rdsd->MixPhaseDetInProg) && !(rdsd->Synchronous)) {

			/* Bitslice Timing Adjust Code (runs after
			 * MixPhaseDetInProg and if RDS is not synchronous to
			 * the FM pilot. */

			u8 BigPh2;	/* Expecting a large value in
					 * PhaseValue[2] */
			u32 MaxRMS = 0;	/*Largest phase RMS */
			s8 MaxPh = 0;	/*Index of largest phase RMS */
			s32 zerocross;

			/* Locate the largest phase RMS
			 * (should be at phase zero) */
			for (Ph = 0; Ph < 4; Ph++)
				if (rdsd->Ph_RMS[Ph] > MaxRMS) {
					MaxRMS = rdsd->Ph_RMS[Ph];
					MaxPh = Ph;
				}

			/* During each bit time we expect the four phases to
			 * take one of the following patterns, where 1
			 * corresponds to maximum modulation:
			 *   1,    0,  -1,    0			Case I
			 *  -1,    0,   1,    0			Case II
			 *   1,  1/2,   0, -1/2			Case III
			 *  -1, -1/2,   0,  1/2			Case IV
			 * We need to distinguish between cases in order to do
			 * the timing adjustment. Below we compare the
			 * correlation of the samples with Case I and Case III
			 * to see which has a bigger abs(correlation).  Thus
			 * BigPh2, if set, means that we decided on Case I or
			 * Case II; if BigPh2 clear, we decided Case III or IV.
			 */
			BigPh2 = abs(rdsd->PhaseValue[0]-rdsd->PhaseValue[2]) >
				 abs(rdsd->PhaseValue[0] +
				 ((rdsd->PhaseValue[1]-
					rdsd->PhaseValue[3])>>1));
			/* If BigPh2, use the difference between phase 1 value
			 * (downgoing for Case I, upgoing for Case II) and
			 * phase 3 value (upgoing for Case I, downgoing for
			 * Case II, thus the subtraction) to indicate timing
			 * error.  If not BigPh2, use the sum of the phase 1
			 * value (downgoing for Case III, upgoing for Case IV)
			 * and phase 3 value (downgoing for Case III, upgoing
			 * for Case IV, thus the addition) to indicate timing
			 * error.  If BigPh2, the slopes at phase 1 & phase 3
			 * are approximately double that if not BigPh2.
			 * Since we are trying to measure timing, scale
			 * by 1/2 in the BigPh2 case. */
			if (BigPh2)
				zerocross = (rdsd->PhaseValue[1]-
					rdsd->PhaseValue[3])>>1;
			else
				zerocross = rdsd->PhaseValue[1]+
					rdsd->PhaseValue[3];
			/* Now if the prev bit was a "1", then the first zero
			 * crossing (phase 1 if BigPh2, phase 2 if !BigPh2)
			 * was a falling one, and if we were late then
			 * zerocross should be negative. If the prev bit was a
			 * "0", then the first zero crossing was a rising one,
			 * and if we were late then zerocross would be
			 * positive. If we are "late" it means that we need to
			 * do a shorter cycle of, say, 15 samples instead of
			 * 16, to "catch up" so that in the future we will be
			 * sampling earlier.  We shorten the cycle by adding
			 * to i, so "late" is going to mean "increment i".
			 * Therefore "late" should be positive, which is done
			 * here by inverting zerocross if the previous bit was
			 * 1.  You could say that this step reflects cases I
			 * and III into II and IV, respectively. */
			if (OldBit)
				zerocross = -zerocross;
			if (!rdsd->DisablePushing) {
				/*The algorithm so far has a stable operating
				 * point 17 phases away from the correct one.
				 * The following code is experimental and may
				 * be deleterious in low SNR conditions, but is
				 * an attempt to move off of the incorrect
				 * operating point. */

				if (MaxPh != 0) {
					/* If it isn't the same MaxPh as the
					 * last non-zero one, clear the counter
					 */
					if (MaxPh != rdsd->PushLastMaxPh) {
						/*Reset the counter */
						rdsd->PushCounter = 0;
						/*Record which phase we're now
						 * counting */
						rdsd->PushLastMaxPh = MaxPh;
					}
					/* If the Max RMS is on the same
					 * non-zero phase, count up */
					rdsd->PushCounter++;
				}
				/* Once every 128 bits, check and then reset
				 * PushCounter */
				if (!(rdsd->BitAlignmentCounter & 0x0FF)) {
					/*If 90% of the time the max phase has
					 * been from the same non-zero phase,
					 * decide that we are latched onto a 0
					 * lock point. Do a large push of the
					 * timing. */
					if (rdsd->PushCounter > 230) {
						s32 pshiph;
						/*Convert from phase number to
						 * the number of filter
						 *  output samples that we need
						 *  to shift */
						if (rdsd->PushLastMaxPh >= 2)
							pshiph =
								4 - (s8)rdsd->
								PushLastMaxPh;
						else
							pshiph =
								-(s8)rdsd->
								PushLastMaxPh;
						/* Scale by the number of i-
						 * phases per output sample */
						pshiph <<=
							RDS_BASISSHIFTS-1;
						/* Perform big pop to get near
						 * correct timing */
						rdsd->i += (RDS_BASISLENGTH<<1)
							+ pshiph;
						/* Set status indicating big
						 * pop was needed.  Reset all
						 * leaky-bucket and summation
						 * variables because the big
						 * timing shift has invalidated
						 * them. Ph_RMS values don't
						 * need to be reset because
						 * they will shift over to
						 * reasonable values again
						 * before their erroneous
						 * values could have effect. */
						rdsd->rds_big_timeshift = 1;
						/*rdsd->Ph_RMS[0] = 0; */
						/*rdsd->Ph_RMS[1] = 0; */
						/*rdsd->Ph_RMS[2] = 0; */
						/*rdsd->Ph_RMS[3] = 0; */
						rdsd->mixandsum1 = 0;
						rdsd->mixandsum2 = 0;
						rdsd->SkipsAccum +=
							pshiph;

						/* Make adjustments in other
						 * values because of the push
						 * (they wouldn't otherwise be
						 * able to use the information
						 * that a push was needed in
						 * their future control
						 * decisions). */
						if (rdsd->PushLastMaxPh != 2) {
							/* If we weren't
							 * pushing from phase
							 * two, accumulate (for
							 * use in adapting
							 * SDNOMINAL) the
							 * phases moved by
							 * pushing. Phase two
							 * pushes are not used;
							 * the push direction
							 * is arbitrary since
							 * Phase 2 is 180
							 * degrees out.  Also,
							 * phase 2 pushes don't
							 * result from
							 * reasonable slippage.
							 * */

							if (rdsd->sdnom_adapt)
								rdsd->SdnomSk
								+= pshiph;

							/* Modify timing_adj to
							 * account for half of
							 * the DC response that
							 * would have occurred
							 * in timing_adj if
							 * that control loop
							 * had seen the push
							 * happen. (Why half?
							 * Because the loop has
							 * already seen a
							 * history of zerocross
							 * values that heads it
							 * in the same
							 * direction as this
							 * adjustment, but may
							 * have seen as few as
							 * half of what it
							 * should have.) */
							rdsd->timing_adj +=
								pshiph <<
								(TADJSH+1);
						}
						/*Set countdown timer that will
						 * prevent any mixer popping
						 * until the Ph_RMS variables
						 * have had enough time to
						 * stabilize */

						/* 2.5 time constants */
						rdsd->PushSafetyZone = 5;
					}
					/*Reset the push counter */
					rdsd->PushCounter = 0;
				}  /*end once every 128 bits */
			}  /*end if !DisablePushing */

			/* Further possible additions:
			 *
			 * 1. Pushes modify timing_adj to decrease convergence
			 *    time.
			 * 2. Separate timing_adj into pilottracking and non-pt
			 *    cases (avoids convergence time after stereo/mono
			 *    transitions)
			 *
			 * Old loop filter was a leaky bucket integrator, and
			 * it always lagged behind if the FM station had RDS
			 * asynchronous to the pilot, because the control loop
			 * needs another integrator to converge on a frequency
			 * error.
			 * New loop filter = 1/(1-1/z) * (a-1/z) * k,
			 * where a = 1+1/256 and k = 1/1024.
			 * You can narrow the loop bandwidth by making "a"
			 * twice as close to 1 and halving k, e.g. a = 1+1/512
			 * and k = 1/2048.
			 * (The way implemented, that narrowing loop BW by
			 * a factor of 2 can be done by incrementing TADJSH.)
			 *
			 * TGR 8/31/2007 */

			/*Integrator, 1/(1-1/z) */
			rdsd->timing_adj += zerocross;
			/*Limit to 1 phase every 8 samples */
			if (rdsd->SkipSafetyZone) {
				rdsd->SkipSafetyZone--;
				rdsd->sampskip = 0;
			} else {
				/*sampskip of non-zero is allowed,
				 * calculate what it really is */

				/*Saturate timing_adj to 2's comp
				 * (2*TADJSH+4)-bit range. */
				if (rdsd->timing_adj > (1<<(2*TADJSH+3))-1)
					rdsd->timing_adj = (1<<(2*TADJSH+3))-1;
				if (rdsd->timing_adj < -(1<<(2*TADJSH+3)))
					rdsd->timing_adj = -(1<<(2*TADJSH+3));

				/* Zero, implemented after the integrator
				 * output.
				 * (a-1/z) = (1+1/256) - 1/z = (1-1/z) + 1/256.
				 * But (1 - 1/z) is timing_adj-
				 * prev_timing_adj = zerocross. */
				rdsd->sampskip = zerocross	/* 1 - 1/z */
					/* 1/256 (with rounding) */
					+ ((rdsd->timing_adj
						+ (1<<(TADJSH-1)))>>TADJSH);
				/*Round and apply k */
				rdsd->sampskip += (1<<(TADJSH+1));
				rdsd->sampskip >>= (TADJSH+2);
				/*Limit to [-1,+1] inclusive */
				if (rdsd->sampskip > 1)
					rdsd->sampskip = 1;
				if (rdsd->sampskip < -1)
					rdsd->sampskip = -1;
				/* If non-zero, start the skip safety zone,
				 * which excludes more sample skipping for a
				 * while.  Note that the safety zone only
				 * applies to the skips -- pushes can still
				 * happen inside a SkipSafetyZone. */
				if (rdsd->sampskip)
					rdsd->SkipSafetyZone = 8-1;
			}
			/**********************************************
			* End Timing Adjust Code
			**********************************************/

			/**********************************************
			* Begin Phase Popper Code
			**********************************************/
			/* If Phase Popping is enabled and 1/2 of a
			 * time constant has gone by... */
			if (rdsd->PhasePoppingEnabled &&
				!(rdsd->BitAlignmentCounter &
					((1<<(RMSALPHASHIFTS-1))-1))) {

				u8 ForcePop = 0; /* Used to force a pop */

				/*Record the maximum of the envelope */
				if (MaxRMS > rdsd->PhasePopMaxRMS)
					rdsd->PhasePopMaxRMS = MaxRMS;
				/* Also track MaxRMS into MixPhase0/1Mag, so
				 * that we can see what the largest RMS on each
				 * of those phases is.  On synchronous stations
				 * (meaning the RDS carrier and bit rate are
				 * synchronized with the pilot), the right mix
				 * phase will always be big and the wrong phase
				 * small. On asynchronous stations (and
				 * stations without RDS), both phases will at
				 * some time or other have about the
				 * same amplitude on each of the phases. */
				if (rdsd->rds_mix_offset) {
					if (MaxRMS > rdsd->MixPhase1Mag)
						rdsd->MixPhase1Mag = MaxRMS;
				} else {
					if (MaxRMS > rdsd->MixPhase0Mag)
						rdsd->MixPhase0Mag = MaxRMS;
				}
				/* Update PopSafetyZone and PushSafetyZone
				 * counters.  With RMSALPHASHIFTS = 5, each
				 * tick is 16/1187.5 =~ 13.5 ms. */
				if (rdsd->PopSafetyZone) {
					rdsd->PopSafetyZone--;
					/* If safety zone just ended and this
					 * mix phase is giving smaller RMS than
					 * before the pop, then the pop was a
					 * mistake.  Go back to previous mixer
					 * phase */
					if (!(rdsd->PopSafetyZone)
						&& (rdsd->PhasePopMaxRMS <
							rdsd->PrePopRMS))
						ForcePop = 1;
				}
				/* If there is no recent push, and Phase 0 has
				 * the maximum RMS, and at least 1/7th of a
				 * second has passed since the last phase pop,
				 * and ((the RMS is less than 1/2 of
				 * PhasePopMaxRMS) or (the RMS is less than
				 * 100)), then try a phase pop. */
				if (/* (rdsd->Ph_RMS[0] == MaxRMS) &&
						* Phase 0 has maximum RMS  */
					!(rdsd->PopSafetyZone))	{
					 /* and Long enough since last
					  * phase pop */

					/* Eligible for a pop, see if one of
					 * the pop conditions is met */
					if ((MaxRMS<<1) <
							rdsd->PhasePopMaxRMS) {
						/*RMS decline from its peak */
						ForcePop = 1;
					} else if ((MaxRMS>>RMSALPHASHIFTS)
						< 50) {
						/*RMS too small to receive,
						 * either there's no RDS or
						 * this is the wrong phase */
						ForcePop = 1;
					}
				}
				if (ForcePop) {

					/*Pop to opposite setting */
					rds_mix_msg(rdsd, 0x8 |
						!rdsd->rds_mix_offset);

					/*Save the pre-pop RMS so that later we
					 * can see if the pop was actually
					 * effective */
					rdsd->PrePopRMS = MaxRMS;
					/*Reset the PhasePopMaxRMS.  We rely on
					 * the PopSafetyZone to give time to
					 * get a new valid max RMS before we're
					 * eligible for the next phase pop.  If
					 * there were no reset we'd be forever
					 * incrementing PhasePopMaxRMS due
					 * to just happenstance large-noise
					 * samples and it might eventually get
					 * some freakish large value causing
					 * frequent erroneous pops. */
					rdsd->PhasePopMaxRMS = 0;
					/* Pop Safety zone length is decided by
					 * how much of an asynchronous
					 * frequency can be supported. Allowing
					* 50 ppm of transmitter error (error
					* between their own pilot, that we
					* should be locked to, and their RDS
					* carrier (which by RDS spec should be
					* locked to their pilot, but we've
					* recently found frequently isn't).
					* 50ppm * 57kHz = 2.85Hz.
					* (2.85 cycles/sec)(4 pops/cycle)
					* = 11.4 pops/second.
					* Safety zone = (1/11.4) seconds =~ 104
					* bits, round down to 96 bits which
					* yields 6 ticks if RMSALPHASHIFTS = 5.
					* */
					rdsd->PopSafetyZone = 96>>
						(RMSALPHASHIFTS-1);
				}
			}
			/******************************************************
			* End Phase Popper Code
			******************************************************/

			/* SDNOMINAL adaption */
			if (rdsd->sdnom_adapt) {
				rdsd->SdnomSk += rdsd->sampskip;
				if (rdsd->pCoefForcedMono &&
					(rdsd->BitAlignmentCounter & 0xFFF) ==
						0x800) {

					rds_sdnominal_msg(rdsd,
						-(rdsd->SdnomSk<<9));

					/*Reset skips counter */
					rdsd->SdnomSk = 0;
				}
			}

			rdsd->SkipsAccum += rdsd->sampskip;
			/* Once per 3.45 seconds, print out signal strength,
			 * skips and pops. Then reset the variables totalling
			 * those occurrences */
			if (!(rdsd->BitAlignmentCounter & 0xFFF)) {
				/* During very noisy input (or if no RDS, or no
				 * station present), timing_adj can go crazy,
				 * since it is the integral of noise.  Although
				 * it is a saturated value (earlier, in the
				 * timing adjust code), the level at which we
				 * can saturate still leaves room for
				 * timing_adj to get too big.  A large value of
				 * timing_adj is a persistent pathology because
				 * the phase is shifting so quickly that the
				 * push detector (which relies on stable
				 * phase-RMS values) never triggers, thus there
				 * is no implemented rescue besides this
				 * clearing that restores proper function. */
				if (abs(rdsd->SkipsAccum) > 300)
					rdsd->timing_adj = 0;
				/*Reset the accumulations. */
				rdsd->SkipsAccum = 0;
			}
		} /*End of bit timing adaption */

		/* If mixer phase determination in progress,
		 * perform actions at certain times */
		if (rdsd->MixPhaseDetInProg) {
			/*~10ms settling time after mixer phase change */
			#define MIXPHASE_STARTMEAS 12
			/*~20ms measurement window */
			#define MIXPHASE_ENDMEAS (MIXPHASE_STARTMEAS+24)
			if (rdsd->BitAlignmentCounter == MIXPHASE_STARTMEAS) {
				/*Reset the RMS variables */
				rdsd->Ph_RMS[0] = 0;
				rdsd->Ph_RMS[1] = 0;
				rdsd->Ph_RMS[2] = 0;
				rdsd->Ph_RMS[3] = 0;
				/* Don't reset mixandsum values because at
				 * least they have filtered continuously.  All
				 * we really need for the mixer phase decision
				 * is a constant measurement window. */
			} else if (rdsd->BitAlignmentCounter ==
					MIXPHASE_ENDMEAS) {
				/*Measurement = mean of RMS values */
				u32 Ndx, MeasVal = 0;
				for (Ndx = 0; Ndx < 4;
					MeasVal += rdsd->Ph_RMS[Ndx++]>>2);
				/*Store measurement in correct place */
				if (rdsd->MixPhaseState == 1) {
					rdsd->MixPhase0Mag = MeasVal;
					/*Go to next mixer setting */
					rds_mix_msg(rdsd, 1);
				} else if (rdsd->MixPhaseState == 2) {
					u8 NextMixSetting;
					rdsd->MixPhase1Mag = MeasVal;
					/* Both measurements done now, see what
					 * mixer setting we need to use.
					 * 0 if MixPhase0Mag > MixPhase1Mag,
					 * 1 otherwise. */
					NextMixSetting = (rdsd->MixPhase0Mag
						<= rdsd->MixPhase1Mag);
					/* If the mixer setting needed is 1,
					 * that is already the current setting.
					 * Terminate mixer phase determination.
					 * Otherwise send message to switch the
					 * mixer phase setting. */
					if (NextMixSetting) {
						rdsd->MixPhaseState = 3;
						rdsd->MixPhaseDetInProg = 0;
					} else
						rds_mix_msg(rdsd, 0);
				}
			}
			/* Reset BitAlignmentCounter if the Mixer just popped
			 * Change state, if required.  States are:
			 * 0: Initial state, send msg causing RDS_MIXOFFSET=>0
			 * 1: Measure with RDS_MIXOFFSET = 0.
			 * 	Lasts just over 30 ms.
			 * 2: Measure with RDS_MIXOFFSET = 1.
			 * 	Lasts just over 30 ms.
			 * 3: At final RDS_MIXOFFSET value.
			 * 	Lasts as long as RDS continues. */
			if (rdsd->MixPopDone) {
				rdsd->MixPopDone = 0;
				rdsd->BitAlignmentCounter = 0;
				rdsd->MixPhaseState++;	/*Go to next state */
				/* If we got to state 3, turn off mixer phase
				 * determination code */
				if (rdsd->MixPhaseState == 3)
					rdsd->MixPhaseDetInProg = 0;
			}
		}

		/* Update status variables */
		rdsd->RDS_BIT_AMP_STAT_REG9 = rdsd->Ph_RMS[0]>>RMSALPHASHIFTS;
		/*Saturate */
		if (rdsd->RDS_BIT_AMP_STAT_REG9 > 511)
			rdsd->RDS_BIT_AMP_STAT_REG9 = 511;
	}  /*End phase 0 code */

	/***************************************************
	* Actions common to all phases
	***************************************************/

	/* Save the output of each phase for possible
	 * calculations during phase 0 */
	rdsd->PhaseValue[phase] = filter_out;

	/*So that we can measure signal amplitude and/or determine what (if */
	/*  any) big jump is needed, maintain the RMS of each phase.  Phase */
	/*  0 RMS is already in Ph_RMS[0] (see bitslicing code, earlier). */
	rdsd->Ph_RMS[phase] += abs(filter_out) -
		(rdsd->Ph_RMS[phase]>>RMSALPHASHIFTS);
}

#if defined(CONFIG_ARM)

/* assembly version for ARM */
#define RDS_MAC(_acc, _x, _y) \
	__asm__ __volatile__ (			 \
	"smlabb	%0, %1, %2, %0\n"		 \
	: "=&r" (_acc)				 \
	: "r" (_x), "r" (_y)			 \
	: "cc")

#else

/* all others, use standard C */
#define RDS_MAC(_acc, _x, _y) 			 \
	do {					 \
		(_acc) += (s16)(_x) * (s16)(_y); \
	} while (0)

#endif

static void rds_demod(const u16 *data, struct stfm1000_rds_demod *rdsd,
	struct stfm1000_rds_bitstream *rbit, int total)
{
	register const s16 *basis0;
	register const s16 *basis1;
	register s16 val;
	register int i;
	register int sampskip;
	register s32 acc1;
	register s32 acc2;

	/* point to the table */
	basis0 = u16_rds_basis;
	basis1 = basis0 + 8;

	rdsd->return_num = 0;

	/* restore state */
	i = rdsd->i;
	acc1 = rdsd->mixandsum1;
	acc2 = rdsd->mixandsum2;	/* 64 bit */
	sampskip = rdsd->sampskip;

	while (total-- > 0) {

		val = data[3];	/* load RDS data */
		data += 4;
		if (val == 0x7fff)	/* illegal RDS sample */
			continue;

		RDS_MAC(acc1, val, basis0[i]);
		RDS_MAC(acc2, val, basis1[i]);

		if (i == 4) {
			i += sampskip;
			sampskip = 0;
		}

		if ((i & (RDS_BASISLENGTH / 2 - 1)) == 0) {

			/* save state */
			rdsd->mixandsum1 = acc1;
			rdsd->mixandsum2 = acc2;
			rdsd->i = i;
			rdsd->sampskip = sampskip;

			demod_loop(rbit, rdsd);

			/* restore state */
			acc1 = rdsd->mixandsum1;
			acc2 = rdsd->mixandsum2;
			i = rdsd->i;
			sampskip = rdsd->sampskip;
		}
		i = (i + 1) & 31;
	}

	/* save state */
	rdsd->mixandsum1 = acc1;
	rdsd->mixandsum2 = acc2;
	rdsd->i = i;
	rdsd->sampskip = sampskip;
}

void stfm1000_rds_demod(struct stfm1000_rds_state *rds, const u16 *dri_data,
	int total)
{
	rds_demod(dri_data, &rds->demod, &rds->bitstream, total);

	/* signal only when we have enough */
	if (bits_filled(&rds->bitstream) > 128)
		stfm1000_monitor_signal(rds_state_to_stfm1000(rds),
				EVENT_RDS_BITS);
}

static void bitstream_reset(struct stfm1000_rds_bitstream *rdsb)
{
	memset(rdsb, 0, sizeof(*rdsb));
}

static void demod_reset(struct stfm1000_rds_demod *rdsd)
{
	memset(rdsd, 0, sizeof(*rdsd));
	rdsd->sdnom_adapt = 0;	/* XXX this doesn't really work right */
					/* it causes underruns at ALSA */
	rdsd->PhasePoppingEnabled = 1;	/* does this? */
}

static void packet_reset(struct stfm1000_rds_pkt *rdsp)
{
	memset(rdsp, 0, sizeof(*rdsp));
	rdsp->state = SYNC_OFFSET_A;
}

static void text_reset(struct stfm1000_rds_text *rdst)
{
	memset(rdst, 0, sizeof(*rdst));
}

void stfm1000_rds_reset(struct stfm1000_rds_state *rds)
{
	bitstream_reset(&rds->bitstream);
	demod_reset(&rds->demod);
	packet_reset(&rds->pkt);
	text_reset(&rds->text);
	rds->reset_req = 0;
}

int stfm1000_rds_bits_available(struct stfm1000_rds_state *rds)
{
	return bits_filled(&rds->bitstream);
}

int stmf1000_rds_get_bit(struct stfm1000_rds_state *rds)
{
	if (bits_filled(&rds->bitstream) == 0)
		return -1;
	return get1bit(&rds->bitstream);
}

int stmf1000_rds_avail_bits(struct stfm1000_rds_state *rds)
{
	return bits_filled(&rds->bitstream);
}

static const u32 rds_ParityCheck[] = {
	0x31B, 0x38F, 0x2A7, 0x0F7, 0x1EE,
	0x3DC, 0x201, 0x1BB, 0x376, 0x355,
	0x313, 0x39F, 0x287, 0x0B7, 0x16E,
	0x2DC, 0x001, 0x002, 0x004, 0x008,
	0x010, 0x020, 0x040, 0x080, 0x100,
	0x200
};

static int calc_syndrome(u32 rdscrc)
{
	int i;
	u32 syndrome = 0;
	int word = 0x1;

	for (i = 0; i < 26; i++) {
		if (rdscrc & word)
			syndrome ^= rds_ParityCheck[i];
		word <<= 1;
	}
	return syndrome;
}

static u32 ecc_table[1024];
static int ecc_table_generated;

static void generate_ecc_table(void)
{
	int i, j, size;
	u32 syndrome, word;

	for (i = 0; i < ECC_TBL_SIZE; i++)
		ecc_table[i] = 0xFFFFFFFF;
	ecc_table[0] = 0x0;

	for (j = 0; j < 5; j++) {
		word = (1 << (j + 1)) - 1;	/* 0x01 0x03 0x07 0x0f 0x1f */
		size = 26 - j; 			/* 26, 25, 24, 23, 22 */
		syndrome = 0;
		for (i = 0; i < size; i++) {
			syndrome = calc_syndrome(word);
			ecc_table[syndrome] = word;
			word <<= 1;
		}
	}
}

static u32 ecc_correct(u32 rdsBits, int *recovered)
{
	u32 syndrome;
	u32 errorBits;

	if (recovered)
		*recovered = 0;

	/* Calculate Syndrome on Received Packet */
	syndrome = calc_syndrome(rdsBits);

	if (syndrome == 0)
		return rdsBits; /* block is clean */

	/* generate table first time we get here */
	if (!ecc_table_generated) {
		generate_ecc_table();
		ecc_table_generated = 1;
	}

	/* Attempt to recover block */
	errorBits = ecc_table[syndrome];
	if (errorBits == UNRECOVERABLE_RDS_BLOCK)
		return UNRECOVERABLE_RDS_BLOCK;	/* Block can not be recovered.
						 * it is bad packet */

	rdsBits = rdsBits ^ errorBits;
	if (recovered)
		(*recovered)++;
	return rdsBits;                 /* ECC correct */
}

/* The following table lists the RDS and RBDS Program Type codes
 * and their meanings:
 * PTY code RDS Program type RBDS Program type */
static const struct stfm1000_rds_pty stc_tss_pty_tab[] = {
	{  0, "No program type",    "No program type"},
	{  1, "News",               "News"},
	{  2, "Current affairs",    "Information"},
	{  3, "Information",        "Sports"},
	{  4, "Sports",             "Talk"},
	{  5, "Education",          "Rock"},
	{  6, "Drama",              "Classic Rock"},
	{  7, "Culture",            "Adult Hits"},
	{  8, "Science",            "Soft Rock"},
	{  9, "Varied",             "Top 40"},
	{ 10, "Pop",                "Music Country"},
	{ 11, "Rock",               "Music Oldies"},
	{ 12, "M.O.R.",             "Music Soft"},
	{ 13, "Light classical",    "Nostalgia"},
	{ 14, "Serious",            "Classical Jazz"},
	{ 15, "Other Music",        "Classical"},
	{ 16, "Weather",            "Rhythm and Blues"},
	{ 17, "Finance",            "Soft Rhythm and Blues"},
	{ 18, "Children's programs", "Language"},
	{ 19, "Social Affairs",     "Religious Music"},
	{ 20, "Religion",           "Religious Talk"},
	{ 21, "Phone In",           "Personality"},
	{ 22, "Travel",             "Public"},
	{ 23, "Leisure",            "College"},
	{ 24, "Jazz Music",         "Unassigned"},
	{ 25, "Country Music",      "Unassigned"},
	{ 26, "National Music",     "Unassigned"},
	{ 27, "Oldies Music",       "Unassigned"},
	{ 28, "Folk Music",         "Unassigned"},
	{ 29, "Documentary",        "Weather"},
	{ 30, "Alarm Test",         "Emergency Test"},
	{ 31, "Alarm",              "Emergency"},
};

#if 0
static const char *rds_group_txt[] = {
	[RDS_GROUP_TYPE_0A] = "Basic tuning and switching information (0A)",
	[RDS_GROUP_TYPE_0B] = "Basic tuning and switching information (0B)",
	[RDS_GROUP_TYPE_1A] = "Program item number and slow labeling codes",
	[RDS_GROUP_TYPE_1B] = "Program item number",
	[RDS_GROUP_TYPE_2A] = "Radio Text (2A)",
	[RDS_GROUP_TYPE_2B] = "Radio Text (2B)",
	[RDS_GROUP_TYPE_3A] = "Application identification for ODA only",
	[RDS_GROUP_TYPE_3B] = "Open data applications",
	[RDS_GROUP_TYPE_4A] = "Clock-time and date",
	[RDS_GROUP_TYPE_4B] = "Open data applications",
	[RDS_GROUP_TYPE_5A] = "Transparent Data Channels (32 ch.) or ODA (5A)",
	[RDS_GROUP_TYPE_5B] = "Transparent Data Channels (32 ch.) or ODA (5B)",
	[RDS_GROUP_TYPE_6A] = "In House Applications or ODA (6A)",
	[RDS_GROUP_TYPE_6B] = "In House Applications or ODA (6B)",
	[RDS_GROUP_TYPE_7A] = "Radio Paging or ODA",
	[RDS_GROUP_TYPE_7B] = "Open Data Applications",
	[RDS_GROUP_TYPE_8A] = "Traffic Message Channel or ODA",
	[RDS_GROUP_TYPE_8B] = "Open Data Applications",
	[RDS_GROUP_TYPE_9A] = "Emergency warning system or ODA",
	[RDS_GROUP_TYPE_9B] = "Open Data Applications",
	[RDS_GROUP_TYPE_10A] = "Program Type Name",
	[RDS_GROUP_TYPE_10B] = "Open Data Applications (10B)",
	[RDS_GROUP_TYPE_11A] = "Open Data Applications (11A)",
	[RDS_GROUP_TYPE_11B] = "Open Data Applications (11B)",
	[RDS_GROUP_TYPE_12A] = "Open Data Applications (12A)",
	[RDS_GROUP_TYPE_12B] = "Open Data Applications (12B)",
	[RDS_GROUP_TYPE_13A] = "Enhanced Radio Paging or ODA",
	[RDS_GROUP_TYPE_13B] = "Open Data Applications",
	[RDS_GROUP_TYPE_14A] = "Enhanced Other Networks information (14A)",
	[RDS_GROUP_TYPE_14B] = "Enhanced Other Networks information (14B)",
	[RDS_GROUP_TYPE_15A] = "Defined in RBDS",
	[RDS_GROUP_TYPE_15B] = "Fast switching information",
};
#endif

static void dump_rds_packet(u8 *buf)
{
	u16 pi, offb;

	pi   = (u16)(buf[0] << 8) | buf[1];
	offb = (u16)(buf[1] << 8) | buf[2];

	printk(KERN_INFO "GRP: "
		"PI=0x%04x "
		"GT=%2d VER=%d TP=%d PTY=%2d "
		"PS_SEG=%2d RT_AB=%2d RT_SEG=%2d\n", pi,
		RDS_GROUP_TYPE(offb), RDS_VERSION(offb), RDS_TP(offb),
		RDS_PTY(offb),
		RDS_PS_SEG(offb), RDS_RT_AB(offb), RDS_RT_SEG(offb));
}

void stfm1000_rds_process_packet(struct stfm1000_rds_state *rds, u8 *buffer)
{
	struct stfm1000_rds_text *rdst = &rds->text;
	/* char tempCallLetters[5] = {0}; */
	struct rds_group_data grp;
	int grpno;
	u32 offset;
	char tps[9];
	int i, seg, idx;

	grp.piCode  = ((u16)buffer[0] << 8) | buffer[1];
	grp.offsetB = ((u16)buffer[2] << 8) | buffer[3];
	grp.offsetC = ((u16)buffer[4] << 8) | buffer[5];
	grp.offsetD = ((u16)buffer[6] << 8) | buffer[7];

	grpno = (grp.offsetB >> (8 + 3)) & 0x1f;

	if (rds_state_to_stfm1000(rds)->rds_info)
		dump_rds_packet(buffer);

	/* Is this the first time through? */
	if (!rdst->bRds_detected) {
		rdst->pi            = grp.piCode;
		rdst->tp            = RDS_TP(grp.offsetB);
		rdst->version       = RDS_VERSION(grp.offsetB);
		rdst->pty.id        = RDS_PTY(grp.offsetB);
		rdst->pty.pRds      = stc_tss_pty_tab[rdst->pty.id].pRds;
		rdst->pty.pRdbs     = stc_tss_pty_tab[rdst->pty.id].pRdbs;
		rdst->bRds_detected = 1;
	}

	/* Have we process too many PI errors? */
	if (grp.piCode != rdst->pi) {
		if (rdst->mismatch++ > 10) {

			/* requested reset of RDS */
			rds->reset_req = 1;

			/* signal monitor thread */
			stfm1000_monitor_signal(rds_state_to_stfm1000(rds),
				EVENT_RDS_RESET);

			if (rds_state_to_stfm1000(rds)->rds_info)
				printk(KERN_INFO "RDS: RESET!!!\n");

			text_reset(rdst);
		}
		rdst->consecutiveGood = 0;
		return;
	}

	if (rdst->consecutiveGood++ > 10)
		rdst->mismatch = 0;            /* reset bad count */

	if (rdst->consecutiveGood > rdst->consecutiveGoodMax)
		rdst->consecutiveGoodMax = rdst->consecutiveGood;

	switch (grpno) {
	case RDS_GROUP_TYPE_0A:
	case RDS_GROUP_TYPE_0B:
		/* Extract Service Name information */
		offset = RDS_PS_SEG(grp.offsetB) * 2;
		rdst->wk_ps[offset]     = buffer[6];	/* better */
		rdst->wk_ps[offset + 1] = buffer[7];
		rdst->wk_ps_mask |= 1 << RDS_PS_SEG(grp.offsetB);

		if (rds_state_to_stfm1000(rds)->rds_info) {
			for (i = 0; i < 8; i++) {
				if (rdst->wk_ps_mask & (1 << i)) {
					tps[i * 2] =
						rdst->wk_ps[i * 2];
					tps[i * 2 + 1] =
						rdst->wk_ps[i * 2 + 1];
				} else {
					tps[i * 2] = '_';
					tps[i * 2 + 1] = '_';
				}
			}
			tps[ARRAY_SIZE(tps) - 1] = '\0';
			if (rds_state_to_stfm1000(rds)->rds_info)
				printk(KERN_INFO "RDS-PS (curr): %s\n", tps);
		}

		if (rdst->wk_ps_mask != ALL_SEGMENT_BITS)
			break;

		if (rdst->ps_valid) {
			if (memcmp(rdst->ps, rdst->wk_ps, 8) != 0) {
				memset(rdst->cp_ps, 0, 8);
				memset(rdst->wk_ps, 0, 8);
				rdst->wk_ps_mask = 0;
			}

			memset(rdst->ps, 0, 8);
			rdst->ps_valid = 0;
			break;
		}

		/* does working buffer == compare buffer */
		if (memcmp(rdst->cp_ps, rdst->wk_ps, 8) != 0) {
			/* just copy from working to compare buffer */
			memcpy(rdst->cp_ps, rdst->wk_ps, 8);
			rdst->wk_ps_mask = 0;
			break;
		}

		/* working buffer matches compare buffer, send to UI */
		memcpy(rdst->ps, rdst->cp_ps, 8);
		rdst->ps_valid = 1;

		if (rds_state_to_stfm1000(rds)->rds_info)
			printk(KERN_INFO "RDS: PS '%s'\n", rdst->ps);

		/* clear working mask-only */
		rdst->wk_ps_mask = 0;
		break;

	case RDS_GROUP_TYPE_2A:

		/* Clear buffer */
		if (rdst->textAB_flag != RDS_RT_AB(grp.offsetB)) {
			memset(rdst->wk_text, 0, 64);
			rdst->wk_text_mask = 0;
			rdst->textAB_flag  = RDS_RT_AB(grp.offsetB);
		}

		/* Extract Text */
		seg = RDS_RT_SEG(grp.offsetB);
		idx = seg * 4;

		#define CNVT_EOT(x) ((x) != RDS_EOT ? (x) : 0)
		rdst->wk_text[idx++] = CNVT_EOT(buffer[4]);
		rdst->wk_text[idx++] = CNVT_EOT(buffer[5]);
		rdst->wk_text[idx++] = CNVT_EOT(buffer[6]);
		rdst->wk_text[idx++] = CNVT_EOT(buffer[7]);

		rdst->wk_text_mask |= 1 << seg;
		/* scan msg data for EOT.  If found set all higher
		 * mask bits */
		for (idx = 0; idx < 4; idx++) {
			if (rdst->text[idx] == RDS_EOT)
				break;
		}
		if (idx < 4) {
			/* set current and all higher bits  */
			for (idx = RDS_RT_SEG(grp.offsetB); idx < 16;
				idx++)
				rdst->wk_text_mask |= 1 << idx;
		}

		/* Process buffer when filled */
		if (rdst->wk_text_mask != ALL_TEXT_BITS)
			break;

		if (!rdst->text_valid)
			rdst->text_valid = 1;
		else if (memcmp(rdst->text, rdst->wk_text, 64) == 0)
			break;

		memcpy(rdst->text, rdst->wk_text, 64);

		if (rds_state_to_stfm1000(rds)->rds_info)
			printk(KERN_INFO "RDS: TEXT '%s'\n", rdst->text);

		memset(rdst->wk_text, 0, 64);
		rdst->wk_text_mask = 0;
		break;

	default:
		break;
	}
}

int stfm1000_rds_packet_dequeue(struct stfm1000_rds_state *rds, u8 *buf)
{
	struct stfm1000_rds_pkt *pkt = &rds->pkt;

	if (pkt->buf_cnt == 0)
		return -1;

	memcpy(buf, &pkt->buf_queue[pkt->buf_tail][0], 8);
	if (++pkt->buf_tail >= RDS_PKT_QUEUE)
		pkt->buf_tail = 0;
	pkt->buf_cnt--;

	return 0;
}

void stfm1000_rds_packet_bit(struct stfm1000_rds_state *rds, int bit)
{
	struct stfm1000_rds_pkt *pkt = &rds->pkt;
	u32 rdsdata, rdscrc, rdscrc_c, rdscrc_cp;
	int correct, correct2, recovered, recovered2;
	int RetVal;

	/* Stick into shift register */
	pkt->rdsstream = ((pkt->rdsstream << 1) | bit) & 0x03ffffff;
	pkt->bitsinfifo++;
	pkt->bitcount++;

	/* wait for 26 bits of block */
	if (pkt->bitsinfifo < 26)
		return;

	rdsdata = pkt->rdsstream & 0x03fffc00;	/* 16 bits of Info. word */
	rdscrc = pkt->rdsstream & 0x3ff;	/* 10 bits of Checkword */

	switch (pkt->state) {
	case SYNC_OFFSET_A:

		RetVal = calc_syndrome(pkt->rdsstream);

		switch (RetVal) {
		case RDS_SYNDROME_OFFSETA:
			pkt->state = OFFSET_B;
			break;
		case RDS_SYNDROME_OFFSETB:
			pkt->state = OFFSET_C_CP;
			break;
		case RDS_SYNDROME_OFFSETC:
			pkt->state = OFFSET_D;
			break;
		case RDS_SYNDROME_OFFSETCP:
			pkt->state = OFFSET_D;
			break;
		case RDS_SYNDROME_OFFSETD:
			pkt->state = OFFSET_A;
			break;
		default:
			pkt->state = SYNC_OFFSET_A;
			break;
		}
		if (pkt->state == SYNC_OFFSET_A) {
			pkt->sync_lost_packets++;
			/* XXX send info? */
			break;
		}

		pkt->good_packets++;

		rdsdata = pkt->rdsstream & 0x03fffc00;

		/* Save type A packet in buffer */
		rdsdata >>= 10;
		pkt->buffer[0] = (rdsdata >> 8);
		pkt->buffer[1] = (rdsdata & 0xff);
		pkt->bitsinfifo = 0;

		/* We found a block with zero errors, but it is not at the
		 * start of the group. */
		if (pkt->state == OFFSET_B)
			pkt->discardpacket = 0;
		else
			pkt->discardpacket = 1;
		break;

	case OFFSET_A:		/* Type A: we are in sync now */
		rdscrc ^= RDS_OFFSETA;
		correct = ecc_correct(rdsdata | rdscrc, &recovered);
		if (correct == UNRECOVERABLE_RDS_BLOCK) {
			pkt->bad_packets++;
			pkt->discardpacket++;
			pkt->state++;
			pkt->bitsinfifo = 0;
			break;
		}

		if (recovered)
			pkt->recovered_packets++;
		pkt->good_packets++;

		/* Attempt to see, if we can get the entire group.
		 * Don't discard. */
		pkt->discardpacket = 0;
		rdsdata = correct & 0x03fffc00;

		/* Save type A packet in buffer */
		rdsdata >>= 10;
		pkt->buffer[0] = (rdsdata >> 8);
		pkt->buffer[1] = (rdsdata & 0xff);
		pkt->bitsinfifo = 0;
		pkt->state++;
		break;

	case OFFSET_B:		/* Waiting for type B */
		rdscrc ^= RDS_OFFSETB;
		correct = ecc_correct(rdsdata | rdscrc, &recovered);
		if (correct == UNRECOVERABLE_RDS_BLOCK) {
			pkt->bad_packets++;
			pkt->discardpacket++;
			pkt->state++;
			pkt->bitsinfifo = 0;
			break;
		}
		if (recovered)
			pkt->recovered_packets++;
		pkt->good_packets++;

		rdsdata = correct & 0x03fffc00;

		/* Save type B packet in buffer */
		rdsdata >>= 10;
		pkt->buffer[2] = (rdsdata >> 8);
		pkt->buffer[3] = (rdsdata & 0xff);
		pkt->bitsinfifo = 0;
		pkt->state++;
		break;

	case OFFSET_C_CP:	/* Waiting for type C or C' */
		rdscrc_c = rdscrc ^ RDS_OFFSETC;
		rdscrc_cp = rdscrc ^ RDS_OFFSETCP;
		correct = ecc_correct(rdsdata | rdscrc_c, &recovered);
		correct2 = ecc_correct(rdsdata | rdscrc_cp, &recovered2);
		if (correct == UNRECOVERABLE_RDS_BLOCK
		    && correct2 == UNRECOVERABLE_RDS_BLOCK) {
			pkt->bad_packets++;
			pkt->discardpacket++;
			pkt->state++;
			pkt->bitsinfifo = 0;
			break;
		}

		if (recovered || recovered2)
			pkt->recovered_packets++;
		pkt->good_packets++;

		if (correct == UNRECOVERABLE_RDS_BLOCK)
			correct = correct2;

		rdsdata = correct & 0x03fffc00;

		/* Save type C packet in buffer */
		rdsdata >>= 10;
		pkt->buffer[4] = (rdsdata >> 8);
		pkt->buffer[5] = (rdsdata & 0xff);
		pkt->bitsinfifo = 0;
		pkt->state++;
		break;

	case OFFSET_D:		/* Waiting for type D */
		rdscrc ^= RDS_OFFSETD;
		correct = ecc_correct(rdsdata | rdscrc, &recovered);
		if (correct == UNRECOVERABLE_RDS_BLOCK) {
			pkt->bad_packets++;
			pkt->discardpacket++;
			pkt->state = OFFSET_A;
			pkt->bitsinfifo = 0;
			break;
		}

		if (recovered)
			pkt->recovered_packets++;
		pkt->good_packets++;

		rdsdata = correct & 0x03fffc00;

		/* Save type D packet in buffer */
		rdsdata >>= 10;
		pkt->buffer[6] = (rdsdata >> 8);
		pkt->buffer[7] = (rdsdata & 0xff);

		/* buffer it if all segments were ok */
		if (pkt->discardpacket) {
			/* We're still in sync, so back to state 1 */
			pkt->state = OFFSET_A;
			pkt->bitsinfifo = 0;
			pkt->discardpacket = 0;
			break;
		}

		pkt->state++;
		/* fall-through */

	case PACKET_OUT:
		pkt->GroupDropOnce = 1;

		/* queue packet */
		if (pkt->buf_cnt < RDS_PKT_QUEUE) {
			memcpy(&pkt->buf_queue[pkt->buf_head][0],
				pkt->buffer, 8);
			if (++pkt->buf_head >= RDS_PKT_QUEUE)
				pkt->buf_head = 0;
			pkt->buf_cnt++;
		} else
			pkt->buf_overruns++;

		/* We're still in sync, so back to state 1 */
		pkt->state = OFFSET_A;
		pkt->bitsinfifo = 0;
		pkt->discardpacket = 0;
		break;

	}

	/* Lots of errors? If so, go back to resync mode */
	if (pkt->discardpacket >= 10) {
		pkt->state = SYNC_OFFSET_A;	/* reset sync state */
		pkt->bitsinfifo = 26;	/* resync a bit faster */
	}
}

/* GROUP_TYPE 0A-0B (buffer must have enough space for 9 bytes) */
int stfm1000_rds_get_ps(struct stfm1000_rds_state *rds, u8 *buffer,
	int bufsize)
{
	struct stfm1000_rds_text *rdst = &rds->text;

	if (bufsize < 9)
		return -1;

	if (!rdst->ps_valid)
		return -1;

	memcpy(buffer, rdst->ps, 8);
	buffer[8] = '\0';

	return 8;
}

/* GROUP_TYPE 2A (buffer must have enough space for 65 bytes) */
int stfm1000_rds_get_text(struct stfm1000_rds_state *rds, u8 *buffer,
	int bufsize)
{
	struct stfm1000_rds_text *rdst = &rds->text;

	if (bufsize < 9)
		return -1;

	if (!rdst->text_valid)
		return -1;

	memcpy(buffer, rdst->text, 64);
	buffer[64] = '\0';

	return 64;
}