[go: nahoru, domu]

11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
4475be4d85a274d0961593db41cf85689db1d583cJoe Perches Copyright (c) Eicon Networks, 2002.
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6475be4d85a274d0961593db41cf85689db1d583cJoe Perches This source file is supplied for the use with
7475be4d85a274d0961593db41cf85689db1d583cJoe Perches Eicon Networks range of DIVA Server Adapters.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
9475be4d85a274d0961593db41cf85689db1d583cJoe Perches Eicon File Revision :    2.1
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
11475be4d85a274d0961593db41cf85689db1d583cJoe Perches This program is free software; you can redistribute it and/or modify
12475be4d85a274d0961593db41cf85689db1d583cJoe Perches it under the terms of the GNU General Public License as published by
13475be4d85a274d0961593db41cf85689db1d583cJoe Perches the Free Software Foundation; either version 2, or (at your option)
14475be4d85a274d0961593db41cf85689db1d583cJoe Perches any later version.
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
16475be4d85a274d0961593db41cf85689db1d583cJoe Perches This program is distributed in the hope that it will be useful,
17475be4d85a274d0961593db41cf85689db1d583cJoe Perches but WITHOUT ANY WARRANTY OF ANY KIND WHATSOEVER INCLUDING ANY
18475be4d85a274d0961593db41cf85689db1d583cJoe Perches implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19475be4d85a274d0961593db41cf85689db1d583cJoe Perches See the GNU General Public License for more details.
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
21475be4d85a274d0961593db41cf85689db1d583cJoe Perches You should have received a copy of the GNU General Public License
22475be4d85a274d0961593db41cf85689db1d583cJoe Perches along with this program; if not, write to the Free Software
23475be4d85a274d0961593db41cf85689db1d583cJoe Perches Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "platform.h"
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30475be4d85a274d0961593db41cf85689db1d583cJoe Perches
31475be4d85a274d0961593db41cf85689db1d583cJoe Perches
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "capidtmf.h"
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* #define TRACE_ */
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FILE_ "CAPIDTMF.C"
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define trace(a)
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic short capidtmf_expand_table_alaw[0x0100] =
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
54475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-5504,   5504,   -344,    344, -22016,  22016,  -1376,   1376,
55475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2752,   2752,    -88,     88, -11008,  11008,   -688,    688,
56475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-7552,   7552,   -472,    472, -30208,  30208,  -1888,   1888,
57475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3776,   3776,   -216,    216, -15104,  15104,   -944,    944,
58475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4480,   4480,   -280,    280, -17920,  17920,  -1120,   1120,
59475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2240,   2240,    -24,     24,  -8960,   8960,   -560,    560,
60475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-6528,   6528,   -408,    408, -26112,  26112,  -1632,   1632,
61475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3264,   3264,   -152,    152, -13056,  13056,   -816,    816,
62475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-6016,   6016,   -376,    376, -24064,  24064,  -1504,   1504,
63475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3008,   3008,   -120,    120, -12032,  12032,   -752,    752,
64475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-8064,   8064,   -504,    504, -32256,  32256,  -2016,   2016,
65475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4032,   4032,   -248,    248, -16128,  16128,  -1008,   1008,
66475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4992,   4992,   -312,    312, -19968,  19968,  -1248,   1248,
67475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2496,   2496,    -56,     56,  -9984,   9984,   -624,    624,
68475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-7040,   7040,   -440,    440, -28160,  28160,  -1760,   1760,
69475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3520,   3520,   -184,    184, -14080,  14080,   -880,    880,
70475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-5248,   5248,   -328,    328, -20992,  20992,  -1312,   1312,
71475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2624,   2624,    -72,     72, -10496,  10496,   -656,    656,
72475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-7296,   7296,   -456,    456, -29184,  29184,  -1824,   1824,
73475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3648,   3648,   -200,    200, -14592,  14592,   -912,    912,
74475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4224,   4224,   -264,    264, -16896,  16896,  -1056,   1056,
75475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2112,   2112,     -8,      8,  -8448,   8448,   -528,    528,
76475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-6272,   6272,   -392,    392, -25088,  25088,  -1568,   1568,
77475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3136,   3136,   -136,    136, -12544,  12544,   -784,    784,
78475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-5760,   5760,   -360,    360, -23040,  23040,  -1440,   1440,
79475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2880,   2880,   -104,    104, -11520,  11520,   -720,    720,
80475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-7808,   7808,   -488,    488, -31232,  31232,  -1952,   1952,
81475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3904,   3904,   -232,    232, -15616,  15616,   -976,    976,
82475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4736,   4736,   -296,    296, -18944,  18944,  -1184,   1184,
83475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2368,   2368,    -40,     40,  -9472,   9472,   -592,    592,
84475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-6784,   6784,   -424,    424, -27136,  27136,  -1696,   1696,
85475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-3392,   3392,   -168,    168, -13568,  13568,   -848,    848
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic short capidtmf_expand_table_ulaw[0x0100] =
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
90475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-32124,  32124,  -1884,   1884,  -7932,   7932,   -372,    372,
91475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-15996,  15996,   -876,    876,  -3900,   3900,   -120,    120,
92475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-23932,  23932,  -1372,   1372,  -5884,   5884,   -244,    244,
93475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-11900,  11900,   -620,    620,  -2876,   2876,    -56,     56,
94475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-28028,  28028,  -1628,   1628,  -6908,   6908,   -308,    308,
95475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-13948,  13948,   -748,    748,  -3388,   3388,    -88,     88,
96475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-19836,  19836,  -1116,   1116,  -4860,   4860,   -180,    180,
97475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-9852,   9852,   -492,    492,  -2364,   2364,    -24,     24,
98475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-30076,  30076,  -1756,   1756,  -7420,   7420,   -340,    340,
99475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-14972,  14972,   -812,    812,  -3644,   3644,   -104,    104,
100475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-21884,  21884,  -1244,   1244,  -5372,   5372,   -212,    212,
101475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-10876,  10876,   -556,    556,  -2620,   2620,    -40,     40,
102475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-25980,  25980,  -1500,   1500,  -6396,   6396,   -276,    276,
103475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-12924,  12924,   -684,    684,  -3132,   3132,    -72,     72,
104475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-17788,  17788,   -988,    988,  -4348,   4348,   -148,    148,
105475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-8828,   8828,   -428,    428,  -2108,   2108,     -8,      8,
106475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-31100,  31100,  -1820,   1820,  -7676,   7676,   -356,    356,
107475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-15484,  15484,   -844,    844,  -3772,   3772,   -112,    112,
108475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-22908,  22908,  -1308,   1308,  -5628,   5628,   -228,    228,
109475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-11388,  11388,   -588,    588,  -2748,   2748,    -48,     48,
110475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-27004,  27004,  -1564,   1564,  -6652,   6652,   -292,    292,
111475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-13436,  13436,   -716,    716,  -3260,   3260,    -80,     80,
112475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-18812,  18812,  -1052,   1052,  -4604,   4604,   -164,    164,
113475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-9340,   9340,   -460,    460,  -2236,   2236,    -16,     16,
114475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-29052,  29052,  -1692,   1692,  -7164,   7164,   -324,    324,
115475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-14460,  14460,   -780,    780,  -3516,   3516,    -96,     96,
116475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-20860,  20860,  -1180,   1180,  -5116,   5116,   -196,    196,
117475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-10364,  10364,   -524,    524,  -2492,   2492,    -32,     32,
118475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-24956,  24956,  -1436,   1436,  -6140,   6140,   -260,    260,
119475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-12412,  12412,   -652,    652,  -3004,   3004,    -64,     64,
120475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-16764,  16764,   -924,    924,  -4092,   4092,   -132,    132,
121475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-8316,   8316,   -396,    396,  -1980,   1980,      0,      0
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic short capidtmf_recv_window_function[CAPIDTMF_RECV_ACCUMULATE_CYCLES] =
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
129475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-500L,   -999L,  -1499L,  -1998L,  -2496L,  -2994L,  -3491L,  -3988L,
130475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-4483L,  -4978L,  -5471L,  -5963L,  -6454L,  -6943L,  -7431L,  -7917L,
131475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-8401L,  -8883L,  -9363L,  -9840L, -10316L, -10789L, -11259L, -11727L,
132475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-12193L, -12655L, -13115L, -13571L, -14024L, -14474L, -14921L, -15364L,
133475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-15804L, -16240L, -16672L, -17100L, -17524L, -17944L, -18360L, -18772L,
134475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-19180L, -19583L, -19981L, -20375L, -20764L, -21148L, -21527L, -21901L,
135475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-22270L, -22634L, -22993L, -23346L, -23694L, -24037L, -24374L, -24705L,
136475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-25030L, -25350L, -25664L, -25971L, -26273L, -26568L, -26858L, -27141L,
137475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-27418L, -27688L, -27952L, -28210L, -28461L, -28705L, -28943L, -29174L,
138475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-29398L, -29615L, -29826L, -30029L, -30226L, -30415L, -30598L, -30773L,
139475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-30941L, -31102L, -31256L, -31402L, -31541L, -31673L, -31797L, -31914L,
140475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-32024L, -32126L, -32221L, -32308L, -32388L, -32460L, -32524L, -32581L,
141475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-32631L, -32673L, -32707L, -32734L, -32753L, -32764L, -32768L, -32764L,
142475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-32753L, -32734L, -32707L, -32673L, -32631L, -32581L, -32524L, -32460L,
143475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-32388L, -32308L, -32221L, -32126L, -32024L, -31914L, -31797L, -31673L,
144475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-31541L, -31402L, -31256L, -31102L, -30941L, -30773L, -30598L, -30415L,
145475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-30226L, -30029L, -29826L, -29615L, -29398L, -29174L, -28943L, -28705L,
146475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-28461L, -28210L, -27952L, -27688L, -27418L, -27141L, -26858L, -26568L,
147475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-26273L, -25971L, -25664L, -25350L, -25030L, -24705L, -24374L, -24037L,
148475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-23694L, -23346L, -22993L, -22634L, -22270L, -21901L, -21527L, -21148L,
149475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-20764L, -20375L, -19981L, -19583L, -19180L, -18772L, -18360L, -17944L,
150475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-17524L, -17100L, -16672L, -16240L, -15804L, -15364L, -14921L, -14474L,
151475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-14024L, -13571L, -13115L, -12655L, -12193L, -11727L, -11259L, -10789L,
152475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-10316L,  -9840L,  -9363L,  -8883L,  -8401L,  -7917L,  -7431L,  -6943L,
153475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-6454L,  -5963L,  -5471L,  -4978L,  -4483L,  -3988L,  -3491L,  -2994L,
154475be4d85a274d0961593db41cf85689db1d583cJoe Perches	-2496L,  -1998L,  -1499L,   -999L,   -500L,
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic byte capidtmf_leading_zeroes_table[0x100] =
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
159475be4d85a274d0961593db41cf85689db1d583cJoe Perches	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
160475be4d85a274d0961593db41cf85689db1d583cJoe Perches	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
161475be4d85a274d0961593db41cf85689db1d583cJoe Perches	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
162475be4d85a274d0961593db41cf85689db1d583cJoe Perches	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
163475be4d85a274d0961593db41cf85689db1d583cJoe Perches	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
164475be4d85a274d0961593db41cf85689db1d583cJoe Perches	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
165475be4d85a274d0961593db41cf85689db1d583cJoe Perches	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
166475be4d85a274d0961593db41cf85689db1d583cJoe Perches	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
167475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define capidtmf_byte_leading_zeroes(b)  (capidtmf_leading_zeroes_table[(BYTE)(b)])
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define capidtmf_word_leading_zeroes(w)  (((w) & 0xff00) ? capidtmf_leading_zeroes_table[(w) >> 8] : 8 + capidtmf_leading_zeroes_table[(w)])
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define capidtmf_dword_leading_zeroes(d)  (((d) & 0xffff0000L) ?    (((d) & 0xff000000L) ? capidtmf_leading_zeroes_table[(d) >> 24] : 8 + capidtmf_leading_zeroes_table[(d) >> 16]) :    (((d) & 0xff00) ? 16 + capidtmf_leading_zeroes_table[(d) >> 8] : 24 + capidtmf_leading_zeroes_table[(d)]))
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
185475be4d85a274d0961593db41cf85689db1d583cJoe Perchesstatic void capidtmf_goertzel_loop(long *buffer, long *coeffs, short *sample, long count)
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
187475be4d85a274d0961593db41cf85689db1d583cJoe Perches	int i, j;
188475be4d85a274d0961593db41cf85689db1d583cJoe Perches	long c, d, q0, q1, q2;
189475be4d85a274d0961593db41cf85689db1d583cJoe Perches
190475be4d85a274d0961593db41cf85689db1d583cJoe Perches	for (i = 0; i < CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT - 1; i++)
191475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
192475be4d85a274d0961593db41cf85689db1d583cJoe Perches		q1 = buffer[i];
193475be4d85a274d0961593db41cf85689db1d583cJoe Perches		q2 = buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT];
194475be4d85a274d0961593db41cf85689db1d583cJoe Perches		d = coeffs[i] >> 1;
195475be4d85a274d0961593db41cf85689db1d583cJoe Perches		c = d << 1;
196475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (c >= 0)
197475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
198475be4d85a274d0961593db41cf85689db1d583cJoe Perches			for (j = 0; j < count; j++)
199475be4d85a274d0961593db41cf85689db1d583cJoe Perches			{
200475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q0 = sample[j] - q2 + (c * (q1 >> 16)) + (((dword)(((dword) d) * ((dword)(q1 & 0xffff)))) >> 15);
201475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q2 = q1;
202475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q1 = q0;
203475be4d85a274d0961593db41cf85689db1d583cJoe Perches			}
204475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
205475be4d85a274d0961593db41cf85689db1d583cJoe Perches		else
206475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
207475be4d85a274d0961593db41cf85689db1d583cJoe Perches			c = -c;
208475be4d85a274d0961593db41cf85689db1d583cJoe Perches			d = -d;
209475be4d85a274d0961593db41cf85689db1d583cJoe Perches			for (j = 0; j < count; j++)
210475be4d85a274d0961593db41cf85689db1d583cJoe Perches			{
211475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q0 = sample[j] - q2 - ((c * (q1 >> 16)) + (((dword)(((dword) d) * ((dword)(q1 & 0xffff)))) >> 15));
212475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q2 = q1;
213475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q1 = q0;
214475be4d85a274d0961593db41cf85689db1d583cJoe Perches			}
215475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
216475be4d85a274d0961593db41cf85689db1d583cJoe Perches		buffer[i] = q1;
217475be4d85a274d0961593db41cf85689db1d583cJoe Perches		buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] = q2;
218475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
219475be4d85a274d0961593db41cf85689db1d583cJoe Perches	q1 = buffer[i];
220475be4d85a274d0961593db41cf85689db1d583cJoe Perches	q2 = buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT];
221475be4d85a274d0961593db41cf85689db1d583cJoe Perches	c = (coeffs[i] >> 1) << 1;
222475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (c >= 0)
223475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
224475be4d85a274d0961593db41cf85689db1d583cJoe Perches		for (j = 0; j < count; j++)
225475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
226475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q0 = sample[j] - q2 + (c * (q1 >> 16)) + (((dword)(((dword)(c >> 1)) * ((dword)(q1 & 0xffff)))) >> 15);
227475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q2 = q1;
228475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q1 = q0;
229475be4d85a274d0961593db41cf85689db1d583cJoe Perches			c -= CAPIDTMF_RECV_FUNDAMENTAL_DECREMENT;
230475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
231475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
232475be4d85a274d0961593db41cf85689db1d583cJoe Perches	else
233475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
234475be4d85a274d0961593db41cf85689db1d583cJoe Perches		c = -c;
235475be4d85a274d0961593db41cf85689db1d583cJoe Perches		for (j = 0; j < count; j++)
236475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
237475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q0 = sample[j] - q2 - ((c * (q1 >> 16)) + (((dword)(((dword)(c >> 1)) * ((dword)(q1 & 0xffff)))) >> 15));
238475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q2 = q1;
239475be4d85a274d0961593db41cf85689db1d583cJoe Perches			q1 = q0;
240475be4d85a274d0961593db41cf85689db1d583cJoe Perches			c += CAPIDTMF_RECV_FUNDAMENTAL_DECREMENT;
241475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
242475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
243475be4d85a274d0961593db41cf85689db1d583cJoe Perches	coeffs[i] = c;
244475be4d85a274d0961593db41cf85689db1d583cJoe Perches	buffer[i] = q1;
245475be4d85a274d0961593db41cf85689db1d583cJoe Perches	buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] = q2;
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
249475be4d85a274d0961593db41cf85689db1d583cJoe Perchesstatic void capidtmf_goertzel_result(long *buffer, long *coeffs)
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
251475be4d85a274d0961593db41cf85689db1d583cJoe Perches	int i;
252475be4d85a274d0961593db41cf85689db1d583cJoe Perches	long d, e, q1, q2, lo, mid, hi;
253475be4d85a274d0961593db41cf85689db1d583cJoe Perches	dword k;
254475be4d85a274d0961593db41cf85689db1d583cJoe Perches
255475be4d85a274d0961593db41cf85689db1d583cJoe Perches	for (i = 0; i < CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT; i++)
256475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
257475be4d85a274d0961593db41cf85689db1d583cJoe Perches		q1 = buffer[i];
258475be4d85a274d0961593db41cf85689db1d583cJoe Perches		q2 = buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT];
259475be4d85a274d0961593db41cf85689db1d583cJoe Perches		d = coeffs[i] >> 1;
260475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (d >= 0)
261475be4d85a274d0961593db41cf85689db1d583cJoe Perches			d = ((d << 1) * (-q1 >> 16)) + (((dword)(((dword) d) * ((dword)(-q1 & 0xffff)))) >> 15);
262475be4d85a274d0961593db41cf85689db1d583cJoe Perches		else
263475be4d85a274d0961593db41cf85689db1d583cJoe Perches			d = ((-d << 1) * (-q1 >> 16)) + (((dword)(((dword) -d) * ((dword)(-q1 & 0xffff)))) >> 15);
264475be4d85a274d0961593db41cf85689db1d583cJoe Perches		e = (q2 >= 0) ? q2 : -q2;
265475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (d >= 0)
266475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
267475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d & 0xffff)) * ((dword)(e & 0xffff));
268475be4d85a274d0961593db41cf85689db1d583cJoe Perches			lo = k & 0xffff;
269475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid = k >> 16;
270475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d >> 16)) * ((dword)(e & 0xffff));
271475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid += k & 0xffff;
272475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi = k >> 16;
273475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d & 0xffff)) * ((dword)(e >> 16));
274475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid += k & 0xffff;
275475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi += k >> 16;
276475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi += ((dword)(d >> 16)) * ((dword)(e >> 16));
277475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
278475be4d85a274d0961593db41cf85689db1d583cJoe Perches		else
279475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
280475be4d85a274d0961593db41cf85689db1d583cJoe Perches			d = -d;
281475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d & 0xffff)) * ((dword)(e & 0xffff));
282475be4d85a274d0961593db41cf85689db1d583cJoe Perches			lo = -((long)(k & 0xffff));
283475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid = -((long)(k >> 16));
284475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d >> 16)) * ((dword)(e & 0xffff));
285475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid -= k & 0xffff;
286475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi = -((long)(k >> 16));
287475be4d85a274d0961593db41cf85689db1d583cJoe Perches			k = ((dword)(d & 0xffff)) * ((dword)(e >> 16));
288475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid -= k & 0xffff;
289475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi -= k >> 16;
290475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi -= ((dword)(d >> 16)) * ((dword)(e >> 16));
291475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
292475be4d85a274d0961593db41cf85689db1d583cJoe Perches		if (q2 < 0)
293475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
294475be4d85a274d0961593db41cf85689db1d583cJoe Perches			lo = -lo;
295475be4d85a274d0961593db41cf85689db1d583cJoe Perches			mid = -mid;
296475be4d85a274d0961593db41cf85689db1d583cJoe Perches			hi = -hi;
297475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
298475be4d85a274d0961593db41cf85689db1d583cJoe Perches		d = (q1 >= 0) ? q1 : -q1;
299475be4d85a274d0961593db41cf85689db1d583cJoe Perches		k = ((dword)(d & 0xffff)) * ((dword)(d & 0xffff));
300475be4d85a274d0961593db41cf85689db1d583cJoe Perches		lo += k & 0xffff;
301475be4d85a274d0961593db41cf85689db1d583cJoe Perches		mid += k >> 16;
302475be4d85a274d0961593db41cf85689db1d583cJoe Perches		k = ((dword)(d >> 16)) * ((dword)(d & 0xffff));
303475be4d85a274d0961593db41cf85689db1d583cJoe Perches		mid += (k & 0xffff) << 1;
304475be4d85a274d0961593db41cf85689db1d583cJoe Perches		hi += (k >> 16) << 1;
305475be4d85a274d0961593db41cf85689db1d583cJoe Perches		hi += ((dword)(d >> 16)) * ((dword)(d >> 16));
306475be4d85a274d0961593db41cf85689db1d583cJoe Perches		d = (q2 >= 0) ? q2 : -q2;
307475be4d85a274d0961593db41cf85689db1d583cJoe Perches		k = ((dword)(d & 0xffff)) * ((dword)(d & 0xffff));
308475be4d85a274d0961593db41cf85689db1d583cJoe Perches		lo += k & 0xffff;
309475be4d85a274d0961593db41cf85689db1d583cJoe Perches		mid += k >> 16;
310475be4d85a274d0961593db41cf85689db1d583cJoe Perches		k = ((dword)(d >> 16)) * ((dword)(d & 0xffff));
311475be4d85a274d0961593db41cf85689db1d583cJoe Perches		mid += (k & 0xffff) << 1;
312475be4d85a274d0961593db41cf85689db1d583cJoe Perches		hi += (k >> 16) << 1;
313475be4d85a274d0961593db41cf85689db1d583cJoe Perches		hi += ((dword)(d >> 16)) * ((dword)(d >> 16));
314475be4d85a274d0961593db41cf85689db1d583cJoe Perches		mid += lo >> 16;
315475be4d85a274d0961593db41cf85689db1d583cJoe Perches		hi += mid >> 16;
316475be4d85a274d0961593db41cf85689db1d583cJoe Perches		buffer[i] = (lo & 0xffff) | (mid << 16);
317475be4d85a274d0961593db41cf85689db1d583cJoe Perches		buffer[i + CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] = hi;
318475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_697     0
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_770     1
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_852     2
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_941     3
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1209    4
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1336    5
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1477    6
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1633    7
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_635     8
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1010    9
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1140    10
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1272    11
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1405    12
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1555    13
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1715    14
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_INDEX_1875    15
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_GUARD_SNR_DONTCARE      0xc000
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_NO_DIGIT                0xff
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_TIME_GRANULARITY        (CAPIDTMF_RECV_ACCUMULATE_CYCLES + 1)
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_INDICATION_DIGIT        0x0001
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic long capidtmf_recv_goertzel_coef_table[CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] =
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
349475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xda97L * 2,  /* 697 Hz (Low group 697 Hz) */
350475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xd299L * 2,  /* 770 Hz (Low group 770 Hz) */
351475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xc8cbL * 2,  /* 852 Hz (Low group 852 Hz) */
352475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xbd36L * 2,  /* 941 Hz (Low group 941 Hz) */
353475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x9501L * 2,  /* 1209 Hz (High group 1209 Hz) */
354475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x7f89L * 2,  /* 1336 Hz (High group 1336 Hz) */
355475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x6639L * 2,  /* 1477 Hz (High group 1477 Hz) */
356475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x48c6L * 2,  /* 1633 Hz (High group 1633 Hz) */
357475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xe14cL * 2,  /* 630 Hz (Lower guard of low group 631 Hz) */
358475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xb2e0L * 2,  /* 1015 Hz (Upper guard of low group 1039 Hz) */
359475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0xa1a0L * 2,  /* 1130 Hz (Lower guard of high group 1140 Hz) */
360475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x8a87L * 2,  /* 1272 Hz (Guard between 1209 Hz and 1336 Hz: 1271 Hz) */
361475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x7353L * 2,  /* 1405 Hz (2nd harmonics of 697 Hz and guard between 1336 Hz and 1477 Hz: 1405 Hz) */
362475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x583bL * 2,  /* 1552 Hz (2nd harmonics of 770 Hz and guard between 1477 Hz and 1633 Hz: 1553 Hz) */
363475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x37d8L * 2,  /* 1720 Hz (2nd harmonics of 852 Hz and upper guard of high group: 1715 Hz) */
364475be4d85a274d0961593db41cf85689db1d583cJoe Perches	0x0000L * 2   /* 100-630 Hz (fundamentals) */
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic word capidtmf_recv_guard_snr_low_table[CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] =
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
370475be4d85a274d0961593db41cf85689db1d583cJoe Perches	14,                                    /* Low group peak versus 697 Hz */
371475be4d85a274d0961593db41cf85689db1d583cJoe Perches	14,                                    /* Low group peak versus 770 Hz */
372475be4d85a274d0961593db41cf85689db1d583cJoe Perches	16,                                    /* Low group peak versus 852 Hz */
373475be4d85a274d0961593db41cf85689db1d583cJoe Perches	16,                                    /* Low group peak versus 941 Hz */
374475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1209 Hz */
375475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1336 Hz */
376475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1477 Hz */
377475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1633 Hz */
378475be4d85a274d0961593db41cf85689db1d583cJoe Perches	14,                                    /* Low group peak versus 635 Hz */
379475be4d85a274d0961593db41cf85689db1d583cJoe Perches	16,                                    /* Low group peak versus 1010 Hz */
380475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1140 Hz */
381475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* Low group peak versus 1272 Hz */
382475be4d85a274d0961593db41cf85689db1d583cJoe Perches	DSPDTMF_RX_HARMONICS_SEL_DEFAULT - 8,  /* Low group peak versus 1405 Hz */
383475be4d85a274d0961593db41cf85689db1d583cJoe Perches	DSPDTMF_RX_HARMONICS_SEL_DEFAULT - 4,  /* Low group peak versus 1555 Hz */
384475be4d85a274d0961593db41cf85689db1d583cJoe Perches	DSPDTMF_RX_HARMONICS_SEL_DEFAULT - 4,  /* Low group peak versus 1715 Hz */
385475be4d85a274d0961593db41cf85689db1d583cJoe Perches	12                                     /* Low group peak versus 100-630 Hz */
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic word capidtmf_recv_guard_snr_high_table[CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT] =
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
391475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 697 Hz */
392475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 770 Hz */
393475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 852 Hz */
394475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 941 Hz */
395475be4d85a274d0961593db41cf85689db1d583cJoe Perches	20,                                    /* High group peak versus 1209 Hz */
396475be4d85a274d0961593db41cf85689db1d583cJoe Perches	20,                                    /* High group peak versus 1336 Hz */
397475be4d85a274d0961593db41cf85689db1d583cJoe Perches	20,                                    /* High group peak versus 1477 Hz */
398475be4d85a274d0961593db41cf85689db1d583cJoe Perches	20,                                    /* High group peak versus 1633 Hz */
399475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 635 Hz */
400475be4d85a274d0961593db41cf85689db1d583cJoe Perches	CAPIDTMF_RECV_GUARD_SNR_DONTCARE,      /* High group peak versus 1010 Hz */
401475be4d85a274d0961593db41cf85689db1d583cJoe Perches	16,                                    /* High group peak versus 1140 Hz */
402475be4d85a274d0961593db41cf85689db1d583cJoe Perches	4,                                     /* High group peak versus 1272 Hz */
403475be4d85a274d0961593db41cf85689db1d583cJoe Perches	6,                                     /* High group peak versus 1405 Hz */
404475be4d85a274d0961593db41cf85689db1d583cJoe Perches	8,                                     /* High group peak versus 1555 Hz */
405475be4d85a274d0961593db41cf85689db1d583cJoe Perches	16,                                    /* High group peak versus 1715 Hz */
406475be4d85a274d0961593db41cf85689db1d583cJoe Perches	12                                     /* High group peak versus 100-630 Hz */
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
412475be4d85a274d0961593db41cf85689db1d583cJoe Perchesstatic void capidtmf_recv_init(t_capidtmf_state *p_state)
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
414475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.min_gap_duration = 1;
415475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.min_digit_duration = 1;
416475be4d85a274d0961593db41cf85689db1d583cJoe Perches
417475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.cycle_counter = 0;
418475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.current_digit_on_time = 0;
419475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.current_digit_off_time = 0;
420475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.current_digit_value = CAPIDTMF_RECV_NO_DIGIT;
421475be4d85a274d0961593db41cf85689db1d583cJoe Perches
422475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.digit_write_pos = 0;
423475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.digit_read_pos = 0;
424475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.indication_state = 0;
425475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.indication_state_ack = 0;
426475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.state = CAPIDTMF_RECV_STATE_IDLE;
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
430475be4d85a274d0961593db41cf85689db1d583cJoe Perchesvoid capidtmf_recv_enable(t_capidtmf_state *p_state, word min_digit_duration, word min_gap_duration)
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
432475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.indication_state_ack &= CAPIDTMF_RECV_INDICATION_DIGIT;
433475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.min_digit_duration = (word)(((((dword) min_digit_duration) * 8) +
434475be4d85a274d0961593db41cf85689db1d583cJoe Perches						   ((dword)(CAPIDTMF_RECV_TIME_GRANULARITY / 2))) / ((dword) CAPIDTMF_RECV_TIME_GRANULARITY));
435475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (p_state->recv.min_digit_duration <= 1)
436475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.min_digit_duration = 1;
437475be4d85a274d0961593db41cf85689db1d583cJoe Perches	else
438475be4d85a274d0961593db41cf85689db1d583cJoe Perches		(p_state->recv.min_digit_duration)--;
439475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.min_gap_duration =
440475be4d85a274d0961593db41cf85689db1d583cJoe Perches		(word)((((dword) min_gap_duration) * 8) / ((dword) CAPIDTMF_RECV_TIME_GRANULARITY));
441475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (p_state->recv.min_gap_duration <= 1)
442475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.min_gap_duration = 1;
443475be4d85a274d0961593db41cf85689db1d583cJoe Perches	else
444475be4d85a274d0961593db41cf85689db1d583cJoe Perches		(p_state->recv.min_gap_duration)--;
445475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.state |= CAPIDTMF_RECV_STATE_DTMF_ACTIVE;
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
449475be4d85a274d0961593db41cf85689db1d583cJoe Perchesvoid capidtmf_recv_disable(t_capidtmf_state *p_state)
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
451475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.state &= ~CAPIDTMF_RECV_STATE_DTMF_ACTIVE;
452475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (p_state->recv.state == CAPIDTMF_RECV_STATE_IDLE)
453475be4d85a274d0961593db41cf85689db1d583cJoe Perches		capidtmf_recv_init(p_state);
454475be4d85a274d0961593db41cf85689db1d583cJoe Perches	else
455475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
456475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.cycle_counter = 0;
457475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.current_digit_on_time = 0;
458475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.current_digit_off_time = 0;
459475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.current_digit_value = CAPIDTMF_RECV_NO_DIGIT;
460475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
464475be4d85a274d0961593db41cf85689db1d583cJoe Perchesword capidtmf_recv_indication(t_capidtmf_state *p_state, byte *buffer)
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
466475be4d85a274d0961593db41cf85689db1d583cJoe Perches	word i, j, k, flags;
467475be4d85a274d0961593db41cf85689db1d583cJoe Perches
468475be4d85a274d0961593db41cf85689db1d583cJoe Perches	flags = p_state->recv.indication_state ^ p_state->recv.indication_state_ack;
469475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.indication_state_ack ^= flags & CAPIDTMF_RECV_INDICATION_DIGIT;
470475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (p_state->recv.digit_write_pos != p_state->recv.digit_read_pos)
471475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
472475be4d85a274d0961593db41cf85689db1d583cJoe Perches		i = 0;
473475be4d85a274d0961593db41cf85689db1d583cJoe Perches		k = p_state->recv.digit_write_pos;
474475be4d85a274d0961593db41cf85689db1d583cJoe Perches		j = p_state->recv.digit_read_pos;
475475be4d85a274d0961593db41cf85689db1d583cJoe Perches		do
476475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
477475be4d85a274d0961593db41cf85689db1d583cJoe Perches			buffer[i++] = p_state->recv.digit_buffer[j];
478475be4d85a274d0961593db41cf85689db1d583cJoe Perches			j = (j == CAPIDTMF_RECV_DIGIT_BUFFER_SIZE - 1) ? 0 : j + 1;
479475be4d85a274d0961593db41cf85689db1d583cJoe Perches		} while (j != k);
480475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.digit_read_pos = k;
481475be4d85a274d0961593db41cf85689db1d583cJoe Perches		return (i);
482475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
483475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->recv.indication_state_ack ^= flags;
484475be4d85a274d0961593db41cf85689db1d583cJoe Perches	return (0);
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CAPIDTMF_RECV_WINDOWED_SAMPLES  32
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
490475be4d85a274d0961593db41cf85689db1d583cJoe Perchesvoid capidtmf_recv_block(t_capidtmf_state *p_state, byte *buffer, word length)
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
492475be4d85a274d0961593db41cf85689db1d583cJoe Perches	byte result_digit;
493475be4d85a274d0961593db41cf85689db1d583cJoe Perches	word sample_number, cycle_counter, n, i;
494475be4d85a274d0961593db41cf85689db1d583cJoe Perches	word low_peak, high_peak;
495475be4d85a274d0961593db41cf85689db1d583cJoe Perches	dword lo, hi;
496475be4d85a274d0961593db41cf85689db1d583cJoe Perches	byte   *p;
497475be4d85a274d0961593db41cf85689db1d583cJoe Perches	short *q;
498475be4d85a274d0961593db41cf85689db1d583cJoe Perches	byte goertzel_result_buffer[CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT];
499475be4d85a274d0961593db41cf85689db1d583cJoe Perches	short windowed_sample_buffer[CAPIDTMF_RECV_WINDOWED_SAMPLES];
500475be4d85a274d0961593db41cf85689db1d583cJoe Perches
501475be4d85a274d0961593db41cf85689db1d583cJoe Perches
502475be4d85a274d0961593db41cf85689db1d583cJoe Perches	if (p_state->recv.state & CAPIDTMF_RECV_STATE_DTMF_ACTIVE)
503475be4d85a274d0961593db41cf85689db1d583cJoe Perches	{
504475be4d85a274d0961593db41cf85689db1d583cJoe Perches		cycle_counter = p_state->recv.cycle_counter;
505475be4d85a274d0961593db41cf85689db1d583cJoe Perches		sample_number = 0;
506475be4d85a274d0961593db41cf85689db1d583cJoe Perches		while (sample_number < length)
507475be4d85a274d0961593db41cf85689db1d583cJoe Perches		{
508475be4d85a274d0961593db41cf85689db1d583cJoe Perches			if (cycle_counter < CAPIDTMF_RECV_ACCUMULATE_CYCLES)
509475be4d85a274d0961593db41cf85689db1d583cJoe Perches			{
510475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (cycle_counter == 0)
511475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
512475be4d85a274d0961593db41cf85689db1d583cJoe Perches					for (i = 0; i < CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT; i++)
513475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
514475be4d85a274d0961593db41cf85689db1d583cJoe Perches						p_state->recv.goertzel_buffer[0][i] = 0;
515475be4d85a274d0961593db41cf85689db1d583cJoe Perches						p_state->recv.goertzel_buffer[1][i] = 0;
516475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
517475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
518475be4d85a274d0961593db41cf85689db1d583cJoe Perches				n = CAPIDTMF_RECV_ACCUMULATE_CYCLES - cycle_counter;
519475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (n > length - sample_number)
520475be4d85a274d0961593db41cf85689db1d583cJoe Perches					n = length - sample_number;
521475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (n > CAPIDTMF_RECV_WINDOWED_SAMPLES)
522475be4d85a274d0961593db41cf85689db1d583cJoe Perches					n = CAPIDTMF_RECV_WINDOWED_SAMPLES;
523475be4d85a274d0961593db41cf85689db1d583cJoe Perches				p = buffer + sample_number;
524475be4d85a274d0961593db41cf85689db1d583cJoe Perches				q = capidtmf_recv_window_function + cycle_counter;
525475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (p_state->ulaw)
526475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
527475be4d85a274d0961593db41cf85689db1d583cJoe Perches					for (i = 0; i < n; i++)
528475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
529475be4d85a274d0961593db41cf85689db1d583cJoe Perches						windowed_sample_buffer[i] =
530475be4d85a274d0961593db41cf85689db1d583cJoe Perches							(short)((capidtmf_expand_table_ulaw[p[i]] * ((long)(q[i]))) >> 15);
531475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
532475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
533475be4d85a274d0961593db41cf85689db1d583cJoe Perches				else
534475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
535475be4d85a274d0961593db41cf85689db1d583cJoe Perches					for (i = 0; i < n; i++)
536475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
537475be4d85a274d0961593db41cf85689db1d583cJoe Perches						windowed_sample_buffer[i] =
538475be4d85a274d0961593db41cf85689db1d583cJoe Perches							(short)((capidtmf_expand_table_alaw[p[i]] * ((long)(q[i]))) >> 15);
539475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
540475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
541475be4d85a274d0961593db41cf85689db1d583cJoe Perches				capidtmf_recv_goertzel_coef_table[CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT - 1] = CAPIDTMF_RECV_FUNDAMENTAL_OFFSET;
542475be4d85a274d0961593db41cf85689db1d583cJoe Perches				capidtmf_goertzel_loop(p_state->recv.goertzel_buffer[0],
543475be4d85a274d0961593db41cf85689db1d583cJoe Perches						       capidtmf_recv_goertzel_coef_table, windowed_sample_buffer, n);
544475be4d85a274d0961593db41cf85689db1d583cJoe Perches				cycle_counter += n;
545475be4d85a274d0961593db41cf85689db1d583cJoe Perches				sample_number += n;
546475be4d85a274d0961593db41cf85689db1d583cJoe Perches			}
547475be4d85a274d0961593db41cf85689db1d583cJoe Perches			else
548475be4d85a274d0961593db41cf85689db1d583cJoe Perches			{
549475be4d85a274d0961593db41cf85689db1d583cJoe Perches				capidtmf_goertzel_result(p_state->recv.goertzel_buffer[0],
550475be4d85a274d0961593db41cf85689db1d583cJoe Perches							 capidtmf_recv_goertzel_coef_table);
551475be4d85a274d0961593db41cf85689db1d583cJoe Perches				for (i = 0; i < CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT; i++)
552475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
553475be4d85a274d0961593db41cf85689db1d583cJoe Perches					lo = (dword)(p_state->recv.goertzel_buffer[0][i]);
554475be4d85a274d0961593db41cf85689db1d583cJoe Perches					hi = (dword)(p_state->recv.goertzel_buffer[1][i]);
555475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if (hi != 0)
556475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
557475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n = capidtmf_dword_leading_zeroes(hi);
558475be4d85a274d0961593db41cf85689db1d583cJoe Perches						hi = (hi << n) | (lo >> (32 - n));
559475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
560475be4d85a274d0961593db41cf85689db1d583cJoe Perches					else
561475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
562475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n = capidtmf_dword_leading_zeroes(lo);
563475be4d85a274d0961593db41cf85689db1d583cJoe Perches						hi = lo << n;
564475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n += 32;
565475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
566475be4d85a274d0961593db41cf85689db1d583cJoe Perches					n = 195 - 3 * n;
567475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if (hi >= 0xcb300000L)
568475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n += 2;
569475be4d85a274d0961593db41cf85689db1d583cJoe Perches					else if (hi >= 0xa1450000L)
570475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n++;
571475be4d85a274d0961593db41cf85689db1d583cJoe Perches					goertzel_result_buffer[i] = (byte) n;
572475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
573475be4d85a274d0961593db41cf85689db1d583cJoe Perches				low_peak = DSPDTMF_RX_SENSITIVITY_LOW_DEFAULT;
574475be4d85a274d0961593db41cf85689db1d583cJoe Perches				result_digit = CAPIDTMF_RECV_NO_DIGIT;
575475be4d85a274d0961593db41cf85689db1d583cJoe Perches				for (i = 0; i < CAPIDTMF_LOW_GROUP_FREQUENCIES; i++)
576475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
577475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if (goertzel_result_buffer[i] > low_peak)
578475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
579475be4d85a274d0961593db41cf85689db1d583cJoe Perches						low_peak = goertzel_result_buffer[i];
580475be4d85a274d0961593db41cf85689db1d583cJoe Perches						result_digit = (byte) i;
581475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
582475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
583475be4d85a274d0961593db41cf85689db1d583cJoe Perches				high_peak = DSPDTMF_RX_SENSITIVITY_HIGH_DEFAULT;
584475be4d85a274d0961593db41cf85689db1d583cJoe Perches				n = CAPIDTMF_RECV_NO_DIGIT;
585475be4d85a274d0961593db41cf85689db1d583cJoe Perches				for (i = CAPIDTMF_LOW_GROUP_FREQUENCIES; i < CAPIDTMF_RECV_BASE_FREQUENCY_COUNT; i++)
586475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
587475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if (goertzel_result_buffer[i] > high_peak)
588475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
589475be4d85a274d0961593db41cf85689db1d583cJoe Perches						high_peak = goertzel_result_buffer[i];
590475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n = (i - CAPIDTMF_LOW_GROUP_FREQUENCIES) << 2;
591475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
592475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
593475be4d85a274d0961593db41cf85689db1d583cJoe Perches				result_digit |= (byte) n;
594475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (low_peak + DSPDTMF_RX_HIGH_EXCEEDING_LOW_DEFAULT < high_peak)
595475be4d85a274d0961593db41cf85689db1d583cJoe Perches					result_digit = CAPIDTMF_RECV_NO_DIGIT;
596475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (high_peak + DSPDTMF_RX_LOW_EXCEEDING_HIGH_DEFAULT < low_peak)
597475be4d85a274d0961593db41cf85689db1d583cJoe Perches					result_digit = CAPIDTMF_RECV_NO_DIGIT;
598475be4d85a274d0961593db41cf85689db1d583cJoe Perches				n = 0;
599475be4d85a274d0961593db41cf85689db1d583cJoe Perches				for (i = 0; i < CAPIDTMF_RECV_TOTAL_FREQUENCY_COUNT; i++)
600475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
601475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if ((((short)(low_peak - goertzel_result_buffer[i] - capidtmf_recv_guard_snr_low_table[i])) < 0)
602475be4d85a274d0961593db41cf85689db1d583cJoe Perches					    || (((short)(high_peak - goertzel_result_buffer[i] - capidtmf_recv_guard_snr_high_table[i])) < 0))
603475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
604475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n++;
605475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
606475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
607475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (n != 2)
608475be4d85a274d0961593db41cf85689db1d583cJoe Perches					result_digit = CAPIDTMF_RECV_NO_DIGIT;
609475be4d85a274d0961593db41cf85689db1d583cJoe Perches
610475be4d85a274d0961593db41cf85689db1d583cJoe Perches				if (result_digit == CAPIDTMF_RECV_NO_DIGIT)
611475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
612475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if (p_state->recv.current_digit_on_time != 0)
613475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
614475be4d85a274d0961593db41cf85689db1d583cJoe Perches						if (++(p_state->recv.current_digit_off_time) >= p_state->recv.min_gap_duration)
615475be4d85a274d0961593db41cf85689db1d583cJoe Perches						{
616475be4d85a274d0961593db41cf85689db1d583cJoe Perches							p_state->recv.current_digit_on_time = 0;
617475be4d85a274d0961593db41cf85689db1d583cJoe Perches							p_state->recv.current_digit_off_time = 0;
618475be4d85a274d0961593db41cf85689db1d583cJoe Perches						}
619475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
620475be4d85a274d0961593db41cf85689db1d583cJoe Perches					else
621475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
622475be4d85a274d0961593db41cf85689db1d583cJoe Perches						if (p_state->recv.current_digit_off_time != 0)
623475be4d85a274d0961593db41cf85689db1d583cJoe Perches							(p_state->recv.current_digit_off_time)--;
624475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
625475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
626475be4d85a274d0961593db41cf85689db1d583cJoe Perches				else
627475be4d85a274d0961593db41cf85689db1d583cJoe Perches				{
628475be4d85a274d0961593db41cf85689db1d583cJoe Perches					if ((p_state->recv.current_digit_on_time == 0)
629475be4d85a274d0961593db41cf85689db1d583cJoe Perches					    && (p_state->recv.current_digit_off_time != 0))
630475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
631475be4d85a274d0961593db41cf85689db1d583cJoe Perches						(p_state->recv.current_digit_off_time)--;
632475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
633475be4d85a274d0961593db41cf85689db1d583cJoe Perches					else
634475be4d85a274d0961593db41cf85689db1d583cJoe Perches					{
635475be4d85a274d0961593db41cf85689db1d583cJoe Perches						n = p_state->recv.current_digit_off_time;
636475be4d85a274d0961593db41cf85689db1d583cJoe Perches						if ((p_state->recv.current_digit_on_time != 0)
637475be4d85a274d0961593db41cf85689db1d583cJoe Perches						    && (result_digit != p_state->recv.current_digit_value))
638475be4d85a274d0961593db41cf85689db1d583cJoe Perches						{
639475be4d85a274d0961593db41cf85689db1d583cJoe Perches							p_state->recv.current_digit_on_time = 0;
640475be4d85a274d0961593db41cf85689db1d583cJoe Perches							n = 0;
641475be4d85a274d0961593db41cf85689db1d583cJoe Perches						}
642475be4d85a274d0961593db41cf85689db1d583cJoe Perches						p_state->recv.current_digit_value = result_digit;
643475be4d85a274d0961593db41cf85689db1d583cJoe Perches						p_state->recv.current_digit_off_time = 0;
644475be4d85a274d0961593db41cf85689db1d583cJoe Perches						if (p_state->recv.current_digit_on_time != 0xffff)
645475be4d85a274d0961593db41cf85689db1d583cJoe Perches						{
646475be4d85a274d0961593db41cf85689db1d583cJoe Perches							p_state->recv.current_digit_on_time += n + 1;
647475be4d85a274d0961593db41cf85689db1d583cJoe Perches							if (p_state->recv.current_digit_on_time >= p_state->recv.min_digit_duration)
648475be4d85a274d0961593db41cf85689db1d583cJoe Perches							{
649475be4d85a274d0961593db41cf85689db1d583cJoe Perches								p_state->recv.current_digit_on_time = 0xffff;
650475be4d85a274d0961593db41cf85689db1d583cJoe Perches								i = (p_state->recv.digit_write_pos == CAPIDTMF_RECV_DIGIT_BUFFER_SIZE - 1) ?
651475be4d85a274d0961593db41cf85689db1d583cJoe Perches									0 : p_state->recv.digit_write_pos + 1;
652475be4d85a274d0961593db41cf85689db1d583cJoe Perches								if (i == p_state->recv.digit_read_pos)
653475be4d85a274d0961593db41cf85689db1d583cJoe Perches								{
654475be4d85a274d0961593db41cf85689db1d583cJoe Perches									trace(dprintf("%s,%d: Receive digit overrun",
655475be4d85a274d0961593db41cf85689db1d583cJoe Perches										      (char *)(FILE_), __LINE__));
656475be4d85a274d0961593db41cf85689db1d583cJoe Perches								}
657475be4d85a274d0961593db41cf85689db1d583cJoe Perches								else
658475be4d85a274d0961593db41cf85689db1d583cJoe Perches								{
659475be4d85a274d0961593db41cf85689db1d583cJoe Perches									p_state->recv.digit_buffer[p_state->recv.digit_write_pos] = result_digit;
660475be4d85a274d0961593db41cf85689db1d583cJoe Perches									p_state->recv.digit_write_pos = i;
661475be4d85a274d0961593db41cf85689db1d583cJoe Perches									p_state->recv.indication_state =
662475be4d85a274d0961593db41cf85689db1d583cJoe Perches										(p_state->recv.indication_state & ~CAPIDTMF_RECV_INDICATION_DIGIT) |
663475be4d85a274d0961593db41cf85689db1d583cJoe Perches										(~p_state->recv.indication_state_ack & CAPIDTMF_RECV_INDICATION_DIGIT);
664475be4d85a274d0961593db41cf85689db1d583cJoe Perches								}
665475be4d85a274d0961593db41cf85689db1d583cJoe Perches							}
666475be4d85a274d0961593db41cf85689db1d583cJoe Perches						}
667475be4d85a274d0961593db41cf85689db1d583cJoe Perches					}
668475be4d85a274d0961593db41cf85689db1d583cJoe Perches				}
669475be4d85a274d0961593db41cf85689db1d583cJoe Perches				cycle_counter = 0;
670475be4d85a274d0961593db41cf85689db1d583cJoe Perches				sample_number++;
671475be4d85a274d0961593db41cf85689db1d583cJoe Perches			}
672475be4d85a274d0961593db41cf85689db1d583cJoe Perches		}
673475be4d85a274d0961593db41cf85689db1d583cJoe Perches		p_state->recv.cycle_counter = cycle_counter;
674475be4d85a274d0961593db41cf85689db1d583cJoe Perches	}
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
678475be4d85a274d0961593db41cf85689db1d583cJoe Perchesvoid capidtmf_init(t_capidtmf_state *p_state, byte ulaw)
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
680475be4d85a274d0961593db41cf85689db1d583cJoe Perches	p_state->ulaw = ulaw;
681475be4d85a274d0961593db41cf85689db1d583cJoe Perches	capidtmf_recv_init(p_state);
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*---------------------------------------------------------------------------*/
686