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