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