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