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