| 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 |
#include <linux/kernel.h> |
| 39 |
#include <linux/slab.h> |
| 40 |
#include <linux/errno.h> |
| 41 |
#include <linux/module.h> |
| 42 |
#include <linux/init.h> |
| 43 |
#include <linux/ctype.h> |
| 44 |
#include <linux/moduleparam.h> |
| 45 |
|
| 46 |
#include <dahdi/kernel.h> |
| 47 |
|
| 48 |
static int debug; |
| 49 |
|
| 50 |
#include "fir.h" |
| 51 |
|
| 52 |
#ifndef NULL |
| 53 |
#define NULL 0 |
| 54 |
#endif |
| 55 |
#ifndef FALSE |
| 56 |
#define FALSE 0 |
| 57 |
#endif |
| 58 |
#ifndef TRUE |
| 59 |
#define TRUE (!FALSE) |
| 60 |
#endif |
| 61 |
|
| 62 |
#define NONUPDATE_DWELL_TIME 600 |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define MIN_TX_POWER_FOR_ADAPTION 512 |
| 68 |
#define MIN_RX_POWER_FOR_ADAPTION 64 |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 77 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec); |
| 78 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec); |
| 79 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size); |
| 80 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val); |
| 81 |
static const char *name = "SEC2"; |
| 82 |
static const char *ec_name(const struct dahdi_chan *chan) { return name; } |
| 83 |
|
| 84 |
static const struct dahdi_echocan_factory my_factory = { |
| 85 |
.get_name = ec_name, |
| 86 |
.owner = THIS_MODULE, |
| 87 |
.echocan_create = echo_can_create, |
| 88 |
}; |
| 89 |
|
| 90 |
static const struct dahdi_echocan_ops my_ops = { |
| 91 |
.echocan_free = echo_can_free, |
| 92 |
.echocan_process = echo_can_process, |
| 93 |
.echocan_traintap = echo_can_traintap, |
| 94 |
}; |
| 95 |
|
| 96 |
struct ec_pvt { |
| 97 |
struct dahdi_echocan_state dahdi; |
| 98 |
int tx_power; |
| 99 |
int rx_power; |
| 100 |
int clean_rx_power; |
| 101 |
|
| 102 |
int rx_power_threshold; |
| 103 |
int nonupdate_dwell; |
| 104 |
|
| 105 |
fir16_state_t fir_state; |
| 106 |
int16_t *fir_taps16; |
| 107 |
int32_t *fir_taps32; |
| 108 |
|
| 109 |
int curr_pos; |
| 110 |
|
| 111 |
int taps; |
| 112 |
int tap_mask; |
| 113 |
int use_nlp; |
| 114 |
int use_suppressor; |
| 115 |
|
| 116 |
int32_t supp_test1; |
| 117 |
int32_t supp_test2; |
| 118 |
int32_t supp1; |
| 119 |
int32_t supp2; |
| 120 |
|
| 121 |
int32_t latest_correction; |
| 122 |
|
| 123 |
|
| 124 |
}; |
| 125 |
|
| 126 |
#define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi) |
| 127 |
|
| 128 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 129 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec) |
| 130 |
{ |
| 131 |
struct ec_pvt *pvt; |
| 132 |
size_t size; |
| 133 |
|
| 134 |
if (ecp->param_count > 0) { |
| 135 |
printk(KERN_WARNING "SEC2 does not support parameters; failing request\n"); |
| 136 |
return -EINVAL; |
| 137 |
} |
| 138 |
|
| 139 |
size = sizeof(*pvt) + ecp->tap_length * sizeof(int32_t) + ecp->tap_length * 3 * sizeof(int16_t); |
| 140 |
|
| 141 |
pvt = kzalloc(size, GFP_KERNEL); |
| 142 |
if (!pvt) |
| 143 |
return -ENOMEM; |
| 144 |
|
| 145 |
pvt->dahdi.ops = &my_ops; |
| 146 |
|
| 147 |
if (ecp->param_count > 0) { |
| 148 |
printk(KERN_WARNING "SEC-2 echo canceler does not support parameters; failing request\n"); |
| 149 |
return -EINVAL; |
| 150 |
} |
| 151 |
|
| 152 |
pvt->taps = ecp->tap_length; |
| 153 |
pvt->curr_pos = ecp->tap_length - 1; |
| 154 |
pvt->tap_mask = ecp->tap_length - 1; |
| 155 |
pvt->fir_taps32 = (int32_t *) (pvt + sizeof(*pvt)); |
| 156 |
pvt->fir_taps16 = (int16_t *) (pvt + sizeof(*pvt) + ecp->tap_length * sizeof(int32_t)); |
| 157 |
|
| 158 |
fir16_create(&pvt->fir_state, pvt->fir_taps16, pvt->taps); |
| 159 |
pvt->rx_power_threshold = 10000000; |
| 160 |
pvt->use_suppressor = FALSE; |
| 161 |
|
| 162 |
|
| 163 |
pvt->use_nlp = FALSE; |
| 164 |
|
| 165 |
*ec = &pvt->dahdi; |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec) |
| 170 |
{ |
| 171 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 172 |
|
| 173 |
fir16_free(&pvt->fir_state); |
| 174 |
kfree(pvt); |
| 175 |
} |
| 176 |
|
| 177 |
static inline int16_t sample_update(struct ec_pvt *pvt, int16_t tx, int16_t rx) |
| 178 |
{ |
| 179 |
int offset1; |
| 180 |
int offset2; |
| 181 |
int32_t echo_value; |
| 182 |
int clean_rx; |
| 183 |
int nsuppr; |
| 184 |
int i; |
| 185 |
int correction; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
echo_value = fir16 (&pvt->fir_state, tx); |
| 202 |
|
| 203 |
|
| 204 |
clean_rx = rx - echo_value; |
| 205 |
|
| 206 |
|
| 207 |
if (pvt->nonupdate_dwell > 0) |
| 208 |
pvt->nonupdate_dwell--; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (pvt->tx_power > MIN_TX_POWER_FOR_ADAPTION && pvt->rx_power > MIN_RX_POWER_FOR_ADAPTION) { |
| 219 |
|
| 220 |
|
| 221 |
if (pvt->tx_power > 2*pvt->rx_power) { |
| 222 |
|
| 223 |
if (pvt->nonupdate_dwell == 0) { |
| 224 |
|
| 225 |
|
| 226 |
nsuppr = clean_rx >> 3; |
| 227 |
|
| 228 |
|
| 229 |
offset2 = pvt->curr_pos + 1; |
| 230 |
offset1 = pvt->taps - offset2; |
| 231 |
pvt->latest_correction = 0; |
| 232 |
for (i = pvt->taps - 1; i >= offset1; i--) { |
| 233 |
correction = pvt->fir_state.history[i - offset1]*nsuppr; |
| 234 |
|
| 235 |
|
| 236 |
pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12); |
| 237 |
pvt->fir_taps32[i] += correction; |
| 238 |
pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15; |
| 239 |
pvt->latest_correction += abs(correction); |
| 240 |
} |
| 241 |
for ( ; i >= 0; i--) { |
| 242 |
correction = pvt->fir_state.history[i + offset2]*nsuppr; |
| 243 |
|
| 244 |
|
| 245 |
pvt->fir_taps32[i] -= (pvt->fir_taps32[i] >> 12); |
| 246 |
pvt->fir_taps32[i] += correction; |
| 247 |
pvt->fir_state.coeffs[i] = pvt->fir_taps32[i] >> 15; |
| 248 |
pvt->latest_correction += abs(correction); |
| 249 |
} |
| 250 |
} else { |
| 251 |
pvt->latest_correction = -1; |
| 252 |
} |
| 253 |
} else { |
| 254 |
pvt->nonupdate_dwell = NONUPDATE_DWELL_TIME; |
| 255 |
pvt->latest_correction = -2; |
| 256 |
} |
| 257 |
} else { |
| 258 |
pvt->nonupdate_dwell = 0; |
| 259 |
pvt->latest_correction = -3; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pvt->tx_power += ((abs(tx) - pvt->tx_power) >> 5); |
| 265 |
pvt->rx_power += ((abs(rx) - pvt->rx_power) >> 5); |
| 266 |
pvt->clean_rx_power += ((abs(clean_rx) - pvt->clean_rx_power) >> 5); |
| 267 |
|
| 268 |
#if defined(XYZZY) |
| 269 |
if (pvt->use_suppressor) { |
| 270 |
pvt->supp_test1 += (pvt->fir_state.history[pvt->curr_pos] - pvt->fir_state.history[(pvt->curr_pos - 7) & pvt->tap_mask]); |
| 271 |
pvt->supp_test2 += (pvt->fir_state.history[(pvt->curr_pos - 24) & pvt->tap_mask] - pvt->fir_state.history[(pvt->curr_pos - 31) & pvt->tap_mask]); |
| 272 |
if (pvt->supp_test1 > 42 && pvt->supp_test2 > 42) |
| 273 |
supp_change = 25; |
| 274 |
else |
| 275 |
supp_change = 50; |
| 276 |
supp = supp_change + k1*pvt->supp1 + k2*pvt->supp2; |
| 277 |
pvt->supp2 = pvt->supp1; |
| 278 |
pvt->supp1 = supp; |
| 279 |
clean_rx *= (1 - supp); |
| 280 |
} |
| 281 |
#endif |
| 282 |
|
| 283 |
if (pvt->use_nlp && pvt->rx_power < 32) |
| 284 |
clean_rx = 0; |
| 285 |
|
| 286 |
|
| 287 |
if (pvt->curr_pos <= 0) |
| 288 |
pvt->curr_pos = pvt->taps; |
| 289 |
pvt->curr_pos--; |
| 290 |
|
| 291 |
return clean_rx; |
| 292 |
} |
| 293 |
|
| 294 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size) |
| 295 |
{ |
| 296 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 297 |
u32 x; |
| 298 |
short result; |
| 299 |
|
| 300 |
for (x = 0; x < size; x++) { |
| 301 |
result = sample_update(pvt, *iref, *isig); |
| 302 |
*isig++ = result; |
| 303 |
++iref; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val) |
| 308 |
{ |
| 309 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
pvt->nonupdate_dwell = pvt->taps << 1; |
| 314 |
if (pos >= pvt->taps) |
| 315 |
return 1; |
| 316 |
pvt->fir_taps32[pos] = val << 17; |
| 317 |
pvt->fir_taps16[pos] = val << 1; |
| 318 |
if (++pos >= pvt->taps) |
| 319 |
return 1; |
| 320 |
else |
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
static int __init mod_init(void) |
| 325 |
{ |
| 326 |
if (dahdi_register_echocan_factory(&my_factory)) { |
| 327 |
module_printk(KERN_ERR, "could not register with DAHDI core\n"); |
| 328 |
|
| 329 |
return -EPERM; |
| 330 |
} |
| 331 |
|
| 332 |
module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n", |
| 333 |
my_factory.get_name(NULL)); |
| 334 |
|
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
static void __exit mod_exit(void) |
| 339 |
{ |
| 340 |
dahdi_unregister_echocan_factory(&my_factory); |
| 341 |
} |
| 342 |
|
| 343 |
module_param(debug, int, S_IRUGO | S_IWUSR); |
| 344 |
|
| 345 |
MODULE_DESCRIPTION("DAHDI 'SEC2' Echo Canceler"); |
| 346 |
MODULE_AUTHOR("Steve Underwood <[email protected]>"); |
| 347 |
MODULE_LICENSE("GPL"); |
| 348 |
|
| 349 |
module_init(mod_init); |
| 350 |
module_exit(mod_exit); |