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