File: /usr/src/linux/drivers/net/hamradio/soundmodem/sm_afsk2666.c

1     /*****************************************************************************/
2     
3     /*
4      *	sm_afsk2666.c  -- soundcard radio modem driver, 2666 baud AFSK modem
5      *
6      *	Copyright (C) 1997  Thomas Sailer (sailer@ife.ee.ethz.ch)
7      *
8      *	This program is free software; you can redistribute it and/or modify
9      *	it under the terms of the GNU General Public License as published by
10      *	the Free Software Foundation; either version 2 of the License, or
11      *	(at your option) any later version.
12      *
13      *	This program is distributed in the hope that it will be useful,
14      *	but WITHOUT ANY WARRANTY; without even the implied warranty of
15      *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      *	GNU General Public License for more details.
17      *
18      *	You should have received a copy of the GNU General Public License
19      *	along with this program; if not, write to the Free Software
20      *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21      *
22      *  Please note that the GPL allows you to use the driver, NOT the radio.
23      *  In order to use the radio, you need a license from the communications
24      *  authority of your country.
25      *
26      */
27     
28     #include "sm.h"
29     #include "sm_tbl_afsk2666.h"
30     
31     /* --------------------------------------------------------------------- */
32     
33     struct demod_state_afsk26 {
34     	unsigned int shreg;
35     	unsigned long descram;
36     	int dem_sum[8];
37     	int dem_sum_mean;
38     	int dem_cnt;
39     	unsigned int bit_pll;
40     	unsigned char last_sample;
41     	unsigned int dcd_shreg;
42     	int dcd_sum0, dcd_sum1, dcd_sum2;
43     	unsigned int dcd_time;
44     };
45     
46     struct mod_state_afsk26 {
47     	unsigned int shreg;
48     	unsigned long scram;
49     	unsigned int bit_pll;
50     	unsigned int phinc;
51     	unsigned int tx_seq;
52     };
53     
54     /* --------------------------------------------------------------------- */
55     
56     #define DESCRAM_TAP1 0x20000
57     #define DESCRAM_TAP2 0x01000
58     #define DESCRAM_TAP3 0x00001
59     
60     #define DESCRAM_TAPSH1 17
61     #define DESCRAM_TAPSH2 12
62     #define DESCRAM_TAPSH3 0
63     
64     #define SCRAM_TAP1 0x20000 /* X^17 */
65     #define SCRAM_TAPN 0x00021 /* X^0+X^5 */
66     
67     /* --------------------------------------------------------------------- */
68     
69     static void modulator_2666_u8(struct sm_state *sm, unsigned char *buf, unsigned int buflen)
70     {
71     	struct mod_state_afsk26 *st = (struct mod_state_afsk26 *)(&sm->m);
72     
73     	for (; buflen > 0; buflen--, buf++) {
74     		if (!st->tx_seq++) {
75     			if (st->shreg <= 1)
76     				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
77     			st->scram = ((st->scram << 1) | (st->scram & 1));
78     			st->scram ^= (!(st->shreg & 1));
79     			st->shreg >>= 1;
80     			if (st->scram & (SCRAM_TAP1 << 1))
81     				st->scram ^= SCRAM_TAPN << 1;
82     			st->phinc = afsk26_carfreq[!(st->scram & (SCRAM_TAP1 << 2))];
83     		}
84     		if (st->tx_seq >= 6)
85     			st->tx_seq = 0;
86     		*buf = OFFSCOS(st->bit_pll);
87     		st->bit_pll += st->phinc;
88     	}
89     }
90     
91     /* --------------------------------------------------------------------- */
92     
93     static void modulator_2666_s16(struct sm_state *sm, short *buf, unsigned int buflen)
94     {
95     	struct mod_state_afsk26 *st = (struct mod_state_afsk26 *)(&sm->m);
96     
97     	for (; buflen > 0; buflen--, buf++) {
98     		if (!st->tx_seq++) {
99     			if (st->shreg <= 1)
100     				st->shreg = hdlcdrv_getbits(&sm->hdrv) | 0x10000;
101     			st->scram = ((st->scram << 1) | (st->scram & 1));
102     			st->scram ^= (!(st->shreg & 1));
103     			st->shreg >>= 1;
104     			if (st->scram & (SCRAM_TAP1 << 1))
105     				st->scram ^= SCRAM_TAPN << 1;
106     			st->phinc = afsk26_carfreq[!(st->scram & (SCRAM_TAP1 << 2))];
107     		}
108     		if (st->tx_seq >= 6)
109     			st->tx_seq = 0;
110     		*buf = COS(st->bit_pll);
111     		st->bit_pll += st->phinc;
112     	}
113     }
114     
115     /* --------------------------------------------------------------------- */
116     
117     extern __inline__ int convolution12_u8(const unsigned char *st, const int *coeff, int csum)
118     {
119     	int sum = -0x80 * csum;
120     	
121     	sum += (st[0] * coeff[0]);
122     	sum += (st[-1] * coeff[1]);
123     	sum += (st[-2] * coeff[2]);
124     	sum += (st[-3] * coeff[3]);
125     	sum += (st[-4] * coeff[4]);
126     	sum += (st[-5] * coeff[5]);
127     	sum += (st[-6] * coeff[6]);
128     	sum += (st[-7] * coeff[7]);
129     	sum += (st[-8] * coeff[8]);
130     	sum += (st[-9] * coeff[9]);
131     	sum += (st[-10] * coeff[10]);
132     	sum += (st[-11] * coeff[11]);
133     
134     	return sum;
135     }
136     
137     extern __inline__ int convolution12_s16(const short *st, const int *coeff, int csum)
138     {
139     	int sum = 0;
140     	
141     	sum += (st[0] * coeff[0]);
142     	sum += (st[-1] * coeff[1]);
143     	sum += (st[-2] * coeff[2]);
144     	sum += (st[-3] * coeff[3]);
145     	sum += (st[-4] * coeff[4]);
146     	sum += (st[-5] * coeff[5]);
147     	sum += (st[-6] * coeff[6]);
148     	sum += (st[-7] * coeff[7]);
149     	sum += (st[-8] * coeff[8]);
150     	sum += (st[-9] * coeff[9]);
151     	sum += (st[-10] * coeff[10]);
152     	sum += (st[-11] * coeff[11]);
153     
154     	sum >>= 8;
155     	return sum;
156     }
157     
158     /* ---------------------------------------------------------------------- */
159     
160     #if 0
161     static int binexp(unsigned int i)
162     {
163     	int ret = 31;
164     	
165     	if (!i)
166     		return 0;
167     	if (i < 0x10000LU) {
168     		i <<= 16;
169     		ret -= 16;
170     	}
171     	if (i < 0x1000000LU) {
172     		i <<= 8;
173     		ret -= 8;
174     	}
175     	if (i < 0x10000000LU) {
176     		i <<= 4;
177     		ret -= 4;
178     	}
179     	if (i < 0x40000000LU) {
180     		i <<= 2;
181     		ret -= 2;
182     	}
183     	if (i < 0x80000000LU)
184     		ret -= 1;
185     	return ret;
186     }
187     
188     static const sqrt_tab[16] = {
189     	00000, 16384, 23170, 28378, 32768, 36636, 40132, 43348,
190     	46341, 49152, 51811, 54340, 56756, 59073, 61303, 63455
191     };
192     
193     
194     static unsigned int int_sqrt_approx(unsigned int i)
195     {
196     	unsigned int j;
197     
198     	if (i < 16)
199     		return sqrt_tab[i] >> 14;
200     	j = binexp(i) >> 1;
201     	i >>= (j * 2 - 2);
202           	return (sqrt_tab[i & 0xf] << j) >> 15;
203     }
204     #endif
205     
206     /* --------------------------------------------------------------------- */
207     
208     extern unsigned int est_pwr(int i, int q)
209     {
210     	unsigned int ui = abs(i);
211     	unsigned int uq = abs(q);
212     
213     	if (uq > ui) {
214     		unsigned int tmp;
215     		tmp = ui;
216     		ui = uq;
217     		uq = tmp;
218     	}
219     	if (uq > (ui >> 1))
220     		return 7*(ui>>3) + 9*(uq>>4);
221     	else
222     		return ui + (uq>>2);
223     }
224     
225     /* --------------------------------------------------------------------- */
226     
227     static void demod_one_sample(struct sm_state *sm, struct demod_state_afsk26 *st, int curval,
228     			     int loi, int loq, int hii, int hiq)
229     {
230     	static const int pll_corr[2] = { -0xa00, 0xa00 };
231     	unsigned char curbit;
232     	unsigned int descx;
233     	int val;
234     
235     	/* 
236     	 * estimate power
237     	 */
238     	val = est_pwr(hii, hiq) - est_pwr(loi, loq);
239     	/*
240     	 * estimate center value
241     	 */
242     	st->dem_sum[0] += val >> 8;
243     	if ((++st->dem_cnt) >= 256) {
244     		st->dem_cnt = 0;
245     		st->dem_sum_mean = (st->dem_sum[0]+st->dem_sum[1]+
246     				    st->dem_sum[2]+st->dem_sum[3]+
247     				    st->dem_sum[4]+st->dem_sum[5]+
248     				    st->dem_sum[6]+st->dem_sum[7]) >> 3;
249     		memmove(st->dem_sum+1, st->dem_sum, 
250     			sizeof(st->dem_sum)-sizeof(st->dem_sum[0]));
251     		st->dem_sum[0] = 0;
252     	}
253     	/*
254     	 * decision and bit clock regen
255     	 */
256     	val -= st->dem_sum_mean;
257     	diag_add(sm, curval, val);
258     	
259     	st->dcd_shreg <<= 1;
260     	st->bit_pll += 0x1555;
261     	curbit = (val > 0);
262     	if (st->last_sample ^ curbit) {
263     		st->dcd_shreg |= 1;
264     		st->bit_pll += pll_corr[st->bit_pll < (0x8000+0x1555)];
265     		st->dcd_sum0 += 4*hweight8(st->dcd_shreg & 0x1e) -
266     			hweight16(st->dcd_shreg & 0xfe00);
267     	}
268     	st->last_sample = curbit;
269     	hdlcdrv_channelbit(&sm->hdrv, curbit);
270     	if ((--st->dcd_time) <= 0) {
271     		hdlcdrv_setdcd(&sm->hdrv, (st->dcd_sum0 + st->dcd_sum1 + 
272     					   st->dcd_sum2) < 0);
273     		st->dcd_sum2 = st->dcd_sum1;
274     		st->dcd_sum1 = st->dcd_sum0;
275     		st->dcd_sum0 = 2; /* slight bias */
276     		st->dcd_time = 400;
277     	}
278     	if (st->bit_pll >= 0x10000) {
279     		st->bit_pll &= 0xffffu;
280     		st->descram = (st->descram << 1) | curbit;
281     		descx = st->descram ^ (st->descram >> 1);
282     		descx ^= ((descx >> DESCRAM_TAPSH1) ^
283     			  (descx >> DESCRAM_TAPSH2));
284     		st->shreg >>= 1;
285     		st->shreg |= (!(descx & 1)) << 16;
286     		if (st->shreg & 1) {
287     			hdlcdrv_putbits(&sm->hdrv, st->shreg >> 1);
288     			st->shreg = 0x10000;
289     		}
290     		diag_trigger(sm);
291     	}
292     }
293     
294     /* --------------------------------------------------------------------- */
295     
296     static void demodulator_2666_u8(struct sm_state *sm, const unsigned char *buf, unsigned int buflen)
297     {
298     	struct demod_state_afsk26 *st = (struct demod_state_afsk26 *)(&sm->d);
299     
300     	for (; buflen > 0; buflen--, buf++) {
301     		demod_one_sample(sm, st, (*buf-0x80)<<8,
302     				 convolution12_u8(buf, afsk26_dem_tables[0][0].i, AFSK26_DEM_SUM_I_0_0),
303     				 convolution12_u8(buf, afsk26_dem_tables[0][0].q, AFSK26_DEM_SUM_Q_0_0),
304     				 convolution12_u8(buf, afsk26_dem_tables[0][1].i, AFSK26_DEM_SUM_I_0_1),
305     				 convolution12_u8(buf, afsk26_dem_tables[0][1].q, AFSK26_DEM_SUM_Q_0_1));
306     		demod_one_sample(sm, st,  (*buf-0x80)<<8,
307     				 convolution12_u8(buf, afsk26_dem_tables[1][0].i, AFSK26_DEM_SUM_I_1_0),
308     				 convolution12_u8(buf, afsk26_dem_tables[1][0].q, AFSK26_DEM_SUM_Q_1_0),
309     				 convolution12_u8(buf, afsk26_dem_tables[1][1].i, AFSK26_DEM_SUM_I_1_1),
310     				 convolution12_u8(buf, afsk26_dem_tables[1][1].q, AFSK26_DEM_SUM_Q_1_1));
311     	}
312     }
313     
314     /* --------------------------------------------------------------------- */
315     
316     static void demodulator_2666_s16(struct sm_state *sm, const short *buf, unsigned int buflen)
317     {
318     	struct demod_state_afsk26 *st = (struct demod_state_afsk26 *)(&sm->d);
319     
320     	for (; buflen > 0; buflen--, buf++) {
321     		demod_one_sample(sm, st, *buf,
322     				 convolution12_s16(buf, afsk26_dem_tables[0][0].i, AFSK26_DEM_SUM_I_0_0),
323     				 convolution12_s16(buf, afsk26_dem_tables[0][0].q, AFSK26_DEM_SUM_Q_0_0),
324     				 convolution12_s16(buf, afsk26_dem_tables[0][1].i, AFSK26_DEM_SUM_I_0_1),
325     				 convolution12_s16(buf, afsk26_dem_tables[0][1].q, AFSK26_DEM_SUM_Q_0_1));
326     		demod_one_sample(sm, st, *buf, 
327     				 convolution12_s16(buf, afsk26_dem_tables[1][0].i, AFSK26_DEM_SUM_I_1_0),
328     				 convolution12_s16(buf, afsk26_dem_tables[1][0].q, AFSK26_DEM_SUM_Q_1_0),
329     				 convolution12_s16(buf, afsk26_dem_tables[1][1].i, AFSK26_DEM_SUM_I_1_1),
330     				 convolution12_s16(buf, afsk26_dem_tables[1][1].q, AFSK26_DEM_SUM_Q_1_1));
331     	}
332     }
333     
334     /* --------------------------------------------------------------------- */
335     
336     static void demod_init_2666(struct sm_state *sm)
337     {
338     	struct demod_state_afsk26 *st = (struct demod_state_afsk26 *)(&sm->d);
339     
340     	st->dcd_time = 400;
341     	st->dcd_sum0 = 2;	
342     }
343     
344     /* --------------------------------------------------------------------- */
345     
346     const struct modem_tx_info sm_afsk2666_tx = {
347     	"afsk2666", sizeof(struct mod_state_afsk26), AFSK26_SAMPLERATE, 2666, 
348     	modulator_2666_u8, modulator_2666_s16, NULL
349     };
350     
351     const struct modem_rx_info sm_afsk2666_rx = {
352     	"afsk2666", sizeof(struct demod_state_afsk26), AFSK26_SAMPLERATE, 2666, 12, 6, 
353     	demodulator_2666_u8, demodulator_2666_s16, demod_init_2666
354     };
355     
356     /* --------------------------------------------------------------------- */
357