update asterisk channel driver for lantiq SoCs to version ce67bdb8
[packages.git] / net / asterisk-1.8.x / src-lantiq / channels / chan_lantiq.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, Luka Perkov
5  * Copyright (C) 2012, John Crispin
6  * Copyright (C) 2012, Andrej Vlašić
7  * Copyright (C) 2012, Kaspar Schleiser for T-Labs
8  *                     (Deutsche Telekom Innovation Laboratories)
9  * Copyright (C) 2012, Mirko Vogt for T-Labs
10  *                     (Deutsche Telekom Innovation Laboratories)
11  *
12  * Luka Perkov <openwrt@lukaperkov.net>
13  * John Crispin <blogic@openwrt.org>
14  * Andrej Vlašić <andrej.vlasic0@gmail.com>
15  * Kaspar Schleiser <kaspar@schleiser.de>
16  * Mirko Vogt <mirko@openwrt.org>
17  *
18  * See http://www.asterisk.org for more information about
19  * the Asterisk project. Please do not directly contact
20  * any of the maintainers of this project for assistance;
21  * the project provides a web site, mailing lists and IRC
22  * channels for your use.
23  *
24  * This program is free software, distributed under the terms of
25  * the GNU General Public License Version 2. See the LICENSE file
26  * at the top of the source tree.
27  */
28
29 /*! \file
30  *
31  * \brief Asterisk channel line driver for Lantiq based TAPI boards
32  *
33  * \author Luka Perkov <openwrt@lukaperkov.net>
34  * \author John Crispin <blogic@openwrt.org>
35  * \author Andrej Vlašić <andrej.vlasic0@gmail.com>
36  * \author Kaspar Schleiser <kaspar@schleiser.de>
37  * \author Mirko Vogt <mirko@openwrt.org>
38  * 
39  * \ingroup channel_drivers
40  */
41
42 #include "asterisk.h"
43
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision: xxx $")
45
46 #include <ctype.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <arpa/inet.h>
50 #include <fcntl.h>
51 #include <sys/ioctl.h>
52 #include <signal.h>
53 #ifdef HAVE_LINUX_COMPILER_H
54 #include <linux/compiler.h>
55 #endif
56 #include <linux/telephony.h>
57
58 #include <asterisk/lock.h>
59 #include <asterisk/channel.h>
60 #include <asterisk/config.h>
61 #include <asterisk/module.h>
62 #include <asterisk/pbx.h>
63 #include <asterisk/utils.h>
64 #include <asterisk/callerid.h>
65 #include <asterisk/causes.h>
66 #include <asterisk/stringfields.h>
67 #include <asterisk/musiconhold.h>
68 #include <asterisk/sched.h>
69
70 /* Lantiq TAPI includes */
71 #include <drv_tapi/drv_tapi_io.h>
72 #include <drv_vmmc/vmmc_io.h>
73
74 #define RTP_HEADER_LEN 12
75
76 #define TAPI_AUDIO_PORT_NUM_MAX                 2
77 #define TAPI_TONE_LOCALE_NONE                   0 
78 #define TAPI_TONE_LOCALE_RINGING_CODE           26
79 #define TAPI_TONE_LOCALE_BUSY_CODE              27
80 #define TAPI_TONE_LOCALE_CONGESTION_CODE        27
81 #define TAPI_TONE_LOCALE_DIAL_CODE              25
82 #define TAPI_TONE_LOCALE_WAITING_CODE           37
83
84 #define LANTIQ_CONTEXT_PREFIX "lantiq"
85
86 static const char config[] = "lantiq.conf";
87
88 static char firmware_filename[PATH_MAX] = "/lib/firmware/ifx_firmware.bin";
89 static char bbd_filename[PATH_MAX] = "/lib/firmware/ifx_bbd_fxs.bin";
90 static char base_path[PATH_MAX] = "/dev/vmmc";
91 static int per_channel_context = 0;
92
93 /*
94  * The private structures of the Phone Jack channels are linked for selecting
95  * outgoing channels.
96  */
97 enum channel_state {
98         ONHOOK,
99         OFFHOOK,
100         DIALING,
101         INCALL,
102         CALL_ENDED,
103         RINGING,
104         UNKNOWN
105 };
106
107 static struct lantiq_pvt {
108         struct ast_channel *owner;         /* Channel we belong to, possibly NULL   */
109         int port_id;                       /* Port number of this object, 0..n      */
110         int channel_state;
111         char context[AST_MAX_CONTEXT];     /* this port's dialplan context          */
112         char ext[AST_MAX_EXTENSION+1];     /* the extension this port is connecting */
113         int dial_timer;                    /* timer handle for autodial timeout     */
114         char dtmfbuf[AST_MAX_EXTENSION+1]; /* buffer holding dialed digits          */
115         int dtmfbuf_len;                   /* lenght of dtmfbuf                     */
116         int rtp_timestamp;                 /* timestamp for RTP packets             */
117         uint16_t rtp_seqno;                /* Sequence nr for RTP packets           */
118         uint32_t call_setup_start;         /* Start of dialling in ms               */
119         uint32_t call_setup_delay;         /* time between ^ and 1st ring in ms     */
120         uint16_t jb_size;                  /* Jitter buffer size                    */
121         uint32_t jb_underflow;             /* Jitter buffer injected samples        */
122         uint32_t jb_overflow;              /* Jitter buffer dropped samples         */
123         uint16_t jb_delay;                 /* Jitter buffer: playout delay          */
124         uint16_t jb_invalid;               /* Jitter buffer: Nr. of invalid packets */
125
126 } *iflist = NULL;
127
128 static struct lantiq_ctx {
129                 int dev_fd;
130                 int channels;
131                 int ch_fd[TAPI_AUDIO_PORT_NUM_MAX];
132 } dev_ctx;
133
134 static int ast_digit_begin(struct ast_channel *ast, char digit);
135 static int ast_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
136 static int ast_lantiq_call(struct ast_channel *ast, char *dest, int timeout);
137 static int ast_lantiq_hangup(struct ast_channel *ast);
138 static int ast_lantiq_answer(struct ast_channel *ast);
139 static struct ast_frame *ast_lantiq_read(struct ast_channel *ast);
140 static int ast_lantiq_write(struct ast_channel *ast, struct ast_frame *frame);
141 static struct ast_frame *ast_lantiq_exception(struct ast_channel *ast);
142 static int ast_lantiq_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
143 static int ast_lantiq_fixup(struct ast_channel *old, struct ast_channel *new);
144 static struct ast_channel *ast_lantiq_requester(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
145 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen);
146 static void lantiq_jb_get_stats(int c);
147
148 static const struct ast_channel_tech lantiq_tech = {
149         .type = "TAPI",
150         .description = "Lantiq TAPI Telephony API Driver",
151         .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_G729A,
152         .send_digit_begin = ast_digit_begin,
153         .send_digit_end = ast_digit_end,
154         .call = ast_lantiq_call,
155         .hangup = ast_lantiq_hangup,
156         .answer = ast_lantiq_answer,
157         .read = ast_lantiq_read,
158         .write = ast_lantiq_write,
159         .exception = ast_lantiq_exception,
160         .indicate = ast_lantiq_indicate,
161         .fixup = ast_lantiq_fixup,
162         .requester = ast_lantiq_requester,
163         .func_channel_read = acf_channel_read
164 };
165
166 /* Protect the interface list (of lantiq_pvt's) */
167 AST_MUTEX_DEFINE_STATIC(iflock);
168
169 /*
170  * Protect the monitoring thread, so only one process can kill or start it, and
171  * not when it's doing something critical.
172  */
173 AST_MUTEX_DEFINE_STATIC(monlock);
174
175 /* Boolean value whether the monitoring thread shall continue. */
176 static unsigned int monitor;
177
178 /* The scheduling thread */
179 struct ast_sched_thread *sched_thread;
180    
181 /*
182  * This is the thread for the monitor which checks for input on the channels
183  * which are not currently in use.
184  */
185 static pthread_t monitor_thread = AST_PTHREADT_NULL;
186
187
188 #define WORDS_BIGENDIAN
189 /* struct taken from some GPLed code by  Mike Borella */
190 typedef struct rtp_header
191 {
192 #if defined(WORDS_BIGENDIAN)
193   uint8_t version:2, padding:1, extension:1, csrc_count:4;
194 #else
195   uint8_t csrc_count:4, extension:1, padding:1, version:2;
196 #endif
197 #if defined(WORDS_BIGENDIAN)
198   uint8_t marker:1, payload_type:7;
199 #else
200   uint8_t payload_type:7, marker:1;
201 #endif
202   uint16_t seqno;
203   uint32_t timestamp;
204   uint32_t ssrc;
205 } rtp_header_t;
206
207 static uint32_t now(void) {
208         struct timespec ts;
209         clock_gettime(CLOCK_MONOTONIC, &ts);
210
211         uint64_t tmp = ts.tv_sec*1000 + (ts.tv_nsec/1000000);
212         return (uint32_t) tmp;
213 }
214
215 static int lantiq_dev_open(const char *dev_path, const int32_t ch_num)
216 {
217         char dev_name[PATH_MAX];
218         memset(dev_name, 0, sizeof(dev_name));
219         snprintf(dev_name, PATH_MAX, "%s%u%u", dev_path, 1, ch_num);
220         return open((const char*)dev_name, O_RDWR, 0644);
221 }
222
223 static void lantiq_ring(int c, int r, const char *cid)
224 {
225         uint8_t status;
226
227         if (r) {
228                 if (!cid) {
229                         status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_START, 0);
230                 } else {
231                         IFX_TAPI_CID_MSG_t msg;
232                         IFX_TAPI_CID_MSG_STRING_t cid_el;
233
234                         memset(&msg, 0, sizeof(msg));
235                         memset(&cid_el, 0, sizeof(cid_el));
236                         
237                         cid_el.elementType = IFX_TAPI_CID_ST_CLI;
238                         cid_el.len = strlen(cid);
239                         strncpy((char*)cid_el.element, cid, (size_t)cid_el.len);
240
241                         msg.txMode = IFX_TAPI_CID_HM_ONHOOK;
242                         msg.messageType = IFX_TAPI_CID_MT_CSUP;
243                         msg.message = (IFX_TAPI_CID_MSG_ELEMENT_t *)&cid_el;
244                         msg.nMsgElements = 1;
245
246                         status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_CID_TX_SEQ_START, (IFX_int32_t) &msg);
247                 }
248         } else {
249                 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_STOP, 0);
250         }
251
252         if (status) {
253                 ast_log(LOG_ERROR, "%s ioctl failed\n",
254                         (r ? "IFX_TAPI_RING_START" : "IFX_TAPI_RING_STOP"));
255         }
256 }
257
258 static int lantiq_play_tone(int c, int t)
259 {
260         /* stop currently playing tone before starting new one */
261         if (t != TAPI_TONE_LOCALE_NONE) {
262                 ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_LOCAL_PLAY, TAPI_TONE_LOCALE_NONE);
263         }
264
265         if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_LOCAL_PLAY, t)) {
266                 ast_log(LOG_ERROR, "IFX_TAPI_TONE_LOCAL_PLAY ioctl failed\n");
267                 return -1;
268         }
269
270         return 0;
271 }
272
273 static enum channel_state lantiq_get_hookstatus(int port)
274 {
275         uint8_t status;
276
277         if (ioctl(dev_ctx.ch_fd[port], IFX_TAPI_LINE_HOOK_STATUS_GET, &status)) {
278                 ast_log(LOG_ERROR, "IFX_TAPI_LINE_HOOK_STATUS_GET ioctl failed\n");
279                 return UNKNOWN;
280         }
281
282         if (status) {
283                 return OFFHOOK;
284         } else {
285                 return ONHOOK;
286         }
287 }
288
289 static int
290 lantiq_dev_binary_buffer_create(const char *path, uint8_t **ppBuf, uint32_t *pBufSz)
291 {
292         FILE *fd;
293         struct stat file_stat;
294         int32_t status = 0;
295
296         fd = fopen(path, "rb");
297         if (fd == NULL) {
298                 ast_log(LOG_ERROR, "binary file %s open failed\n", path);
299                 return -1;
300         }
301
302         if (stat(path, &file_stat)) {
303                 ast_log(LOG_ERROR, "file %s statistics get failed\n", path);
304                 return -1;
305         }
306
307         *ppBuf = malloc(file_stat.st_size);
308         if (*ppBuf == NULL) {
309                 ast_log(LOG_ERROR, "binary file %s memory allocation failed\n", path);
310                 status = -1;
311                 goto on_exit;
312         }
313
314         if (fread (*ppBuf, sizeof(uint8_t), file_stat.st_size, fd) <= 0) {
315                 ast_log(LOG_ERROR, "file %s read failed\n", path);
316                 status = -1;
317                 goto on_exit;
318         }
319
320         *pBufSz = file_stat.st_size;
321
322 on_exit:
323         if (fd != NULL)
324                 fclose(fd);
325
326         if (*ppBuf != NULL && status)
327                 free(*ppBuf);
328
329         return status;
330 }
331
332 static int32_t lantiq_dev_firmware_download(int32_t fd, const char *path)
333 {
334         uint8_t *firmware = NULL;
335         uint32_t size = 0;
336         VMMC_IO_INIT vmmc_io_init;
337
338         ast_log(LOG_DEBUG, "loading firmware: \"%s\".", path);
339
340         if (lantiq_dev_binary_buffer_create(path, &firmware, &size))
341                 return -1;
342
343         memset(&vmmc_io_init, 0, sizeof(VMMC_IO_INIT));
344         vmmc_io_init.pPRAMfw = firmware;
345         vmmc_io_init.pram_size = size;
346
347         if (ioctl(fd, FIO_FW_DOWNLOAD, &vmmc_io_init)) {
348                 ast_log(LOG_ERROR, "FIO_FW_DOWNLOAD ioctl failed\n");
349                 return -1;
350         }
351
352         if (firmware != NULL)
353                 free(firmware);
354
355         return 0;
356 }
357
358 static const char *state_string(enum channel_state s)
359 {
360         switch (s) {
361                 case ONHOOK: return "ONHOOK";
362                 case OFFHOOK: return "OFFHOOK";
363                 case DIALING: return "DIALING";
364                 case INCALL: return "INCALL";
365                 case CALL_ENDED: return "CALL_ENDED";
366                 case RINGING: return "RINGING";
367                 default: return "UNKNOWN";
368         }
369 }
370
371 static const char *control_string(int c)
372 {
373         switch (c) {
374                 case AST_CONTROL_HANGUP: return "Other end has hungup";
375                 case AST_CONTROL_RING: return "Local ring";
376                 case AST_CONTROL_RINGING: return "Remote end is ringing";
377                 case AST_CONTROL_ANSWER: return "Remote end has answered";
378                 case AST_CONTROL_BUSY: return "Remote end is busy";
379                 case AST_CONTROL_TAKEOFFHOOK: return "Make it go off hook";
380                 case AST_CONTROL_OFFHOOK: return "Line is off hook";
381                 case AST_CONTROL_CONGESTION: return "Congestion (circuits busy)";
382                 case AST_CONTROL_FLASH: return "Flash hook";
383                 case AST_CONTROL_WINK: return "Wink";
384                 case AST_CONTROL_OPTION: return "Set a low-level option";
385                 case AST_CONTROL_RADIO_KEY: return "Key Radio";
386                 case AST_CONTROL_RADIO_UNKEY: return "Un-Key Radio";
387                 case AST_CONTROL_PROGRESS: return "Remote end is making Progress";
388                 case AST_CONTROL_PROCEEDING: return "Remote end is proceeding";
389                 case AST_CONTROL_HOLD: return "Hold";
390                 case AST_CONTROL_UNHOLD: return "Unhold";
391                 case AST_CONTROL_SRCUPDATE: return "Media Source Update";
392                 case AST_CONTROL_CONNECTED_LINE: return "Connected Line";
393                 case AST_CONTROL_REDIRECTING: return "Redirecting";
394                 case AST_CONTROL_INCOMPLETE: return "Incomplete";
395                 case -1: return "Stop tone";
396                 default: return "Unknown";
397         }
398 }
399
400 static int ast_lantiq_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
401 {
402         ast_verb(3, "phone indication \"%s\"\n", control_string(condition));
403
404         struct lantiq_pvt *pvt = chan->tech_pvt;
405
406         switch (condition) {
407                 case -1:
408                         {
409                                 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_NONE);
410                                 return 0;
411                         }
412                 case AST_CONTROL_CONGESTION:
413                 case AST_CONTROL_BUSY:
414                         {
415                                 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
416                                 return 0;
417                         }
418                 case AST_CONTROL_RINGING:
419                         {
420                                 pvt->call_setup_delay = now() - pvt->call_setup_start;
421                                 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_RINGING_CODE);
422                                 return 0;
423                         }
424                 default:
425                         {
426                                 /* -1 lets asterisk generate the tone */
427                                 return -1;
428                         }
429         }
430 }
431
432 static int ast_lantiq_fixup(struct ast_channel *old, struct ast_channel *new)
433 {
434         ast_log(LOG_DEBUG, "entering... no code here...\n");
435         return 0;
436 }
437
438 static int ast_digit_begin(struct ast_channel *chan, char digit)
439 {
440         /* TODO: Modify this callback to let Asterisk support controlling the length of DTMF */
441         ast_log(LOG_DEBUG, "entering... no code here...\n");
442         return 0;
443 }
444
445 static int ast_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
446 {
447         ast_log(LOG_DEBUG, "entering... no code here...\n");
448         return 0;
449 }
450
451 static int ast_lantiq_call(struct ast_channel *ast, char *dest, int timeout)
452 {
453         /* lock to prevent simultaneous access with do_monitor thread processing */
454         ast_mutex_lock(&iflock);
455
456         struct lantiq_pvt *pvt = ast->tech_pvt;
457         ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
458
459         if (pvt->channel_state == ONHOOK) {
460                 ast_log(LOG_DEBUG, "port %i is ringing\n", pvt->port_id);
461
462                 char *cid = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
463                 ast_log(LOG_DEBUG, "port %i CID: %s\n", pvt->port_id, cid ? cid : "none");
464
465                 lantiq_ring(pvt->port_id, 1, cid);
466                 pvt->channel_state = RINGING;
467
468                 ast_setstate(ast, AST_STATE_RINGING);
469                 ast_queue_control(ast, AST_CONTROL_RINGING);
470         } else {
471                 ast_log(LOG_DEBUG, "port %i is busy\n", pvt->port_id);
472                 ast_setstate(ast, AST_STATE_BUSY);
473                 ast_queue_control(ast, AST_CONTROL_BUSY);
474         }
475
476         ast_mutex_unlock(&iflock);
477
478         return 0;
479 }
480
481 static int ast_lantiq_hangup(struct ast_channel *ast)
482 {
483         /* lock to prevent simultaneous access with do_monitor thread processing */
484         ast_mutex_lock(&iflock);
485
486         struct lantiq_pvt *pvt = ast->tech_pvt;
487         ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
488         
489         if (ast->_state == AST_STATE_RINGING) {
490                 // FIXME
491                 ast_debug(1, "TAPI: ast_lantiq_hangup(): ast->_state == AST_STATE_RINGING\n");
492         }
493
494         switch (pvt->channel_state) {
495                 case RINGING:
496                 case ONHOOK: 
497                         lantiq_ring(pvt->port_id, 0, NULL);
498                         pvt->channel_state = ONHOOK;
499                         break;
500                 default:
501                         ast_log(LOG_DEBUG, "we were hung up, play busy tone\n");
502                         pvt->channel_state = CALL_ENDED;
503                         lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
504         }
505
506         lantiq_jb_get_stats(pvt->port_id);
507
508         ast_setstate(ast, AST_STATE_DOWN);
509         ast_module_unref(ast_module_info->self);
510         ast->tech_pvt = NULL;
511         pvt->owner = NULL;
512
513         ast_mutex_unlock(&iflock);
514
515         return 0;
516 }
517
518 static int ast_lantiq_answer(struct ast_channel *ast)
519 {
520         ast_log(LOG_DEBUG, "entering... no code here...\n");
521         return 0;
522 }
523
524 static struct ast_frame * ast_lantiq_read(struct ast_channel *ast)
525 {
526         ast_log(LOG_DEBUG, "entering... no code here...\n");
527         return NULL;
528 }
529
530 static int ast_lantiq_write(struct ast_channel *ast, struct ast_frame *frame)
531 {
532         char buf[2048];
533         struct lantiq_pvt *pvt = ast->tech_pvt;
534         int ret = -1;
535         rtp_header_t *rtp_header = (rtp_header_t *) buf;
536
537         if(frame->frametype != AST_FRAME_VOICE) {
538                 ast_log(LOG_DEBUG, "unhandled frame type\n");
539                 return 0;
540         }
541         
542         if (frame->datalen == 0) {
543                 ast_log(LOG_DEBUG, "we've been prodded\n");
544                 return 0;
545         }
546
547         memset(buf, 0, sizeof(rtp_header_t));
548         rtp_header->version      = 2;
549         rtp_header->padding      = 0;
550         rtp_header->extension    = 0;
551         rtp_header->csrc_count   = 0;
552         rtp_header->marker       = 0;
553         rtp_header->timestamp    = pvt->rtp_timestamp;
554         rtp_header->seqno        = pvt->rtp_seqno++;
555         rtp_header->ssrc         = 0;
556         rtp_header->payload_type = (uint8_t) frame->subclass.codec;
557
558         pvt->rtp_timestamp += 160;
559
560         memcpy(buf+RTP_HEADER_LEN, frame->data.ptr, frame->datalen);
561
562         ret = write(dev_ctx.ch_fd[pvt->port_id], buf, frame->datalen+RTP_HEADER_LEN);
563         if (ret <= 0) {
564                 ast_debug(1, "TAPI: ast_lantiq_write(): error writing.\n");
565                 return -1;
566         }
567
568 #ifdef TODO_DEVEL_INFO
569         ast_debug(1, "ast_lantiq_write(): size: %i version: %i padding: %i extension: %i csrc_count: %i\n"
570                  "marker: %i payload_type: %s seqno: %i timestamp: %i ssrc: %i\n", 
571                          (int)ret,
572                          (int)rtp_header->version,
573                          (int)rtp_header->padding,
574                          (int)rtp_header->extension,
575                          (int)rtp_header->csrc_count,
576                          (int)rtp_header->marker,
577                          ast_codec2str(rtp_header->payload_type),
578                          (int)rtp_header->seqno,
579                          (int)rtp_header->timestamp,
580                          (int)rtp_header->ssrc);
581 #endif
582
583         return 0;
584 }
585
586 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen)
587 {
588         struct lantiq_pvt *pvt;
589         int res = 0;
590
591         if (!chan || chan->tech != &lantiq_tech) {
592                 ast_log(LOG_ERROR, "This function requires a valid Lantiq TAPI channel\n");
593                 return -1;
594         }
595
596         ast_mutex_lock(&iflock);
597
598         pvt = (struct lantiq_pvt*) chan->tech_pvt;
599
600         if (!strcasecmp(args, "csd")) {
601                 snprintf(buf, buflen, "%lu", (unsigned long int) pvt->call_setup_delay);
602         } else if (!strcasecmp(args, "jitter_stats")){
603                 lantiq_jb_get_stats(pvt->port_id);
604                 snprintf(buf, buflen, "jbBufSize=%u,jbUnderflow=%u,jbOverflow=%u,jbDelay=%u,jbInvalid=%u",
605                                 (uint32_t) pvt->jb_size,
606                                 (uint32_t) pvt->jb_underflow,
607                                 (uint32_t) pvt->jb_overflow,
608                                 (uint32_t) pvt->jb_delay,
609                                 (uint32_t) pvt->jb_invalid);
610         } else {
611                 res = -1;
612         }
613
614         ast_mutex_unlock(&iflock);
615
616         return res;
617 }
618
619
620 static struct ast_frame * ast_lantiq_exception(struct ast_channel *ast)
621 {
622         ast_log(LOG_DEBUG, "entering... no code here...\n");
623         return NULL;
624 }
625
626 static void lantiq_jb_get_stats(int c) {
627         struct lantiq_pvt *pvt = &iflist[c];
628
629         IFX_TAPI_JB_STATISTICS_t param;
630         memset (&param, 0, sizeof (param));
631         if (ioctl (dev_ctx.ch_fd[c], IFX_TAPI_JB_STATISTICS_GET, (IFX_int32_t) &param) != IFX_SUCCESS) {
632                 ast_debug(1, "Error getting jitter buffer  stats.\n");
633         } else {
634                 ast_debug(1, "Jitter buffer stats:  dev=%u, ch=%u, nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u", 
635                                 (uint32_t) param.dev,
636                                 (uint32_t) param.ch,
637                                 (uint32_t) param.nType,
638                                 (uint32_t) param.nBufSize,
639                                 (uint32_t) param.nIsUnderflow,
640                                 (uint32_t) param.nDsOverflow,
641                                 (uint32_t) param.nPODelay,
642                                 (uint32_t) param.nInvalid);
643                 
644                 pvt->jb_size = param.nBufSize;
645                 pvt->jb_underflow = param.nIsUnderflow;
646                 pvt->jb_overflow = param.nDsOverflow;
647                 pvt->jb_invalid = param.nInvalid;
648                 pvt->jb_delay = param.nPODelay;
649         }
650 }
651
652
653 static int lantiq_standby(int c)
654 {
655         if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_STANDBY)) {
656                 ast_log(LOG_ERROR, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
657                 return -1;
658         }
659
660         if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_STOP, 0)) {
661                 ast_log(LOG_ERROR, "IFX_TAPI_ENC_STOP ioctl failed\n");
662                 return -1;
663         }
664
665         if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_DEC_STOP, 0)) {
666                 ast_log(LOG_ERROR, "IFX_TAPI_DEC_STOP ioctl failed\n");
667                 return -1;
668         }
669
670         return lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
671 }
672
673 static int lantiq_end_dialing(int c)
674 {
675         ast_log(LOG_DEBUG, "TODO - DEBUG MSG\n");
676         struct lantiq_pvt *pvt = &iflist[c];
677
678         if (pvt->dial_timer) {
679                 ast_sched_thread_del(sched_thread, pvt->dial_timer);
680                 pvt->dial_timer = 0;
681         }
682
683         if(pvt->owner) {
684                 ast_hangup(pvt->owner);
685         }
686
687         return 0;
688 }
689
690 static int lantiq_end_call(int c)
691 {
692         ast_log(LOG_DEBUG, "TODO - DEBUG MSG\n");
693
694         struct lantiq_pvt *pvt = &iflist[c];
695         
696         if(pvt->owner) {
697                 lantiq_jb_get_stats(c);
698                 ast_queue_hangup(pvt->owner);
699         }
700
701         return 0;
702 }
703
704 static struct ast_channel * lantiq_channel(int state, int c, char *ext, char *ctx)
705 {
706         ast_log(LOG_DEBUG, "TODO - DEBUG MSG\n");
707
708         struct ast_channel *chan = NULL;
709
710         struct lantiq_pvt *pvt = &iflist[c];
711
712         chan = ast_channel_alloc(1, state, NULL, NULL, "", ext, ctx, 0, c, "TAPI/%s", "1");
713
714         chan->tech = &lantiq_tech;
715         chan->nativeformats = AST_FORMAT_ULAW;
716         chan->readformat  = AST_FORMAT_ULAW;
717         chan->writeformat = AST_FORMAT_ULAW;
718         chan->tech_pvt = pvt;
719
720         pvt->owner = chan;
721
722         return chan;
723 }
724
725 static struct ast_channel * ast_lantiq_requester(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
726 {
727         ast_mutex_lock(&iflock);
728
729         char buf[BUFSIZ];
730         struct ast_channel *chan = NULL;
731         int port_id = -1;
732
733         ast_debug(1, "Asked to create a TAPI channel with formats: %s\n", ast_getformatname_multiple(buf, sizeof(buf), format));
734
735
736         /* check for correct data argument */
737         if (ast_strlen_zero(data)) {
738                 ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
739                 *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
740                 return NULL;
741         }
742
743         /* get our port number */
744         port_id = atoi((char*) data);
745         if (port_id < 1 || port_id > dev_ctx.channels) {
746                 ast_log(LOG_ERROR, "Unknown channel ID: \"%s\"\n", (char*) data);
747                 *cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
748                 return NULL;
749         }
750
751         /* on asterisk user's side, we're using port 1-2.
752          * Here in non normal human's world, we begin
753          * counting at 0.
754          */
755         port_id -= 1;
756
757         chan = lantiq_channel(AST_STATE_DOWN, port_id, NULL, NULL);
758
759         ast_mutex_unlock(&iflock);
760         return chan;
761 }
762
763 static int lantiq_dev_data_handler(int c)
764 {
765         char buf[BUFSIZ];
766         struct ast_frame frame = {0};
767
768         int res = read(dev_ctx.ch_fd[c], buf, sizeof(buf));
769         if (res <= 0) ast_log(LOG_ERROR, "we got read error %i\n", res);
770         
771         rtp_header_t *rtp = (rtp_header_t*) buf;
772
773         frame.src = "TAPI";
774         frame.frametype = AST_FRAME_VOICE;
775         frame.subclass.codec = rtp->payload_type;
776         frame.samples = res - RTP_HEADER_LEN;
777         frame.datalen = res - RTP_HEADER_LEN;
778         frame.data.ptr = buf + RTP_HEADER_LEN;
779
780         struct lantiq_pvt *pvt = (struct lantiq_pvt *) &iflist[c];
781         if (pvt->owner && (pvt->owner->_state == AST_STATE_UP)) {
782                 if(!ast_channel_trylock(pvt->owner)) {
783                         ast_queue_frame(pvt->owner, &frame);
784                         ast_channel_unlock(pvt->owner);
785                 }
786         }
787
788 /*      ast_debug(1, "lantiq_dev_data_handler(): size: %i version: %i padding: %i extension: %i csrc_count: %i \n"
789                                  "marker: %i payload_type: %s seqno: %i timestamp: %i ssrc: %i\n", 
790                                  (int)res,
791                                  (int)rtp->version,
792                                  (int)rtp->padding,
793                                  (int)rtp->extension,
794                                  (int)rtp->csrc_count,
795                                  (int)rtp->marker,
796                                  ast_codec2str(rtp->payload_type),
797                                  (int)rtp->seqno,
798                                  (int)rtp->timestamp,
799                                  (int)rtp->ssrc);
800 */
801         return 0;
802 }
803
804 static int accept_call(int c)
805
806         ast_log(LOG_DEBUG, "TODO - DEBUG MSG\n");
807
808         struct lantiq_pvt *pvt = &iflist[c];
809
810         if (pvt->owner) {
811                 struct ast_channel *chan = pvt->owner;
812
813                 switch (chan->_state) {
814                         case AST_STATE_RINGING:
815                                 lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
816                                 ast_queue_control(pvt->owner, AST_CONTROL_ANSWER);
817                                 pvt->channel_state = INCALL;
818                                 break;
819                         default:
820                                 ast_log(LOG_WARNING, "entered unhandled state %s\n", ast_state2str(chan->_state));
821                 }
822         }
823
824         return 0;
825 }
826
827 static int lantiq_dev_event_hook(int c, int state)
828 {
829         ast_mutex_lock(&iflock);
830
831         ast_log(LOG_DEBUG, "on port %i detected event %s hook\n", c, state ? "on" : "off");
832
833         int ret = -1;
834         if (state) { /* going onhook */
835                 switch (iflist[c].channel_state) {
836                         case OFFHOOK: 
837                                 ret = lantiq_standby(c);
838                                 break;
839                         case DIALING: 
840                                 ret = lantiq_end_dialing(c);
841                                 break;
842                         case INCALL: 
843                                 ret = lantiq_end_call(c);
844                                 break;
845                         case CALL_ENDED:
846                                 ret = lantiq_standby(c); // TODO: are we sure for this ?
847                                 break;
848                         default:
849                                 break;
850                 }
851                 iflist[c].channel_state = ONHOOK;
852         } else { /* going offhook */
853                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_ACTIVE)) {
854                         ast_log(LOG_ERROR, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
855                         goto out;
856                 }
857
858                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_START, 0)) {
859                         ast_log(LOG_ERROR, "IFX_TAPI_ENC_START ioctl failed\n");
860                         goto out;
861                 }
862
863                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_DEC_START, 0)) {
864                         ast_log(LOG_ERROR, "IFX_TAPI_DEC_START ioctl failed\n");
865                         goto out;
866                 }
867
868                 switch (iflist[c].channel_state) {
869                         case RINGING: 
870                                 ret = accept_call(c);
871                                 break;
872                         default:
873                                 iflist[c].channel_state = OFFHOOK;
874                                 lantiq_play_tone(c, TAPI_TONE_LOCALE_DIAL_CODE);
875                                 ret = 0;
876                                 break;
877                 }
878
879         }
880
881 out:
882         ast_mutex_unlock(&iflock);
883
884         return ret;
885 }
886
887 static void lantiq_reset_dtmfbuf(struct lantiq_pvt *pvt)
888 {
889         pvt->dtmfbuf[0] = '\0';
890         pvt->dtmfbuf_len = 0;
891         pvt->ext[0] = '\0';
892 }
893
894 static void lantiq_dial(struct lantiq_pvt *pvt)
895 {
896         struct ast_channel *chan = NULL;
897
898         ast_log(LOG_DEBUG, "user want's to dial %s.\n", pvt->dtmfbuf);
899
900         if (ast_exists_extension(NULL, pvt->context, pvt->dtmfbuf, 1, NULL)) {
901                 ast_debug(1, "found extension %s, dialing\n", pvt->dtmfbuf);
902
903                 strcpy(pvt->ext, pvt->dtmfbuf);
904
905                 ast_verbose(VERBOSE_PREFIX_3 " extension exists, starting PBX %s\n", pvt->ext);
906
907                 chan = lantiq_channel(AST_STATE_UP, pvt->port_id, pvt->ext+1, pvt->context);
908                 chan->tech_pvt = pvt;
909                 pvt->owner = chan;
910
911                 strcpy(chan->exten, pvt->ext);
912                 ast_setstate(chan, AST_STATE_RING);
913                 pvt->channel_state = INCALL;
914
915                 pvt->call_setup_start = now();
916
917                 if (ast_pbx_start(chan)) {
918                         ast_log(LOG_WARNING, " unable to start PBX on %s\n", chan->name);
919                         ast_hangup(chan);
920                 }
921         } else {
922                 ast_log(LOG_DEBUG, "no extension found\n");
923                 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
924                 pvt->channel_state = CALL_ENDED;
925         }
926         
927         lantiq_reset_dtmfbuf(pvt);
928 }
929
930 static int lantiq_event_dial_timeout(const void* data)
931 {
932         ast_debug(1, "TAPI: lantiq_event_dial_timeout()\n");
933
934         struct lantiq_pvt *pvt = (struct lantiq_pvt *) data;
935         pvt->dial_timer = 0;
936
937         if (! pvt->channel_state == ONHOOK) {
938                 lantiq_dial(pvt);
939         } else {
940                 ast_debug(1, "TAPI: lantiq_event_dial_timeout(): dial timeout in state ONHOOK.\n");
941         }
942
943         return 0;
944 }
945
946 static int lantiq_send_digit(int c, char digit) 
947 {
948         struct lantiq_pvt *pvt = &iflist[c];
949
950         struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = digit };
951
952         if (pvt->owner) {
953                 ast_log(LOG_DEBUG, "Port %i transmitting digit \"%c\"\n", c, digit);
954                 return ast_queue_frame(pvt->owner, &f);
955         } else {
956                 ast_debug(1, "Warning: lantiq_send_digit() without owner!\n");
957                 return -1;
958         }
959 }
960
961 static void lantiq_dev_event_digit(int c, char digit)
962 {
963         ast_mutex_lock(&iflock);
964
965         ast_log(LOG_DEBUG, "on port %i detected digit \"%c\"\n", c, digit);
966
967         struct lantiq_pvt *pvt = &iflist[c];
968
969         switch (pvt->channel_state) {
970                 case INCALL:
971                         {
972                                 lantiq_send_digit(c, digit);
973                                 break;
974                         }
975                 case OFFHOOK:  
976                         pvt->channel_state = DIALING;
977
978                         lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
979
980                         /* fall through */
981                 case DIALING: 
982                         if (digit == '#') {
983                                 if (pvt->dial_timer) {
984                                         ast_sched_thread_del(sched_thread, pvt->dial_timer);
985                                         pvt->dial_timer = 0;
986                                 }
987
988                                 lantiq_dial(pvt);
989                         } else {
990                                 pvt->dtmfbuf[pvt->dtmfbuf_len] = digit;
991                                 pvt->dtmfbuf_len++;
992                                 pvt->dtmfbuf[pvt->dtmfbuf_len] = '\0';
993
994                                 /* setup autodial timer */
995                                 if (!pvt->dial_timer) {
996                                         ast_log(LOG_DEBUG, "setting new timer\n");
997                                         pvt->dial_timer = ast_sched_thread_add(sched_thread, 2000, lantiq_event_dial_timeout, (const void*) pvt);
998                                 } else {
999                                         ast_log(LOG_DEBUG, "replacing timer\n");
1000                                         struct sched_context *sched = ast_sched_thread_get_context(sched_thread);
1001                                         AST_SCHED_REPLACE(pvt->dial_timer, sched, 2000, lantiq_event_dial_timeout, (const void*) pvt);
1002                                 }
1003                         }
1004                         break;
1005                 default:
1006                         ast_log(LOG_ERROR, "don't know what to do in unhandled state\n");
1007                         break;
1008         }
1009
1010         ast_mutex_unlock(&iflock);
1011         return;
1012 }
1013
1014 static void lantiq_dev_event_handler(void)
1015 {
1016         IFX_TAPI_EVENT_t event;
1017         unsigned int i;
1018
1019         for (i = 0; i < dev_ctx.channels ; i++) {
1020                 ast_mutex_lock(&iflock);
1021
1022                 memset (&event, 0, sizeof(event));
1023                 event.ch = i;
1024                 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_EVENT_GET, &event)) {
1025                         ast_mutex_unlock(&iflock);
1026                         continue;
1027                 }
1028                 if (event.id == IFX_TAPI_EVENT_NONE) {
1029                         ast_mutex_unlock(&iflock);
1030                         continue;
1031                 }
1032
1033                 ast_mutex_unlock(&iflock);
1034
1035                 switch(event.id) {
1036                         case IFX_TAPI_EVENT_FXS_ONHOOK:
1037                                 lantiq_dev_event_hook(i, 1);
1038                                 break;
1039                         case IFX_TAPI_EVENT_FXS_OFFHOOK:
1040                                 lantiq_dev_event_hook(i, 0);
1041                                 break;
1042                         case IFX_TAPI_EVENT_DTMF_DIGIT:
1043                                 lantiq_dev_event_digit(i, (char)event.data.dtmf.ascii);
1044                                 break;
1045                         case IFX_TAPI_EVENT_PULSE_DIGIT:
1046                                 if (event.data.pulse.digit == 0xB) {
1047                                         lantiq_dev_event_digit(i, '0');
1048                                 } else {
1049                                         lantiq_dev_event_digit(i, '0' + (char)event.data.pulse.digit);
1050                                 }
1051                                 break;
1052                         case IFX_TAPI_EVENT_COD_DEC_CHG:
1053                         case IFX_TAPI_EVENT_TONE_GEN_END:
1054                         case IFX_TAPI_EVENT_CID_TX_SEQ_END:
1055                                 break;
1056                         default:
1057                                 ast_log(LOG_ERROR, "unknown TAPI event %08X\n", event.id);
1058                                 break;
1059                 }
1060         }
1061 }
1062
1063 static void * lantiq_events_monitor(void *data)
1064 {
1065         ast_verbose("TAPI thread started\n");
1066
1067         struct pollfd fds[3];
1068
1069         fds[0].fd = dev_ctx.dev_fd;
1070         fds[0].events = POLLIN;
1071         fds[1].fd = dev_ctx.ch_fd[0];
1072         fds[1].events = POLLIN;
1073         fds[2].fd = dev_ctx.ch_fd[1];
1074         fds[2].events = POLLIN;
1075
1076         while (monitor) {
1077                 ast_mutex_lock(&monlock);
1078
1079                 if (poll(fds, dev_ctx.channels + 1, 2000) <= 0) {
1080                         ast_mutex_unlock(&monlock);
1081                         continue;
1082                 }
1083
1084                 if (fds[0].revents & POLLIN) {
1085                         lantiq_dev_event_handler();
1086                 }
1087
1088                 ast_mutex_unlock(&monlock);
1089
1090                 if ((fds[1].revents & POLLIN) && (lantiq_dev_data_handler(0))) {
1091                         ast_log(LOG_ERROR, "data handler 0 failed\n");
1092                         break;
1093                 }
1094
1095                 if ((fds[2].revents & POLLIN) && (lantiq_dev_data_handler(1))) {
1096                         ast_log(LOG_ERROR, "data handler 1 failed\n");
1097                         break;
1098                 }
1099         }
1100
1101         return NULL;
1102 }
1103
1104 static int restart_monitor(void)
1105 {
1106         /* If we're supposed to be stopped -- stay stopped */
1107         if (monitor_thread == AST_PTHREADT_STOP)
1108                 return 0;
1109
1110         ast_mutex_lock(&monlock);
1111
1112         if (monitor_thread == pthread_self()) {
1113                 ast_mutex_unlock(&monlock);
1114                 ast_log(LOG_WARNING, "Cannot kill myself\n");
1115                 return -1;
1116         }
1117
1118         if (monitor_thread != AST_PTHREADT_NULL) {
1119                 if (ast_mutex_lock(&iflock)) {
1120                         ast_mutex_unlock(&monlock);
1121                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1122                         return -1;
1123                 }
1124                 monitor = 0;
1125                 while (pthread_kill(monitor_thread, SIGURG) == 0)
1126                         sched_yield();
1127                 pthread_join(monitor_thread, NULL);
1128                 ast_mutex_unlock(&iflock);
1129         }
1130
1131         monitor = 1;
1132         /* Start a new monitor */
1133         if (ast_pthread_create_background(&monitor_thread, NULL, lantiq_events_monitor, NULL) < 0) {
1134                 ast_mutex_unlock(&monlock);
1135                 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1136                 return -1;
1137         }
1138
1139         ast_mutex_unlock(&monlock);
1140
1141         return 0;
1142 }
1143
1144 static void lantiq_cleanup(void)
1145 {
1146         int c;
1147
1148         for (c = 0; c < dev_ctx.channels ; c++) { 
1149                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_STANDBY)) {
1150                         ast_log(LOG_WARNING, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1151                 }
1152
1153                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_STOP, 0)) {
1154                         ast_log(LOG_WARNING, "IFX_TAPI_ENC_STOP ioctl failed\n");
1155                 }
1156
1157                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_DEC_STOP, 0)) {
1158                         ast_log(LOG_WARNING, "IFX_TAPI_DEC_STOP ioctl failed\n");
1159                 }
1160         }
1161
1162         if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_STOP, 0)) {
1163                 ast_log(LOG_WARNING, "IFX_TAPI_DEV_STOP ioctl failed\n");
1164         }
1165
1166         close(dev_ctx.dev_fd);
1167 }
1168
1169 static int unload_module(void)
1170 {
1171         int c;
1172
1173         ast_channel_unregister(&lantiq_tech);
1174
1175         if (!ast_mutex_lock(&iflock)) {
1176                 for (c = 0; c < dev_ctx.channels ; c++) {
1177                         if (iflist[c].owner)
1178                                 ast_softhangup(iflist[c].owner, AST_SOFTHANGUP_APPUNLOAD);
1179                 }
1180                 ast_mutex_unlock(&iflock);
1181         } else {
1182                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1183                 return -1;
1184         }
1185
1186         if (!ast_mutex_lock(&monlock)) {
1187                 if (monitor_thread > AST_PTHREADT_NULL) {
1188                         monitor = 0;
1189                         while (pthread_kill(monitor_thread, SIGURG) == 0)
1190                                 sched_yield();
1191                         pthread_join(monitor_thread, NULL);
1192                 }
1193                 monitor_thread = AST_PTHREADT_STOP;
1194                 ast_mutex_unlock(&monlock);
1195         } else {
1196                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1197                 return -1;
1198         }
1199
1200         return 0;
1201 }
1202
1203 static struct lantiq_pvt *lantiq_init_pvt(struct lantiq_pvt *pvt)
1204 {
1205         if (pvt) {
1206                 pvt->owner = NULL;
1207                 pvt->port_id = -1;
1208                 pvt->channel_state = UNKNOWN;
1209                 pvt->context[0] = '\0';
1210                 pvt->ext[0] = '\0';
1211                 pvt->dial_timer = 0;
1212                 pvt->dtmfbuf[0] = '\0';
1213                 pvt->dtmfbuf_len = 0;
1214                 pvt->call_setup_start = 0;
1215                 pvt->call_setup_delay = 0;
1216                 pvt->jb_size = 0;
1217                 pvt->jb_underflow = 0;
1218                 pvt->jb_overflow = 0;
1219                 pvt->jb_delay = 0;
1220                 pvt->jb_invalid = 0;
1221         } else {
1222                 ast_log(LOG_ERROR, "unable to clear pvt structure\n");
1223         }
1224
1225         return pvt;
1226 }
1227
1228 static int lantiq_create_pvts(void)
1229 {
1230         int i;
1231
1232         iflist = ast_calloc(1, sizeof(struct lantiq_pvt) * dev_ctx.channels);
1233
1234         if (iflist) { 
1235                 for (i=0 ; i<dev_ctx.channels ; i++) {
1236                         lantiq_init_pvt(&iflist[i]);
1237                         iflist[i].port_id = i;
1238                         if (per_channel_context) {
1239                                 snprintf(iflist[i].context, AST_MAX_CONTEXT, "%s%i", LANTIQ_CONTEXT_PREFIX, i+1);
1240                                 ast_debug(1, "Context for channel %i: %s\n", i, iflist[i].context);
1241                         } else {
1242                                 snprintf(iflist[i].context, AST_MAX_CONTEXT, "default");
1243                         }
1244                 }
1245                 return 0;
1246         } else {
1247                 ast_log(LOG_ERROR, "unable to allocate memory\n");
1248                 return -1;
1249         }
1250 }
1251
1252 static int lantiq_setup_rtp(int c)
1253 {
1254         /* Configure RTP payload type tables */
1255         IFX_TAPI_PKT_RTP_PT_CFG_t rtpPTConf;
1256
1257         memset((char*)&rtpPTConf, '\0', sizeof(rtpPTConf));
1258
1259         rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_MLAW] = AST_FORMAT_ULAW;
1260         rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_ALAW] = AST_FORMAT_ALAW;
1261 //      rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_G723_63] = AST_FORMAT_G723_1;
1262 //      rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_G723_53] = 4;
1263 //      rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_G729] = AST_FORMAT_G729A;
1264 //      rtpPTConf.nPTup[IFX_TAPI_COD_TYPE_G722_64] = AST_FORMAT_G722;
1265
1266         rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_MLAW] = AST_FORMAT_ULAW;
1267         rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_ALAW] = AST_FORMAT_ALAW;
1268 //      rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_G723_63] = AST_FORMAT_G723_1;
1269 //      rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_G723_53] = AST_FORMAT_G723_1;
1270 //      rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_G729] = AST_FORMAT_G729A;
1271 //      rtpPTConf.nPTdown[IFX_TAPI_COD_TYPE_G722_64] = AST_FORMAT_G722;
1272
1273         int ret;
1274         if ((ret = ioctl(dev_ctx.ch_fd[c], IFX_TAPI_PKT_RTP_PT_CFG_SET, (IFX_int32_t) &rtpPTConf))) {
1275                 ast_log(LOG_ERROR, "IFX_TAPI_PKT_RTP_PT_CFG_SET failed: ret=%i\n", ret);
1276                 return -1;
1277         }
1278
1279         return 0;
1280 }
1281
1282 static int load_module(void)
1283 {
1284         struct ast_config *cfg;
1285         struct ast_variable *v;
1286         int txgain = 0;
1287         int rxgain = 0;
1288         int wlec_type = 0;
1289         int wlec_nlp = 0;
1290         int wlec_nbfe = 0;
1291         int wlec_nbne = 0;
1292         int wlec_wbne = 0;
1293         int jb_type = IFX_TAPI_JB_TYPE_ADAPTIVE;
1294         int jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_VOICE;
1295         int jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT;
1296         int jb_scaling = 0x10;
1297         int jb_initialsize = 0x2d0;
1298         int jb_minsize = 0x50;
1299         int jb_maxsize = 0x5a0;
1300         int cid_type = IFX_TAPI_CID_STD_TELCORDIA;
1301         int vad_type = IFX_TAPI_ENC_VAD_NOVAD;
1302         dev_ctx.channels = TAPI_AUDIO_PORT_NUM_MAX;
1303         struct ast_flags config_flags = { 0 };
1304         
1305         if (!(sched_thread = ast_sched_thread_create())) {
1306                 ast_log(LOG_ERROR, "Unable to create scheduler thread\n");
1307                 return AST_MODULE_LOAD_FAILURE;
1308         }
1309
1310         if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
1311                 ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", config);
1312                 return AST_MODULE_LOAD_DECLINE;
1313         }
1314
1315         /* We *must* have a config file otherwise stop immediately */
1316         if (!cfg) {
1317                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1318                 return AST_MODULE_LOAD_DECLINE;
1319         }
1320
1321         if (ast_mutex_lock(&iflock)) {
1322                 ast_log(LOG_ERROR, "Unable to lock interface list.\n");
1323                 return AST_MODULE_LOAD_FAILURE;
1324         }
1325
1326         for (v = ast_variable_browse(cfg, "interfaces"); v; v = v->next) {
1327                 if (!strcasecmp(v->name, "channels")) {
1328                         dev_ctx.channels = atoi(v->value);
1329                         if (!dev_ctx.channels) {
1330                                 ast_log(LOG_ERROR, "Invalid value for channels in config %s\n", config);
1331                                 ast_config_destroy(cfg);
1332                                 return AST_MODULE_LOAD_DECLINE;
1333                         }
1334                 } else if (!strcasecmp(v->name, "firmwarefilename")) {
1335                         ast_copy_string(firmware_filename, v->value, sizeof(firmware_filename));
1336                 } else if (!strcasecmp(v->name, "bbdfilename")) {
1337                         ast_copy_string(bbd_filename, v->value, sizeof(bbd_filename));
1338                 } else if (!strcasecmp(v->name, "basepath")) {
1339                         ast_copy_string(base_path, v->value, sizeof(base_path));
1340                 } else if (!strcasecmp(v->name, "per_channel_context")) {
1341                         if (!strcasecmp(v->value, "on")) {
1342                                 per_channel_context = 1;
1343                         } else if (!strcasecmp(v->value, "off")) {
1344                                 per_channel_context = 0;
1345                         } else {
1346                                 ast_log(LOG_ERROR, "Unknown per_channel_context value '%s'. Try 'on' or 'off'.\n", v->value);
1347                                 ast_config_destroy(cfg);
1348                                 return AST_MODULE_LOAD_DECLINE;
1349                         }
1350                 }
1351         }
1352
1353         for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
1354                 if (!strcasecmp(v->name, "rxgain")) {
1355                         rxgain = atoi(v->value);
1356                         if (!rxgain) {
1357                                 rxgain = 0;
1358                                 ast_log(LOG_WARNING, "Invalid rxgain: %s, using default.\n", v->value);
1359                         }
1360                 } else if (!strcasecmp(v->name, "txgain")) {
1361                         txgain = atoi(v->value);
1362                         if (!txgain) {
1363                                 txgain = 0;
1364                                 ast_log(LOG_WARNING, "Invalid txgain: %s, using default.\n", v->value);
1365                         }
1366                 } else if (!strcasecmp(v->name, "echocancel")) {
1367                         if (!strcasecmp(v->value, "off")) {
1368                                 wlec_type = IFX_TAPI_WLEC_TYPE_OFF;
1369                         } else if (!strcasecmp(v->value, "nlec")) {
1370                                 wlec_type = IFX_TAPI_WLEC_TYPE_NE;
1371                                 if (!strcasecmp(v->name, "echocancelfixedwindowsize")) {
1372                                         wlec_nbne = atoi(v->value);
1373                                 }
1374                         } else if (!strcasecmp(v->value, "wlec")) {
1375                                 wlec_type = IFX_TAPI_WLEC_TYPE_NFE;
1376                                 if (!strcasecmp(v->name, "echocancelnfemovingwindowsize")) {
1377                                         wlec_nbfe = atoi(v->value);
1378                                 } else if (!strcasecmp(v->name, "echocancelfixedwindowsize")) {
1379                                         wlec_nbne = atoi(v->value);
1380                                 } else if (!strcasecmp(v->name, "echocancelwidefixedwindowsize")) {
1381                                         wlec_wbne = atoi(v->value);
1382                                 }
1383                         } else if (!strcasecmp(v->value, "nees")) {
1384                                 wlec_type = IFX_TAPI_WLEC_TYPE_NE_ES;
1385                         } else if (!strcasecmp(v->value, "nfees")) {
1386                                 wlec_type = IFX_TAPI_WLEC_TYPE_NFE_ES;
1387                         } else if (!strcasecmp(v->value, "es")) {
1388                                 wlec_type = IFX_TAPI_WLEC_TYPE_ES;
1389                         } else {
1390                                 wlec_type = IFX_TAPI_WLEC_TYPE_OFF;
1391                                 ast_log(LOG_ERROR, "Unknown echo cancellation type '%s'\n", v->value);
1392                                 ast_config_destroy(cfg);
1393                                 return AST_MODULE_LOAD_DECLINE;
1394                         }
1395                 } else if (!strcasecmp(v->name, "echocancelnlp")) {
1396                         if (!strcasecmp(v->value, "on")) {
1397                                 wlec_nlp = IFX_TAPI_WLEC_NLP_ON;
1398                         } else if (!strcasecmp(v->value, "off")) {
1399                                 wlec_nlp = IFX_TAPI_WLEC_NLP_OFF;
1400                         } else {
1401                                 ast_log(LOG_ERROR, "Unknown echo cancellation nlp '%s'\n", v->value);
1402                                 ast_config_destroy(cfg);
1403                                 return AST_MODULE_LOAD_DECLINE;
1404                         }
1405                 } else if (!strcasecmp(v->name, "jitterbuffertype")) {
1406                         if (!strcasecmp(v->value, "fixed")) {
1407                                 jb_type = IFX_TAPI_JB_TYPE_FIXED;
1408                         } else if (!strcasecmp(v->value, "adaptive")) {
1409                                 jb_type = IFX_TAPI_JB_TYPE_ADAPTIVE;
1410                                 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT;
1411                                 if (!strcasecmp(v->name, "jitterbufferadaptation")) {
1412                                         if (!strcasecmp(v->value, "on")) {
1413                                                 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_ON;
1414                                         } else if (!strcasecmp(v->value, "off")) {
1415                                                 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_OFF;
1416                                         }
1417                                 } else if (!strcasecmp(v->name, "jitterbufferscalling")) {
1418                                         jb_scaling = atoi(v->value);
1419                                 } else if (!strcasecmp(v->name, "jitterbufferinitialsize")) {
1420                                         jb_initialsize = atoi(v->value);
1421                                 } else if (!strcasecmp(v->name, "jitterbufferminsize")) {
1422                                         jb_minsize = atoi(v->value);
1423                                 } else if (!strcasecmp(v->name, "jitterbuffermaxsize")) {
1424                                         jb_maxsize = atoi(v->value);
1425                                 }
1426                         } else {
1427                                 ast_log(LOG_ERROR, "Unknown jitter buffer type '%s'\n", v->value);
1428                                 ast_config_destroy(cfg);
1429                                 return AST_MODULE_LOAD_DECLINE;
1430                         }
1431                 } else if (!strcasecmp(v->name, "jitterbufferpackettype")) {
1432                         if (!strcasecmp(v->value, "voice")) {
1433                                 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_VOICE;
1434                         } else if (!strcasecmp(v->value, "data")) {
1435                                 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_DATA;
1436                         } else if (!strcasecmp(v->value, "datanorep")) {
1437                                 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_DATA_NO_REP;
1438                         } else {
1439                                 ast_log(LOG_ERROR, "Unknown jitter buffer packet adaptation type '%s'\n", v->value);
1440                                 ast_config_destroy(cfg);
1441                                 return AST_MODULE_LOAD_DECLINE;
1442                         }
1443                 } else if (!strcasecmp(v->name, "calleridtype")) {
1444                         ast_log(LOG_DEBUG, "Setting CID type to %s.\n", v->value);
1445                         if (!strcasecmp(v->value, "telecordia")) {
1446                                 cid_type = IFX_TAPI_CID_STD_TELCORDIA;
1447                         } else if (!strcasecmp(v->value, "etsifsk")) {
1448                                 cid_type = IFX_TAPI_CID_STD_ETSI_FSK;
1449                         } else if (!strcasecmp(v->value, "etsidtmf")) {
1450                                 cid_type = IFX_TAPI_CID_STD_ETSI_DTMF;
1451                         } else if (!strcasecmp(v->value, "sin")) {
1452                                 cid_type = IFX_TAPI_CID_STD_SIN;
1453                         } else if (!strcasecmp(v->value, "ntt")) {
1454                                 cid_type = IFX_TAPI_CID_STD_NTT;
1455                         } else if (!strcasecmp(v->value, "kpndtmf")) {
1456                                 cid_type = IFX_TAPI_CID_STD_KPN_DTMF;
1457                         } else if (!strcasecmp(v->value, "kpndtmffsk")) {
1458                                 cid_type = IFX_TAPI_CID_STD_KPN_DTMF_FSK;
1459                         } else {
1460                                 ast_log(LOG_ERROR, "Unknown caller id type '%s'\n", v->value);
1461                                 ast_config_destroy(cfg);
1462                                 return AST_MODULE_LOAD_DECLINE;
1463                         }
1464                 } else if (!strcasecmp(v->name, "voiceactivitydetection")) {
1465                         if (!strcasecmp(v->value, "on")) {
1466                                 vad_type = IFX_TAPI_ENC_VAD_ON;
1467                         } else if (!strcasecmp(v->value, "g711")) {
1468                                 vad_type = IFX_TAPI_ENC_VAD_G711;
1469                         } else if (!strcasecmp(v->value, "cng")) {
1470                                 vad_type = IFX_TAPI_ENC_VAD_CNG_ONLY;
1471                         } else if (!strcasecmp(v->value, "sc")) {
1472                                 vad_type = IFX_TAPI_ENC_VAD_SC_ONLY;
1473                         } else {
1474                                 ast_log(LOG_ERROR, "Unknown voice activity detection value '%s'\n", v->value);
1475                                 ast_config_destroy(cfg);
1476                                 return AST_MODULE_LOAD_DECLINE;
1477                         }
1478                 }
1479         }
1480
1481         lantiq_create_pvts();
1482
1483         ast_mutex_unlock(&iflock);
1484
1485         if (ast_channel_register(&lantiq_tech)) {
1486                 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
1487                 ast_config_destroy(cfg);
1488                 unload_module();
1489                 return AST_MODULE_LOAD_FAILURE;
1490         }
1491         ast_config_destroy(cfg);
1492         
1493         /* tapi */
1494 #ifdef TODO_TONES
1495         IFX_TAPI_TONE_t tone;
1496 #endif
1497         IFX_TAPI_DEV_START_CFG_t dev_start;
1498         IFX_TAPI_MAP_DATA_t map_data;
1499         IFX_TAPI_ENC_CFG_t enc_cfg;
1500         IFX_TAPI_LINE_VOLUME_t line_vol;
1501         IFX_TAPI_WLEC_CFG_t wlec_cfg;
1502         IFX_TAPI_JB_CFG_t jb_cfg;
1503         IFX_TAPI_CID_CFG_t cid_cfg;
1504         uint8_t c;
1505
1506         /* open device */
1507         dev_ctx.dev_fd = lantiq_dev_open(base_path, 0);
1508
1509         if (dev_ctx.dev_fd < 0) {
1510                 ast_log(LOG_ERROR, "lantiq tapi device open function failed\n");
1511                 return AST_MODULE_LOAD_FAILURE;
1512         }
1513
1514         for (c = 0; c < dev_ctx.channels ; c++) {
1515                 dev_ctx.ch_fd[c] = lantiq_dev_open(base_path, c + 1);
1516
1517                 if (dev_ctx.ch_fd[c] < 0) {
1518                         ast_log(LOG_ERROR, "lantiq tapi channel %d open function failed\n", c);
1519                         return AST_MODULE_LOAD_FAILURE;
1520                 }
1521         }
1522
1523         if (lantiq_dev_firmware_download(dev_ctx.dev_fd, firmware_filename)) {
1524                 ast_log(LOG_ERROR, "voice firmware download failed\n");
1525                 return AST_MODULE_LOAD_FAILURE;
1526         }
1527
1528         if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_STOP, 0)) {
1529                 ast_log(LOG_ERROR, "IFX_TAPI_DEV_STOP ioctl failed\n");
1530                 return AST_MODULE_LOAD_FAILURE;
1531         }
1532
1533         memset(&dev_start, 0x0, sizeof(IFX_TAPI_DEV_START_CFG_t));
1534         dev_start.nMode = IFX_TAPI_INIT_MODE_VOICE_CODER;
1535
1536         /* Start TAPI */
1537         if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_START, &dev_start)) {
1538                 ast_log(LOG_ERROR, "IFX_TAPI_DEV_START ioctl failed\n");
1539                 return AST_MODULE_LOAD_FAILURE;
1540         }
1541
1542         for (c = 0; c < dev_ctx.channels ; c++) {
1543                 /* tones */
1544 #ifdef TODO_TONES
1545                 memset(&tone, 0, sizeof(IFX_TAPI_TONE_t));
1546                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_TABLE_CFG_SET, &tone)) {
1547                         ast_log(LOG_ERROR, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c);
1548                         return AST_MODULE_LOAD_FAILURE;
1549                 }
1550 #endif
1551                 /* ringing type */
1552                 IFX_TAPI_RING_CFG_t ringingType;
1553                 memset(&ringingType, 0, sizeof(IFX_TAPI_RING_CFG_t));
1554                 ringingType.nMode = IFX_TAPI_RING_CFG_MODE_INTERNAL_BALANCED;
1555                 ringingType.nSubmode = IFX_TAPI_RING_CFG_SUBMODE_DC_RNG_TRIP_FAST;
1556                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_CFG_SET, (IFX_int32_t) &ringingType)) {
1557                         ast_log(LOG_ERROR, "IFX_TAPI_RING_CFG_SET failed\n");
1558                         return AST_MODULE_LOAD_FAILURE;
1559                 }
1560
1561                 /* ring cadence */
1562                 IFX_char_t data[15] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1563                                                                 0x00, 0x00, 0x00, 0x00, 0x00,     
1564                                                                 0x00, 0x00, 0x00, 0x00, 0x00 };
1565
1566                 IFX_TAPI_RING_CADENCE_t ringCadence;
1567                 memset(&ringCadence, 0, sizeof(IFX_TAPI_RING_CADENCE_t));
1568                 memcpy(&ringCadence.data, data, sizeof(data));
1569                 ringCadence.nr = sizeof(data) * 8;
1570
1571                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_CADENCE_HR_SET, &ringCadence)) {
1572                         ast_log(LOG_ERROR, "IFX_TAPI_RING_CADENCE_HR_SET failed\n");
1573                         return AST_MODULE_LOAD_FAILURE;
1574                 }
1575
1576                 /* perform mapping */
1577                 memset(&map_data, 0x0, sizeof(IFX_TAPI_MAP_DATA_t));
1578                 map_data.nDstCh = c;
1579                 map_data.nChType = IFX_TAPI_MAP_TYPE_PHONE;
1580
1581                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_MAP_DATA_ADD, &map_data)) {
1582                         ast_log(LOG_ERROR, "IFX_TAPI_MAP_DATA_ADD %d failed\n", c);
1583                         return AST_MODULE_LOAD_FAILURE;
1584                 }
1585
1586                 /* set line feed */
1587                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_STANDBY)) {
1588                         ast_log(LOG_ERROR, "IFX_TAPI_LINE_FEED_SET %d failed\n", c);
1589                         return AST_MODULE_LOAD_FAILURE;
1590                 }
1591
1592                 /* Configure encoder */
1593                 memset(&enc_cfg, 0x0, sizeof(IFX_TAPI_ENC_CFG_t));
1594                 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
1595                 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_MLAW;
1596
1597                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_CFG_SET, &enc_cfg)) {
1598                         ast_log(LOG_ERROR, "IFX_TAPI_ENC_CFG_SET %d failed\n", c);
1599                         return AST_MODULE_LOAD_FAILURE;
1600                 }
1601
1602                 /* set volume */
1603                 memset(&line_vol, 0, sizeof(line_vol));
1604                 line_vol.nGainRx = rxgain;
1605                 line_vol.nGainTx = txgain;
1606
1607                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_PHONE_VOLUME_SET, &line_vol)) {
1608                         ast_log(LOG_ERROR, "IFX_TAPI_PHONE_VOLUME_SET %d failed\n", c);
1609                         return AST_MODULE_LOAD_FAILURE;
1610                 }
1611
1612                 /* Configure line echo canceller */
1613                 memset(&wlec_cfg, 0, sizeof(wlec_cfg));
1614                 wlec_cfg.nType = wlec_type;
1615                 wlec_cfg.bNlp = wlec_nlp;
1616                 wlec_cfg.nNBFEwindow = wlec_nbfe;
1617                 wlec_cfg.nNBNEwindow = wlec_nbne;
1618                 wlec_cfg.nWBNEwindow = wlec_wbne;
1619
1620                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_WLEC_PHONE_CFG_SET, &wlec_cfg)) {
1621                         ast_log(LOG_ERROR, "IFX_TAPI_WLEC_PHONE_CFG_SET %d failed\n", c);
1622                         return AST_MODULE_LOAD_FAILURE;
1623                 }
1624
1625                 /* Configure jitter buffer */
1626                 memset(&jb_cfg, 0, sizeof(jb_cfg));
1627                 jb_cfg.nJbType = jb_type;
1628                 jb_cfg.nPckAdpt = jb_pckadpt;
1629                 jb_cfg.nLocalAdpt = jb_localadpt;
1630                 jb_cfg.nScaling = jb_scaling;
1631                 jb_cfg.nInitialSize = jb_initialsize;
1632                 jb_cfg.nMinSize = jb_minsize;
1633                 jb_cfg.nMaxSize = jb_maxsize;
1634
1635                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_JB_CFG_SET, &jb_cfg)) {
1636                         ast_log(LOG_ERROR, "IFX_TAPI_JB_CFG_SET %d failed\n", c);
1637                         return AST_MODULE_LOAD_FAILURE;
1638                 }
1639
1640                 /* Configure Caller ID type */
1641                 memset(&cid_cfg, 0, sizeof(cid_cfg));
1642                 cid_cfg.nStandard = cid_type;
1643
1644                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_CID_CFG_SET, &cid_cfg)) {
1645                         ast_log(LOG_ERROR, "IIFX_TAPI_CID_CFG_SET %d failed\n", c);
1646                         return AST_MODULE_LOAD_FAILURE;
1647                 }
1648
1649                 /* Configure voice activity detection */
1650                 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_VAD_CFG_SET, vad_type)) {
1651                         ast_log(LOG_ERROR, "IFX_TAPI_ENC_VAD_CFG_SET %d failed\n", c);
1652                         return AST_MODULE_LOAD_FAILURE;
1653                 }
1654
1655                 /* Setup TAPI <-> Asterisk codec type mapping */
1656                 if (lantiq_setup_rtp(c)) {
1657                         return AST_MODULE_LOAD_FAILURE;
1658                 }
1659
1660                 /* Set initial hook status */
1661                 iflist[c].channel_state = lantiq_get_hookstatus(c);
1662                 
1663                 if (iflist[c].channel_state == UNKNOWN) {
1664                         return AST_MODULE_LOAD_FAILURE;
1665                 }
1666         }
1667
1668         /* make sure our device will be closed properly */
1669         ast_register_atexit(lantiq_cleanup);
1670
1671         restart_monitor();
1672         return AST_MODULE_LOAD_SUCCESS;
1673 }
1674
1675 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Lantiq TAPI Telephony API Support");