don't use '%i' at all, since we have no current use cases that need non base-10 parsi...
[asterisk/asterisk.git] / channels / chan_phone.c
index dc985c1..dd8aa65 100755 (executable)
@@ -3,7 +3,7 @@
  *
  * Generic Linux Telephony Interface driver
  * 
- * Copyright (C) 1999-2004, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
 
 #include <stdio.h>
 #include <string.h>
-#include <asterisk/lock.h>
-#include <asterisk/channel.h>
-#include <asterisk/channel_pvt.h>
-#include <asterisk/config.h>
-#include <asterisk/logger.h>
-#include <asterisk/module.h>
-#include <asterisk/pbx.h>
-#include <asterisk/options.h>
-#include <asterisk/utils.h>
-#include <asterisk/callerid.h>
+#include <ctype.h>
+#include "asterisk/lock.h"
+#include "asterisk/channel.h"
+#include "asterisk/config.h"
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/callerid.h"
+#include "asterisk/causes.h"
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <errno.h>
 #include <sys/ioctl.h>
 #include <linux/telephony.h>
 /* Still use some IXJ specific stuff */
+#include <linux/version.h>
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
+# include <linux/compiler.h>
+#endif
 #include <linux/ixjuser.h>
 #include "DialTone.h"
 
 #define IXJ_PHONE_RING_START(x)        ioctl(p->fd, PHONE_RING_START, &x);
 #endif
 
-
 #define DEFAULT_CALLER_ID "Unknown"
 #define PHONE_MAX_BUF 480
 #define DEFAULT_GAIN 0x100
 
-static char *desc = "Linux Telephony API Support";
-static char *type = "Phone";
-static char *tdesc = "Standard Linux Telephony API Driver";
-static char *config = "phone.conf";
+static const char desc[] = "Linux Telephony API Support";
+static const char type[] = "Phone";
+static const char tdesc[] = "Standard Linux Telephony API Driver";
+static const char config[] = "phone.conf";
 
 /* Default context for dialtone mode */
 static char context[AST_MAX_EXTENSION] = "default";
@@ -83,7 +87,7 @@ AST_MUTEX_DEFINE_STATIC(iflock);
 /* Protect the monitoring thread, so only one process can kill or start it, and not
    when it's doing something critical. */
 AST_MUTEX_DEFINE_STATIC(monlock);
-
+   
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
 static pthread_t monitor_thread = AST_PTHREADT_NULL;
@@ -96,7 +100,8 @@ static int restart_monitor(void);
 #define MODE_DIALTONE  1
 #define MODE_IMMEDIATE 2
 #define MODE_FXO               3
