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