Restoring some code to sig_pri. Not sure if it is really needed.
[asterisk/asterisk.git] / channels / sig_pri.c
index e0d4097..6d9bf66 100644 (file)
@@ -17,7 +17,7 @@
  */
 
 /*! \file
- * 
+ *
  * \brief PRI signaling module
  *
  * \author Matthew Fredrickson <creslin@digium.com>
 
 #include "sig_pri.h"
 
+/* define this to send PRI user-user information elements */
+#undef SUPPORT_USERUSER
+
+#if 0
+#define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
+#else
+#define DEFAULT_PRI_DEBUG 0
+#endif
+
 static int pri_matchdigittimeout = 3000;
 
 static int pri_gendigittimeout = 8000;
@@ -62,7 +71,7 @@ static int pri_gendigittimeout = 8000;
 
 #define DCHAN_AVAILABLE        (DCHAN_NOTINALARM | DCHAN_UP)
 
-#define PRI_DEADLOCK_AVOIDANCE(lock) \
+#define PRI_DEADLOCK_AVOIDANCE(p) \
        do { \
                sig_pri_unlock_private(p); \
                usleep(1); \
@@ -94,7 +103,66 @@ static void sig_pri_handle_dchan_exception(struct sig_pri_pri *pri, int index)
 static void sig_pri_set_dialing(struct sig_pri_chan *p, int flag)
 {
        if (p->calls->set_dialing)
-               p->calls->set_dialing(p, flag);
+               p->calls->set_dialing(p->chan_pvt, flag);
+}
+
+/*!
+ * \internal
+ * \brief Set the caller id information in the parent module.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_caller_id(struct sig_pri_chan *p)
+{
+       struct ast_party_caller caller;
+
+       if (p->calls->set_callerid) {
+               ast_party_caller_init(&caller);
+               caller.id.number = p->cid_num;
+               caller.id.name = p->cid_name;
+               caller.id.number_type = p->cid_ton;
+               caller.id.number_presentation = p->callingpres;
+               caller.ani = p->cid_ani;
+               caller.ani2 = p->cid_ani2;
+               p->calls->set_callerid(p->chan_pvt, &caller);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Set the Dialed Number Identifier.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ * \param dnid Dialed Number Identifier string.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
+{
+       if (p->calls->set_dnid) {
+               p->calls->set_dnid(p->chan_pvt, dnid);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Set the Redirecting Directory Number Information Service (RDNIS).
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
+{
+       if (p->calls->set_rdnis) {
+               p->calls->set_rdnis(p->chan_pvt, rdnis);
+       }
 }
 
 static void sig_pri_unlock_private(struct sig_pri_chan *p)
@@ -124,6 +192,22 @@ static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_pri *pri)
        return 0;
 }
 
+/*!
+ * \internal
+ * \brief Reset DTMF detector.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ *
+ * \return Nothing
+ */
+static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
+{
+       if (p->calls->dsp_reset_and_flush_digits) {
+               p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
+       }
+}
+
 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
 {
        if (p->calls->set_echocanceller)
@@ -193,7 +277,7 @@ static char *pri_order(int level)
                return "Quaternary";
        default:
                return "<Unknown>";
-       }               
+       }
 }
 
 /* Returns index of the active dchan */
@@ -311,7 +395,7 @@ static int pri_find_principle(struct sig_pri_pri *pri, int channel)
                        break;
                }
        }
-       
+
        return principle;
 }
 
@@ -323,9 +407,9 @@ static int pri_fixup_principle(struct sig_pri_pri *pri, int principle, q931_call
                        return -1;
                return principle;
        }
