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