2 * Asterisk -- A telephony toolkit for Linux.
4 * Generic Linux Telephony Interface driver
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <sys/socket.h>
29 #include <arpa/inet.h>
31 #include <sys/ioctl.h>
32 #include <linux/telephony.h>
33 /* Still use some IXJ specific stuff */
34 #include <linux/ixjuser.h>
37 #define phone_MAX_BUF 480
39 static char *desc = "Linux Telephony API Support";
40 static char *type = "Phone";
41 static char *tdesc = "Standard Linux Telephony API Driver";
42 static char *config = "phone.conf";
44 /* Default context for dialtone mode */
45 static char context[AST_MAX_EXTENSION] = "default";
49 static int echocancel = AEC_OFF;
51 static int silencesupression = 0;
53 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR;
55 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
57 /* Protect the interface list (of phone_pvt's) */
58 static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
60 /* Protect the monitoring thread, so only one process can kill or start it, and not
61 when it's doing something critical. */
62 static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
64 /* This is the thread for the monitor which checks for input on the channels
65 which are not currently in use. */
66 static pthread_t monitor_thread = -1;
68 static int restart_monitor(void);
70 /* The private structures of the Phone Jack channels are linked for
71 selecting outgoing channels */
73 #define MODE_DIALTONE 1
74 #define MODE_IMMEDIATE 2
76 static struct phone_pvt {
77 int fd; /* Raw file descriptor for this device */
78 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
79 int mode; /* Is this in the */
80 int lastformat; /* Last output format */
81 int lastinput; /* Last input format */
82 int ministate; /* Miniature state, for dialtone mode */
83 char dev[256]; /* Device name */
84 struct phone_pvt *next; /* Next channel in list */
85 struct ast_frame fr; /* Frame */
86 char offset[AST_FRIENDLY_OFFSET];
87 char buf[phone_MAX_BUF]; /* Static buffer for reading frames */
90 int silencesupression;
91 char context[AST_MAX_EXTENSION];
92 char obuf[phone_MAX_BUF * 2];
93 char ext[AST_MAX_EXTENSION];
96 static int phone_digit(struct ast_channel *ast, char digit)
112 outdigit = digit - '0' + 1;
121 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
124 ioctl(p->fd, PHONE_PLAY_TONE, digit);
128 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
132 if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
133 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
136 /* When we call, it just works, really, there's no destination... Just
137 ring the phone and wait for someone to answer */
139 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fd);
140 ioctl(p->fd, PHONE_RING_START);
141 ast->state = AST_STATE_RINGING;
145 static int phone_hangup(struct ast_channel *ast)
150 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
151 if (!ast->pvt->pvt) {
152 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
155 /* XXX Is there anything we can do to really hang up except stop recording? */
156 ast->state = AST_STATE_DOWN;
157 if (ioctl(p->fd, PHONE_REC_STOP))
158 ast_log(LOG_WARNING, "Failed to stop recording\n");
159 if (ioctl(p->fd, PHONE_PLAY_STOP))
160 ast_log(LOG_WARNING, "Failed to stop playing\n");
161 if (ioctl(p->fd, PHONE_RING_STOP))
162 ast_log(LOG_WARNING, "Failed to stop ringing\n");
163 if (ioctl(p->fd, PHONE_CPT_STOP))
164 ast_log(LOG_WARNING, "Failed to stop sounds\n");
165 /* If they're off hook, give a busy signal */
166 if (ioctl(p->fd, PHONE_HOOKSTATE)) {
168 ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
169 ioctl(p->fd, PHONE_BUSY);
176 memset(p->ext, 0, sizeof(p->ext));
177 ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
178 pthread_mutex_lock(&usecnt_lock);
181 ast_log(LOG_WARNING, "Usecnt < 0???\n");
182 pthread_mutex_unlock(&usecnt_lock);
183 ast_update_use_count();
184 if (option_verbose > 2)
185 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
186 ast->pvt->pvt = NULL;
187 ast->state = AST_STATE_DOWN;
192 static int phone_setup(struct ast_channel *ast)
196 ioctl(p->fd, PHONE_CPT_STOP);
197 /* Nothing to answering really, just start recording */
198 if (ast->format & AST_FORMAT_G723_1) {
200 ioctl(p->fd, PHONE_REC_STOP);
201 if (p->lastinput != AST_FORMAT_G723_1) {
202 p->lastinput = AST_FORMAT_G723_1;
203 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
204 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
208 } else if (ast->format & AST_FORMAT_SLINEAR) {
209 ioctl(p->fd, PHONE_REC_STOP);
210 if (p->lastinput != AST_FORMAT_SLINEAR) {
211 p->lastinput = AST_FORMAT_SLINEAR;
212 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
213 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
218 ast_log(LOG_WARNING, "Can't do format %d\n", ast->format);
221 if (ioctl(p->fd, PHONE_REC_START)) {
222 ast_log(LOG_WARNING, "Failed to start recording\n");
228 static int phone_answer(struct ast_channel *ast)
232 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
234 ast->state = AST_STATE_UP;
238 static char phone_2digit(char c)
244 else if ((c < 10) && (c >= 0))
250 static struct ast_frame *phone_read(struct ast_channel *ast)
253 union telephony_exception phonee;
254 struct phone_pvt *p = ast->pvt->pvt;
257 /* Some nice norms */
265 phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
266 if (phonee.bits.dtmf_ready) {
267 /* We've got a digit -- Just handle this nicely and easily */
268 digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
269 p->fr.subclass = digit;
270 p->fr.frametype = AST_FRAME_DTMF;
273 if (phonee.bits.hookstate) {
274 res = ioctl(p->fd, PHONE_HOOKSTATE);
275 /* See if we've gone on hook, if so, notify by returning NULL */
279 if (ast->state == AST_STATE_RINGING) {
280 /* They've picked up the phone */
281 p->fr.frametype = AST_FRAME_CONTROL;
282 p->fr.subclass = AST_CONTROL_ANSWER;
284 ast->state = AST_STATE_UP;
287 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->state);
291 if (phonee.bits.pstn_ring)
292 ast_verbose("Unit is ringing\n");
293 if (phonee.bits.caller_id) {
294 ast_verbose("We have caller ID: %s\n");
297 /* Try to read some data... */
299 res = read(p->fd, p->buf, phone_MAX_BUF);
303 if (errno == EAGAIN) {
304 ast_log(LOG_WARNING, "Null frame received\n");
305 p->fr.frametype = AST_FRAME_NULL;
310 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
314 switch(p->buf[0] & 0x3) {
321 /* VAD/CNG, only send two words */
326 p->fr.frametype = AST_FRAME_VOICE;
327 p->fr.subclass = p->lastinput;
328 p->fr.offset = AST_FRIENDLY_OFFSET;
332 static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
335 /* Store as much of the buffer as we can, then write fixed frames */
336 int space = sizeof(p->obuf) - p->obuflen;
337 /* Make sure we have enough buffer space to store the frame */
340 memcpy(p->obuf + p->obuflen, buf, len);
342 while(p->obuflen > frlen) {
345 ast_log(LOG_DEBUG, "Wrote %d bytes\n", res);
347 res = write(p->fd, p->obuf, frlen);
351 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
354 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
358 /* Move memory if necessary */
360 memmove(p->obuf, p->obuf + frlen, p->obuflen);
365 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
367 struct phone_pvt *p = ast->pvt->pvt;
374 /* Write a frame of (presumably voice) data */
375 if (frame->frametype != AST_FRAME_VOICE) {
376 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
380 if (!(frame->subclass & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR))) {
381 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
385 /* If we're not in up mode, go into up mode now */
386 if (ast->state != AST_STATE_UP) {
387 ast->state = AST_STATE_UP;
390 if (frame->subclass == AST_FORMAT_G723_1) {
391 if (p->lastformat != AST_FORMAT_G723_1) {
392 ioctl(p->fd, PHONE_PLAY_STOP);
393 ioctl(p->fd, PHONE_REC_STOP);
394 if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
395 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
398 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
399 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
402 p->lastformat = AST_FORMAT_G723_1;
403 p->lastinput = AST_FORMAT_G723_1;
404 /* Reset output buffer */
407 if (frame->datalen > 24) {
408 ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
412 } else if (frame->subclass == AST_FORMAT_SLINEAR) {
413 if (p->lastformat != AST_FORMAT_SLINEAR) {
414 ioctl(p->fd, PHONE_PLAY_STOP);
415 ioctl(p->fd, PHONE_REC_STOP);
416 if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
417 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
420 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
421 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
424 p->lastformat = AST_FORMAT_SLINEAR;
425 p->lastinput = AST_FORMAT_SLINEAR;
426 /* Reset output buffer */
431 if (ioctl(p->fd, PHONE_PLAY_START)) {
432 ast_log(LOG_WARNING, "Failed to start playback\n");
435 if (ioctl(p->fd, PHONE_REC_START)) {
436 ast_log(LOG_WARNING, "Failed to start recording\n");
439 /* If we get here, we have a voice frame of Appropriate data */
442 while(sofar < frame->datalen) {
443 /* Write in no more than maxfr sized frames */
444 expected = frame->datalen - sofar;
445 if (maxfr < expected)
447 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
448 we have to pad it to 24 bytes still. */
449 if (frame->datalen == 4) {
450 if (p->silencesupression) {
451 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
452 memcpy(tmpbuf, frame->data, 4);
454 res = phone_write_buf(p, tmpbuf, expected, maxfr);
459 res = phone_write_buf(p, pos, expected, maxfr);
461 if (res != expected) {
463 ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
465 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
474 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
476 struct ast_channel *tmp;
477 tmp = ast_channel_alloc();
479 snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
482 /* XXX Switching formats silently causes kernel panics XXX */
483 tmp->format = prefformat;
485 if (state == AST_STATE_RING)
488 tmp->pvt->send_digit = phone_digit;
489 tmp->pvt->call = phone_call;
490 tmp->pvt->hangup = phone_hangup;
491 tmp->pvt->answer = phone_answer;
492 tmp->pvt->read = phone_read;
493 tmp->pvt->write = phone_write;
494 strncpy(tmp->context, context, sizeof(tmp->context));
496 strncpy(tmp->exten, i->ext, sizeof(tmp->exten));
498 pthread_mutex_lock(&usecnt_lock);
500 pthread_mutex_unlock(&usecnt_lock);
501 ast_update_use_count();
502 if (state != AST_STATE_DOWN) {
503 if (state == AST_STATE_RING) {
504 ioctl(tmp->fd, PHONE_RINGBACK);
506 if (ast_pbx_start(tmp)) {
507 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
512 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
516 static void phone_mini_packet(struct phone_pvt *i)
520 /* Ignore stuff we read... */
521 res = read(i->fd, buf, sizeof(buf));
523 ast_log(LOG_WARNING, "Read returned %d\n", res);
528 static void phone_check_exception(struct phone_pvt *i)
531 char digit[2] = {0 , 0};
532 union telephony_exception phonee;
533 /* XXX Do something XXX */
535 ast_log(LOG_DEBUG, "Exception!\n");
537 phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
538 if (phonee.bits.dtmf_ready) {
539 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
540 if (i->mode == MODE_DIALTONE) {
541 ioctl(i->fd, PHONE_PLAY_STOP);
542 ioctl(i->fd, PHONE_REC_STOP);
543 ioctl(i->fd, PHONE_CPT_STOP);
545 if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
546 strcat(i->ext, digit);
547 if (ast_exists_extension(NULL, i->context, i->ext, 1)) {
548 /* It's a valid extension in its context, get moving! */
549 phone_new(i, AST_STATE_RING, i->context);
550 /* No need to restart monitor, we are the monitor */
552 pthread_mutex_lock(&usecnt_lock);
554 pthread_mutex_unlock(&usecnt_lock);
555 ast_update_use_count();
557 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1)) {
558 /* There is nothing in the specified extension that can match anymore.
560 if (ast_exists_extension(NULL, "default", i->ext, 1)) {
561 /* Check the default, too... */
562 phone_new(i, AST_STATE_RING, "default");
564 pthread_mutex_lock(&usecnt_lock);
566 pthread_mutex_unlock(&usecnt_lock);
567 ast_update_use_count();
569 /* XXX This should probably be justified better XXX */
570 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1)) {
571 /* It's not a valid extension, give a busy signal */
573 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
574 ioctl(i->fd, PHONE_BUSY);
578 ast_verbose("Extension is %s\n", i->ext);
582 if (phonee.bits.hookstate) {
583 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
585 if (i->mode == MODE_IMMEDIATE) {
586 phone_new(i, AST_STATE_RING, i->context);
587 } else if (i->mode == MODE_DIALTONE) {
588 pthread_mutex_lock(&usecnt_lock);
590 pthread_mutex_unlock(&usecnt_lock);
591 ast_update_use_count();
592 /* Reset the extension */
594 /* Play the dialtone */
596 ioctl(i->fd, PHONE_PLAY_STOP);
597 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
598 ioctl(i->fd, PHONE_PLAY_START);
602 pthread_mutex_lock(&usecnt_lock);
604 pthread_mutex_unlock(&usecnt_lock);
605 ast_update_use_count();
607 memset(i->ext, 0, sizeof(i->ext));
608 ioctl(i->fd, PHONE_CPT_STOP);
609 ioctl(i->fd, PHONE_PLAY_STOP);
610 ioctl(i->fd, PHONE_REC_STOP);
614 if (phonee.bits.pstn_ring)
615 ast_verbose("Unit is ringing\n");
616 if (phonee.bits.caller_id)
617 ast_verbose("We have caller ID\n");
622 static void *do_monitor(void *data)
628 /* The tone we're playing this round */
629 struct timeval tv = {0,0};
631 /* This thread monitors all the frame relay interfaces which are not yet in use
632 (and thus do not have a separate thread) indefinitely */
633 /* From here on out, we die whenever asked */
634 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
635 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
639 /* Don't let anybody kill us right away. Nobody should lock the interface list
640 and wait for the monitor list, but the other way around is okay. */
641 if (pthread_mutex_lock(&monlock)) {
642 ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
645 /* Lock the interface list */
646 if (pthread_mutex_lock(&iflock)) {
647 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
648 pthread_mutex_unlock(&monlock);
651 /* Build the stuff we're going to select on, that is the socket of every
652 phone_pvt that does not have an associated owner channel */
659 if (FD_ISSET(i->fd, &rfds))
660 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
662 /* This needs to be watched, as it lacks an owner */
663 FD_SET(i->fd, &rfds);
664 FD_SET(i->fd, &efds);
668 /* Remember we're going to have to come back and play
670 if (!tv.tv_usec && !tv.tv_sec) {
671 /* If we're due for a dialtone, play one */
672 if (write(i->fd, DialTone + tonepos, 240) != 240)
673 ast_log(LOG_WARNING, "Dial tone write error\n");
681 /* Okay, now that we know what to do, release the interface lock */
682 pthread_mutex_unlock(&iflock);
684 /* And from now on, we're okay to be killed, so release the monitor lock as well */
685 pthread_mutex_unlock(&monlock);
686 /* Wait indefinitely for something to happen */
688 /* If we're ready to recycle the time, set it to 30 ms */
690 if (tonepos >= sizeof(DialTone))
692 if (!tv.tv_usec && !tv.tv_sec) {
696 res = select(n + 1, &rfds, NULL, &efds, &tv);
698 res = select(n + 1, &rfds, NULL, &efds, NULL);
703 /* Okay, select has finished. Let's see what happened. */
705 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
708 /* If there are no fd's changed, just continue, it's probably time
709 to play some more dialtones */
712 /* Alright, lock the interface list again, and let's look and see what has
714 if (pthread_mutex_lock(&iflock)) {
715 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
720 if (FD_ISSET(i->fd, &rfds)) {
722 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
725 phone_mini_packet(i);
727 if (FD_ISSET(i->fd, &efds)) {
729 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
732 phone_check_exception(i);
736 pthread_mutex_unlock(&iflock);
743 static int restart_monitor()
745 /* If we're supposed to be stopped -- stay stopped */
746 if (monitor_thread == -2)
748 if (pthread_mutex_lock(&monlock)) {
749 ast_log(LOG_WARNING, "Unable to lock monitor\n");
752 if (monitor_thread == pthread_self()) {
753 pthread_mutex_unlock(&monlock);
754 ast_log(LOG_WARNING, "Cannot kill myself\n");
757 if (monitor_thread != -1) {
758 pthread_cancel(monitor_thread);
760 pthread_join(monitor_thread, NULL);
763 /* Start a new monitor */
764 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
765 pthread_mutex_unlock(&monlock);
766 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
769 pthread_mutex_unlock(&monlock);
773 static struct phone_pvt *mkif(char *iface, int mode)
775 /* Make a phone_pvt structure for this interface */
776 struct phone_pvt *tmp;
781 tmp = malloc(sizeof(struct phone_pvt));
783 tmp->fd = open(iface, O_RDWR);
785 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
789 ioctl(tmp->fd, PHONE_PLAY_STOP);
790 ioctl(tmp->fd, PHONE_REC_STOP);
791 ioctl(tmp->fd, PHONE_RING_STOP);
792 ioctl(tmp->fd, PHONE_CPT_STOP);
793 ioctl(tmp->fd, PHONE_REC_DEPTH, 4);
794 if (echocancel != AEC_OFF)
795 ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
796 if (silencesupression)
797 tmp->silencesupression = 1;
798 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
801 flags = fcntl(tmp->fd, F_GETFL);
802 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
805 tmp->lastformat = -1;
808 memset(tmp->ext, 0, sizeof(tmp->ext));
809 strncpy(tmp->dev, iface, sizeof(tmp->dev));
810 strncpy(tmp->context, context, sizeof(tmp->context));
818 static struct ast_channel *phone_request(char *type, int format, void *data)
822 struct ast_channel *tmp = NULL;
826 format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR);
828 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
831 /* Search for an unowned channel */
832 if (pthread_mutex_lock(&iflock)) {
833 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
838 if (!strcmp(name, p->dev + 5)) {
840 tmp = phone_new(p, AST_STATE_DOWN, p->context);
846 pthread_mutex_unlock(&iflock);
853 struct ast_config *cfg;
854 struct ast_variable *v;
855 struct phone_pvt *tmp;
856 int mode = MODE_IMMEDIATE;
857 cfg = ast_load(config);
859 /* We *must* have a config file otherwise stop immediately */
861 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
864 if (pthread_mutex_lock(&iflock)) {
865 /* It's a little silly to lock it, but we mind as well just to be sure */
866 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
869 v = ast_variable_browse(cfg, "interfaces");
871 /* Create the interface list */
872 if (!strcasecmp(v->name, "device")) {
873 tmp = mkif(v->value, mode);
879 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
881 pthread_mutex_unlock(&iflock);
885 } else if (!strcasecmp(v->name, "silencesupression")) {
886 silencesupression = ast_true(v->value);
887 } else if (!strcasecmp(v->name, "mode")) {
888 if (!strncasecmp(v->value, "di", 2))
889 mode = MODE_DIALTONE;
890 else if (!strncasecmp(v->value, "im", 2))
891 mode = MODE_IMMEDIATE;
893 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
894 } else if (!strcasecmp(v->name, "context")) {
895 strncpy(context, v->value, sizeof(context));
896 } else if (!strcasecmp(v->name, "format")) {
897 if (!strcasecmp(v->value, "g723.1")) {
898 prefformat = AST_FORMAT_G723_1;
899 } else if (!strcasecmp(v->value, "slinear")) {
900 prefformat = AST_FORMAT_SLINEAR;
902 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
903 } else if (!strcasecmp(v->name, "echocancel")) {
904 if (!strcasecmp(v->value, "off")) {
905 echocancel = AEC_OFF;
906 } else if (!strcasecmp(v->value, "low")) {
907 echocancel = AEC_LOW;
908 } else if (!strcasecmp(v->value, "medium")) {
909 echocancel = AEC_MED;
910 } else if (!strcasecmp(v->value, "high")) {
911 echocancel = AEC_HIGH;
913 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
917 pthread_mutex_unlock(&iflock);
918 /* Make sure we can register our Adtranphone channel type */
919 if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, phone_request)) {
920 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
926 /* And start the monitor for the first time */
935 struct phone_pvt *p, *pl;
936 /* First, take us out of the channel loop */
937 ast_channel_unregister(type);
938 if (!pthread_mutex_lock(&iflock)) {
939 /* Hangup all interfaces if they have an owner */
943 ast_softhangup(p->owner);
947 pthread_mutex_unlock(&iflock);
949 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
952 if (!pthread_mutex_lock(&monlock)) {
953 if (monitor_thread > -1) {
954 pthread_cancel(monitor_thread);
955 pthread_join(monitor_thread, NULL);
958 pthread_mutex_unlock(&monlock);
960 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
964 if (!pthread_mutex_lock(&iflock)) {
965 /* Destroy all the interfaces and free their memory */
968 /* Close the socket, assuming it's real */
973 /* Free associated memory */
977 pthread_mutex_unlock(&iflock);
979 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
989 pthread_mutex_lock(&usecnt_lock);
991 pthread_mutex_unlock(&usecnt_lock);