-       if ((principle > -1) && 
-               (principle < pri->numchans) && 
-               (pri->pvts[principle]) && 
+       if ((principle > -1) &&
+               (principle < pri->numchans) &&
+               (pri->pvts[principle]) &&
                (pri->pvts[principle]->call == c))
                return principle;
        /* First, check for other bearers */
@@ -338,7 +422,7 @@ static int pri_fixup_principle(struct sig_pri_pri *pri, int principle, q931_call
                                struct sig_pri_chan *new = pri->pvts[principle], *old = pri->pvts[x];
 
                                ast_verb(3, "Moving call from channel %d to channel %d\n",
-                                               old->channel, new->channel);
+                                       old->channel, new->channel);
                                if (new->owner) {
                                        ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
                                                old->channel, new->channel, new->channel);
@@ -464,7 +548,7 @@ static int pri_find_empty_chan(struct sig_pri_pri *pri, int backwards)
                if (!backwards && (x >= pri->numchans))
                        break;
                if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
-                       ast_debug(1, "Found empty available channel %d/%d\n", 
+                       ast_debug(1, "Found empty available channel %d/%d\n",
                                pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
                        return x;
                }
@@ -533,10 +617,16 @@ static void *pri_ss_thread(void *data)
        int len;
        int timeout;
 
-       /* in the bizarre case where the channel has become a zombie before we
-          even get started here, abort safely
-       */
-       if (!p) {
+       if (!chan) {
+               /* We lost the owner before we could get started. */
+               return NULL;
+       }
+
+       /*
+        * In the bizarre case where the channel has become a zombie before we
+        * even get started here, abort safely.
+        */
+       if (!chan->tech_pvt) {
                ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
                ast_hangup(chan);
                return NULL;
@@ -544,6 +634,8 @@ static void *pri_ss_thread(void *data)
 
        ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
 
+       sig_pri_dsp_reset_and_flush_digits(p);
+
        /* Now loop looking for an extension */
        ast_copy_string(exten, p->exten, sizeof(exten));
        len = strlen(exten);
@@ -578,6 +670,7 @@ static void *pri_ss_thread(void *data)
        if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
                /* Start the real PBX */
                ast_copy_string(chan->exten, exten, sizeof(chan->exten));
+               sig_pri_dsp_reset_and_flush_digits(p);
                sig_pri_set_echocanceller(p, 1);
                ast_setstate(chan, AST_STATE_RING);
                res = ast_pbx_run(chan);
@@ -597,7 +690,7 @@ static void *pri_ss_thread(void *data)
 exit:
        res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
        if (res < 0)
-                       ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
+               ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
        ast_hangup(chan);
        return NULL;
 }
@@ -616,6 +709,39 @@ void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
                pri_restart(pri->dchans[index]);
 }
 
+#if defined(SUPPORT_USERUSER)
+/*!
+ * \internal
+ * \brief Obtain the sig_pri owner channel lock if the owner exists.
+ * \since 1.6.3
+ *
+ * \param pri sig_pri PRI control structure.
+ * \param chanpos Channel position in the span.
+ *
+ * \note Assumes the pri->lock is already obtained.
+ * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
+ *
+ * \return Nothing
+ */
+static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
+{
+       for (;;) {
+               if (!pri->pvts[chanpos]->owner) {
+                       /* There is no owner lock to get. */
+                       break;
+               }
+               if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
+                       /* We got the lock */
+                       break;
+               }
+               /* We must unlock the PRI to avoid the possibility of a deadlock */
+               ast_mutex_unlock(&pri->lock);
+               PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
+               ast_mutex_lock(&pri->lock);
+       }
+}
+#endif /* defined(SUPPORT_USERUSER) */
+
 static void *pri_dchannel(void *vpri)
 {
        struct sig_pri_pri *pri = vpri;
@@ -632,10 +758,9 @@ static void *pri_dchannel(void *vpri)
        int i, which=-1;
        int numdchans;
        pthread_t threadid;
-       pthread_attr_t attr;
        char ani2str[6];
-       char plancallingnum[256];
-       char plancallingani[256];
+       char plancallingnum[AST_MAX_EXTENSION];
+       char plancallingani[AST_MAX_EXTENSION];
        char calledtonstr[10];
        struct timeval lastidle = { 0, 0 };
        pthread_t p;
@@ -644,7 +769,7 @@ static void *pri_dchannel(void *vpri)
        int nextidle = -1;
        int haveidles;
        int activeidles;
-       
+
        gettimeofday(&lastidle, NULL);
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
 
@@ -693,8 +818,8 @@ static void *pri_dchannel(void *vpri)
                        haveidles = 0;
                        activeidles = 0;
                        for (x = pri->numchans; x >= 0; x--) {
-                               if (pri->pvts[x] && !pri->pvts[x]->owner && 
-                                   !pri->pvts[x]->call) {
+                               if (pri->pvts[x] && !pri->pvts[x]->owner &&
+                                       !pri->pvts[x]->call) {
                                        if (haveidles < pri->minunused) {
                                                haveidles++;
                                        } else if (!pri->pvts[x]->resetting) {
@@ -720,8 +845,8 @@ static void *pri_dchannel(void *vpri)
                                        gettimeofday(&lastidle, NULL);
                                }
                        } else if ((haveidles < pri->minunused) &&
-                                  (activeidles > pri->minidle)) {
-                               /* Mark something for hangup if there is something 
+                               (activeidles > pri->minidle)) {
+                               /* Mark something for hangup if there is something
                                   that can be hungup */
                                for (x = pri->numchans; x >= 0; x--) {
                                        /* find a candidate channel */
@@ -731,9 +856,9 @@ static void *pri_dchannel(void *vpri)
                                                /* Stop if we have enough idle channels or
                                                  can't spare any more active idle ones */
                                                if ((haveidles >= pri->minunused) ||
-                                                   (activeidles <= pri->minidle))
+                                                       (activeidles <= pri->minidle))
                                                        break;
-                                       } 
+                                       }
                                }
                        }
                }
@@ -810,12 +935,12 @@ static void *pri_dchannel(void *vpri)
 
                        if (e->e != PRI_EVENT_DCHAN_DOWN) {
                                if (!(pri->dchanavail[which] & DCHAN_UP)) {
-                                               ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
+                                       ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
                                }
                                pri->dchanavail[which] |= DCHAN_UP;
                        } else {
                                if (pri->dchanavail[which] & DCHAN_UP) {
-                                               ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
+                                       ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
                                }
                                pri->dchanavail[which] &= ~DCHAN_UP;
                        }
