52
52
#define DELAY_UNTIL_DIALTONE 3000
54
#define BAT_THRESHOLD 3
55
#define BAT_DEBOUNCE 3 /* compensate for battery voltage fluctuation (in poll_battery_interval's) */
54
57
/*---------------- FXO Protocol Commands ----------------------------------*/
56
59
static /* 0x0F */ DECLARE_CMD(FXO, CHAN_ENABLE, xpp_line_t lines, bool on);
57
60
static /* 0x0F */ DECLARE_CMD(FXO, CHAN_CID, int pos);
58
61
static /* 0x0F */ DECLARE_CMD(FXO, RING, int pos, bool on);
59
static /* 0x0F */ DECLARE_CMD(FXO, SETHOOK, int pos, bool offhook);
60
62
static /* 0x0F */ DECLARE_CMD(FXO, RELAY_OUT, byte which, bool on);
61
63
static /* 0x0F */ DECLARE_CMD(FXO, DAA_QUERY, int pos, byte reg_num);
70
72
#define PROC_DAA_FNAME "slics"
71
73
#define PROC_FXO_INFO_FNAME "fxo_info"
76
#define POLL_RING_INTERVAL 2
77
#define RING_THRESHOLD 3
78
#define NORING_THRESHOLD 10
80
#define DAA_RING_REGISTER 0x05
73
82
struct FXO_priv_data {
74
83
struct proc_dir_entry *xpd_slic;
75
84
struct proc_dir_entry *fxo_info;
76
86
slic_reply_t requested_reply;
77
87
slic_reply_t last_reply;
78
88
xpp_line_t battery;
89
ushort battery_debounce[CHANNELS_PERXPD];
79
90
xpp_line_t ledstate[NUM_LEDS]; /* 0 - OFF, 1 - ON */
91
xpp_line_t ledcontrol[NUM_LEDS]; /* 0 - OFF, 1 - ON */
80
92
int blinking[NUM_LEDS][CHANNELS_PERXPD];
94
ushort ring_thresh[CHANNELS_PERXPD];
95
ushort noring_thresh[CHANNELS_PERXPD];
96
/* ring_sig is set when Reg5, bit 2 (Ring Detect) is set.
97
* While ring_sig=1 we check R5 bit 20H and 40H for ringing.
98
* When it drops to 0 that's the end of the ring sequence and
99
* we clear the ring detection variables */
100
ushort ring_sig[CHANNELS_PERXPD];
83
104
/*---------------- FXO: Static functions ----------------------------------*/
85
106
#define IS_BLINKING(priv,pos,color) ((priv)->blinking[color][pos] != 0)
86
#define DO_BLINK(priv,pos,color,val) ((priv)->blinking[color][pos] = (val))
107
#define MARK_BLINK(priv,pos,color,val) ((priv)->blinking[color][pos] = (val))
109
void MARK_LED(xpd_t *xpd, lineno_t pos, byte color, bool on)
111
struct FXO_priv_data *priv = xpd->priv;
114
BIT_SET(priv->ledcontrol[color], pos);
116
BIT_CLR(priv->ledcontrol[color], pos);
89
120
* LED control is done via DAA register 0x20
146
177
if(IS_BLINKING(priv,i,color)) {
147
178
// led state is toggled
148
179
if((timer_count % LED_BLINK_PERIOD) == 0) {
149
DBG("%s/%s/%d: led_state=%s\n", xpd->xbus->busname, xpd->xpdname, i,
180
DBG("%s/%s/%d: ledstate=%s\n", xpd->xbus->busname, xpd->xpdname, i,
150
181
(IS_SET(priv->ledstate[color], i))?"ON":"OFF");
151
182
if(!IS_SET(priv->ledstate[color], i)) {
152
183
do_led(xpd, i, color, 1);
154
185
do_led(xpd, i, color, 0);
188
} else if(IS_SET(priv->ledcontrol[color], i) && !IS_SET(priv->ledstate[color], i)) {
189
do_led(xpd, i, color, 1);
190
} else if(!IS_SET(priv->ledcontrol[color], i) && IS_SET(priv->ledstate[color], i)) {
191
do_led(xpd, i, color, 0);
159
194
spin_unlock_irqrestore(&xpd->lock, flags);
162
static void do_sethook(xpd_t *xpd, int pos, bool offhook)
197
static void mark_ring(xpd_t *xpd, lineno_t pos, bool on)
199
struct FXO_priv_data *priv;
203
if(on && !xpd->ringing[pos]) {
204
DBG("%s/%s/%d: START\n", xpd->xbus->busname, xpd->xpdname, pos);
205
xpd->ringing[pos] = 1;
206
MARK_BLINK(priv, pos, LED_GREEN, LED_BLINK);
207
update_zap_ring(xpd, pos, 1);
208
} else if(!on && xpd->ringing[pos]) {
209
DBG("%s/%s/%d: STOP\n", xpd->xbus->busname, xpd->xpdname, pos);
210
xpd->ringing[pos] = 0;
211
if(IS_BLINKING(priv, pos, LED_GREEN))
212
MARK_BLINK(priv, pos, LED_GREEN, 0);
213
update_zap_ring(xpd, pos, 0);
217
static int do_sethook(xpd_t *xpd, int pos, bool to_offhook)
164
219
unsigned long flags;
165
221
struct FXO_priv_data *priv;
168
229
BUG_ON(xpd->direction == TO_PHONE); // We can SETHOOK state only on PSTN
169
231
priv = xpd->priv;
171
233
if(!IS_SET(priv->battery, pos)) {
172
DBG("%s/%s/%d: WARNING: called while battery is off\n", xpd->xbus->busname, xpd->xpdname, pos);
234
NOTICE("%s/%s/%d: WARNING: called while battery is off\n", xbus->busname, xpd->xpdname, pos);
174
236
spin_lock_irqsave(&xpd->lock, flags);
175
xpd->ringing[pos] = 0; // No more rings
176
CALL_XMETHOD(SETHOOK, xpd->xbus, xpd, pos, offhook);
178
BIT_SET(xpd->hookstate, pos);
237
mark_ring(xpd, pos, 0); // No more rings
238
value = (to_offhook) ? 0x09 : 0x08; /* Bit 3 is for CID */
239
DBG("%s/%s/%d: SETHOOK: value=0x%02X %s\n", xbus->busname, xpd->xpdname, pos, value, (to_offhook)?"OFFHOOK":"ONHOOK");
240
MARK_LED(xpd, pos, LED_GREEN, (to_offhook)?LED_ON:LED_OFF);
241
XPACKET_NEW(pack, xbus, FXO, DAA_WRITE, xpd->id);
242
sc = &RPACKET_FIELD(pack, FXO, DAA_WRITE, slic_cmd);
243
len = slic_cmd_direct_write(sc, BIT(pos), DAA_RING_REGISTER, value);
245
packet_send(xbus, pack);
247
priv->ring_sig[pos] = 0;
250
BIT_SET(xpd->offhook, pos);
180
BIT_CLR(xpd->hookstate, pos);
252
BIT_CLR(xpd->offhook, pos);
253
BIT_CLR(xpd->cid_on, pos);
181
254
xpd->delay_until_dialtone[pos] = 0;
183
256
spin_unlock_irqrestore(&xpd->lock, flags);
185
258
wake_up_interruptible(&xpd->txstateq[pos]);
188
262
/*---------------- FXO: Methods -------------------------------------------*/
300
373
cur_chan->pvt = xpd;
301
374
cur_chan->sigcap = FXO_DEFAULT_SIGCAP;
303
spin_lock_irqsave(&xpd->lock, flags);
304
do_led(xpd, ALL_LINES, LED_GREEN, LED_OFF);
305
spin_unlock_irqrestore(&xpd->lock, flags);
376
MARK_LED(xpd, ALL_LINES, LED_GREEN, LED_OFF);
306
377
for_each_line(xpd, i) {
307
do_led(xpd, i, LED_GREEN, LED_ON);
378
MARK_LED(xpd, i, LED_GREEN, LED_ON);
359
int FXO_card_sethook(xbus_t *xbus, xpd_t *xpd, int pos, int hookstate)
362
struct FXO_priv_data *priv;
364
DBG("%s/%s/%d: %s\n", xbus->busname, xpd->xpdname, pos, hookstate2str(hookstate));
366
/* On-hook, off-hook: The PBX is playing a phone on an FXO line. */
368
do_sethook(xpd, pos, 0);
371
DBG("%s/%s/%d: fall through ZT_OFFHOOK\n", xbus->busname, xpd->xpdname, pos);
372
xpd->delay_until_dialtone[pos] = DELAY_UNTIL_DIALTONE;
375
do_sethook(xpd, pos, 1);
376
wait_event_interruptible(xpd->txstateq[pos], xpd->delay_until_dialtone[pos] <= 0);
379
WARN("No code yet\n");
382
WARN("No code yet\n");
385
DBG("%s/%s/%d: ZT_RING: %d\n", xbus->busname, xpd->xpdname, pos, xpd->ringing[pos]);
388
WARN("No code yet\n");
395
428
static void poll_battery(xbus_t *xbus, xpd_t *xpd)
438
static void poll_ring(xbus_t *xbus, xpd_t *xpd)
441
struct FXO_priv_data *priv;
445
for_each_line(xpd, i) {
446
if(priv->ring_sig[i])
447
CALL_PROTO(FXO, DAA_QUERY, xbus, xpd, i, DAA_RING_REGISTER);
405
452
static int FXO_card_tick(xbus_t *xbus, xpd_t *xpd)
407
static unsigned rate_limit = 0;
408
454
struct FXO_priv_data *priv;
411
457
priv = xpd->priv;
414
if(poll_battery_interval != 0 && (rate_limit % poll_battery_interval) == 0) {
459
if(poll_battery_interval != 0 && (priv->poll_counter % poll_battery_interval) == 0) {
415
460
poll_battery(xbus, xpd);
463
if((priv->poll_counter % POLL_RING_INTERVAL) == 0)
464
poll_ring(xbus, xpd);
417
466
handle_fxo_leds(xpd);
467
priv->poll_counter++;
436
DBG("cmd: 0x%x, expecting: 0x%x, pos=%d.\n", cmd, WCTDM_SET_ECHOTUNE, pos);
486
DBG("cmd: 0x%X, expecting: 0x%X, pos=%d.\n", cmd, WCTDM_SET_ECHOTUNE, pos);
438
488
case WCTDM_SET_ECHOTUNE:
439
489
DBG("-- Setting echo registers: \n");
440
490
/* first off: check if this span is fxs. If not: -EINVALID */
441
491
if (copy_from_user(&echoregs.wctdm_struct,
442
(struct wctdm_echo_coefs*)arg, sizeof(echoregs.wctdm_struct)))
492
(struct wctdm_echo_coefs __user *)arg, sizeof(echoregs.wctdm_struct)))
445
495
/* Set the ACIM register */
486
535
DBG("Channel Activation: 0x%4X %s\n", lines, (on) ? "on" : "off");
488
537
for_each_line(xpd, i) {
489
spin_lock_irqsave(&xpd->lock, flags);
490
do_led(xpd, i, LED_GREEN, LED_ON);
491
spin_unlock_irqrestore(&xpd->lock, flags);
538
MARK_LED(xpd, i, LED_GREEN, LED_ON);
494
541
for_each_line(xpd, i) {
495
spin_lock_irqsave(&xpd->lock, flags);
496
do_led(xpd, i, LED_GREEN, LED_OFF);
497
spin_unlock_irqrestore(&xpd->lock, flags);
542
MARK_LED(xpd, i, LED_GREEN, LED_OFF);
542
static /* 0x0F */ HOSTCMD(FXO, SETHOOK, int pos, bool offhook)
553
value = (offhook) ? 0x09 : 0x08;
554
// value |= BIT(3); /* Bit 3 is for CID */
555
DBG("%s/%s/%d: SETHOOK: value=0x%02X %s\n", xbus->busname, xpd->xpdname, pos, value, (offhook)?"OFFHOOK":"ONHOOK");
556
spin_lock_irqsave(&xpd->lock, flags);
557
XPACKET_NEW(pack, xbus, FXO, DAA_WRITE, xpd->id);
558
sc = &RPACKET_FIELD(pack, FXO, DAA_WRITE, slic_cmd);
559
len = slic_cmd_direct_write(sc, BIT(pos), 0x05, value);
561
packet_send(xbus, pack);
562
do_led(xpd, pos, LED_GREEN, (offhook)?LED_ON:LED_OFF);
563
spin_unlock_irqrestore(&xpd->lock, flags);
567
587
static /* 0x0F */ HOSTCMD(FXO, RELAY_OUT, byte which, bool on)
597
617
xpp_line_t sig_toggles = RPACKET_FIELD(pack, FXO, SIG_CHANGED, sig_toggles);
598
618
unsigned long flags;
620
struct FXO_priv_data *priv;
602
623
NOTICE("%s: received %s for non-existing xpd: %d\n",
603
624
__FUNCTION__, cmd->name, XPD_NUM(pack->content.addr));
606
629
DBG("%s/%s: (PSTN) sig_toggles=0x%04X sig_status=0x%04X\n", xpd->xbus->busname, xpd->xpdname, sig_toggles, sig_status);
607
630
spin_lock_irqsave(&xpd->lock, flags);
608
631
for_each_line(xpd, i) {
609
if(IS_SET(sig_status, i)) {
632
if(IS_SET(sig_toggles, i)) {
633
if(!IS_SET(priv->battery, i)) {
634
DBG("%s/%s/%d: battery is off. ignore false alarm.\n",
635
xbus->busname, xpd->xpdname, i);
638
if(IS_SET(sig_status, i)) {
640
priv->ring_sig[i]=1; /* trigger register polling */
641
/* reset ring check counters */
642
priv->ring_thresh[i] = 0;
643
priv->noring_thresh[i] = 0;
645
mark_ring(xpd, i, 1);
649
priv->ring_sig[i] = 0;
651
mark_ring(xpd, i, 0);
615
655
spin_unlock_irqrestore(&xpd->lock, flags);
632
672
priv = xpd->priv;
634
674
if(!info->indirect && info->reg_num == DAA_VBAT_REGISTER) {
635
xpp_line_t last_batt_on = priv->battery;
636
xpp_line_t changed_lines;
639
if(abs(info->data_low) < BAT_THRESHOLD) {
640
priv->battery &= ~lines;
641
// DBG("%s/%s: BATTERY OFF (%04X) = %d\n", xpd->xbus->busname, xpd->xpdname, lines, info->data_low);
643
priv->battery |= lines;
644
// DBG("%s/%s: BATTERY ON (%04X) = %d\n", xpd->xbus->busname, xpd->xpdname, lines, info->data_low);
646
changed_lines = last_batt_on ^ priv->battery;
647
for_each_line(xpd, i) {
648
if(IS_SET(changed_lines, i)) {
649
update_line_status(xpd, i, IS_SET(priv->battery, i));
675
byte bat = abs((signed char)info->data_low);
678
for_each_line(xpd, i) {
679
if(!IS_SET(lines, i))
681
if(bat < BAT_THRESHOLD) {
683
* Check for battery voltage fluctuations
685
if(IS_SET(priv->battery, i) && priv->battery_debounce[i]++ > BAT_DEBOUNCE) {
686
DBG("%s/%s: BATTERY OFF (%04X) voltage=%d\n", xpd->xbus->busname, xpd->xpdname, lines, bat);
687
BIT_CLR(priv->battery, i);
688
update_line_status(xpd, i, 0);
691
priv->battery_debounce[i] = 0;
692
if(!IS_SET(priv->battery, i)) {
693
DBG("%s/%s: BATTERY ON (%04X) voltage=%d\n", xpd->xbus->busname, xpd->xpdname, lines, bat);
694
BIT_SET(priv->battery, i);
700
if(!info->indirect && info->reg_num == DAA_RING_REGISTER) {
701
bool ringit = (info->data_low & (0x20 | 0x40)) ? 1 : 0; /* Ring positive | Ring negative */
704
for_each_line(xpd, i) {
705
if(!IS_SET(lines, i))
707
if (!priv->ring_sig[i])
710
if(priv->ring_thresh[i] > RING_THRESHOLD) {
711
mark_ring(xpd, i, 1);
712
priv->noring_thresh[i] = 0;
714
priv->ring_thresh[i]++;
716
if(priv->noring_thresh[i] > NORING_THRESHOLD) {
717
mark_ring(xpd, i, 0);
718
priv->ring_thresh[i] = 0;
720
priv->noring_thresh[i]++;
726
if (info->reg_num != 29)
654
727
DBG("DAA_REPLY: xpd #%d %s reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
655
728
xpd->id, (info->indirect)?"I":"D",
656
729
info->reg_num, info->data_low, info->data_high);
681
754
.card_remove = FXO_card_remove,
682
755
.card_zaptel_preregistration = FXO_card_zaptel_preregistration,
683
756
.card_zaptel_postregistration = FXO_card_zaptel_postregistration,
685
757
.card_hooksig = FXO_card_hooksig,
687
.card_sethook = FXO_card_sethook,
689
758
.card_tick = FXO_card_tick,
690
759
.card_ioctl = FXO_card_ioctl,
692
761
.RING = XPROTO_CALLER(FXO, RING),
693
.SETHOOK = XPROTO_CALLER(FXO, SETHOOK),
694
762
.RELAY_OUT = XPROTO_CALLER(FXO, RELAY_OUT),
695
763
.CHAN_ENABLE = XPROTO_CALLER(FXO, CHAN_ENABLE),
696
764
.CHAN_CID = XPROTO_CALLER(FXO, CHAN_CID),
734
802
len += sprintf(page + len, "\t%-17s: ", "Channel");
735
803
for_each_line(xpd, i) {
736
804
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
737
len += sprintf(page + len, "%d ", i % 10);
805
len += sprintf(page + len, "%2d ", i % 10);
739
807
len += sprintf(page + len, "\n\t%-17s: ", "ledstate");
740
808
for_each_line(xpd, i) {
741
809
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
742
len += sprintf(page + len, "%d ", IS_SET(priv->ledstate[LED_GREEN], i));
810
len += sprintf(page + len, "%2d ", IS_SET(priv->ledstate[LED_GREEN], i));
812
len += sprintf(page + len, "\n\t%-17s: ", "ledcontrol");
813
for_each_line(xpd, i) {
814
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
815
len += sprintf(page + len, "%2d ", IS_SET(priv->ledcontrol[LED_GREEN], i));
744
817
len += sprintf(page + len, "\n\t%-17s: ", "blinking");
745
818
for_each_line(xpd, i) {
746
819
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
747
len += sprintf(page + len, "%d ", IS_BLINKING(priv,i,LED_GREEN));
820
len += sprintf(page + len, "%2d ", IS_BLINKING(priv,i,LED_GREEN));
823
len += sprintf(page + len, "\n\t%-17s: ", "ring_thresh");
824
for_each_line(xpd, i) {
825
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
826
len += sprintf(page + len, "%2d ", priv->ring_thresh[i]);
828
len += sprintf(page + len, "\n\t%-17s: ", "noring_thresh");
829
for_each_line(xpd, i) {
830
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
831
len += sprintf(page + len, "%2d ", priv->noring_thresh[i]);
833
len += sprintf(page + len, "\n\t%-17s: ", "ring_sig");
834
for_each_line(xpd, i) {
835
if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
836
len += sprintf(page + len, "%d ", priv->ring_sig[i]);
749
839
len += sprintf(page + len, "\n\t%-17s: ", "battery");
750
840
for_each_line(xpd, i) {
751
len += sprintf(page + len, "%d ", IS_SET(priv->battery, i));
841
len += sprintf(page + len, "%2d ", IS_SET(priv->battery, i));
843
len += sprintf(page + len, "\n\t%-17s: ", "battery_debounce");
844
for_each_line(xpd, i) {
845
len += sprintf(page + len, "%2d ", priv->battery_debounce[i]);
753
847
len += sprintf(page + len, "\n");
754
848
spin_unlock_irqrestore(&xpd->lock, flags);