-   
+#define MODE_FXS        4
+
 static struct phone_pvt {
        int fd;                                                 /* Raw file descriptor for this device */
        struct ast_channel *owner;              /* Channel we belong to, possibly NULL */
@@ -126,11 +131,63 @@ static struct phone_pvt {
 static char cid_num[AST_MAX_EXTENSION];
 static char cid_name[AST_MAX_EXTENSION];
 
+static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
+static int phone_digit(struct ast_channel *ast, char digit);
+static int phone_call(struct ast_channel *ast, char *dest, int timeout);
+static int phone_hangup(struct ast_channel *ast);
+static int phone_answer(struct ast_channel *ast);
+static struct ast_frame *phone_read(struct ast_channel *ast);
+static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
+static struct ast_frame *phone_exception(struct ast_channel *ast);
+static int phone_send_text(struct ast_channel *ast, const char *text);
+static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
+
+static const struct ast_channel_tech phone_tech = {
+       .type = type,
+       .description = tdesc,
+       .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
+       .requester = phone_request,
+       .send_digit = phone_digit,
+       .call = phone_call,
+       .hangup = phone_hangup,
+       .answer = phone_answer,
+       .read = phone_read,
+       .write = phone_write,
+       .exception = phone_exception,
+       .fixup = phone_fixup
+};
+
+static struct ast_channel_tech phone_tech_fxs = {
+       .type = type,
+       .description = tdesc,
+       .requester = phone_request,
+       .send_digit = phone_digit,
+       .call = phone_call,
+       .hangup = phone_hangup,
+       .answer = phone_answer,
+       .read = phone_read,
+       .write = phone_write,
+       .exception = phone_exception,
+       .write_video = phone_write,
+       .send_text = phone_send_text,
+       .fixup = phone_fixup
+};
+
+static struct ast_channel_tech *cur_tech;
+
+static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
+{
+       struct phone_pvt *pvt = old->tech_pvt;
+       if (pvt && pvt->owner == old)
+               pvt->owner = new;
+       return 0;
+}
+
 static int phone_digit(struct ast_channel *ast, char digit)
 {
        struct phone_pvt *p;
        int outdigit;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        ast_log(LOG_NOTICE, "Dialed %c\n", digit);
        switch(digit) {
        case '0':
@@ -151,7 +208,7 @@ static int phone_digit(struct ast_channel *ast, char digit)
        case '#':
                outdigit = 12;
                break;
-       case 'f':       //flash
+       case 'f':       /*flash*/
        case 'F':
                ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
                usleep(320000);
@@ -162,7 +219,7 @@ static int phone_digit(struct ast_channel *ast, char digit)
                ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
                return -1;
        }
-       ast_log(LOG_NOTICE, "Dialed %i\n", outdigit);
+       ast_log(LOG_NOTICE, "Dialed %d\n", outdigit);
        ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
        p->lastformat = -1;
        return 0;
@@ -175,6 +232,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        PHONE_CID cid;
        time_t UtcTime;
        struct tm tm;
+       int start;
 
        time(&UtcTime);
        localtime_r(&UtcTime,&tm);
@@ -195,7 +253,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        if (ast->cid.cid_num) 
                strncpy(cid.number, ast->cid.cid_num, sizeof(cid.number) - 1);
 
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
 
        if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
                ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
@@ -204,8 +262,21 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        if (option_debug)
                ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
 
-       IXJ_PHONE_RING_START(cid);
-       ast_setstate(ast, AST_STATE_RINGING);
+       start = IXJ_PHONE_RING_START(cid);
+       if (start == -1)
+               return -1;
+       
+       if (p->mode == MODE_FXS) {
+               char *digit = strchr(dest, '/');
+               if (digit)
+               {
+                 digit++;
+                 while (*digit)
+                   phone_digit(ast, *digit++);
+               }
+       }
+       ast_setstate(ast, AST_STATE_RINGING);
        ast_queue_control(ast, AST_CONTROL_RINGING);
        return 0;
 }
@@ -213,10 +284,10 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 static int phone_hangup(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        if (option_debug)
                ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
-       if (!ast->pvt->pvt) {
+       if (!ast->tech_pvt) {
                ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
                return 0;
        }
@@ -250,7 +321,7 @@ static int phone_hangup(struct ast_channel *ast)
        p->obuflen = 0;
        p->dialtone = 0;
        memset(p->ext, 0, sizeof(p->ext));
-       ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
+       ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
        ast_mutex_lock(&usecnt_lock);
        usecnt--;
        if (usecnt < 0) 
@@ -259,7 +330,7 @@ static int phone_hangup(struct ast_channel *ast)
        ast_update_use_count();
        if (option_verbose > 2) 
                ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
-       ast->pvt->pvt = NULL;
+       ast->tech_pvt = NULL;
        ast_setstate(ast, AST_STATE_DOWN);
        restart_monitor();
        return 0;
@@ -268,10 +339,10 @@ static int phone_hangup(struct ast_channel *ast)
 static int phone_setup(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        ioctl(p->fd, PHONE_CPT_STOP);
        /* Nothing to answering really, just start recording */
-       if (ast->pvt->rawreadformat == AST_FORMAT_G723_1) {
+       if (ast->rawreadformat == AST_FORMAT_G723_1) {
                /* Prefer g723 */
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_G723_1) {
@@ -281,7 +352,7 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
-       } else if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
+       } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_SLINEAR) {
                        p->lastinput = AST_FORMAT_SLINEAR;
@@ -290,7 +361,7 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
-       } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
+       } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_ULAW) {
                        p->lastinput = AST_FORMAT_ULAW;
@@ -299,15 +370,25 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
+       } else if (p->mode == MODE_FXS) {
+               ioctl(p->fd, PHONE_REC_STOP);
+               if (p->lastinput != ast->rawreadformat) {
+                       p->lastinput = ast->rawreadformat;
+                       if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
+                               ast_log(LOG_WARNING, "Failed to set codec to %d\n", 
+                                       ast->rawreadformat);
+                               return -1;
+                       }
+               }
        } else {
-               ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->pvt->rawreadformat));
+               ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
                return -1;
        }
        if (ioctl(p->fd, PHONE_REC_START)) {
                ast_log(LOG_WARNING, "Failed to start recording\n");
                return -1;
        }