@@ -877,13 +1002,13 @@ static void *pri_dchannel(void *vpri)
                                if (e->restart.channel > -1) {
                                        chanpos = pri_find_principle(pri, e->restart.channel);
                                        if (chanpos < 0)
-                                               ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n", 
+                                               ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
                                                        PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
                                        else {
 #ifdef HAVE_PRI_SERVICE_MESSAGES
                                                char db_chan_name[20], db_answer[5], state;
                                                int why, skipit = 0;
-                                               
+
                                                ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
                                                snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, pri->pvts[chanpos]->channel);
                                                ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
@@ -900,8 +1025,8 @@ static void *pri_dchannel(void *vpri)
                                                }
                                                if (!skipit) {
 #endif
-                                                       ast_verb(3, "B-channel %d/%d restarted on span %d\n", 
-                                                                       PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
+                                                       ast_verb(3, "B-channel %d/%d restarted on span %d\n",
+                                                               PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
                                                        sig_pri_lock_private(pri->pvts[chanpos]);
                                                        if (pri->pvts[chanpos]->call) {
                                                                pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
@@ -933,52 +1058,52 @@ static void *pri_dchannel(void *vpri)
                        case PRI_EVENT_KEYPAD_DIGIT:
                                chanpos = pri_find_principle(pri, e->digit.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
                                        if (chanpos > -1) {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
                                                /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
-                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
+                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && pri->pvts[chanpos]->call == e->digit.call
+                                                       && pri->pvts[chanpos]->owner) {
                                                        /* how to do that */
                                                        int digitlen = strlen(e->digit.digits);
-                                                       char digit;
-                                                       int i;                                  
-                                                       for (i = 0; i < digitlen; i++) {        
-                                                               digit = e->digit.digits[i];
-                                                               {
-                                                                       struct ast_frame f = { AST_FRAME_DTMF, digit, };
-                                                                       pri_queue_frame(pri->pvts[chanpos], &f, pri);
-                                                               }
+                                                       int i;
+
+                                                       for (i = 0; i < digitlen; i++) {
+                                                               struct ast_frame f = { AST_FRAME_DTMF, e->digit.digits[i], };
+
+                                                               pri_queue_frame(pri->pvts[chanpos], &f, pri);
                                                        }
                                                }
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                        }
                                }
                                break;
-                               
+
                        case PRI_EVENT_INFO_RECEIVED:
                                chanpos = pri_find_principle(pri, e->ring.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
                                        if (chanpos > -1) {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
                                                /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
-                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
+                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && pri->pvts[chanpos]->call == e->ring.call
+                                                       && pri->pvts[chanpos]->owner) {
                                                        /* how to do that */
                                                        int digitlen = strlen(e->ring.callednum);
-                                                       char digit;
-                                                       int i;                                  
-                                                       for (i = 0; i < digitlen; i++) {        
-                                                               digit = e->ring.callednum[i];
-                                                               {
-                                                                       struct ast_frame f = { AST_FRAME_DTMF, digit, };
-                                                                       pri_queue_frame(pri->pvts[chanpos], &f, pri);
-                                                               }
+                                                       int i;
+
+                                                       for (i = 0; i < digitlen; i++) {
+                                                               struct ast_frame f = { AST_FRAME_DTMF, e->ring.callednum[i], };
+
+                                                               pri_queue_frame(pri->pvts[chanpos], &f, pri);
                                                        }
                                                }
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
@@ -998,7 +1123,7 @@ static void *pri_dchannel(void *vpri)
                                        ast_mutex_lock(&pri->pvts[chanpos]->service_lock);
                                        ch = pri->pvts[chanpos]->channel;
                                        ast_mutex_unlock(&pri->pvts[chanpos]->service_lock);
-                                       
+
                                        snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->pvts[chanpos]->pri->span, ch);
                                        if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
                                                sscanf(db_answer, "%c:%d", &state, &why);
@@ -1048,18 +1173,19 @@ static void *pri_dchannel(void *vpri)
                                        chanpos = pri_find_principle(pri, e->ring.channel);
                                /* if no channel specified find one empty */
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
                                } else {
                                        sig_pri_lock_private(pri->pvts[chanpos]);
                                        if (pri->pvts[chanpos]->owner) {
                                                if (pri->pvts[chanpos]->call == e->ring.call) {
-                                                       ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n", 
+                                                       ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
                                                                PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
+                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
                                                        break;
                                                } else {
                                                        /* This is where we handle initial glare */
-                                                       ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d.  Attempting to renegotiating channel.\n", 
+                                                       ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d.  Attempting to renegotiating channel.\n",
                                                        PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
                                                        sig_pri_unlock_private(pri->pvts[chanpos]);
                                                        chanpos = -1;
@@ -1073,7 +1199,15 @@ static void *pri_dchannel(void *vpri)
                                if (chanpos > -1) {
                                        sig_pri_lock_private(pri->pvts[chanpos]);
                                        pri->pvts[chanpos]->call = e->ring.call;
+
+                                       /* Use plancallingnum as a scratch buffer since it is initialized next. */
+                                       apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
+                                               e->ring.redirectingnum, e->ring.callingplanrdnis);
+                                       sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
+
+                                       /* Setup caller-id info */
                                        apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
+                                       pri->pvts[chanpos]->cid_ani2 = 0;
                                        if (pri->pvts[chanpos]->use_callerid) {
                                                ast_shrink_phone_number(plancallingnum);
                                                ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
@@ -1088,14 +1222,22 @@ static void *pri_dchannel(void *vpri)
 #endif
                                                ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
                                                pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
+                                               pri->pvts[chanpos]->callingpres = e->ring.callingpres;
+                                               if (e->ring.ani2 >= 0) {
+                                                       pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
+                                               }
                                        } else {
                                                pri->pvts[chanpos]->cid_num[0] = '\0';
                                                pri->pvts[chanpos]->cid_ani[0] = '\0';
                                                pri->pvts[chanpos]->cid_name[0] = '\0';
                                                pri->pvts[chanpos]->cid_ton = 0;
+                                               pri->pvts[chanpos]->callingpres = 0;
                                        }
-                                       apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
-                                                            e->ring.redirectingnum, e->ring.callingplanrdnis);
+                                       sig_pri_set_caller_id(pri->pvts[chanpos]);
+
+                                       /* Set DNID on all incoming calls -- even immediate */
+                                       sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
+
                                        /* If immediate=yes go to s|1 */
                                        if (pri->pvts[chanpos]->immediate) {
                                                ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
@@ -1105,7 +1247,6 @@ static void *pri_dchannel(void *vpri)
                                        /* Get called number */
                                        else if (!ast_strlen_zero(e->ring.callednum)) {
                                                ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
-                                               ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
                                        } else if (pri->overlapdial)
                                                pri->pvts[chanpos]->exten[0] = '\0';
                                        else {
@@ -1113,15 +1254,13 @@ static void *pri_dchannel(void *vpri)
                                                pri->pvts[chanpos]->exten[0] = 's';
                                                pri->pvts[chanpos]->exten[1] = '\0';
                                        }
-                                       /* Set DNID on all incoming calls -- even immediate */
-                                       if (!ast_strlen_zero(e->ring.callednum))
-                                               ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
                                        /* No number yet, but received "sending complete"? */
                                        if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
                                                ast_verb(3, "Going to extension s|1 because of Complete received\n");
                                                pri->pvts[chanpos]->exten[0] = 's';
                                                pri->pvts[chanpos]->exten[1] = '\0';
                                        }
+
                                        /* Make sure extension exists (or in overlap dial mode, can exist) */
                                        if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
                                                ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
@@ -1131,57 +1270,56 @@ static void *pri_dchannel(void *vpri)
                                                        pri->pvts[chanpos]->proceeding = 1;
                                                        pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
                                                } else {
-                                                       if (pri->switchtype != PRI_SWITCH_GR303_TMC) 
+                                                       if (pri->switchtype != PRI_SWITCH_GR303_TMC)
                                                                pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
                                                        else
                                                                pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
                                                }
-                                               /* Get the use_callingpres state */
-                                               pri->pvts[chanpos]->callingpres = e->ring.callingpres;
-                                       
+
                                                /* Start PBX */
-                                               if (!e->ring.complete && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
-                                                       /* Release the PRI lock while we create the channel */
+                                               if (!e->ring.complete
+                                                       && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
+                                                       /*
+                                                        * Release the PRI lock while we create the channel
+                                                        * so other threads can send D channel messages.
+                                                        */
                                                        ast_mutex_unlock(&pri->lock);
-
-                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RESERVED, 0, (e->ring.layer1 = PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
-
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-
-                                                       if (!ast_strlen_zero(e->ring.callingsubaddr)) {
-                                                               pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
-                                                       }
-                                                       if (e->ring.ani2 >= 0) {
-                                                               snprintf(ani2str, 5, "%.2d", e->ring.ani2);
-                                                               pbx_builtin_setvar_helper(c, "ANI2", ani2str);
-                                                               pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
-                                                       }
+                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos],
+                                                               AST_STATE_RESERVED, 0,
+                                                               (e->ring.layer1 == PRI_LAYER_1_ALAW)
+                                                                       ? SIG_PRI_ALAW : SIG_PRI_ULAW,
+                                                               e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
+                                                       ast_mutex_lock(&pri->lock);
+                                                       if (c) {
+                                                               if (!ast_strlen_zero(e->ring.callingsubaddr)) {
+                                                                       pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
+                                                               }
+                                                               if (e->ring.ani2 >= 0) {
+                                                                       snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
+                                                                       pbx_builtin_setvar_helper(c, "ANI2", ani2str);
+                                                               }
 
 #ifdef SUPPORT_USERUSER
-                                                       if (!ast_strlen_zero(e->ring.useruserinfo)) {
-                                                               pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
-                                                       }
+                                                               if (!ast_strlen_zero(e->ring.useruserinfo)) {
+                                                                       pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
+                                                               }
 #endif
 
-                                                       snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
-                                                       pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
-                                                       if (e->ring.redirectingreason >= 0)
-                                                               pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
+                                                               snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
+                                                               pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
+                                                               if (e->ring.redirectingreason >= 0)
+                                                                       pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
 #if defined(HAVE_PRI_REVERSE_CHARGE)
-                                                       pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
+                                                               pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
 #endif
-
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
-                                                       ast_mutex_lock(&pri->lock);
-
-                                                       pthread_attr_init(&attr);
-                                                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-                                                       if (c && !ast_pthread_create(&threadid, &attr, pri_ss_thread, pri->pvts[chanpos])) {
+                                                       }
+                                                       if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
                                                                ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
-                                                                               plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
-                                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
+                                                                       plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
+                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
                                                        } else {
-                                                               ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", 
+                                                               ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
                                                                if (c)
                                                                        ast_hangup(c);
@@ -1190,21 +1328,33 @@ static void *pri_dchannel(void *vpri)
                                                                        pri->pvts[chanpos]->call = NULL;
                                                                }
                                                        }
-                                                       pthread_attr_destroy(&attr);
-                                               } else  {
+                                               } else {
+                                                       /*
+                                                        * Release the PRI lock while we create the channel
+                                                        * so other threads can send D channel messages.
+                                                        */
                                                        ast_mutex_unlock(&pri->lock);
-                                                       /* Release PRI lock while we create the channel */
-                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RING, 1, (e->ring.layer1 == PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
-                                               
+                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos],
+                                                               AST_STATE_RING, 0,
+                                                               (e->ring.layer1 == PRI_LAYER_1_ALAW)
+                                                                       ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype,
+                                                               pri->pvts[chanpos]->exten, NULL);
+                                                       ast_mutex_lock(&pri->lock);
                                                        if (c) {
-                                                               char calledtonstr[10];
-
-                                                               sig_pri_unlock_private(pri->pvts[chanpos]);
-
+                                                               /*
+                                                                * It is reasonably safe to set the following
+                                                                * channel variables while the PRI and DAHDI private
+                                                                * structures are locked.  The PBX has not been
+                                                                * started yet and it is unlikely that any other task
+                                                                * will do anything with the channel we have just
+                                                                * created.
+                                                                */
+                                                               if (!ast_strlen_zero(e->ring.callingsubaddr)) {
+                                                                       pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
+                                                               }
                                                                if (e->ring.ani2 >= 0) {
-                                                                       snprintf(ani2str, 5, "%d", e->ring.ani2);
+                                                                       snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
                                                                        pbx_builtin_setvar_helper(c, "ANI2", ani2str);
-                                                                       pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
                                                                }
 
 #ifdef SUPPORT_USERUSER
@@ -1219,30 +1369,29 @@ static void *pri_dchannel(void *vpri)
                                                                pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
 #endif
 
-                                                               snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
+                                                               snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
                                                                pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
-
-                                                               sig_pri_lock_private(pri->pvts[chanpos]);
-                                                               ast_mutex_lock(&pri->lock);
-
+                                                       }
+                                                       if (c && !ast_pbx_start(c)) {
                                                                ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
-                                                                               plancallingnum, pri->pvts[chanpos]->exten, 
-                                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
+                                                                       plancallingnum, pri->pvts[chanpos]->exten,
+                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
                                                                sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
                                                        } else {
-
-                                                               ast_mutex_lock(&pri->lock);
-
                                                                ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
-                                                               pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
-                                                               pri->pvts[chanpos]->call = NULL;
+                                                               if (c) {
+                                                                       ast_hangup(c);
+                                                               } else {
+                                                                       pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
+                                                                       pri->pvts[chanpos]->call = NULL;
+                                                               }
                                                        }
                                                }
                                        } else {
                                                ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
-                                                               pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan, 
-                                                                       pri->pvts[chanpos]->prioffset, pri->span);
+                                                       pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
+                                                       pri->pvts[chanpos]->prioffset, pri->span);
                                                pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
                                                pri->pvts[chanpos]->call = NULL;
                                                pri->pvts[chanpos]->exten[0] = '\0';
@@ -1258,31 +1407,30 @@ static void *pri_dchannel(void *vpri)
                        case PRI_EVENT_RINGING:
                                chanpos = pri_find_principle(pri, e->ringing.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
                                        if (chanpos < 0) {
-                                               ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n", 
+                                               ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
                                                        PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
                                        } else {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
                                                sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
                                                pri_queue_control(pri->pvts[chanpos], AST_CONTROL_RINGING, pri);
                                                pri->pvts[chanpos]->alerting = 1;
-#ifdef PRI_PROGRESS_MASK
-                                               if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
-#else
-                                               if (e->ringing.progress == 8) {
-#endif
-                                               }
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->ringing.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->ringing.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1294,11 +1442,13 @@ static void *pri_dchannel(void *vpri)
                                /* Get chan value if e->e is not PRI_EVNT_RINGING */
                                chanpos = pri_find_principle(pri, e->proceeding.channel);
                                if (chanpos > -1) {
+                                       if ((!pri->pvts[chanpos]->progress)
 #ifdef PRI_PROGRESS_MASK
-                                       if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
+                                               || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
 #else
-                                       if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
+                                               || (e->proceeding.progress == 8)
 #endif
+                                               ) {
                                                struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
 
                                                if (e->proceeding.cause > -1) {
@@ -1314,16 +1464,18 @@ static void *pri_dchannel(void *vpri)
                                                                }
                                                        }
                                                }
-                                               
+
                                                sig_pri_lock_private(pri->pvts[chanpos]);
-                                               ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
-                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
+                                               ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
+                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
                                                pri_queue_frame(pri->pvts[chanpos], &f, pri);
+                                               if (
 #ifdef PRI_PROGRESS_MASK
-                                               if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
+                                                       e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
 #else
-                                               if (e->proceeding.progress == 8) {
+                                                       e->proceeding.progress == 8
 #endif
+                                                       ) {
                                                        /* Bring voice path up */
                                                        f.subclass = AST_CONTROL_PROGRESS;
                                                        pri_queue_frame(pri->pvts[chanpos], &f, pri);
@@ -1339,16 +1491,18 @@ static void *pri_dchannel(void *vpri)
                                if (chanpos > -1) {
                                        if (!pri->pvts[chanpos]->proceeding) {
                                                struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
-                                               
+
                                                sig_pri_lock_private(pri->pvts[chanpos]);
-                                               ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
-                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
+                                               ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
+                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
                                                pri_queue_frame(pri->pvts[chanpos], &f, pri);
+                                               if (
 #ifdef PRI_PROGRESS_MASK
-                                               if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
+                                                       e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
 #else
-                                               if (e->proceeding.progress == 8) {
+                                                       e->proceeding.progress == 8
 #endif
+                                                       ) {
                                                        /* Bring voice path up */
                                                        f.subclass = AST_CONTROL_PROGRESS;
                                                        pri_queue_frame(pri->pvts[chanpos], &f, pri);
@@ -1362,12 +1516,12 @@ static void *pri_dchannel(void *vpri)
                        case PRI_EVENT_FACNAME:
                                chanpos = pri_find_principle(pri, e->facname.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
                                        if (chanpos < 0) {
-                                               ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n", 
+                                               ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
                                                        PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
                                        } else {
                                                /* Re-use *69 field for PRI */
@@ -1379,16 +1533,16 @@ static void *pri_dchannel(void *vpri)
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                        }
                                }
-                               break;                          
+                               break;
                        case PRI_EVENT_ANSWER:
                                chanpos = pri_find_principle(pri, e->answer.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
                                        if (chanpos < 0) {
-                                               ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n", 
+                                               ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
                                                        PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
                                        } else {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
@@ -1399,21 +1553,26 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->answer.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->answer.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                        }
                                }
-                               break;                          
+                               break;
                        case PRI_EVENT_HANGUP:
                                chanpos = pri_find_principle(pri, e->hangup.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
@@ -1429,53 +1588,59 @@ static void *pri_dchannel(void *vpri)
                                                                        ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
                                                                else {
                                                                        switch (e->hangup.cause) {
-                                                                               case PRI_CAUSE_USER_BUSY:
-                                                                                       pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
-                                                                                       break;
-                                                                               case PRI_CAUSE_CALL_REJECTED:
-                                                                               case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
-                                                                               case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
-                                                                               case PRI_CAUSE_SWITCH_CONGESTION:
-                                                                               case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
-                                                                               case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
-                                                                                       pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
-                                                                                       break;
-                                                                               default:
-                                                                                       ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
+                                                                       case PRI_CAUSE_USER_BUSY:
+                                                                               pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
+                                                                               break;
+                                                                       case PRI_CAUSE_CALL_REJECTED:
+                                                                       case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
+                                                                       case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
+                                                                       case PRI_CAUSE_SWITCH_CONGESTION:
+                                                                       case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
+                                                                       case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
+                                                                               pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
+                                                                               break;
+                                                                       default:
+                                                                               ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
+                                                                               break;
                                                                        }
                                                                }
                                                        }
-                                                       ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n", 
-                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
+                                                       ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
+                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
                                                } else {
                                                        pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
                                                        pri->pvts[chanpos]->call = NULL;
                                                }
                                                if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
-                                                       ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n", 
-                                                                       PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
+                                                       ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
+                                                               PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                                        pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
                                                        pri->pvts[chanpos]->resetting = 1;
                                                }
                                                if (e->hangup.aoc_units > -1)
                                                        ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
-                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
+                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
 
 #ifdef SUPPORT_USERUSER
-                                               if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                               if (!ast_strlen_zero(e->hangup.useruserinfo)) {
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                        } else {
-                                               ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n", 
+                                               ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
                                                        PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                        }
-                               } 
+                               }
                                break;
 #ifndef PRI_EVENT_HANGUP_REQ
 #error please update libpri
@@ -1483,7 +1648,7 @@ static void *pri_dchannel(void *vpri)
                        case PRI_EVENT_HANGUP_REQ:
                                chanpos = pri_find_principle(pri, e->hangup.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
@@ -1495,42 +1660,48 @@ static void *pri_dchannel(void *vpri)
                                                                ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
                                                        else {
                                                                switch (e->hangup.cause) {
-                                                                       case PRI_CAUSE_USER_BUSY:
-                                                                               pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
-                                                                               break;
-                                                                       case PRI_CAUSE_CALL_REJECTED:
-                                                                       case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
-                                                                       case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
-                                                                       case PRI_CAUSE_SWITCH_CONGESTION:
-                                                                       case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
-                                                                       case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
-                                                                               pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
-                                                                               break;
-                                                                       default:
-                                                                               ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
+                                                               case PRI_CAUSE_USER_BUSY:
+                                                                       pri_queue_control(pri->pvts[chanpos], AST_CONTROL_BUSY, pri);
+                                                                       break;
+                                                               case PRI_CAUSE_CALL_REJECTED:
+                                                               case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
+                                                               case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
+                                                               case PRI_CAUSE_SWITCH_CONGESTION:
+                                                               case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
+                                                               case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
+                                                                       pri_queue_control(pri->pvts[chanpos], AST_CONTROL_CONGESTION, pri);
+                                                                       break;
+                                                               default:
+                                                                       ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
+                                                                       break;
                                                                }
                                                        }
                                                        ast_verb(3, "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause);
                                                        if (e->hangup.aoc_units > -1)
                                                                ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
-                                                                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
+                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
                                                } else {
                                                        pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
                                                        pri->pvts[chanpos]->call = NULL;
                                                }
                                                if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
-                                                       ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n", 
-                                                                       PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
+                                                       ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
+                                                               PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                                        pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
                                                        pri->pvts[chanpos]->resetting = 1;
                                                }
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1538,12 +1709,12 @@ static void *pri_dchannel(void *vpri)
                                        } else {
                                                ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                        }
-                               } 
+                               }
                                break;
                        case PRI_EVENT_HANGUP_ACK:
                                chanpos = pri_find_principle(pri, e->hangup.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
                                                PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
@@ -1557,10 +1728,15 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1581,16 +1757,16 @@ static void *pri_dchannel(void *vpri)
                                                if (pri->pvts[x] && pri->pvts[x]->resetting) {
                                                        chanpos = x;
                                                        sig_pri_lock_private(pri->pvts[chanpos]);
-                                                       ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan, 
-                                                                       pri->pvts[chanpos]->prioffset, pri->span);
+                                                       ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
+                                                               pri->pvts[chanpos]->prioffset, pri->span);
                                                        if (pri->pvts[chanpos]->owner) {
-                                                               ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan, 
+                                                               ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
                                                                        pri->pvts[chanpos]->prioffset, pri->span);
                                                                ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
                                                        }
                                                        pri->pvts[chanpos]->resetting = 0;
-                                                       ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, 
-                                                                       pri->pvts[chanpos]->prioffset, pri->span);
+                                                       ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
+                                                               pri->pvts[chanpos]->prioffset, pri->span);
                                                        sig_pri_unlock_private(pri->pvts[chanpos]);
                                                        if (pri->resetting)
                                                                pri_check_restart(pri);
