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