-       //set the DTMF times (the default is too short)
+       /* set the DTMF times (the default is too short) */
        ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
        ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
        return 0;
@@ -316,7 +397,7 @@ static int phone_setup(struct ast_channel *ast)
 static int phone_answer(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        /* In case it's a LineJack, take it off hook */
        if (p->mode == MODE_FXO) {
                if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK)) 
@@ -350,7 +431,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
 {
        int res;
        union telephony_exception phonee;
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        char digit;
 
        /* Some nice norms */
@@ -362,7 +443,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
        p->fr.mallocd=0;
        p->fr.delivery.tv_sec = 0;
        p->fr.delivery.tv_usec = 0;
-
+       
        phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
        if (phonee.bits.dtmf_ready)  {
                if (option_debug)
@@ -413,7 +494,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
 static struct ast_frame  *phone_read(struct ast_channel *ast)
 {
        int res;
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        
 
        /* Some nice norms */
@@ -429,7 +510,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
        /* Try to read some data... */
        CHECK_BLOCKING(ast);
        res = read(p->fd, p->buf, PHONE_MAX_BUF);
-       ast->blocking = 0;
+       ast_clear_flag(ast, AST_FLAG_BLOCKING);
        if (res < 0) {
 #if 0
                if (errno == EAGAIN) {
@@ -443,6 +524,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
                return NULL;
        }
        p->fr.data = p->buf;
+       if (p->mode != MODE_FXS)
        switch(p->buf[0] & 0x3) {
        case '0':
        case '1':
@@ -456,13 +538,19 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
        }
        p->fr.samples = 240;
        p->fr.datalen = res;
-       p->fr.frametype = AST_FRAME_VOICE;
+       p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
+                          AST_FRAME_VOICE : 
+                         p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE 
+                         : AST_FRAME_VIDEO;
        p->fr.subclass = p->lastinput;
        p->fr.offset = AST_FRIENDLY_OFFSET;
+       /* Byteswap from little-endian to native-endian */
+       if (p->fr.subclass == AST_FORMAT_SLINEAR)
+               ast_frame_byteswap_le(&p->fr);
        return &p->fr;
 }
 
-static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
+static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
 {
        int res;
        /* Store as much of the buffer as we can, then write fixed frames */
@@ -470,7 +558,10 @@ static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
        /* Make sure we have enough buffer space to store the frame */
        if (space < len)
                len = space;
-       memcpy(p->obuf + p->obuflen, buf, len);
+       if (swap)
+               ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
+       else
+               memcpy(p->obuf + p->obuflen, buf, len);
        p->obuflen += len;
        while(p->obuflen > frlen) {
                res = write(p->fd, p->obuf, frlen);
@@ -493,9 +584,16 @@ static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
        return len;
 }
 
+static int phone_send_text(struct ast_channel *ast, const char *text)
+{
+    int length = strlen(text);
+    return phone_write_buf(ast->tech_pvt, text, length, length, 0) == 
+           length ? 0 : -1;
+}
+
 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
 {
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        int res;
        int maxfr=0;
        char *pos;
@@ -504,13 +602,14 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
        int codecset = 0;
        char tmpbuf[4];
        /* Write a frame of (presumably voice) data */
-       if (frame->frametype != AST_FRAME_VOICE) {
+       if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
                if (frame->frametype != AST_FRAME_IMAGE)
                        ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
                return 0;
        }
        if (!(frame->subclass &
-               (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW))) {
+               (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) && 
+           p->mode != MODE_FXS) {
                ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
                return -1;
        }
@@ -587,8 +686,29 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                        p->obuflen = 0;
                }
                maxfr = 240;
+       } else {
+               if (p->lastformat != frame->subclass) {
+                       ioctl(p->fd, PHONE_PLAY_STOP);
+                       ioctl(p->fd, PHONE_REC_STOP);
+                       if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
+                               ast_log(LOG_WARNING, "Unable to set %d mode\n",
+                                       frame->subclass);
+                               return -1;
+                       }
+                       if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
+                               ast_log(LOG_WARNING, "Unable to set %d mode\n",
+                                       frame->subclass);
+                               return -1;
+                       }
+                       p->lastformat = frame->subclass;
+                       p->lastinput = frame->subclass;
+                       codecset = 1;
+                       /* Reset output buffer */
+                       p->obuflen = 0;
+               }
+               maxfr = 480;
        }
-       if (codecset) {
+       if (codecset) {
                ioctl(p->fd, PHONE_REC_DEPTH, 3);
                ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
                if (ioctl(p->fd, PHONE_PLAY_START)) {
@@ -600,7 +720,7 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                        return -1;
                }
        }
-       /* If we get here, we have a voice frame of Appropriate data */
+       /* If we get here, we have a frame of Appropriate data */
        sofar = 0;
        pos = frame->data;
        while(sofar < frame->datalen) {
@@ -615,12 +735,17 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                                memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
                                memcpy(tmpbuf, frame->data, 4);
                                expected = 24;
-                               res = phone_write_buf(p, tmpbuf, expected, maxfr);
+                               res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
                        }
                        res = 4;
                        expected=4;
                } else {
-                       res = phone_write_buf(p, pos, expected, maxfr);
+                       int swap = 0;
+#if __BYTE_ORDER == __BIG_ENDIAN
+                       if (frame->subclass == AST_FORMAT_SLINEAR)
+                               swap = 1; /* Swap big-endian samples to little-endian as we copy */
+#endif
+                       res = phone_write_buf(p, pos, expected, maxfr, swap);
                }
                if (res != expected) {
                        if ((errno != EAGAIN) && (errno != EINTR)) {
@@ -647,26 +772,37 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
 {
        struct ast_channel *tmp;
+       struct phone_codec_data codec;
        tmp = ast_channel_alloc(1);
        if (tmp) {
+               tmp->tech = cur_tech;
                snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
                tmp->type = type;
                tmp->fds[0] = i->fd;
                /* XXX Switching formats silently causes kernel panics XXX */
-               tmp->nativeformats = prefformat;
-               tmp->pvt->rawreadformat = prefformat;
-               tmp->pvt->rawwriteformat = prefformat;
+               if (i->mode == MODE_FXS &&
+                   ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
+                       if (codec.type == LINEAR16)
+                               tmp->nativeformats =
+                               tmp->rawreadformat =
+                               tmp->rawwriteformat =
+                               AST_FORMAT_SLINEAR;
+                       else {
+                               tmp->nativeformats =
+                               tmp->rawreadformat =
+                               tmp->rawwriteformat =
+                               prefformat & ~AST_FORMAT_SLINEAR;
+                       }
+               }
+               else {
+                       tmp->nativeformats = prefformat;
+                       tmp->rawreadformat = prefformat;
+                       tmp->rawwriteformat = prefformat;
+               }
                ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
-               tmp->pvt->pvt = i;
-               tmp->pvt->send_digit = phone_digit;
-               tmp->pvt->call = phone_call;
-               tmp->pvt->hangup = phone_hangup;
-               tmp->pvt->answer = phone_answer;
-               tmp->pvt->read = phone_read;
-               tmp->pvt->write = phone_write;
-               tmp->pvt->exception = phone_exception;
+               tmp->tech_pvt = i;
                strncpy(tmp->context, context, sizeof(tmp->context)-1);
                if (strlen(i->ext))
                        strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
@@ -722,35 +858,26 @@ static void phone_check_exception(struct phone_pvt *i)
        phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
        if (phonee.bits.dtmf_ready)  {
                digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
-               if (i->mode == MODE_DIALTONE) {
+               if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS) {
                        ioctl(i->fd, PHONE_PLAY_STOP);
                        ioctl(i->fd, PHONE_REC_STOP);
                        ioctl(i->fd, PHONE_CPT_STOP);
                        i->dialtone = 0;
                        if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
                                strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
-                       if (ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
+                       if ((i->mode != MODE_FXS ||
+                            !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
+                            !phonee.bits.dtmf_ready) &&
+                           ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
                                /* It's a valid extension in its context, get moving! */
                                phone_new(i, AST_STATE_RING, i->context);
                                /* No need to restart monitor, we are the monitor */
-                               if (i->owner) {
-                                       ast_mutex_lock(&usecnt_lock);
-                                       usecnt--;
-                                       ast_mutex_unlock(&usecnt_lock);
-                                       ast_update_use_count();
-                               }
                        } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
                                /* There is nothing in the specified extension that can match anymore.
                                   Try the default */
                                if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
                                        /* Check the default, too... */
                                        phone_new(i, AST_STATE_RING, "default");
-                                       if (i->owner) {
-                                               ast_mutex_lock(&usecnt_lock);
-                                               usecnt--;
-                                               ast_mutex_unlock(&usecnt_lock);
-                                               ast_update_use_count();
-                                       }
                                        /* XXX This should probably be justified better XXX */
                                }  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
                                        /* It's not a valid extension, give a busy signal */
@@ -874,9 +1001,10 @@ static void *do_monitor(void *data)
                }
                /* Okay, now that we know what to do, release the interface lock */
                ast_mutex_unlock(&iflock);
-               
+
                /* And from now on, we're okay to be killed, so release the monitor lock as well */
                ast_mutex_unlock(&monlock);
+
                /* Wait indefinitely for something to happen */
                if (dotone) {
                        /* If we're ready to recycle the time, set it to 30 ms */
@@ -909,23 +1037,21 @@ static void *do_monitor(void *data)
                        ast_log(LOG_WARNING, "Unable to lock the interface list\n");
                        continue;
                }
+
                i = iflist;
-               while(i) {
+               for(; i; i=i->next) {
                        if (FD_ISSET(i->fd, &rfds)) {
                                if (i->owner) {
-                                       ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
                                        continue;
                                }
                                phone_mini_packet(i);
                        }
                        if (FD_ISSET(i->fd, &efds)) {
                                if (i->owner) {
-                                       ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d, %s)...\n", i->fd, i->dev);
                                        continue;
                                }
                                phone_check_exception(i);
                        }
-                       i=i->next;
                }
                ast_mutex_unlock(&iflock);
        }
@@ -949,10 +1075,16 @@ static int restart_monitor()
                return -1;
        }
        if (monitor_thread != AST_PTHREADT_NULL) {
+               if (ast_mutex_lock(&iflock)) {
+                 ast_mutex_unlock(&monlock);
+                 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
+                 return -1;
+               }
                pthread_cancel(monitor_thread);
 #if 0
                pthread_join(monitor_thread, NULL);
 #endif
+               ast_mutex_unlock(&iflock);
        }
        /* Start a new monitor */
        if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
@@ -983,7 +1115,8 @@ static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
                                ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
                } else {
                        if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS)) 
-                               ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
+                                if (mode != MODE_FXS)
+                                     ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
                }
                ioctl(tmp->fd, PHONE_PLAY_STOP);
                ioctl(tmp->fd, PHONE_REC_STOP);
@@ -1023,19 +1156,13 @@ static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
        return tmp;
 }
 