@@ -1598,7 +1774,7 @@ static void *pri_dchannel(void *vpri)
                                                }
                                        }
                                        if (chanpos < 0) {
-                                               ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n", 
+                                               ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
                                                        PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
                                        }
                                } else {
@@ -1610,8 +1786,8 @@ static void *pri_dchannel(void *vpri)
                                                        ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
                                                }
                                                pri->pvts[chanpos]->resetting = 0;
-                                               ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, 
-                                                                       pri->pvts[chanpos]->prioffset, pri->span);
+                                               ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
+                                                       pri->pvts[chanpos]->prioffset, pri->span);
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                                if (pri->resetting)
                                                        pri_check_restart(pri);
@@ -1621,7 +1797,7 @@ static void *pri_dchannel(void *vpri)
                        case PRI_EVENT_SETUP_ACK:
                                chanpos = pri_find_principle(pri, e->setup_ack.channel);
                                if (chanpos < 0) {
-                                       ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n", 
+                                       ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
                                } else {
                                        chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
@@ -1630,8 +1806,8 @@ static void *pri_dchannel(void *vpri)
                                                pri->pvts[chanpos]->setup_ack = 1;
                                                /* Send any queued digits */
                                                for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
-                                                       ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
-                                                       pri_information(pri->pri, pri->pvts[chanpos]->call, 
+                                                       ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
+                                                       pri_information(pri->pri, pri->pvts[chanpos]->call,
                                                                pri->pvts[chanpos]->dialdest[x]);
                                                }
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
@@ -1644,8 +1820,9 @@ static void *pri_dchannel(void *vpri)
                                if (chanpos < 0) {
                                        ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
-                               } else {
+                               } else if (!pri->discardremoteholdretrieval) {
                                        struct ast_frame f = { AST_FRAME_CONTROL, };
+
                                        sig_pri_lock_private(pri->pvts[chanpos]);
                                        switch (e->notify.info) {
                                        case PRI_NOTIFY_REMOTE_HOLD:
@@ -1661,9 +1838,9 @@ static void *pri_dchannel(void *vpri)
                                }
                                break;
                        default:
-                               ast_log(LOG_DEBUG, "Event: %d\n", e->e);
+                               ast_debug(1, "Event: %d\n", e->e);
                        }
-               }       
+               }
                ast_mutex_unlock(&pri->lock);
        }
        /* Never reached */
