| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <linux/kernel.h> |
| 23 |
#include <linux/slab.h> |
| 24 |
#include <linux/errno.h> |
| 25 |
#include <linux/module.h> |
| 26 |
#include <linux/init.h> |
| 27 |
#include <linux/ctype.h> |
| 28 |
#include <linux/moduleparam.h> |
| 29 |
|
| 30 |
#include <dahdi/kernel.h> |
| 31 |
|
| 32 |
static int debug; |
| 33 |
|
| 34 |
#include "hpec_user.h" |
| 35 |
#include "hpec.h" |
| 36 |
|
| 37 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 38 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec); |
| 39 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec); |
| 40 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, __u32 size); |
| 41 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val); |
| 42 |
static const char *name = "HPEC"; |
| 43 |
static const char *ec_name(const struct dahdi_chan *chan) { return name; } |
| 44 |
|
| 45 |
static const struct dahdi_echocan_factory my_factory = { |
| 46 |
.get_name = ec_name, |
| 47 |
.owner = THIS_MODULE, |
| 48 |
.echocan_create = echo_can_create, |
| 49 |
}; |
| 50 |
|
| 51 |
static const struct dahdi_echocan_features my_features = { |
| 52 |
.NLP_automatic = 1, |
| 53 |
.CED_tx_detect = 1, |
| 54 |
.CED_rx_detect = 1, |
| 55 |
}; |
| 56 |
|
| 57 |
static const struct dahdi_echocan_ops my_ops = { |
| 58 |
.echocan_free = echo_can_free, |
| 59 |
.echocan_process = echo_can_process, |
| 60 |
.echocan_traintap = echo_can_traintap, |
| 61 |
}; |
| 62 |
|
| 63 |
struct ec_pvt { |
| 64 |
struct hpec_state *hpec; |
| 65 |
struct dahdi_echocan_state dahdi; |
| 66 |
}; |
| 67 |
|
| 68 |
#define dahdi_to_pvt(a) container_of(a, struct ec_pvt, dahdi) |
| 69 |
|
| 70 |
static int __attribute__((regparm(0), format(printf, 1, 2))) logger(const char *format, ...) |
| 71 |
{ |
| 72 |
int res; |
| 73 |
va_list args; |
| 74 |
|
| 75 |
va_start(args, format); |
| 76 |
res = vprintk(format, args); |
| 77 |
va_end(args); |
| 78 |
|
| 79 |
return res; |
| 80 |
} |
| 81 |
|
| 82 |
static void *memalloc(size_t len) |
| 83 |
{ |
| 84 |
return kmalloc(len, GFP_KERNEL); |
| 85 |
} |
| 86 |
|
| 87 |
static void memfree(void *ptr) |
| 88 |
{ |
| 89 |
kfree(ptr); |
| 90 |
} |
| 91 |
|
| 92 |
static void echo_can_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec) |
| 93 |
{ |
| 94 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 95 |
|
| 96 |
hpec_channel_free(pvt->hpec); |
| 97 |
kfree(pvt); |
| 98 |
} |
| 99 |
|
| 100 |
static void echo_can_process(struct dahdi_echocan_state *ec, short *isig, const short *iref, __u32 size) |
| 101 |
{ |
| 102 |
struct ec_pvt *pvt = dahdi_to_pvt(ec); |
| 103 |
|
| 104 |
hpec_channel_update(pvt->hpec, isig, iref); |
| 105 |
} |
| 106 |
|
| 107 |
DEFINE_SEMAPHORE(license_lock); |
| 108 |
|
| 109 |
static int echo_can_create(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp, |
| 110 |
struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec) |
| 111 |
{ |
| 112 |
struct ec_pvt *pvt; |
| 113 |
|
| 114 |
if (ecp->param_count > 0) { |
| 115 |
printk(KERN_WARNING "HPEC does not support parameters; failing request\n"); |
| 116 |
return -EINVAL; |
| 117 |
} |
| 118 |
|
| 119 |
pvt = kzalloc(sizeof(*pvt), GFP_KERNEL); |
| 120 |
if (!pvt) |
| 121 |
return -ENOMEM; |
| 122 |
|
| 123 |
pvt->dahdi.ops = &my_ops; |
| 124 |
pvt->dahdi.features = my_features; |
| 125 |
|
| 126 |
if (down_interruptible(&license_lock)) |
| 127 |
return -ENOTTY; |
| 128 |
|
| 129 |
pvt->hpec = hpec_channel_alloc(ecp->tap_length); |
| 130 |
|
| 131 |
up(&license_lock); |
| 132 |
|
| 133 |
if (!pvt->hpec) { |
| 134 |
kfree(pvt); |
| 135 |
*ec = NULL; |
| 136 |
return -ENOTTY; |
| 137 |
} else { |
| 138 |
*ec = &pvt->dahdi; |
| 139 |
return 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static int echo_can_traintap(struct dahdi_echocan_state *ec, int pos, short val) |
| 144 |
{ |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
static int hpec_license_ioctl(unsigned int cmd, unsigned long data) |
| 149 |
{ |
| 150 |
struct hpec_challenge challenge; |
| 151 |
struct hpec_license license; |
| 152 |
int result = 0; |
| 153 |
|
| 154 |
switch (cmd) { |
| 155 |
case DAHDI_EC_LICENSE_CHALLENGE: |
| 156 |
if (down_interruptible(&license_lock)) |
| 157 |
return -EINTR; |
| 158 |
memset(&challenge, 0, sizeof(challenge)); |
| 159 |
if (hpec_license_challenge(&challenge)) |
| 160 |
result = -ENODEV; |
| 161 |
if (!result && copy_to_user((unsigned char *) data, &challenge, sizeof(challenge))) |
| 162 |
result = -EFAULT; |
| 163 |
up(&license_lock); |
| 164 |
return result; |
| 165 |
case DAHDI_EC_LICENSE_RESPONSE: |
| 166 |
if (down_interruptible(&license_lock)) |
| 167 |
return -EINTR; |
| 168 |
if (copy_from_user(&license, (unsigned char *) data, sizeof(license))) |
| 169 |
result = -EFAULT; |
| 170 |
if (!result && hpec_license_check(&license)) |
| 171 |
result = -EACCES; |
| 172 |
up(&license_lock); |
| 173 |
return result; |
| 174 |
default: |
| 175 |
return -ENOSYS; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static int __init mod_init(void) |
| 180 |
{ |
| 181 |
if (dahdi_register_echocan_factory(&my_factory)) { |
| 182 |
module_printk(KERN_ERR, "could not register with DAHDI core\n"); |
| 183 |
|
| 184 |
return -EPERM; |
| 185 |
} |
| 186 |
|
| 187 |
module_printk(KERN_NOTICE, "Registered echo canceler '%s'\n", |
| 188 |
my_factory.get_name(NULL)); |
| 189 |
|
| 190 |
hpec_init(logger, debug, DAHDI_CHUNKSIZE, memalloc, memfree); |
| 191 |
|
| 192 |
dahdi_set_hpec_ioctl(hpec_license_ioctl); |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static void __exit mod_exit(void) |
| 198 |
{ |
| 199 |
dahdi_unregister_echocan_factory(&my_factory); |
| 200 |
|
| 201 |
dahdi_set_hpec_ioctl(NULL); |
| 202 |
|
| 203 |
hpec_shutdown(); |
| 204 |
} |
| 205 |
|
| 206 |
module_param(debug, int, S_IRUGO | S_IWUSR); |
| 207 |
|
| 208 |
MODULE_DESCRIPTION("DAHDI High Performance Echo Canceller"); |
| 209 |
MODULE_AUTHOR("Kevin P. Fleming <[email protected]>"); |
| 210 |
MODULE_LICENSE("Digium Commercial"); |
| 211 |
|
| 212 |
module_init(mod_init); |
| 213 |
module_exit(mod_exit); |