-static struct ast_channel *phone_request(char *type, int format, void *data)
+static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause)
 {
        int oldformat;
        struct phone_pvt *p;
        struct ast_channel *tmp = NULL;
        char *name = data;
-       
-       oldformat = format;
-       format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
-       if (!format) {
-               ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
-               return NULL;
-       }
+
        /* Search for an unowned channel */
        if (ast_mutex_lock(&iflock)) {
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
@@ -1043,16 +1170,30 @@ static struct ast_channel *phone_request(char *type, int format, void *data)
        }
        p = iflist;
        while(p) {
-               if (!strcmp(name, p->dev + 5)) {
-                       if (!p->owner) {
-                               tmp = phone_new(p, AST_STATE_DOWN, p->context);
-                               break;
-                       }
+               if (p->mode == MODE_FXS ||
+                   format & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) {
+                   size_t length = strlen(p->dev + 5);
+               if (strncmp(name, p->dev + 5, length) == 0 &&
+                   !isalnum(name[length])) {
+                   if (!p->owner) {
+                     tmp = phone_new(p, AST_STATE_DOWN, p->context);
+                     break;
+                } else
+                     *cause = AST_CAUSE_BUSY;
+            }
                }
                p = p->next;
        }
        ast_mutex_unlock(&iflock);
        restart_monitor();
+       if (tmp == NULL) {
+               oldformat = format;
+               format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
+               if (!format) {
+                       ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
+                       return NULL;
+               }
+       }
        return tmp;
 }
 
@@ -1083,7 +1224,7 @@ static int __unload_module(void)
 {
        struct phone_pvt *p, *pl;
        /* First, take us out of the channel loop */
-       ast_channel_unregister(type);
+       ast_channel_unregister(cur_tech);
        if (!ast_mutex_lock(&iflock)) {
                /* Hangup all interfaces if they have an owner */
                p = iflist;
@@ -1144,7 +1285,7 @@ int load_module()
        struct phone_pvt *tmp;
        int mode = MODE_IMMEDIATE;
        int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
-       cfg = ast_load(config);
+       cfg = ast_config_load(config);
 
        /* We *must* have a config file otherwise stop immediately */
        if (!cfg) {
@@ -1167,7 +1308,7 @@ int load_module()
                                        
                                } else {
                                        ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
-                                       ast_destroy(cfg);
+                                       ast_config_destroy(cfg);
                                        ast_mutex_unlock(&iflock);
                                        __unload_module();
                                        return -1;
@@ -1183,6 +1324,10 @@ int load_module()
                                mode = MODE_DIALTONE;
                        else if (!strncasecmp(v->value, "im", 2))
                                mode = MODE_IMMEDIATE;
+                       else if (!strncasecmp(v->value, "fxs", 3)) {
+                               mode = MODE_FXS;
+                               prefformat = 0x01ff0000; /* All non-voice */
+                       }
                        else if (!strncasecmp(v->value, "fx", 2))
                                mode = MODE_FXO;
                        else
@@ -1193,7 +1338,9 @@ int load_module()
                        if (!strcasecmp(v->value, "g723.1")) {
                                prefformat = AST_FORMAT_G723_1;
                        } else if (!strcasecmp(v->value, "slinear")) {
-                               prefformat = AST_FORMAT_SLINEAR;
+                               if (mode == MODE_FXS)
+                                   prefformat |= AST_FORMAT_SLINEAR;
+                               else prefformat = AST_FORMAT_SLINEAR;
                        } else if (!strcasecmp(v->value, "ulaw")) {
                                prefformat = AST_FORMAT_ULAW;
                        } else
@@ -1217,15 +1364,22 @@ int load_module()
                v = v->next;
        }
        ast_mutex_unlock(&iflock);
+
+       if (mode == MODE_FXS) {
+               phone_tech_fxs.capabilities = prefformat;
+               cur_tech = &phone_tech_fxs;
+       } else
+               cur_tech = (struct ast_channel_tech *) &phone_tech;
+
        /* Make sure we can register our Adtranphone channel type */
-       if (ast_channel_register(type, tdesc, 
-                        AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, phone_request)) {
+
+       if (ast_channel_register(cur_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
-               ast_destroy(cfg);
+               ast_config_destroy(cfg);
                __unload_module();
                return -1;
        }
-       ast_destroy(cfg);
+       ast_config_destroy(cfg);
        /* And start the monitor for the first time */
        restart_monitor();
        return 0;
@@ -1242,7 +1396,7 @@ int usecount()
 
 char *description()
 {
-       return desc;
+       return (char *) desc;
 }
 
 char *key()