@@ -1695,7 +1872,7 @@ int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
                ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
                return 0;
        }
-       
+
        p->owner = NULL;
        p->outgoing = 0;
        p->digital = 0;
@@ -1703,10 +1880,9 @@ int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
        p->progress = 0;
        p->alerting = 0;
        p->setup_ack = 0;
-       p->rdnis[0] = '\0';
        p->exten[0] = '\0';
        sig_pri_set_dialing(p, 0);
-       
+
        if (!p->call) {
                res = 0;
                goto exit;
@@ -1739,9 +1915,9 @@ int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
                        }
                        pri_hangup(p->pri->pri, p->call, icause);
                }
-               if (res < 0) 
+               if (res < 0)
                        ast_log(LOG_WARNING, "pri_disconnect failed\n");
-               pri_rel(p->pri);                        
+               pri_rel(p->pri);
        } else {
                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
                res = -1;
@@ -1795,7 +1971,6 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
 
        l = NULL;
        n = NULL;
-
        if (!p->hidecallerid) {
                l = ast->connected.id.number;
                if (!p->hidecalleridname) {
@@ -1803,7 +1978,6 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
                }
        }
 
-
        if (strlen(c) < p->stripmsd) {
                ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
                return -1;
@@ -1833,32 +2007,32 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
        } else {
                exclusive = 0;
        }
-       
+
        pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
-       pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability, 
-                               (p->digital ? -1 : layer1));
-                                       
+       pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
+               (p->digital ? -1 : layer1));
+
        if (p->pri->facilityenable)
                pri_facility_enable(p->pri->pri);
 
        ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
        dp_strip = 0;
