chan_phone portability (bug #1788)
[asterisk/asterisk.git] / channels / chan_phone.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Generic Linux Telephony Interface driver
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <pthread.h>
16 #include <string.h>
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/utils.h>
26 #include <asterisk/callerid.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <arpa/inet.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #include <linux/telephony.h>
36 /* Still use some IXJ specific stuff */
37 #include <linux/ixjuser.h>
38 #include "DialTone.h"
39
40 #ifdef QTI_PHONEJACK_TJ_PCI     /* check for the newer quicknet driver v.3.1.0 which has this symbol */
41 #define QNDRV_VER 310
42 #else
43 #define QNDRV_VER 100
44 #endif
45
46 #if QNDRV_VER > 100
47 #ifdef __linux__
48 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
49 #else /* FreeBSD and others */
50 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
51 #endif /* __linux__ */
52 #else   /* older driver */
53 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
54 #endif
55
56
57 #define DEFAULT_CALLER_ID "Unknown"
58 #define PHONE_MAX_BUF 480
59 #define DEFAULT_GAIN 0x100
60
61 static char *desc = "Linux Telephony API Support";
62 static char *type = "Phone";
63 static char *tdesc = "Standard Linux Telephony API Driver";
64 static char *config = "phone.conf";
65
66 /* Default context for dialtone mode */
67 static char context[AST_MAX_EXTENSION] = "default";
68
69 /* Default language */
70 static char language[MAX_LANGUAGE] = "";
71 static int usecnt =0;
72
73 static int echocancel = AEC_OFF;
74
75 static int silencesupression = 0;
76
77 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
78
79 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
80
81 /* Protect the interface list (of phone_pvt's) */
82 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
83
84 /* Protect the monitoring thread, so only one process can kill or start it, and not
85    when it's doing something critical. */
86 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
87
88 /* This is the thread for the monitor which checks for input on the channels
89    which are not currently in use.  */
90 static pthread_t monitor_thread = AST_PTHREADT_NULL;
91
92 static int restart_monitor(void);
93
94 /* The private structures of the Phone Jack channels are linked for
95    selecting outgoing channels */
96    
97 #define MODE_DIALTONE   1
98 #define MODE_IMMEDIATE  2
99 #define MODE_FXO                3
100    
101 static struct phone_pvt {
102         int fd;                                                 /* Raw file descriptor for this device */
103         struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
104         int mode;                                               /* Is this in the  */
105         int lastformat;                                 /* Last output format */
106         int lastinput;                                  /* Last input format */
107         int ministate;                                  /* Miniature state, for dialtone mode */
108         char dev[256];                                  /* Device name */
109         struct phone_pvt *next;                 /* Next channel in list */
110         struct ast_frame fr;                    /* Frame */
111         char offset[AST_FRIENDLY_OFFSET];
112         char buf[PHONE_MAX_BUF];                                        /* Static buffer for reading frames */
113         int obuflen;
114         int dialtone;
115         int txgain, rxgain;             /* gain control for playing, recording  */
116                                                                         /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
117         int cpt;                                                /* Call Progress Tone playing? */
118         int silencesupression;
119         char context[AST_MAX_EXTENSION];
120         char obuf[PHONE_MAX_BUF * 2];
121         char ext[AST_MAX_EXTENSION];
122         char language[MAX_LANGUAGE];
123         char callerid[AST_MAX_EXTENSION];
124 } *iflist = NULL;
125
126 static char callerid[AST_MAX_EXTENSION];
127
128 static int phone_digit(struct ast_channel *ast, char digit)
129 {
130         struct phone_pvt *p;
131         int outdigit;
132         p = ast->pvt->pvt;
133         ast_log(LOG_NOTICE, "Dialed %c\n", digit);
134         switch(digit) {
135         case '0':
136         case '1':
137         case '2':
138         case '3':
139         case '4':
140         case '5':
141         case '6':
142         case '7':
143         case '8':
144         case '9':
145                 outdigit = digit - '0';
146                 break;
147         case '*':
148                 outdigit = 11;
149                 break;
150         case '#':
151                 outdigit = 12;
152                 break;
153         case 'f':       //flash
154         case 'F':
155                 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
156                 usleep(320000);
157                 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
158                 p->lastformat = -1;
159                 return 0;
160         default:
161                 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
162                 return -1;
163         }
164         ast_log(LOG_NOTICE, "Dialed %i\n", outdigit);
165         ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
166         p->lastformat = -1;
167         return 0;
168 }
169
170 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
171 {
172         struct phone_pvt *p;
173
174         PHONE_CID cid;
175         time_t UtcTime;
176         struct tm tm;
177
178         time(&UtcTime);
179         localtime_r(&UtcTime,&tm);
180
181         if(&tm != NULL) {
182                 sprintf(cid.month, "%02d",(tm.tm_mon + 1));
183                 sprintf(cid.day,   "%02d", tm.tm_mday);
184                 sprintf(cid.hour,  "%02d", tm.tm_hour);
185                 sprintf(cid.min,   "%02d", tm.tm_min);
186         }
187         /* the standard format of ast->callerid is:  "name" <number>, but not always complete */
188         if (!ast->callerid || ast_strlen_zero(ast->callerid)){
189                 strcpy(cid.name, DEFAULT_CALLER_ID);
190                 cid.number[0]='\0';
191         } else {
192                 char *n, *l;
193                 char callerid[256] = "";
194                 strncpy(callerid, ast->callerid, sizeof(callerid) - 1);
195                 ast_callerid_parse(callerid, &n, &l);
196                 if (l) {
197                         ast_shrink_phone_number(l);
198                         if (!ast_isphonenumber(l))
199                                 l = NULL;
200                 }
201                 if (l)
202                         strncpy(cid.number, l, sizeof(cid.number));
203                 if (n)
204                         strncpy(cid.name, n, sizeof(cid.name));
205         }
206
207         p = ast->pvt->pvt;
208
209         if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
210                 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
211                 return -1;
212         }
213         if (option_debug)
214                 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
215
216         IXJ_PHONE_RING_START(cid);
217         ast_setstate(ast, AST_STATE_RINGING);
218         ast_queue_control(ast, AST_CONTROL_RINGING);
219         return 0;
220 }
221
222 static int phone_hangup(struct ast_channel *ast)
223 {
224         struct phone_pvt *p;
225         p = ast->pvt->pvt;
226         if (option_debug)
227                 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
228         if (!ast->pvt->pvt) {
229                 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
230                 return 0;
231         }
232         /* XXX Is there anything we can do to really hang up except stop recording? */
233         ast_setstate(ast, AST_STATE_DOWN);
234         if (ioctl(p->fd, PHONE_REC_STOP))
235                 ast_log(LOG_WARNING, "Failed to stop recording\n");
236         if (ioctl(p->fd, PHONE_PLAY_STOP))
237                 ast_log(LOG_WARNING, "Failed to stop playing\n");
238         if (ioctl(p->fd, PHONE_RING_STOP))
239                 ast_log(LOG_WARNING, "Failed to stop ringing\n");
240         if (ioctl(p->fd, PHONE_CPT_STOP))
241                 ast_log(LOG_WARNING, "Failed to stop sounds\n");
242
243         /* If it's an FXO, hang them up */
244         if (p->mode == MODE_FXO) {
245                 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK)) 
246                         ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
247         }
248
249         /* If they're off hook, give a busy signal */
250         if (ioctl(p->fd, PHONE_HOOKSTATE)) {
251                 if (option_debug)
252                         ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
253                 ioctl(p->fd, PHONE_BUSY);
254                 p->cpt = 1;
255         }
256         p->lastformat = -1;
257         p->lastinput = -1;
258         p->ministate = 0;
259         p->obuflen = 0;
260         p->dialtone = 0;
261         memset(p->ext, 0, sizeof(p->ext));
262         ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
263         ast_mutex_lock(&usecnt_lock);
264         usecnt--;
265         if (usecnt < 0) 
266                 ast_log(LOG_WARNING, "Usecnt < 0???\n");
267         ast_mutex_unlock(&usecnt_lock);
268         ast_update_use_count();
269         if (option_verbose > 2) 
270                 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
271         ast->pvt->pvt = NULL;
272         ast_setstate(ast, AST_STATE_DOWN);
273         restart_monitor();
274         return 0;
275 }
276
277 static int phone_setup(struct ast_channel *ast)
278 {
279         struct phone_pvt *p;
280         p = ast->pvt->pvt;
281         ioctl(p->fd, PHONE_CPT_STOP);
282         /* Nothing to answering really, just start recording */
283         if (ast->pvt->rawreadformat == AST_FORMAT_G723_1) {
284                 /* Prefer g723 */
285                 ioctl(p->fd, PHONE_REC_STOP);
286                 if (p->lastinput != AST_FORMAT_G723_1) {
287                         p->lastinput = AST_FORMAT_G723_1;
288                         if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
289                                 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
290                                 return -1;
291                         }
292                 }
293         } else if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
294                 ioctl(p->fd, PHONE_REC_STOP);
295                 if (p->lastinput != AST_FORMAT_SLINEAR) {
296                         p->lastinput = AST_FORMAT_SLINEAR;
297                         if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
298                                 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
299                                 return -1;
300                         }
301                 }
302         } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
303                 ioctl(p->fd, PHONE_REC_STOP);
304                 if (p->lastinput != AST_FORMAT_ULAW) {
305                         p->lastinput = AST_FORMAT_ULAW;
306                         if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
307                                 ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
308                                 return -1;
309                         }
310                 }
311         } else {
312                 ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->pvt->rawreadformat));
313                 return -1;
314         }
315         if (ioctl(p->fd, PHONE_REC_START)) {
316                 ast_log(LOG_WARNING, "Failed to start recording\n");
317                 return -1;
318         }
319         //set the DTMF times (the default is too short)
320         ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
321         ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
322         return 0;
323 }
324
325 static int phone_answer(struct ast_channel *ast)
326 {
327         struct phone_pvt *p;
328         p = ast->pvt->pvt;
329         /* In case it's a LineJack, take it off hook */
330         if (p->mode == MODE_FXO) {
331                 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK)) 
332                         ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
333                 else
334                         ast_log(LOG_DEBUG, "Took linejack off hook\n");
335         }
336         phone_setup(ast);
337         if (option_debug)
338                 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
339         ast->rings = 0;
340         ast_setstate(ast, AST_STATE_UP);
341         return 0;
342 }
343
344 #if 0
345 static char phone_2digit(char c)
346 {
347         if (c == 12)
348                 return '#';
349         else if (c == 11)
350                 return '*';
351         else if ((c < 10) && (c >= 0))
352                 return '0' + c - 1;
353         else
354                 return '?';
355 }
356 #endif
357
358 static struct ast_frame  *phone_exception(struct ast_channel *ast)
359 {
360         int res;
361         union telephony_exception phonee;
362         struct phone_pvt *p = ast->pvt->pvt;
363         char digit;
364
365         /* Some nice norms */
366         p->fr.datalen = 0;
367         p->fr.samples = 0;
368         p->fr.data =  NULL;
369         p->fr.src = type;
370         p->fr.offset = 0;
371         p->fr.mallocd=0;
372
373         phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
374         if (phonee.bits.dtmf_ready)  {
375                 if (option_debug)
376                         ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
377         
378                 /* We've got a digit -- Just handle this nicely and easily */
379                 digit =  ioctl(p->fd, PHONE_GET_DTMF_ASCII);
380                 p->fr.subclass = digit;
381                 p->fr.frametype = AST_FRAME_DTMF;
382                 return &p->fr;
383         }
384         if (phonee.bits.hookstate) {
385                 if (option_debug)
386                         ast_log(LOG_DEBUG, "Hookstate changed\n");
387                 res = ioctl(p->fd, PHONE_HOOKSTATE);
388                 /* See if we've gone on hook, if so, notify by returning NULL */
389                 if (option_debug)
390                         ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
391                 if (!res && (p->mode != MODE_FXO))
392                         return NULL;
393                 else {
394                         if (ast->_state == AST_STATE_RINGING) {
395                                 /* They've picked up the phone */
396                                 p->fr.frametype = AST_FRAME_CONTROL;
397                                 p->fr.subclass = AST_CONTROL_ANSWER;
398                                 phone_setup(ast);
399                                 ast_setstate(ast, AST_STATE_UP);
400                                 return &p->fr;
401                         }  else 
402                                 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
403                 }
404         }
405 #if 1
406         if (phonee.bits.pstn_ring)
407                 ast_verbose("Unit is ringing\n");
408         if (phonee.bits.caller_id) {
409                 ast_verbose("We have caller ID\n");
410         }
411         if (phonee.bits.pstn_wink)
412                 ast_verbose("Detected Wink\n");
413 #endif
414         /* Strange -- nothing there.. */
415         p->fr.frametype = AST_FRAME_NULL;
416         p->fr.subclass = 0;
417         return &p->fr;
418 }
419
420 static struct ast_frame  *phone_read(struct ast_channel *ast)
421 {
422         int res;
423         struct phone_pvt *p = ast->pvt->pvt;
424         
425
426         /* Some nice norms */
427         p->fr.datalen = 0;
428         p->fr.samples = 0;
429         p->fr.data =  NULL;
430         p->fr.src = type;
431         p->fr.offset = 0;
432         p->fr.mallocd=0;
433
434         /* Try to read some data... */
435         CHECK_BLOCKING(ast);
436         res = read(p->fd, p->buf, PHONE_MAX_BUF);
437         ast->blocking = 0;
438         if (res < 0) {
439 #if 0
440                 if (errno == EAGAIN) {
441                         ast_log(LOG_WARNING, "Null frame received\n");
442                         p->fr.frametype = AST_FRAME_NULL;
443                         p->fr.subclass = 0;
444                         return &p->fr;
445                 }
446 #endif
447                 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
448                 return NULL;
449         }
450         p->fr.data = p->buf;
451         switch(p->buf[0] & 0x3) {
452         case '0':
453         case '1':
454                 /* Normal */
455                 break;
456         case '2':
457         case '3':
458                 /* VAD/CNG, only send two words */
459                 res = 4;
460                 break;
461         }
462         p->fr.samples = 240;
463         p->fr.datalen = res;
464         p->fr.frametype = AST_FRAME_VOICE;
465         p->fr.subclass = p->lastinput;
466         p->fr.offset = AST_FRIENDLY_OFFSET;
467         return &p->fr;
468 }
469
470 static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
471 {
472         int res;
473         /* Store as much of the buffer as we can, then write fixed frames */
474         int space = sizeof(p->obuf) - p->obuflen;
475         /* Make sure we have enough buffer space to store the frame */
476         if (space < len)
477                 len = space;
478         memcpy(p->obuf + p->obuflen, buf, len);
479         p->obuflen += len;
480         while(p->obuflen > frlen) {
481                 res = write(p->fd, p->obuf, frlen);
482                 if (res != frlen) {
483                         if (res < 1) {
484 /*
485  * Card is in non-blocking mode now and it works well now, but there are
486  * lot of messages like this. So, this message is temporarily disabled.
487  */
488                                 return 0;
489                         } else {
490                                 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
491                         }
492                 }
493                 p->obuflen -= frlen;
494                 /* Move memory if necessary */
495                 if (p->obuflen) 
496                         memmove(p->obuf, p->obuf + frlen, p->obuflen);
497         }
498         return len;
499 }
500
501 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
502 {
503         struct phone_pvt *p = ast->pvt->pvt;
504         int res;
505         int maxfr=0;
506         char *pos;
507         int sofar;
508         int expected;
509         int codecset = 0;
510         char tmpbuf[4];
511         /* Write a frame of (presumably voice) data */
512         if (frame->frametype != AST_FRAME_VOICE) {
513                 if (frame->frametype != AST_FRAME_IMAGE)
514                         ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
515                 return 0;
516         }
517         if (!(frame->subclass &
518                 (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW))) {
519                 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
520                 return -1;
521         }
522 #if 0
523         /* If we're not in up mode, go into up mode now */
524         if (ast->_state != AST_STATE_UP) {
525                 ast_setstate(ast, AST_STATE_UP);
526                 phone_setup(ast);
527         }
528 #else
529         if (ast->_state != AST_STATE_UP) {
530                 /* Don't try tos end audio on-hook */
531                 return 0;
532         }
533 #endif  
534         if (frame->subclass == AST_FORMAT_G723_1) {
535                 if (p->lastformat != AST_FORMAT_G723_1) {
536                         ioctl(p->fd, PHONE_PLAY_STOP);
537                         ioctl(p->fd, PHONE_REC_STOP);
538                         if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
539                                 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
540                                 return -1;
541                         }
542                         if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
543                                 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
544                                 return -1;
545                         }
546                         p->lastformat = AST_FORMAT_G723_1;
547                         p->lastinput = AST_FORMAT_G723_1;
548                         /* Reset output buffer */
549                         p->obuflen = 0;
550                         codecset = 1;
551                 }
552                 if (frame->datalen > 24) {
553                         ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
554                         return -1;
555                 }
556                 maxfr = 24;
557         } else if (frame->subclass == AST_FORMAT_SLINEAR) {
558                 if (p->lastformat != AST_FORMAT_SLINEAR) {
559                         ioctl(p->fd, PHONE_PLAY_STOP);
560                         ioctl(p->fd, PHONE_REC_STOP);
561                         if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
562                                 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
563                                 return -1;
564                         }
565                         if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
566                                 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
567                                 return -1;
568                         }
569                         p->lastformat = AST_FORMAT_SLINEAR;
570                         p->lastinput = AST_FORMAT_SLINEAR;
571                         codecset = 1;
572                         /* Reset output buffer */
573                         p->obuflen = 0;
574                 }
575                 maxfr = 480;
576         } else if (frame->subclass == AST_FORMAT_ULAW) {
577                 if (p->lastformat != AST_FORMAT_ULAW) {
578                         ioctl(p->fd, PHONE_PLAY_STOP);
579                         ioctl(p->fd, PHONE_REC_STOP);
580                         if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
581                                 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
582                                 return -1;
583                         }
584                         if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
585                                 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
586                                 return -1;
587                         }
588                         p->lastformat = AST_FORMAT_ULAW;
589                         p->lastinput = AST_FORMAT_ULAW;
590                         codecset = 1;
591                         /* Reset output buffer */
592                         p->obuflen = 0;
593                 }
594                 maxfr = 240;
595         }
596         if (codecset) {
597                 ioctl(p->fd, PHONE_REC_DEPTH, 3);
598                 ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
599                 if (ioctl(p->fd, PHONE_PLAY_START)) {
600                         ast_log(LOG_WARNING, "Failed to start playback\n");
601                         return -1;
602                 }
603                 if (ioctl(p->fd, PHONE_REC_START)) {
604                         ast_log(LOG_WARNING, "Failed to start recording\n");
605                         return -1;
606                 }
607         }
608         /* If we get here, we have a voice frame of Appropriate data */
609         sofar = 0;
610         pos = frame->data;
611         while(sofar < frame->datalen) {
612                 /* Write in no more than maxfr sized frames */
613                 expected = frame->datalen - sofar;
614                 if (maxfr < expected)
615                         expected = maxfr;
616                 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX 
617                    we have to pad it to 24 bytes still.  */
618                 if (frame->datalen == 4) {
619                         if (p->silencesupression) {
620                                 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
621                                 memcpy(tmpbuf, frame->data, 4);
622                                 expected = 24;
623                                 res = phone_write_buf(p, tmpbuf, expected, maxfr);
624                         }
625                         res = 4;
626                         expected=4;
627                 } else {
628                         res = phone_write_buf(p, pos, expected, maxfr);
629                 }
630                 if (res != expected) {
631                         if ((errno != EAGAIN) && (errno != EINTR)) {
632                                 if (res < 0) 
633                                         ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
634         /*
635          * Card is in non-blocking mode now and it works well now, but there are
636          * lot of messages like this. So, this message is temporarily disabled.
637          */
638 #if 0
639                                 else
640                                         ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
641 #endif
642                                 return -1;
643                         } else /* Pretend it worked */
644                                 res = expected;
645                 }
646                 sofar += res;
647                 pos += res;
648         }
649         return 0;
650 }
651
652 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
653 {
654         struct ast_channel *tmp;
655         tmp = ast_channel_alloc(1);
656         if (tmp) {
657                 snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
658                 tmp->type = type;
659                 tmp->fds[0] = i->fd;
660                 /* XXX Switching formats silently causes kernel panics XXX */
661                 tmp->nativeformats = prefformat;
662                 tmp->pvt->rawreadformat = prefformat;
663                 tmp->pvt->rawwriteformat = prefformat;
664                 ast_setstate(tmp, state);
665                 if (state == AST_STATE_RING)
666                         tmp->rings = 1;
667                 tmp->pvt->pvt = i;
668                 tmp->pvt->send_digit = phone_digit;
669                 tmp->pvt->call = phone_call;
670                 tmp->pvt->hangup = phone_hangup;
671                 tmp->pvt->answer = phone_answer;
672                 tmp->pvt->read = phone_read;
673                 tmp->pvt->write = phone_write;
674                 tmp->pvt->exception = phone_exception;
675                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
676                 if (strlen(i->ext))
677                         strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
678                 else
679                         strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
680                 if (strlen(i->language))
681                         strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
682                 if (strlen(i->callerid))
683                         tmp->callerid = strdup(i->callerid);
684                 i->owner = tmp;
685                 ast_mutex_lock(&usecnt_lock);
686                 usecnt++;
687                 ast_mutex_unlock(&usecnt_lock);
688                 ast_update_use_count();
689                 if (state != AST_STATE_DOWN) {
690                         if (state == AST_STATE_RING) {
691                                 ioctl(tmp->fds[0], PHONE_RINGBACK);
692                                 i->cpt = 1;
693                         }
694                         if (ast_pbx_start(tmp)) {
695                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
696                                 ast_hangup(tmp);
697                         }
698                 }
699         } else
700                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
701         return tmp;
702 }
703
704 static void phone_mini_packet(struct phone_pvt *i)
705 {
706         int res;
707         char buf[1024];
708         /* Ignore stuff we read... */
709         res = read(i->fd, buf, sizeof(buf));
710         if (res < 1) {
711                 ast_log(LOG_WARNING, "Read returned %d\n", res);
712                 return;
713         }
714 }
715
716 static void phone_check_exception(struct phone_pvt *i)
717 {
718         int offhook=0;
719         char digit[2] = {0 , 0};
720         union telephony_exception phonee;
721         /* XXX Do something XXX */
722 #if 0
723         ast_log(LOG_DEBUG, "Exception!\n");
724 #endif
725         phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
726         if (phonee.bits.dtmf_ready)  {
727                 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
728                 if (i->mode == MODE_DIALTONE) {
729                         ioctl(i->fd, PHONE_PLAY_STOP);
730                         ioctl(i->fd, PHONE_REC_STOP);
731                         ioctl(i->fd, PHONE_CPT_STOP);
732                         i->dialtone = 0;
733                         if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
734                                 strcat(i->ext, digit);
735                         if (ast_exists_extension(NULL, i->context, i->ext, 1, i->callerid)) {
736                                 /* It's a valid extension in its context, get moving! */
737                                 phone_new(i, AST_STATE_RING, i->context);
738                                 /* No need to restart monitor, we are the monitor */
739                                 if (i->owner) {
740                                         ast_mutex_lock(&usecnt_lock);
741                                         usecnt--;
742                                         ast_mutex_unlock(&usecnt_lock);
743                                         ast_update_use_count();
744                                 }
745                         } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->callerid)) {
746                                 /* There is nothing in the specified extension that can match anymore.
747                                    Try the default */
748                                 if (ast_exists_extension(NULL, "default", i->ext, 1, i->callerid)) {
749                                         /* Check the default, too... */
750                                         phone_new(i, AST_STATE_RING, "default");
751                                         if (i->owner) {
752                                                 ast_mutex_lock(&usecnt_lock);
753                                                 usecnt--;
754                                                 ast_mutex_unlock(&usecnt_lock);
755                                                 ast_update_use_count();
756                                         }
757                                         /* XXX This should probably be justified better XXX */
758                                 }  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->callerid)) {
759                                         /* It's not a valid extension, give a busy signal */
760                                         if (option_debug)
761                                                 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
762                                         ioctl(i->fd, PHONE_BUSY);
763                                         i->cpt = 1;
764                                 }
765                         }
766 #if 0
767                         ast_verbose("Extension is %s\n", i->ext);
768 #endif
769                 }
770         }
771         if (phonee.bits.hookstate) {
772                 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
773                 if (offhook) {
774                         if (i->mode == MODE_IMMEDIATE) {
775                                 phone_new(i, AST_STATE_RING, i->context);
776                         } else if (i->mode == MODE_DIALTONE) {
777                                 ast_mutex_lock(&usecnt_lock);
778                                 usecnt++;
779                                 ast_mutex_unlock(&usecnt_lock);
780                                 ast_update_use_count();
781                                 /* Reset the extension */
782                                 i->ext[0] = '\0';
783                                 /* Play the dialtone */
784                                 i->dialtone++;
785                                 ioctl(i->fd, PHONE_PLAY_STOP);
786                                 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
787                                 ioctl(i->fd, PHONE_PLAY_START);
788                                 i->lastformat = -1;
789                         }
790                 } else {
791                         if (i->dialtone) {
792                                 ast_mutex_lock(&usecnt_lock);
793                                 usecnt--;
794                                 ast_mutex_unlock(&usecnt_lock);
795                                 ast_update_use_count();
796                         }
797                         memset(i->ext, 0, sizeof(i->ext));
798                         if (i->cpt)
799                         {
800                                 ioctl(i->fd, PHONE_CPT_STOP);
801                                 i->cpt = 0;
802                         }
803                         ioctl(i->fd, PHONE_PLAY_STOP);
804                         ioctl(i->fd, PHONE_REC_STOP);
805                         i->dialtone = 0;
806                         i->lastformat = -1;
807                 }
808         }
809         if (phonee.bits.pstn_ring) {
810                 ast_verbose("Unit is ringing\n");
811                 phone_new(i, AST_STATE_RING, i->context);
812         }
813         if (phonee.bits.caller_id)
814                 ast_verbose("We have caller ID\n");
815         
816         
817 }
818
819 static void *do_monitor(void *data)
820 {
821         fd_set rfds, efds;
822         int n, res;
823         struct phone_pvt *i;
824         int tonepos = 0;
825         /* The tone we're playing this round */
826         struct timeval tv = {0,0};
827         int dotone;
828         /* This thread monitors all the frame relay interfaces which are not yet in use
829            (and thus do not have a separate thread) indefinitely */
830         /* From here on out, we die whenever asked */
831         if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
832                 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
833                 return NULL;
834         }
835         for(;;) {
836                 /* Don't let anybody kill us right away.  Nobody should lock the interface list
837                    and wait for the monitor list, but the other way around is okay. */
838                 if (ast_mutex_lock(&monlock)) {
839                         ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
840                         return NULL;
841                 }
842                 /* Lock the interface list */
843                 if (ast_mutex_lock(&iflock)) {
844                         ast_log(LOG_ERROR, "Unable to grab interface lock\n");
845                         ast_mutex_unlock(&monlock);
846                         return NULL;
847                 }
848                 /* Build the stuff we're going to select on, that is the socket of every
849                    phone_pvt that does not have an associated owner channel */
850                 n = -1;
851                 FD_ZERO(&rfds);
852                 FD_ZERO(&efds);
853                 i = iflist;
854                 dotone = 0;
855                 while(i) {
856                         if (FD_ISSET(i->fd, &rfds)) 
857                                 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
858                         if (!i->owner) {
859                                 /* This needs to be watched, as it lacks an owner */
860                                 FD_SET(i->fd, &rfds);
861                                 FD_SET(i->fd, &efds);
862                                 if (i->fd > n)
863                                         n = i->fd;
864                                 if (i->dialtone) {
865                                         /* Remember we're going to have to come back and play
866                                            more dialtones */
867                                         if (!tv.tv_usec && !tv.tv_sec) {
868                                                 /* If we're due for a dialtone, play one */
869                                                 if (write(i->fd, DialTone + tonepos, 240) != 240)
870                                                         ast_log(LOG_WARNING, "Dial tone write error\n");
871                                         }
872                                         dotone++;
873                                 }
874                         }
875                         
876                         i = i->next;
877                 }
878                 /* Okay, now that we know what to do, release the interface lock */
879                 ast_mutex_unlock(&iflock);
880                 
881                 /* And from now on, we're okay to be killed, so release the monitor lock as well */
882                 ast_mutex_unlock(&monlock);
883                 /* Wait indefinitely for something to happen */
884                 if (dotone) {
885                         /* If we're ready to recycle the time, set it to 30 ms */
886                         tonepos += 240;
887                         if (tonepos >= sizeof(DialTone))
888                                         tonepos = 0;
889                         if (!tv.tv_usec && !tv.tv_sec) {
890                                 tv.tv_usec = 30000;
891                                 tv.tv_sec = 0;
892                         }
893                         res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
894                 } else {
895                         res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
896                         tv.tv_usec = 0;
897                         tv.tv_sec = 0;
898                         tonepos = 0;
899                 }
900                 /* Okay, select has finished.  Let's see what happened.  */
901                 if (res < 0) {
902                         ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
903                         continue;
904                 }
905                 /* If there are no fd's changed, just continue, it's probably time
906                    to play some more dialtones */
907                 if (!res)
908                         continue;
909                 /* Alright, lock the interface list again, and let's look and see what has
910                    happened */
911                 if (ast_mutex_lock(&iflock)) {
912                         ast_log(LOG_WARNING, "Unable to lock the interface list\n");
913                         continue;
914                 }
915                 i = iflist;
916                 while(i) {
917                         if (FD_ISSET(i->fd, &rfds)) {
918                                 if (i->owner) {
919                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
920                                         continue;
921                                 }
922                                 phone_mini_packet(i);
923                         }
924                         if (FD_ISSET(i->fd, &efds)) {
925                                 if (i->owner) {
926                                         ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
927                                         continue;
928                                 }
929                                 phone_check_exception(i);
930                         }
931                         i=i->next;
932                 }
933                 ast_mutex_unlock(&iflock);
934         }
935         /* Never reached */
936         return NULL;
937         
938 }
939
940 static int restart_monitor()
941 {
942         /* If we're supposed to be stopped -- stay stopped */
943         if (monitor_thread == AST_PTHREADT_STOP)
944                 return 0;
945         if (ast_mutex_lock(&monlock)) {
946                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
947                 return -1;
948         }
949         if (monitor_thread == pthread_self()) {
950                 ast_mutex_unlock(&monlock);
951                 ast_log(LOG_WARNING, "Cannot kill myself\n");
952                 return -1;
953         }
954         if (monitor_thread != AST_PTHREADT_NULL) {
955                 pthread_cancel(monitor_thread);
956 #if 0
957                 pthread_join(monitor_thread, NULL);
958 #endif
959         }
960         /* Start a new monitor */
961         if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
962                 ast_mutex_unlock(&monlock);
963                 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
964                 return -1;
965         }
966         ast_mutex_unlock(&monlock);
967         return 0;
968 }
969
970 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
971 {
972         /* Make a phone_pvt structure for this interface */
973         struct phone_pvt *tmp;
974         int flags;      
975         
976         tmp = malloc(sizeof(struct phone_pvt));
977         if (tmp) {
978                 tmp->fd = open(iface, O_RDWR);
979                 if (tmp->fd < 0) {
980                         ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
981                         free(tmp);
982                         return NULL;
983                 }
984                 if (mode == MODE_FXO) {
985                         if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN)) 
986                                 ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
987                 } else {
988                         if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS)) 
989                                 ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
990                 }
991                 ioctl(tmp->fd, PHONE_PLAY_STOP);
992                 ioctl(tmp->fd, PHONE_REC_STOP);
993                 ioctl(tmp->fd, PHONE_RING_STOP);
994                 ioctl(tmp->fd, PHONE_CPT_STOP);
995                 if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK)) 
996                         ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
997                 if (echocancel != AEC_OFF)
998                         ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
999                 if (silencesupression) 
1000                         tmp->silencesupression = 1;
1001 #ifdef PHONE_VAD
1002                 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
1003 #endif
1004                 tmp->mode = mode;
1005                 flags = fcntl(tmp->fd, F_GETFL);
1006                 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
1007                 tmp->owner = NULL;
1008                 tmp->lastformat = -1;
1009                 tmp->lastinput = -1;
1010                 tmp->ministate = 0;
1011                 memset(tmp->ext, 0, sizeof(tmp->ext));
1012                 strncpy(tmp->language, language, sizeof(tmp->language)-1);
1013                 strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
1014                 strncpy(tmp->context, context, sizeof(tmp->context)-1);
1015                 tmp->next = NULL;
1016                 tmp->obuflen = 0;
1017                 tmp->dialtone = 0;
1018                 tmp->cpt = 0;
1019                 strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
1020                 tmp->txgain = txgain;
1021                 ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
1022                 tmp->rxgain = rxgain;
1023                 ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
1024         }
1025         return tmp;
1026 }
1027
1028 static struct ast_channel *phone_request(char *type, int format, void *data)
1029 {
1030         int oldformat;
1031         struct phone_pvt *p;
1032         struct ast_channel *tmp = NULL;
1033         char *name = data;
1034         
1035         oldformat = format;
1036         format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
1037         if (!format) {
1038                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1039                 return NULL;
1040         }
1041         /* Search for an unowned channel */
1042         if (ast_mutex_lock(&iflock)) {
1043                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1044                 return NULL;
1045         }
1046         p = iflist;
1047         while(p) {
1048                 if (!strcmp(name, p->dev + 5)) {
1049                         if (!p->owner) {
1050                                 tmp = phone_new(p, AST_STATE_DOWN, p->context);
1051                                 break;
1052                         }
1053                 }
1054                 p = p->next;
1055         }
1056         ast_mutex_unlock(&iflock);
1057         restart_monitor();
1058         return tmp;
1059 }
1060
1061 /* parse gain value from config file */
1062 static int parse_gain_value(char *gain_type, char *value)
1063 {
1064         float gain;
1065
1066         /* try to scan number */
1067         if (sscanf(value, "%f", &gain) != 1)
1068         {
1069                 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1070                         value, gain_type, config);
1071                 return DEFAULT_GAIN;
1072         }
1073
1074         /* multiplicate gain by 1.0 gain value */ 
1075         gain = gain * (float)DEFAULT_GAIN;
1076
1077         /* percentage? */
1078         if (value[strlen(value) - 1] == '%')
1079                 return (int)(gain / (float)100);
1080
1081         return (int)gain;
1082 }
1083
1084 static int __unload_module(void)
1085 {
1086         struct phone_pvt *p, *pl;
1087         /* First, take us out of the channel loop */
1088         ast_channel_unregister(type);
1089         if (!ast_mutex_lock(&iflock)) {
1090                 /* Hangup all interfaces if they have an owner */
1091                 p = iflist;
1092                 while(p) {
1093                         if (p->owner)
1094                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1095                         p = p->next;
1096                 }
1097                 iflist = NULL;
1098                 ast_mutex_unlock(&iflock);
1099         } else {
1100                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1101                 return -1;
1102         }
1103         if (!ast_mutex_lock(&monlock)) {
1104                 if (monitor_thread > AST_PTHREADT_NULL) {
1105                         pthread_cancel(monitor_thread);
1106                         pthread_join(monitor_thread, NULL);
1107                 }
1108                 monitor_thread = AST_PTHREADT_STOP;
1109                 ast_mutex_unlock(&monlock);
1110         } else {
1111                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1112                 return -1;
1113         }
1114
1115         if (!ast_mutex_lock(&iflock)) {
1116                 /* Destroy all the interfaces and free their memory */
1117                 p = iflist;
1118                 while(p) {
1119                         /* Close the socket, assuming it's real */
1120                         if (p->fd > -1)
1121                                 close(p->fd);
1122                         pl = p;
1123                         p = p->next;
1124                         /* Free associated memory */
1125                         free(pl);
1126                 }
1127                 iflist = NULL;
1128                 ast_mutex_unlock(&iflock);
1129         } else {
1130                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1131                 return -1;
1132         }
1133                 
1134         return 0;
1135 }
1136
1137 int unload_module(void)
1138 {
1139         return __unload_module();
1140 }
1141
1142 int load_module()
1143 {
1144         struct ast_config *cfg;
1145         struct ast_variable *v;
1146         struct phone_pvt *tmp;
1147         int mode = MODE_IMMEDIATE;
1148         int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
1149         cfg = ast_load(config);
1150
1151         /* We *must* have a config file otherwise stop immediately */
1152         if (!cfg) {
1153                 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1154                 return -1;
1155         }
1156         if (ast_mutex_lock(&iflock)) {
1157                 /* It's a little silly to lock it, but we mind as well just to be sure */
1158                 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1159                 return -1;
1160         }
1161         v = ast_variable_browse(cfg, "interfaces");
1162         while(v) {
1163                 /* Create the interface list */
1164                 if (!strcasecmp(v->name, "device")) {
1165                                 tmp = mkif(v->value, mode, txgain, rxgain);
1166                                 if (tmp) {
1167                                         tmp->next = iflist;
1168                                         iflist = tmp;
1169                                         
1170                                 } else {
1171                                         ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1172                                         ast_destroy(cfg);
1173                                         ast_mutex_unlock(&iflock);
1174                                         __unload_module();
1175                                         return -1;
1176                                 }
1177                 } else if (!strcasecmp(v->name, "silencesupression")) {
1178                         silencesupression = ast_true(v->value);
1179                 } else if (!strcasecmp(v->name, "language")) {
1180                         strncpy(language, v->value, sizeof(language)-1);
1181                 } else if (!strcasecmp(v->name, "callerid")) {
1182                         strncpy(callerid, v->value, sizeof(callerid)-1);
1183                 } else if (!strcasecmp(v->name, "mode")) {
1184                         if (!strncasecmp(v->value, "di", 2)) 
1185                                 mode = MODE_DIALTONE;
1186                         else if (!strncasecmp(v->value, "im", 2))
1187                                 mode = MODE_IMMEDIATE;
1188                         else if (!strncasecmp(v->value, "fx", 2))
1189                                 mode = MODE_FXO;
1190                         else
1191                                 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1192                 } else if (!strcasecmp(v->name, "context")) {
1193                         strncpy(context, v->value, sizeof(context)-1);
1194                 } else if (!strcasecmp(v->name, "format")) {
1195                         if (!strcasecmp(v->value, "g723.1")) {
1196                                 prefformat = AST_FORMAT_G723_1;
1197                         } else if (!strcasecmp(v->value, "slinear")) {
1198                                 prefformat = AST_FORMAT_SLINEAR;
1199                         } else if (!strcasecmp(v->value, "ulaw")) {
1200                                 prefformat = AST_FORMAT_ULAW;
1201                         } else
1202                                 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
1203                 } else if (!strcasecmp(v->name, "echocancel")) {
1204                         if (!strcasecmp(v->value, "off")) {
1205                                 echocancel = AEC_OFF;
1206                         } else if (!strcasecmp(v->value, "low")) {
1207                                 echocancel = AEC_LOW;
1208                         } else if (!strcasecmp(v->value, "medium")) {
1209                                 echocancel = AEC_MED;
1210                         } else if (!strcasecmp(v->value, "high")) {
1211                                 echocancel = AEC_HIGH;
1212                         } else 
1213                                 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
1214                 } else if (!strcasecmp(v->name, "txgain")) {
1215                         txgain = parse_gain_value(v->name, v->value);
1216                 } else if (!strcasecmp(v->name, "rxgain")) {
1217                         rxgain = parse_gain_value(v->name, v->value);
1218                 }       
1219                 v = v->next;
1220         }
1221         ast_mutex_unlock(&iflock);
1222         /* Make sure we can register our Adtranphone channel type */
1223         if (ast_channel_register(type, tdesc, 
1224                          AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, phone_request)) {
1225                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1226                 ast_destroy(cfg);
1227                 __unload_module();
1228                 return -1;
1229         }
1230         ast_destroy(cfg);
1231         /* And start the monitor for the first time */
1232         restart_monitor();
1233         return 0;
1234 }
1235
1236 int usecount()
1237 {
1238         int res;
1239         ast_mutex_lock(&usecnt_lock);
1240         res = usecnt;
1241         ast_mutex_unlock(&usecnt_lock);
1242         return res;
1243 }
1244
1245 char *description()
1246 {
1247         return desc;
1248 }
1249
1250 char *key()
1251 {
1252         return ASTERISK_GPL_KEY;
1253 }