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