-       pridialplan = p->pri->dialplan - 1;
-       if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
-               if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
+       pridialplan = p->pri->dialplan - 1;
+       if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
+               if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
                        if (pridialplan == -2) {
-                               dp_strip = strlen(p->pri->internationalprefix);
+                               dp_strip = strlen(p->pri->internationalprefix);
                        }
-                       pridialplan = PRI_INTERNATIONAL_ISDN;
-               } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
+                       pridialplan = PRI_INTERNATIONAL_ISDN;
+               } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
                        if (pridialplan == -2) {
-                               dp_strip = strlen(p->pri->nationalprefix);
+                               dp_strip = strlen(p->pri->nationalprefix);
                        }
-                       pridialplan = PRI_NATIONAL_ISDN;
-               } else {
+                       pridialplan = PRI_NATIONAL_ISDN;
+               } else {
                        pridialplan = PRI_LOCAL_ISDN;
-               }
-       }
+               }
+       }
        while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
                switch (c[p->stripmsd]) {
                case 'U':
@@ -1917,7 +2091,7 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
                }
                c++;
        }
-       pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
+       pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
 
        ldp_strip = 0;
        prilocaldialplan = p->pri->localdialplan - 1;
@@ -2017,7 +2191,7 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
 #endif
 
        if (pri_setup(p->pri->pri, p->call, sr)) {
-               ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n", 
+               ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
                        c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
                pri_rel(p->pri);
                pri_sr_free(sr);
@@ -2032,7 +2206,7 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
 
 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
 {
-       int res = 0;
+       int res = -1;
 
        switch (condition) {
        case AST_CONTROL_BUSY:
@@ -2059,7 +2233,7 @@ int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condi
                break;
        case AST_CONTROL_RINGING:
                if ((!p->alerting) && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
-                       if (p->pri->pri) {              
+                       if (p->pri->pri) {
                                if (!pri_grab(p, p->pri)) {
                                        pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
                                        pri_rel(p->pri);
@@ -2078,7 +2252,7 @@ int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condi
        case AST_CONTROL_PROCEEDING:
                ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
                if (!p->proceeding && p->pri && !p->outgoing) {
-                       if (p->pri->pri) {              
+                       if (p->pri->pri) {
                                if (!pri_grab(p, p->pri)) {
                                        pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
                                        pri_rel(p->pri);
@@ -2093,12 +2267,16 @@ int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condi
                res = 0;
                break;
        case AST_CONTROL_PROGRESS:
-               ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
+               ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
                p->digital = 0; /* Digital-only calls isn't allowing any inband progress messages */
                if (!p->progress && p->pri && !p->outgoing) {
-                       if (p->pri->pri) {              
+                       if (p->pri->pri) {
                                if (!pri_grab(p, p->pri)) {
+#ifdef HAVE_PRI_PROG_W_CAUSE
+                                       pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);  /* no cause at all */
+#else
                                        pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
+#endif
                                        pri_rel(p->pri);
                                } else {
                                        ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
@@ -2116,7 +2294,7 @@ int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condi
                        chan->_softhangup |= AST_SOFTHANGUP_DEV;
                        res = 0;
                } else if (!p->progress && p->pri && !p->outgoing) {
-                       if (p->pri) {           
+                       if (p->pri) {
                                if (!pri_grab(p, p->pri)) {
 #ifdef HAVE_PRI_PROG_W_CAUSE
                                        pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION); /* cause = 42 */
@@ -2188,7 +2366,7 @@ int sig_pri_available(struct sig_pri_chan *p, int channelmatch, ast_group_t grou
 #ifdef HAVE_PRI_SERVICE_MESSAGES
                        char db_chan_name[20], db_answer[5], state;
                        int why = 0;
-                                               
+
                        snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, p->pri->span, p->channel);
                        if (!ast_db_get(db_chan_name, SRVST_DBKEY, db_answer, sizeof(db_answer))) {
                                sscanf(db_answer, "%c:%d", &state, &why);
@@ -2238,7 +2416,7 @@ int sig_pri_start_pri(struct sig_pri_pri *pri)
 {
        int x;
        int i;
-       
+
        ast_mutex_init(&pri->lock);
 
        for (i = 0; i < NUM_DCHANS; i++) {
@@ -2256,15 +2434,20 @@ int sig_pri_start_pri(struct sig_pri_pri *pri)
                default:
                        pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
 #ifdef HAVE_PRI_SERVICE_MESSAGES
-                               if (pri->enable_service_message_support) {
-                                       pri_set_service_message_support(pri->dchans[i], 1);
-                               }
+                       if (pri->enable_service_message_support) {
+                               pri_set_service_message_support(pri->dchans[i], 1);
+                       }
 #endif
+                       break;
                }
 
-               /* Force overlap dial if we're doing GR-303! */
-               pri_set_overlapdial(pri->dchans[i], pri->overlapdial);
+               pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
+#ifdef HAVE_PRI_PROG_W_CAUSE
+               pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
+#endif
+#ifdef HAVE_PRI_INBANDDISCONNECT
                pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
+#endif
                /* Enslave to master if appropriate */
                if (i)
                        pri_enslave(pri->dchans[0], pri->dchans[i]);
@@ -2275,7 +2458,7 @@ int sig_pri_start_pri(struct sig_pri_pri *pri)
                        ast_log(LOG_ERROR, "Unable to create PRI structure\n");
                        return -1;
                }
-               pri_set_debug(pri->dchans[i], 0);
+               pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
                pri_set_nsf(pri->dchans[i], pri->nsf);
 #ifdef PRI_GETSET_TIMERS
                for (x = 0; x < PRI_MAX_TIMERS; x++) {
@@ -2465,6 +2648,7 @@ int pri_send_callrerouting_facility_exec(struct sig_pri_chan *p, enum ast_channe
        return res;
 }
 
+#ifdef HAVE_PRI_SERVICE_MESSAGES
 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
 {
        int channel = PVT_TO_CHANNEL(p);
@@ -2472,5 +2656,6 @@ int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int change
 
        return pri_maintenance_service(pri, span, channel, changestatus);
 }
+#endif
 
 #endif /* HAVE_PRI */