Restoring some code to sig_pri. Not sure if it is really needed.
[asterisk/asterisk.git] / channels / sig_pri.c
index 6b6613f..6d9bf66 100644 (file)
 
 #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)
@@ -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);
@@ -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;
@@ -694,7 +819,7 @@ static void *pri_dchannel(void *vpri)
                        activeidles = 0;
                        for (x = pri->numchans; x >= 0; x--) {
                                if (pri->pvts[x] && !pri->pvts[x]->owner &&
-                                   !pri->pvts[x]->call) {
+                                       !pri->pvts[x]->call) {
                                        if (haveidles < pri->minunused) {
                                                haveidles++;
                                        } else if (!pri->pvts[x]->resetting) {
@@ -720,7 +845,7 @@ static void *pri_dchannel(void *vpri)
                                        gettimeofday(&lastidle, NULL);
                                }
                        } else if ((haveidles < pri->minunused) &&
-                                  (activeidles > pri->minidle)) {
+                               (activeidles > pri->minidle)) {
                                /* Mark something for hangup if there is something
                                   that can be hungup */
                                for (x = pri->numchans; x >= 0; x--) {
@@ -731,7 +856,7 @@ 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;
                        }
@@ -901,7 +1026,7 @@ 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);
+                                                               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);
@@ -940,17 +1065,17 @@ static void *pri_dchannel(void *vpri)
                                        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);
-                                                               }
+                                                               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]);
@@ -968,17 +1093,17 @@ static void *pri_dchannel(void *vpri)
                                        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);
-                                                               }
+                                                               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]);
@@ -1056,6 +1181,7 @@ static void *pri_dchannel(void *vpri)
                                                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",
                                                                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 */
@@ -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)) {
@@ -1136,50 +1275,49 @@ static void *pri_dchannel(void *vpri)
                                                        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",
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
@@ -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';
@@ -1270,19 +1419,18 @@ static void *pri_dchannel(void *vpri)
                                                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) {
@@ -1316,14 +1466,16 @@ 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);
@@ -1341,14 +1493,16 @@ static void *pri_dchannel(void *vpri)
                                                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);
@@ -1399,10 +1553,15 @@ 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
 
@@ -1429,44 +1588,50 @@ 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);
+                                                               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);
+                                                               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
 
@@ -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);
+                                                               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
 
@@ -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,8 +1757,8 @@ 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,
                                                                        pri->pvts[chanpos]->prioffset, pri->span);
@@ -1590,7 +1766,7 @@ static void *pri_dchannel(void *vpri)
                                                        }
                                                        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);
+                                                               pri->pvts[chanpos]->prioffset, pri->span);
                                                        sig_pri_unlock_private(pri->pvts[chanpos]);
                                                        if (pri->resetting)
                                                                pri_check_restart(pri);
@@ -1611,7 +1787,7 @@ static void *pri_dchannel(void *vpri)
                                                }
                                                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);
+                                                       pri->pvts[chanpos]->prioffset, pri->span);
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
                                                if (pri->resetting)
                                                        pri_check_restart(pri);
@@ -1630,7 +1806,7 @@ 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]);
+                                                       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]);
                                                }
@@ -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,7 +1838,7 @@ 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);
@@ -1703,7 +1880,6 @@ 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);
 
@@ -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;
@@ -1836,29 +2010,29 @@ int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, i
 
        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));
+               (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;
@@ -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:
@@ -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 (!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);
@@ -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++) {