2 * Written by Oron Peled <oron@actcom.co.il>
3 * Copyright (C) 2004-2006, Xorcom
5 * Parts derived from Cologne demo driver for the chip.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/init.h>
26 #include <linux/module.h>
28 #include <linux/delay.h>
31 #include "xpp_dahdi.h"
33 #include "dahdi_debug.h"
34 #include "xbus-core.h"
36 static const char rcsid[] = "$Id$";
38 /* must be before dahdi_debug.h */
39 static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
40 static DEF_PARM(uint, poll_interval, 500, 0644,
41 "Poll channel state interval in milliseconds (0 - disable)");
43 #define PRI_LINES_BITMASK BITMASK(31)
44 #define PRI_SIGCAP ( \
50 DAHDI_SIG_HARDHDLC | \
60 static bool is_sigtype_dchan(int sigtype)
62 if ((sigtype & DAHDI_SIG_HDLCRAW) == DAHDI_SIG_HDLCRAW)
64 if ((sigtype & DAHDI_SIG_HDLCFCS) == DAHDI_SIG_HDLCFCS)
66 if ((sigtype & DAHDI_SIG_HARDHDLC) == DAHDI_SIG_HARDHDLC)
71 #define MAX_SLAVES 4 /* we have MUX of 4 clocks */
73 #define PRI_PORT(xpd) ((xpd)->addr.subunit)
74 #define CHAN_PER_REGS(p) (((p)->is_esf) ? 2 : 4)
76 /*---------------- PRI Protocol Commands ----------------------------------*/
78 static void dchan_state(xpd_t *xpd, bool up);
79 static bool pri_packet_is_valid(xpacket_t *pack);
80 static void pri_packet_dump(const char *msg, xpacket_t *pack);
81 static int pri_startup(struct file *file, struct dahdi_span *span);
82 static int pri_shutdown(struct dahdi_span *span);
83 static int pri_rbsbits(struct dahdi_chan *chan, int bits);
84 static int pri_lineconfig(xpd_t *xpd, int lineconfig);
85 static void send_idlebits(xpd_t *xpd, bool saveold);
86 static int apply_pri_protocol(xpd_t *xpd);
95 static const char *protocol_names[] = {
96 [PRI_PROTO_0] = "??", /* unknown */
97 [PRI_PROTO_E1] = "E1",
98 [PRI_PROTO_T1] = "T1",
102 static enum spantypes pri_protocol2spantype(enum pri_protocol pri_protocol)
104 switch (pri_protocol) {
105 case PRI_PROTO_E1: return SPANTYPE_DIGITAL_E1;
106 case PRI_PROTO_T1: return SPANTYPE_DIGITAL_T1;
107 case PRI_PROTO_J1: return SPANTYPE_DIGITAL_J1;
109 return SPANTYPE_INVALID;
113 static const char *pri_protocol_name(enum pri_protocol pri_protocol)
115 return protocol_names[pri_protocol];
118 static int pri_num_channels(enum pri_protocol pri_protocol)
120 static int num_channels[] = {
126 return num_channels[pri_protocol];
129 static const char *type_name(enum pri_protocol pri_protocol)
131 static const char *names[4] = {
132 [PRI_PROTO_0] = "PRI-Unknown",
133 [PRI_PROTO_E1] = "E1",
134 [PRI_PROTO_T1] = "T1",
135 [PRI_PROTO_J1] = "J1"
138 return names[pri_protocol];
141 static int pri_linecompat(enum pri_protocol pri_protocol)
143 static const int linecompat[] = {
147 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4 |
149 DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3,
152 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF |
154 DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS,
158 DBG(GENERAL, "pri_linecompat: pri_protocol=%d\n", pri_protocol);
159 return linecompat[pri_protocol];
162 #define PRI_DCHAN_IDX(priv) ((priv)->dchan_num - 1)
168 * We blink by software from driver, so that
169 * if the driver malfunction that blink would stop.
171 // PRI_LED_BLINK_SLOW = 0x2, /* 1/2 a second blink cycle */
172 // PRI_LED_BLINK_FAST = 0x3 /* 1/4 a second blink cycle */
175 enum pri_led_selectors {
177 BOTTOM_GREEN_LED = 1,
185 __u8 state:2; /* enum pri_led_state */
186 __u8 led_sel:2; /* enum pri_led_selectors */
190 #define REG_CCB1_T 0x2F /* Clear Channel Register 1 */
192 #define REG_FRS0 0x4C /* Framer Receive Status Register 0 */
193 #define REG_FRS0_T1_FSR BIT(0) /* T1 - Frame Search Restart Flag */
194 #define REG_FRS0_LMFA BIT(1) /* Loss of Multiframe Alignment */
195 #define REG_FRS0_E1_NMF BIT(2) /* E1 - No Multiframe Alignment Found */
196 #define REG_FRS0_RRA BIT(4) /* Receive Remote Alarm: T1-YELLOW-Alarm */
197 #define REG_FRS0_LFA BIT(5) /* Loss of Frame Alignment */
198 #define REG_FRS0_AIS BIT(6) /* Alarm Indication Signal: T1-BLUE-Alarm */
199 #define REG_FRS0_LOS BIT(7) /* Los Of Signal: T1-RED-Alarm */
201 #define REG_FRS1 0x4D /* Framer Receive Status Register 1 */
203 #define REG_LIM0 0x36
205 * Master Mode, DCO-R circuitry is frequency synchronized
206 * to the clock supplied by SYNC
208 #define REG_LIM0_MAS BIT(0)
210 * Receive Termination Resistance Selection:
211 * integrated resistor to create 75 Ohm termination (100 || 300 = 75)
215 #define REG_LIM0_RTRS BIT(5)
216 #define REG_LIM0_LL BIT(1) /* LL (Local Loopback) */
218 #define REG_FMR0 0x1C
219 #define REG_FMR0_E_RC0 BIT(4) /* Receive Code - LSB */
220 #define REG_FMR0_E_RC1 BIT(5) /* Receive Code - MSB */
221 #define REG_FMR0_E_XC0 BIT(6) /* Transmit Code - LSB */
222 #define REG_FMR0_E_XC1 BIT(7) /* Transmit Code - MSB */
224 #define REG_FMR1 0x1D
225 #define REG_FMR1_XAIS BIT(0) /* Transmit AIS toward transmit end */
226 #define REG_FMR1_SSD0 BIT(1)
227 #define REG_FMR1_ECM BIT(2)
228 #define REG_FMR1_T_CRC BIT(3) /* Enable CRC6 */
229 #define REG_FMR1_E_XFS BIT(3) /* Transmit Framing Select */
230 #define REG_FMR1_PMOD BIT(4) /* E1 = 0, T1/J1 = 1 */
231 #define REG_FMR1_EDL BIT(5)
232 #define REG_FMR1_AFR BIT(6)
234 #define REG_FMR2 0x1E
235 #define REG_FMR2_E_ALMF BIT(0) /* Automatic Loss of Multiframe */
236 #define REG_FMR2_T_EXZE BIT(0) /* Excessive Zeros Detection Enable */
237 #define REG_FMR2_E_AXRA BIT(1) /* Automatic Transmit Remote Alarm */
238 #define REG_FMR2_T_AXRA BIT(1) /* Automatic Transmit Remote Alarm */
239 #define REG_FMR2_E_PLB BIT(2) /* Payload Loop-Back */
240 #define REG_FMR2_E_RFS0 BIT(6) /* Receive Framing Select - LSB */
241 #define REG_FMR2_E_RFS1 BIT(7) /* Receive Framing Select - MSB */
242 /* Select Synchronization/Resynchronization Procedure */
243 #define REG_FMR2_T_SSP BIT(5)
244 /* Multiple Candidates Synchronization Procedure */
245 #define REG_FMR2_T_MCSP BIT(6)
246 /* Automatic Force Resynchronization */
247 #define REG_FMR2_T_AFRS BIT(7)
249 #define REG_FMR3 0x31
250 #define REG_FMR3_EXTIW BIT(0) /* Extended CRC4 to Non-CRC4 Interworking */
252 #define REG_FMR4 0x20
253 #define REG_FMR4_FM0 BIT(0)
254 #define REG_FMR4_FM1 BIT(1)
255 #define REG_FMR4_AUTO BIT(2)
256 #define REG_FMR4_SSC0 BIT(3)
257 #define REG_FMR4_SSC1 BIT(4)
258 #define REG_FMR4_XRA BIT(5) /* Transmit Remote Alarm (Yellow Alarm) */
259 #define REG_FMR4_TM BIT(6)
260 #define REG_FMR4_AIS3 BIT(7)
262 #define REG_XSW_E 0x20
263 #define REG_XSW_E_XY4 BIT(0)
264 #define REG_XSW_E_XY3 BIT(1)
265 #define REG_XSW_E_XY2 BIT(2)
266 #define REG_XSW_E_XY1 BIT(3)
267 #define REG_XSW_E_XY0 BIT(4)
268 #define REG_XSW_E_XRA BIT(5) /* Transmit Remote Alarm (Yellow Alarm) */
269 #define REG_XSW_E_XTM BIT(6)
270 #define REG_XSW_E_XSIS BIT(7)
272 #define REG_XSP_E 0x21
273 #define REG_FMR5_T 0x21
274 /* Transmit Spare Bit For International Use (FAS Word) */
275 #define REG_XSP_E_XSIF BIT(2)
276 #define REG_FMR5_T_XTM BIT(2) /* Transmit Transparent Mode */
277 /* Automatic Transmission of Submultiframe Status */
278 #define REG_XSP_E_AXS BIT(3)
279 /* E-Bit Polarity, Si-bit position of every outgoing CRC multiframe */
280 #define REG_XSP_E_EBP BIT(4)
281 #define REG_XSP_E_CASEN BIT(6) /* CAS: Channel Associated Signaling Enable */
282 #define REG_FMR5_T_EIBR BIT(6) /* CAS: Enable Internal Bit Robbing Access */
284 #define REG_XC0_T 0x22 /* Transmit Control 0 */
285 #define REG_XC0_BRIF BIT(5) /* Bit Robbing Idle Function */
287 #define REG_CMDR_E 0x02 /* Command Register */
288 #define REG_CMDR_RRES BIT(6) /* Receiver reset */
289 #define REG_CMDR_XRES BIT(4) /* Transmitter reset */
292 #define REG_RC0_SJR BIT(7) /* T1 = 0, J1 = 1 */
294 #define REG_CMR1 0x44
295 #define REG_CMR1_DRSS (BIT(7) | BIT(6))
296 #define REG_CMR1_RS (BIT(5) | BIT(4))
297 #define REG_CMR1_STF BIT(2)
299 #define REG_RS1_E 0x70 /* Receive CAS Register 1 */
300 #define REG_RS2_E 0x71 /* Receive CAS Register 2 */
301 #define REG_RS3_E 0x72 /* Receive CAS Register 3 */
302 #define REG_RS4_E 0x73 /* Receive CAS Register 4 */
303 #define REG_RS5_E 0x74 /* Receive CAS Register 5 */
304 #define REG_RS6_E 0x75 /* Receive CAS Register 6 */
305 #define REG_RS7_E 0x76 /* Receive CAS Register 7 */
306 #define REG_RS8_E 0x77 /* Receive CAS Register 8 */
307 #define REG_RS9_E 0x78 /* Receive CAS Register 9 */
308 #define REG_RS10_E 0x79 /* Receive CAS Register 10 */
309 #define REG_RS11_E 0x7A /* Receive CAS Register 11 */
310 #define REG_RS12_E 0x7B /* Receive CAS Register 12 */
311 #define REG_RS13_E 0x7C /* Receive CAS Register 13 */
312 #define REG_RS14_E 0x7D /* Receive CAS Register 14 */
313 #define REG_RS15_E 0x7E /* Receive CAS Register 15 */
314 #define REG_RS16_E 0x7F /* Receive CAS Register 16 */
316 #define REG_PC2 0x81 /* Port Configuration 2 */
317 #define REG_PC3 0x82 /* Port Configuration 3 */
318 #define REG_PC4 0x83 /* Port Configuration 4 */
320 #define REG_XPM2 0x28 /* Transmit Pulse Mask 2 */
322 #define VAL_PC_SYPR 0x00 /* Synchronous Pulse Receive (Input, low active) */
323 #define VAL_PC_GPI 0x90 /* General purpose input */
324 #define VAL_PC_GPOH 0x0A /* General Purpose Output, high level */
325 #define VAL_PC_GPOL 0x0B /* General Purpose Output, low level */
327 #define NUM_CAS_RS_E (REG_RS16_E - REG_RS2_E + 1)
328 /* and of those, the ones used in T1: */
329 #define NUM_CAS_RS_T (REG_RS12_E - REG_RS1_E + 1)
331 struct PRI_priv_data {
333 enum pri_protocol pri_protocol;
336 unsigned int dchan_num;
341 unsigned int chanconfig_dchan;
343 #define DCHAN(p) ((p)->chanconfig_dchan)
344 #define VALID_DCHAN(p) (DCHAN(p) != NO_DCHAN)
345 #define SET_DCHAN(p, d) do { DCHAN(p) = (d); } while (0);
347 __u8 cas_rs_e[NUM_CAS_RS_E];
348 __u8 cas_ts_e[NUM_CAS_RS_E];
358 __u8 dchan_tx_sample;
359 __u8 dchan_rx_sample;
360 uint dchan_tx_counter;
361 uint dchan_rx_counter;
363 uint dchan_alive_ticks;
364 enum pri_led_state ledstate[NUM_LEDS];
367 static xproto_table_t PROTO_TABLE(PRI);
369 DEF_RPACKET_DATA(PRI, SET_LED, /* Set one of the LED's */
370 struct pri_leds pri_leds;);
372 static /* 0x33 */ DECLARE_CMD(PRI, SET_LED, enum pri_led_selectors led_sel,
373 enum pri_led_state to_led_state);
375 #define DO_LED(xpd, which, tostate) \
376 CALL_PROTO(PRI, SET_LED, (xpd)->xbus, (xpd), (which), (tostate))
378 /*---------------- PRI: Methods -------------------------------------------*/
380 static int query_subunit(xpd_t *xpd, __u8 regnum)
382 XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X\n", xpd->addr.unit,
383 xpd->addr.subunit, regnum);
384 return xpp_register_request(xpd->xbus, xpd, PRI_PORT(xpd), /* portno */
386 regnum, 0, /* do_subreg */
391 0, /* should_reply */
396 static int write_subunit(xpd_t *xpd, __u8 regnum, __u8 val)
398 XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X dataL=0x%02X\n", xpd->addr.unit,
399 xpd->addr.subunit, regnum, val);
400 return xpp_register_request(xpd->xbus, xpd,
401 PRI_PORT(xpd), /* portno */
403 regnum, 0, /* do_subreg */
408 0, /* should_reply */
413 static int pri_write_reg(xpd_t *xpd, int regnum, __u8 val)
415 XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X dataL=0x%02X\n", xpd->addr.unit,
416 xpd->addr.subunit, regnum, val);
417 return xpp_register_request(xpd->xbus, xpd, 0, /* portno=0 */
419 regnum, 0, /* do_subreg */
424 0, /* should_reply */
429 static int cas_regbase(xpd_t *xpd)
431 struct PRI_priv_data *priv;
434 switch (priv->pri_protocol) {
449 static int cas_numregs(xpd_t *xpd)
451 struct PRI_priv_data *priv;
454 switch (priv->pri_protocol) {
469 static int write_cas_reg(xpd_t *xpd, int rsnum, __u8 val)
471 struct PRI_priv_data *priv;
472 int regbase = cas_regbase(xpd);
473 int num_cas_rs = cas_numregs(xpd);
479 if ((priv->pri_protocol == PRI_PROTO_T1) && !priv->is_esf) {
480 /* same data should be copied to RS7..12 in D4 only */
483 if (rsnum < 0 || rsnum >= num_cas_rs) {
484 XPD_ERR(xpd, "RBS(TX): rsnum=%d\n", rsnum);
487 regnum = regbase + rsnum;
488 priv->cas_ts_e[rsnum] = val;
489 XPD_DBG(SIGNAL, xpd, "RBS(TX): reg=0x%X val=0x%02X\n", regnum, val);
490 write_subunit(xpd, regbase + rsnum, val);
492 /* same data should be copied to RS7..12 in D4 only */
493 regnum = REG_RS7_E + rsnum;
494 XPD_DBG(SIGNAL, xpd, "RBS(TX): reg=0x%X val=0x%02X\n", regnum,
496 write_subunit(xpd, regnum, val);
501 static bool valid_pri_modes(const xpd_t *xpd)
503 struct PRI_priv_data *priv;
507 if (priv->pri_protocol != PRI_PROTO_E1
508 && priv->pri_protocol != PRI_PROTO_T1
509 && priv->pri_protocol != PRI_PROTO_J1)
514 static void PRI_card_pcm_recompute(xpd_t *xpd, xpp_line_t pcm_mask)
516 struct PRI_priv_data *priv;
524 spin_lock_irqsave(&PHONEDEV(xpd).lock_recompute_pcm, flags);
525 //XPD_DBG(SIGNAL, xpd, "pcm_mask=0x%X\n", pcm_mask);
526 /* Add/remove all the trivial cases */
527 pcm_mask |= PHONEDEV(xpd).offhook_state;
529 pcm_mask |= BITMASK(PHONEDEV(xpd).channels);
530 for_each_line(xpd, i)
531 if (IS_SET(pcm_mask, i))
535 if (priv->pri_protocol == PRI_PROTO_E1) {
536 /* CAS: Don't send PCM to D-Channel */
538 pcm_mask &= ~BIT(PRI_DCHAN_IDX(priv));
542 * FIXME: Workaround a bug in sync code of the Astribank.
543 * Send dummy PCM for sync.
545 if (xpd->addr.unit == 0 && pcm_mask == 0) {
549 pcm_len = (line_count)
550 ? RPACKET_HEADERSIZE + sizeof(xpp_line_t) +
551 line_count * DAHDI_CHUNKSIZE : 0L;
552 update_wanted_pcm_mask(xpd, pcm_mask, pcm_len);
553 spin_unlock_irqrestore(&PHONEDEV(xpd).lock_recompute_pcm, flags);
558 * May only be called on unregistered xpd's
559 * (the span and channel description are set according to this)
561 static int set_pri_proto(xpd_t *xpd, enum pri_protocol set_proto)
563 struct PRI_priv_data *priv;
565 unsigned int dchan_num;
566 int default_lineconfig = 0;
568 struct phonedev *phonedev;
572 phonedev = &PHONEDEV(xpd);
573 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &phonedev->span.flags)) {
574 XPD_NOTICE(xpd, "%s: %s already assigned as span %d\n",
575 __func__, phonedev->span.name,
576 phonedev->span.spanno);
579 if (priv->pri_protocol != PRI_PROTO_0) {
580 if (priv->pri_protocol == set_proto) {
581 XPD_NOTICE(xpd, "Already in protocol %s. Ignored\n",
582 pri_protocol_name(set_proto));
585 XPD_INFO(xpd, "Switching from %s to %s\n",
586 pri_protocol_name(priv->pri_protocol),
587 pri_protocol_name(set_proto));
592 deflaw = DAHDI_LAW_ALAW;
595 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4 | DAHDI_CONFIG_HDB3;
598 deflaw = DAHDI_LAW_MULAW;
600 default_lineconfig = DAHDI_CONFIG_ESF | DAHDI_CONFIG_B8ZS;
604 * Check all assumptions
606 deflaw = DAHDI_LAW_MULAW;
608 default_lineconfig = 0; /* FIXME: J1??? */
609 XPD_NOTICE(xpd, "J1 is not supported yet\n");
612 XPD_ERR(xpd, "%s: Unknown pri protocol = %d\n", __func__,
616 priv->pri_protocol = set_proto;
618 phonedev_alloc_channels(xpd, pri_num_channels(set_proto));
619 phonedev->offhook_state = BITMASK(phonedev->channels);
620 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
621 priv->deflaw = deflaw;
622 priv->dchan_num = dchan_num;
623 priv->local_loopback = 0;
624 xpd->type_name = type_name(priv->pri_protocol);
625 XPD_DBG(GENERAL, xpd, "%s, channels=%d, dchan_num=%d, deflaw=%d\n",
626 pri_protocol_name(set_proto), phonedev->channels,
627 priv->dchan_num, priv->deflaw);
629 * Must set default now, so layer1 polling (Register REG_FRS0) would
630 * give reliable results.
632 ret = pri_lineconfig(xpd, default_lineconfig);
634 XPD_NOTICE(xpd, "Failed setting PRI default line config\n");
637 return apply_pri_protocol(xpd);
640 static void dahdi_update_syncsrc(xpd_t *xpd)
642 struct PRI_priv_data *priv;
647 if (!SPAN_REGISTERED(xpd))
649 for (i = 0; i < MAX_SLAVES; i++) {
650 subxpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, i);
654 if (priv->clock_source && priv->alarms == 0) {
657 "Duplicate XPD with clock_source=1\n");
658 best_spanno = PHONEDEV(subxpd).span.spanno;
661 for (i = 0; i < MAX_SLAVES; i++) {
662 subxpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, i);
665 if (PHONEDEV(subxpd).span.syncsrc == best_spanno)
666 XPD_DBG(SYNC, xpd, "Setting SyncSource to span %d\n",
669 XPD_DBG(SYNC, xpd, "Slaving to span %d\n", best_spanno);
670 PHONEDEV(subxpd).span.syncsrc = best_spanno;
676 * - set_master_mode() --
677 * As a result of dahdi_cfg
678 * - layer1_state() --
679 * As a result of an alarm.
681 static void set_clocking(xpd_t *xpd)
684 xpd_t *best_xpd = NULL;
685 int best_subunit = -1; /* invalid */
686 unsigned int best_subunit_prio = INT_MAX;
690 /* Find subunit with best timing priority */
691 for (i = 0; i < MAX_SLAVES; i++) {
692 struct PRI_priv_data *priv;
695 subxpd = xpd_byaddr(xbus, xpd->addr.unit, i);
699 if (priv->alarms != 0)
701 if (PHONEDEV(subxpd).timing_priority > 0
702 && PHONEDEV(subxpd).timing_priority < best_subunit_prio) {
705 best_subunit_prio = PHONEDEV(subxpd).timing_priority;
710 && ((struct PRI_priv_data *)(best_xpd->priv))->clock_source == 0) {
711 __u8 reg_pc_init[] = { VAL_PC_SYPR, VAL_PC_GPI, VAL_PC_GPI };
713 for (i = 0; i < ARRAY_SIZE(reg_pc_init); i++) {
714 __u8 reg_pc = reg_pc_init[i];
717 (best_subunit & (1 << i)) ? VAL_PC_GPOH :
719 XPD_DBG(SYNC, best_xpd,
720 "ClockSource Set: PC%d=0x%02X\n", 2 + i,
722 pri_write_reg(xpd, REG_PC2 + i, reg_pc);
724 ((struct PRI_priv_data *)(best_xpd->priv))->clock_source = 1;
726 /* clear old clock sources */
727 for (i = 0; i < MAX_SLAVES; i++) {
728 struct PRI_priv_data *priv;
731 subxpd = xpd_byaddr(xbus, xpd->addr.unit, i);
732 if (subxpd && subxpd != best_xpd) {
733 XPD_DBG(SYNC, subxpd, "Clearing clock source\n");
735 priv->clock_source = 0;
738 dahdi_update_syncsrc(xpd);
741 static void set_reg_lim0(const char *msg, xpd_t *xpd)
743 struct PRI_priv_data *priv;
751 is_master_mode = PHONEDEV(xpd).timing_priority == 0;
752 localloop = priv->local_loopback;
753 lim0 |= (localloop) ? REG_LIM0_LL : 0;
755 lim0 |= REG_LIM0_MAS;
757 lim0 &= ~REG_LIM0_MAS;
758 XPD_DBG(SIGNAL, xpd, "%s(%s): %s, %s\n", __func__, msg,
759 (is_master_mode) ? "MASTER" : "SLAVE",
760 (localloop) ? "LOCALLOOP" : "NO_LOCALLOOP");
761 write_subunit(xpd, REG_LIM0, lim0);
765 * Normally set by the timing parameter in /etc/dahdi/system.conf
766 * If this is called by dahdi_cfg, than it's too late to change
767 * dahdi sync priority (we are already registered)
769 * Also called from set_localloop()
771 static int set_master_mode(const char *msg, xpd_t *xpd)
774 XPD_DBG(SIGNAL, xpd, "\n");
775 set_reg_lim0(__func__, xpd);
780 static int set_localloop(xpd_t *xpd, bool localloop)
782 struct PRI_priv_data *priv;
786 if (SPAN_REGISTERED(xpd)) {
787 XPD_NOTICE(xpd, "Registered as span %d. Cannot do %s\n",
788 PHONEDEV(xpd).span.spanno, __func__);
791 priv->local_loopback = localloop;
792 XPD_DBG(SIGNAL, xpd, "%s: %s\n", __func__,
793 (localloop) ? "LOCALLOOP" : "NO");
794 set_master_mode(__func__, xpd);
798 #define VALID_CONFIG(bit, flg, str) [bit] = { .flags = flg, .name = str }
800 static const struct {
803 } valid_spanconfigs[sizeof(unsigned int) * 8] = {
804 /* These apply to T1 */
805 VALID_CONFIG(4, DAHDI_CONFIG_D4, "D4"),
806 VALID_CONFIG(5, DAHDI_CONFIG_ESF, "ESF"),
807 VALID_CONFIG(6, DAHDI_CONFIG_AMI, "AMI"),
808 VALID_CONFIG(7, DAHDI_CONFIG_B8ZS, "B8ZS"),
809 /* These apply to E1 */
810 VALID_CONFIG(8, DAHDI_CONFIG_CCS, "CCS"),
811 VALID_CONFIG(9, DAHDI_CONFIG_HDB3, "HDB3"),
812 VALID_CONFIG(10, DAHDI_CONFIG_CRC4, "CRC4"),
816 * Mark the lines as CLEAR or RBS signalling.
817 * With T1, we need to mark the CLEAR lines on the REG_CCB1_T registers
818 * Should be called only when we are registered to DAHDI
819 * The channo parameter:
820 * channo == 0: set lines for the whole span
821 * channo != 0: only set modified lines
823 static void set_rbslines(xpd_t *xpd, int channo)
825 struct PRI_priv_data *priv;
826 xpp_line_t new_rbslines = 0;
827 xpp_line_t modified_lines;
831 for_each_line(xpd, i) {
832 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
834 if (chan->flags & DAHDI_FLAG_CLEAR)
835 BIT_CLR(new_rbslines, i);
837 BIT_SET(new_rbslines, i);
839 new_rbslines &= BITMASK(PHONEDEV(xpd).channels);
840 modified_lines = priv->rbslines ^ new_rbslines;
841 XPD_DBG(DEVICES, xpd, "RBSLINES-%d(%s): 0x%X\n", channo,
842 pri_protocol_name(priv->pri_protocol), new_rbslines);
843 if ((priv->pri_protocol == PRI_PROTO_T1)
844 || (priv->pri_protocol == PRI_PROTO_J1)) {
845 __u8 clear_lines = 0; /* Mark clear lines */
846 bool reg_changed = 0;
848 for_each_line(xpd, i) {
852 if (!IS_SET(new_rbslines, i))
853 BIT_SET(clear_lines, (7 - bitnum));
854 if (IS_SET(modified_lines, i))
857 if (channo == 0 || reg_changed) {
858 bytenum += REG_CCB1_T;
859 XPD_DBG(DEVICES, xpd,
860 "RBS(%s): modified=0x%X rbslines=0x%X reg=0x%X clear_lines=0x%X\n",
861 pri_protocol_name(priv->pri_protocol),
862 modified_lines, new_rbslines,
863 bytenum, clear_lines);
864 write_subunit(xpd, bytenum, clear_lines);
871 priv->rbslines = new_rbslines;
874 static int set_mode_cas(xpd_t *xpd, bool want_cas)
876 struct PRI_priv_data *priv;
879 XPD_INFO(xpd, "Setting TDM to %s\n", (want_cas) ? "CAS" : "PRI");
882 priv->dchan_alive = 0;
889 static int pri_lineconfig(xpd_t *xpd, int lineconfig)
891 struct PRI_priv_data *priv;
892 const char *framingstr = "";
893 const char *codingstr = "";
894 const char *crcstr = "";
895 #ifdef JAPANEZE_SUPPORT
896 __u8 rc0 = 0; /* FIXME: PCM offsets */
899 __u8 fmr1 = REG_FMR1_ECM;
901 __u8 fmr3 = 0; /* write only for CRC4 */
903 __u8 cmdr = REG_CMDR_RRES | REG_CMDR_XRES;
905 unsigned int bad_bits;
914 bad_bits = lineconfig & pri_linecompat(priv->pri_protocol);
915 bad_bits = bad_bits ^ lineconfig;
916 for (i = 0; i < ARRAY_SIZE(valid_spanconfigs); i++) {
917 unsigned int flags = valid_spanconfigs[i].flags;
919 if (bad_bits & BIT(i)) {
922 "Bad config item '%s' for %s. Ignore\n",
923 valid_spanconfigs[i].name,
924 pri_protocol_name(priv->pri_protocol));
926 /* we got real garbage */
928 "Unknown config item 0x%lX for %s. "
931 pri_protocol_name(priv->pri_protocol));
934 if (flags && flags != BIT(i)) {
935 ERR("%s: BUG: i=%d flags=0x%X\n", __func__, i, flags);
941 if (priv->pri_protocol == PRI_PROTO_E1) {
942 fmr1 |= REG_FMR1_AFR;
943 fmr2 = REG_FMR2_E_AXRA | REG_FMR2_E_ALMF; /* 0x03 */
944 fmr4 = 0x9F; /* E1.XSW: All spare bits = 1 */
945 xsp |= REG_XSP_E_EBP | REG_XSP_E_AXS | REG_XSP_E_XSIF;
946 } else if (priv->pri_protocol == PRI_PROTO_T1) {
947 fmr1 |= REG_FMR1_PMOD | REG_FMR1_T_CRC;
948 fmr2 = REG_FMR2_T_SSP | REG_FMR2_T_AXRA; /* 0x22 */
950 xsp &= ~REG_FMR5_T_XTM;
951 force_cas = 1; /* T1 - Chip always in CAS mode */
952 } else if (priv->pri_protocol == PRI_PROTO_J1) {
953 fmr1 |= REG_FMR1_PMOD;
955 xsp &= ~REG_FMR5_T_XTM;
956 force_cas = 1; /* T1 - Chip always in CAS mode */
957 XPD_ERR(xpd, "J1 unsupported yet\n");
960 if (priv->local_loopback)
961 fmr2 |= REG_FMR2_E_PLB;
963 if (lineconfig & DAHDI_CONFIG_B8ZS) {
966 REG_FMR0_E_XC1 | REG_FMR0_E_XC0 | REG_FMR0_E_RC1 |
968 } else if (lineconfig & DAHDI_CONFIG_AMI) {
970 fmr0 = REG_FMR0_E_XC1 | REG_FMR0_E_RC1;
972 * From Infineon Errata Sheet: PEF 22554, Version 3.1
973 * Problem: Incorrect CAS Receiption when
974 * using AMI receive line code
975 * Workaround: For E1,
976 * "...The receive line coding HDB3 is
977 * recommended instead."
979 * "...in T1 mode it is recommended to
980 * configure the Rx side to B8ZS coding"
981 * For both cases this is the same bit in FMR0
983 if (priv->pri_protocol == PRI_PROTO_J1)
984 XPD_NOTICE(xpd, "J1 is not supported yet\n");
986 fmr0 |= REG_FMR0_E_RC0;
987 } else if (lineconfig & DAHDI_CONFIG_HDB3) {
990 REG_FMR0_E_XC1 | REG_FMR0_E_XC0 | REG_FMR0_E_RC1 |
994 "Bad lineconfig. Not (B8ZS|AMI|HDB3). Ignored.\n");
999 if (lineconfig & DAHDI_CONFIG_ESF) {
1001 fmr4 |= REG_FMR4_FM1;
1002 fmr2 |= REG_FMR2_T_AXRA | REG_FMR2_T_MCSP | REG_FMR2_T_SSP;
1004 } else if (lineconfig & DAHDI_CONFIG_D4) {
1006 } else if (lineconfig & DAHDI_CONFIG_CCS) {
1008 /* In E1 we know right from the span statement. */
1009 set_mode_cas(xpd, 0);
1011 /* In E1 we know right from the span statement. */
1014 set_mode_cas(xpd, 1);
1016 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
1018 * E1's can enable CRC checking
1019 * CRC4 is legal only for E1, and it is checked by pri_linecompat()
1020 * in the beginning of the function.
1022 if (lineconfig & DAHDI_CONFIG_CRC4) {
1024 fmr1 |= REG_FMR1_E_XFS;
1025 fmr2 |= REG_FMR2_E_RFS1;
1026 fmr3 |= REG_FMR3_EXTIW;
1028 XPD_DBG(GENERAL, xpd, "[%s] lineconfig=%s/%s/%s %s (0x%X)\n",
1029 (priv->clock_source) ? "MASTER" : "SLAVE", framingstr,
1031 (lineconfig & DAHDI_CONFIG_NOTOPEN) ? "YELLOW" : "",
1033 set_reg_lim0(__func__, xpd);
1034 XPD_DBG(GENERAL, xpd, "%s: fmr1(0x%02X) = 0x%02X\n", __func__, REG_FMR1,
1036 write_subunit(xpd, REG_FMR1, fmr1);
1037 XPD_DBG(GENERAL, xpd, "%s: fmr2(0x%02X) = 0x%02X\n", __func__, REG_FMR2,
1039 write_subunit(xpd, REG_FMR2, fmr2);
1040 XPD_DBG(GENERAL, xpd, "%s: fmr0(0x%02X) = 0x%02X\n", __func__, REG_FMR0,
1042 write_subunit(xpd, REG_FMR0, fmr0);
1043 XPD_DBG(GENERAL, xpd, "%s: fmr4(0x%02X) = 0x%02X\n", __func__, REG_FMR4,
1045 write_subunit(xpd, REG_FMR4, fmr4);
1047 XPD_DBG(GENERAL, xpd, "%s: fmr3(0x%02X) = 0x%02X\n", __func__,
1049 write_subunit(xpd, REG_FMR3, fmr3);
1051 XPD_DBG(GENERAL, xpd, "%s: cmdr(0x%02X) = 0x%02X\n", __func__,
1053 write_subunit(xpd, REG_CMDR_E, cmdr);
1054 #ifdef JAPANEZE_SUPPORT
1056 XPD_DBG(GENERAL, xpd, "%s: rc0(0x%02X) = 0x%02X\n", __func__,
1058 write_subunit(xpd, REG_RC0, rc0);
1062 if (priv->pri_protocol == PRI_PROTO_E1) {
1066 * Set correct X1-X3 bits in the E1 CAS MFAS
1067 * They are unused in E1 and should be 1
1069 XPD_DBG(GENERAL, xpd, "%s: rs1(0x%02X) = 0x%02X\n",
1070 __func__, REG_RS1_E, rs1);
1071 write_subunit(xpd, REG_RS1_E, rs1);
1073 xsp |= REG_XSP_E_CASEN; /* Same as REG_FMR5_T_EIBR for T1 */
1075 XPD_DBG(GENERAL, xpd, "%s: xsp(0x%02X) = 0x%02X\n", __func__, REG_XSP_E,
1077 write_subunit(xpd, REG_XSP_E, xsp);
1080 XPD_ERR(xpd, "Bad lineconfig. Abort\n");
1084 static int pri_set_spantype(struct dahdi_span *span, enum spantypes spantype)
1086 struct phonedev *phonedev = container_of(span, struct phonedev, span);
1087 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1088 enum pri_protocol set_proto = PRI_PROTO_0;
1091 XPD_INFO(xpd, "%s: %s\n", __func__, dahdi_spantype2str(spantype));
1093 case SPANTYPE_DIGITAL_E1:
1094 set_proto = PRI_PROTO_E1;
1096 case SPANTYPE_DIGITAL_T1:
1097 set_proto = PRI_PROTO_T1;
1099 case SPANTYPE_DIGITAL_J1:
1100 set_proto = PRI_PROTO_J1;
1103 XPD_NOTICE(xpd, "%s: bad spantype '%s'\n",
1104 __func__, dahdi_spantype2str(spantype));
1107 ret = set_pri_proto(xpd, set_proto);
1109 XPD_ERR(xpd, "%s: set_pri_proto failed\n", __func__);
1112 dahdi_init_span(span);
1116 static int PRI_card_open(xpd_t *xpd, lineno_t pos)
1118 struct PRI_priv_data *priv;
1122 * DAHDI without AUDIO_NOTIFY.
1123 * Need to offhook all channels when D-Chan is up
1126 d = PRI_DCHAN_IDX(priv);
1129 #ifndef DAHDI_AUDIO_NOTIFY
1132 LINE_DBG(SIGNAL, xpd, pos, "OFFHOOK the whole span\n");
1133 for_each_line(xpd, i) {
1135 BIT_SET(PHONEDEV(xpd).offhook_state, i);
1137 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
1139 priv->dchan_is_open = 1;
1144 static int PRI_card_close(xpd_t *xpd, lineno_t pos)
1146 struct PRI_priv_data *priv;
1150 d = PRI_DCHAN_IDX(priv);
1153 LINE_DBG(SIGNAL, xpd, pos, "OFFHOOK the whole span\n");
1154 for_each_line(xpd, i) {
1156 BIT_CLR(PHONEDEV(xpd).offhook_state, i);
1158 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
1159 dchan_state(xpd, 0);
1160 priv->dchan_is_open = 0;
1161 } else if (!priv->dchan_is_open)
1162 mark_offhook(xpd, pos, 0); /* e.g: patgen/pattest */
1167 * Called only for 'span' keyword in /etc/dahdi/system.conf
1170 static int pri_spanconfig(struct file *file, struct dahdi_span *span,
1171 struct dahdi_lineconfig *lc)
1173 struct phonedev *phonedev = container_of(span, struct phonedev, span);
1174 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1175 struct PRI_priv_data *priv;
1180 if (lc->span != PHONEDEV(xpd).span.spanno) {
1181 XPD_ERR(xpd, "I am span %d but got spanconfig for span %d\n",
1182 PHONEDEV(xpd).span.spanno, lc->span);
1186 * FIXME: lc->name is unused by dahdi_cfg and dahdi...
1187 * We currently ignore it also.
1189 XPD_DBG(GENERAL, xpd, "[%s] lbo=%d lineconfig=0x%X sync=%d\n",
1190 (priv->clock_source) ? "MASTER" : "SLAVE", lc->lbo,
1191 lc->lineconfig, lc->sync);
1192 ret = pri_lineconfig(xpd, lc->lineconfig);
1194 span->lineconfig = lc->lineconfig;
1195 PHONEDEV(xpd).timing_priority = lc->sync;
1196 set_master_mode("spanconfig", xpd);
1197 elect_syncer("PRI-master_mode");
1203 * Set signalling type (if appropriate)
1204 * Called from dahdi with spinlock held on chan. Must not call back
1207 static int pri_chanconfig(struct file *file, struct dahdi_chan *chan,
1210 struct phonedev *phonedev =
1211 container_of(chan->span, struct phonedev, span);
1212 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1213 struct PRI_priv_data *priv;
1217 DBG(GENERAL, "channel %d (%s) -> %s\n", chan->channo, chan->name,
1220 * Some bookkeeping to check if we have DChan defined or not
1221 * FIXME: actually use this to prevent duplicate DChan definitions
1222 * and prevent DChan definitions with CAS.
1224 if (is_sigtype_dchan(sigtype)) {
1225 if (VALID_DCHAN(priv) && DCHAN(priv) != chan->channo) {
1226 ERR("channel %d (%s) marked DChan but "
1227 "also channel %d.\n",
1228 chan->channo, chan->name, DCHAN(priv));
1231 XPD_DBG(GENERAL, xpd, "channel %d (%s) marked as DChan\n",
1232 chan->channo, chan->name);
1233 SET_DCHAN(priv, chan->channo);
1234 /* In T1, we don't know before-hand */
1235 if (priv->pri_protocol != PRI_PROTO_E1 && priv->is_cas != 0)
1236 set_mode_cas(xpd, 0);
1238 if (chan->chanpos == 1) {
1239 XPD_DBG(GENERAL, xpd,
1240 "channel %d (%s) marked a not DChan\n",
1241 chan->channo, chan->name);
1242 SET_DCHAN(priv, NO_DCHAN);
1244 /* In T1, we don't know before-hand */
1245 if (priv->pri_protocol != PRI_PROTO_E1 && priv->is_cas != 1)
1246 set_mode_cas(xpd, 1);
1248 if (PHONEDEV(xpd).span.flags & DAHDI_FLAG_RUNNING) {
1249 XPD_DBG(DEVICES, xpd, "Span is RUNNING. Updating rbslines.\n");
1250 set_rbslines(xpd, chan->channo);
1252 // FIXME: sanity checks:
1253 // - should be supported (within the sigcap)
1254 // - should not replace fxs <->fxo ??? (covered by previous?)
1258 static xpd_t *PRI_card_new(xbus_t *xbus, int unit, int subunit,
1259 const xproto_table_t *proto_table,
1260 const struct unit_descriptor *unit_descriptor,
1264 struct PRI_priv_data *priv;
1265 int channels = min(31, CHANNELS_PERXPD); /* worst case */
1267 if ((unit_descriptor->ports_per_chip < 1) ||
1268 (unit_descriptor->ports_per_chip > 4)) {
1269 XBUS_ERR(xbus, "Bad ports_per_chip=%d\n",
1270 unit_descriptor->ports_per_chip);
1273 if (unit_descriptor->numchips != 1) {
1274 XBUS_ERR(xbus, "Bad numchips=%d\n",
1275 unit_descriptor->numchips);
1278 XBUS_DBG(GENERAL, xbus, "\n");
1280 xpd_alloc(xbus, unit, subunit,
1281 sizeof(struct PRI_priv_data), proto_table, unit_descriptor, channels);
1285 /* Default, changes in set_pri_proto() */
1286 priv->pri_protocol = PRI_PROTO_0;
1287 /* Default, changes in set_pri_proto() */
1288 priv->deflaw = DAHDI_LAW_DEFAULT;
1289 xpd->type_name = type_name(priv->pri_protocol);
1290 xbus->sync_mode_default = SYNC_MODE_AB;
1294 static int PRI_card_init(xbus_t *xbus, xpd_t *xpd)
1296 struct PRI_priv_data *priv;
1300 XPD_DBG(GENERAL, xpd, "\n");
1301 xpd->xpd_type = XPD_TYPE_PRI;
1303 if (priv->pri_protocol == PRI_PROTO_0) {
1305 * init_card_* script didn't set pri protocol
1306 * Let's have a default E1
1308 ret = set_pri_proto(xpd, PRI_PROTO_E1);
1312 SET_DCHAN(priv, NO_DCHAN);
1314 * initialization script should have set correct
1317 if (!valid_pri_modes(xpd)) {
1318 XPD_NOTICE(xpd, "PRI protocol not set\n");
1321 xpd->type_name = type_name(priv->pri_protocol);
1322 PHONEDEV(xpd).direction = TO_PSTN;
1323 XPD_DBG(DEVICES, xpd, "%s\n", xpd->type_name);
1324 PHONEDEV(xpd).timing_priority = 1; /* High priority SLAVE */
1325 set_master_mode(__func__, xpd);
1326 for (ret = 0; ret < NUM_LEDS; ret++) {
1327 DO_LED(xpd, ret, PRI_LED_ON);
1329 DO_LED(xpd, ret, PRI_LED_OFF);
1331 priv->initialized = 1;
1332 priv->dchan_is_open = 0;
1335 XPD_ERR(xpd, "Failed initializing registers (%d)\n", ret);
1339 static int PRI_card_remove(xbus_t *xbus, xpd_t *xpd)
1342 XPD_DBG(GENERAL, xpd, "\n");
1346 #ifdef DAHDI_AUDIO_NOTIFY
1347 static int pri_audio_notify(struct dahdi_chan *chan, int on)
1349 xpd_t *xpd = chan->pvt;
1350 int pos = chan->chanpos - 1;
1353 LINE_DBG(SIGNAL, xpd, pos, "PRI-AUDIO: %s\n", (on) ? "on" : "off");
1354 mark_offhook(xpd, pos, on);
1359 static const struct dahdi_span_ops PRI_span_ops = {
1360 .owner = THIS_MODULE,
1361 .set_spantype = pri_set_spantype,
1362 .spanconfig = pri_spanconfig,
1363 .chanconfig = pri_chanconfig,
1364 .startup = pri_startup,
1365 .shutdown = pri_shutdown,
1366 .rbsbits = pri_rbsbits,
1371 .echocan_create = xpp_echocan_create,
1372 .echocan_name = xpp_echocan_name,
1373 .assigned = xpp_span_assigned,
1374 #ifdef DAHDI_SYNC_TICK
1375 .sync_tick = dahdi_sync_tick,
1377 #ifdef CONFIG_DAHDI_WATCHDOG
1378 .watchdog = xpp_watchdog,
1381 #ifdef DAHDI_AUDIO_NOTIFY
1382 .audio_notify = pri_audio_notify,
1386 static int apply_pri_protocol(xpd_t *xpd)
1389 struct PRI_priv_data *priv;
1396 XPD_DBG(GENERAL, xpd, "\n");
1397 PHONEDEV(xpd).span.spantype = pri_protocol2spantype(priv->pri_protocol);
1398 PHONEDEV(xpd).span.linecompat = pri_linecompat(priv->pri_protocol);
1399 PHONEDEV(xpd).span.deflaw = priv->deflaw;
1400 PHONEDEV(xpd).span.alarms = DAHDI_ALARM_NONE;
1401 for_each_line(xpd, i) {
1402 struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
1403 bool is_dchan = i == PRI_DCHAN_IDX(priv);
1405 XPD_DBG(GENERAL, xpd, "setting PRI channel %d (%s)\n", i,
1406 (is_dchan) ? "DCHAN" : "CLEAR");
1407 snprintf(cur_chan->name, MAX_CHANNAME, "XPP_%s/%02d/%1d%1d/%d",
1408 xpd->type_name, xbus->num, xpd->addr.unit,
1409 xpd->addr.subunit, i);
1410 cur_chan->chanpos = i + 1;
1411 cur_chan->pvt = xpd;
1412 cur_chan->sigcap = PRI_SIGCAP;
1413 if (is_dchan && !priv->is_cas) { /* D-CHAN */
1414 //FIXME: cur_chan->flags |= DAHDI_FLAG_PRIDCHAN;
1415 cur_chan->flags &= ~DAHDI_FLAG_HDLC;
1418 PHONEDEV(xpd).offhook_state = PHONEDEV(xpd).wanted_pcm_mask;
1419 PHONEDEV(xpd).span.ops = &PRI_span_ops;
1420 PHONEDEV(xpd).span.channels = PHONEDEV(xpd).channels;
1421 xpd_set_spanname(xpd);
1425 static int PRI_card_dahdi_preregistration(xpd_t *xpd, bool on)
1428 struct PRI_priv_data *priv;
1434 XPD_DBG(GENERAL, xpd, "%s (proto=%s, channels=%d, deflaw=%d)\n",
1435 (on) ? "on" : "off", pri_protocol_name(priv->pri_protocol),
1436 PHONEDEV(xpd).channels, priv->deflaw);
1438 /* Nothing to do yet */
1441 return apply_pri_protocol(xpd);
1444 static int PRI_card_dahdi_postregistration(xpd_t *xpd, bool on)
1451 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
1452 dahdi_update_syncsrc(xpd);
1456 static void dchan_state(xpd_t *xpd, bool up)
1458 struct PRI_priv_data *priv;
1465 if (priv->dchan_alive == up)
1467 if (!priv->layer1_up) /* No layer1, kill dchan */
1470 XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel RUNNING\n");
1471 priv->dchan_alive = 1;
1473 int d = PRI_DCHAN_IDX(priv);
1475 if (SPAN_REGISTERED(xpd) && d >= 0
1476 && d < PHONEDEV(xpd).channels) {
1479 pcm = (__u8 *)XPD_CHAN(xpd, d)->readchunk;
1481 pcm = (__u8 *)XPD_CHAN(xpd, d)->writechunk;
1484 XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel STOPPED\n");
1485 priv->dchan_rx_counter = priv->dchan_tx_counter = 0;
1486 priv->dchan_alive = 0;
1487 priv->dchan_alive_ticks = 0;
1488 priv->dchan_rx_sample = priv->dchan_tx_sample = 0x00;
1493 * LED managment is done by the driver now:
1494 * - Turn constant ON RED/GREEN led to indicate MASTER/SLAVE port
1495 * - Very fast "Double Blink" to indicate Layer1 alive (without D-Channel)
1496 * - Constant blink (1/2 sec cycle) to indicate D-Channel alive.
1498 static void handle_leds(xbus_t *xbus, xpd_t *xpd)
1500 struct PRI_priv_data *priv;
1501 unsigned int timer_count;
1504 enum pri_led_state ledstate;
1510 if (PHONEDEV(xpd).timing_priority == 0) {
1511 which_led = TOP_RED_LED;
1512 other_led = BOTTOM_GREEN_LED;
1514 which_led = BOTTOM_GREEN_LED;
1515 other_led = TOP_RED_LED;
1517 ledstate = priv->ledstate[which_led];
1518 timer_count = xpd->timer_count;
1519 if (xpd->blink_mode) {
1520 if ((timer_count % DEFAULT_LED_PERIOD) == 0) {
1521 // led state is toggled
1522 if (ledstate == PRI_LED_OFF) {
1523 DO_LED(xpd, which_led, PRI_LED_ON);
1524 DO_LED(xpd, other_led, PRI_LED_ON);
1526 DO_LED(xpd, which_led, PRI_LED_OFF);
1527 DO_LED(xpd, other_led, PRI_LED_OFF);
1532 if (priv->ledstate[other_led] != PRI_LED_OFF)
1533 DO_LED(xpd, other_led, PRI_LED_OFF);
1534 if (priv->dchan_alive) {
1535 mod = timer_count % 1000;
1538 DO_LED(xpd, which_led, PRI_LED_ON);
1541 DO_LED(xpd, which_led, PRI_LED_OFF);
1544 } else if (priv->layer1_up) {
1545 mod = timer_count % 1000;
1549 DO_LED(xpd, which_led, PRI_LED_ON);
1553 DO_LED(xpd, which_led, PRI_LED_OFF);
1557 if (priv->ledstate[which_led] != PRI_LED_ON)
1558 DO_LED(xpd, which_led, PRI_LED_ON);
1562 static int PRI_card_tick(xbus_t *xbus, xpd_t *xpd)
1564 struct PRI_priv_data *priv;
1569 if (!priv->initialized || !xbus->self_ticking)
1572 * Poll layer1 status (cascade subunits)
1574 if (poll_interval != 0 && ((xpd->timer_count % poll_interval) == 0)) {
1575 priv->poll_noreplies++;
1576 query_subunit(xpd, REG_FRS0);
1577 //query_subunit(xpd, REG_FRS1);
1579 if (priv->dchan_tx_counter >= 1 && priv->dchan_rx_counter > 1) {
1580 dchan_state(xpd, 1);
1581 priv->dchan_alive_ticks++;
1583 handle_leds(xbus, xpd);
1587 static int PRI_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd,
1590 struct dahdi_chan *chan;
1593 if (!XBUS_IS(xpd->xbus, READY))
1595 chan = XPD_CHAN(xpd, pos);
1598 * Asterisk may send FXS type ioctl()'s to us:
1599 * - Some are sent to everybody (DAHDI_TONEDETECT)
1600 * - Some are sent because we may be in CAS mode
1604 case DAHDI_TONEDETECT:
1605 LINE_DBG(SIGNAL, xpd, pos, "PRI: TONEDETECT (%s)\n",
1607 flags & DAHDI_FLAG_AUDIO) ? "AUDIO" : "SILENCE");
1609 case DAHDI_ONHOOKTRANSFER:
1610 LINE_DBG(SIGNAL, xpd, pos, "PRI: ONHOOKTRANSFER\n");
1613 LINE_DBG(SIGNAL, xpd, pos, "PRI: VMWI\n");
1615 case DAHDI_VMWI_CONFIG:
1616 LINE_DBG(SIGNAL, xpd, pos, "PRI: VMWI_CONFIG\n");
1618 case DAHDI_SETPOLARITY:
1619 LINE_DBG(SIGNAL, xpd, pos, "PRI: SETPOLARITY\n");
1621 /* report on really bad ioctl()'s */
1623 report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
1630 * Called only for 'span' keyword in /etc/dahdi/system.conf
1632 static int pri_startup(struct file *file, struct dahdi_span *span)
1634 struct phonedev *phonedev = container_of(span, struct phonedev, span);
1635 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1636 struct PRI_priv_data *priv;
1641 if (!XBUS_IS(xpd->xbus, READY)) {
1642 XPD_DBG(GENERAL, xpd,
1643 "Startup called by dahdi. No Hardware. Ignored\n");
1646 XPD_DBG(GENERAL, xpd, "STARTUP\n");
1647 // Turn on all channels
1648 CALL_PHONE_METHOD(card_state, xpd, 1);
1649 set_rbslines(xpd, 0);
1650 write_subunit(xpd, REG_XPM2, 0x00);
1655 * Called only for 'span' keyword in /etc/dahdi/system.conf
1657 static int pri_shutdown(struct dahdi_span *span)
1659 struct phonedev *phonedev = container_of(span, struct phonedev, span);
1660 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1661 struct PRI_priv_data *priv;
1666 if (!XBUS_IS(xpd->xbus, READY)) {
1667 XPD_DBG(GENERAL, xpd,
1668 "Shutdown called by dahdi. No Hardware. Ignored\n");
1671 XPD_DBG(GENERAL, xpd, "SHUTDOWN\n");
1672 // Turn off all channels
1673 CALL_PHONE_METHOD(card_state, xpd, 0);
1677 static int encode_rbsbits_e1(xpd_t *xpd, int pos, int bits)
1679 struct PRI_priv_data *priv;
1686 BUG_ON(priv->pri_protocol != PRI_PROTO_E1);
1688 return 0; /* Don't write dchan in CAS */
1689 if (pos < 0 || pos > 31) {
1690 XPD_NOTICE(xpd, "%s: pos=%d out of range. Ignore\n", __func__,
1697 val = (priv->cas_ts_e[rsnum] & 0xF0) | (bits & 0x0F);
1701 val = (priv->cas_ts_e[rsnum] & 0x0F) | ((bits << 4) & 0xF0);
1703 LINE_DBG(SIGNAL, xpd, pos, "RBS: TX: bits=0x%X\n", bits);
1704 write_cas_reg(xpd, rsnum, val);
1708 static int encode_rbsbits_t1(xpd_t *xpd, int pos, int bits)
1710 struct PRI_priv_data *priv;
1715 uint tx_bits = bits;
1722 BUG_ON(priv->pri_protocol != PRI_PROTO_T1);
1723 if (pos < 0 || pos >= PHONEDEV(xpd).channels) {
1724 XPD_ERR(xpd, "%s: Bad pos=%d\n", __func__, pos);
1727 chan_per_reg = CHAN_PER_REGS(priv);
1728 width = 8 / chan_per_reg;
1729 rsnum = pos / chan_per_reg;
1730 offset = pos % chan_per_reg;
1731 mask = BITMASK(width) << (chan_per_reg - offset - 1) * width;
1734 tx_bits &= BITMASK(width);
1735 tx_bits <<= (chan_per_reg - offset - 1) * width;
1736 val = priv->cas_ts_e[rsnum];
1739 LINE_DBG(SIGNAL, xpd, pos,
1740 "bits=0x%02X RS%02d(%s) offset=%d tx_bits=0x%02X\n", bits,
1741 rsnum + 1, (priv->is_esf) ? "esf" : "d4", offset, tx_bits);
1742 write_cas_reg(xpd, rsnum, val);
1743 priv->dchan_tx_counter++;
1747 static void send_idlebits(xpd_t *xpd, bool saveold)
1749 struct PRI_priv_data *priv;
1750 __u8 save_rs[NUM_CAS_RS_E];
1753 if (!SPAN_REGISTERED(xpd))
1757 XPD_DBG(SIGNAL, xpd, "saveold=%d\n", saveold);
1759 memcpy(save_rs, priv->cas_ts_e, sizeof(save_rs));
1760 for_each_line(xpd, i) {
1761 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
1763 pri_rbsbits(chan, chan->idlebits);
1766 memcpy(priv->cas_ts_e, save_rs, sizeof(save_rs));
1769 static void send_oldbits(xpd_t *xpd)
1771 struct PRI_priv_data *priv;
1777 XPD_DBG(SIGNAL, xpd, "\n");
1778 for (i = 0; i < cas_numregs(xpd); i++)
1779 write_cas_reg(xpd, i, priv->cas_ts_e[i]);
1782 static int pri_rbsbits(struct dahdi_chan *chan, int bits)
1785 struct PRI_priv_data *priv;
1790 pos = chan->chanpos - 1;
1793 if (!priv->is_cas) {
1794 XPD_DBG(SIGNAL, xpd, "RBS: TX: not in CAS mode. Ignore.\n");
1797 if (chan->sig == DAHDI_SIG_NONE) {
1798 LINE_DBG(SIGNAL, xpd, pos,
1799 "RBS: TX: sigtyp=%s. , bits=0x%X. Ignore.\n",
1800 sig2str(chan->sig), bits);
1803 if (!priv->layer1_up)
1804 XPD_DBG(SIGNAL, xpd, "RBS: TX: No layer1 yet. Keep going.\n");
1805 if (priv->pri_protocol == PRI_PROTO_E1) {
1806 if (encode_rbsbits_e1(xpd, pos, bits) < 0)
1808 } else if (priv->pri_protocol == PRI_PROTO_T1) {
1809 if (encode_rbsbits_t1(xpd, pos, bits) < 0)
1813 "%s: protocol %s is not supported yet with CAS\n",
1814 __func__, pri_protocol_name(priv->pri_protocol));
1820 /*! Copy PCM chunks from the buffers of the xpd to a new packet
1821 * \param xbus xbus of source xpd.
1822 * \param xpd source xpd.
1823 * \param lines a bitmask of the active channels that need to be copied.
1824 * \param pack packet to be filled.
1826 * On PRI this function is should also shift the lines mask one bit, as
1827 * channel 0 on the wire is an internal chip control channel. We only
1828 * send 31 channels to the device, but they should be called 1-31 rather
1831 static void PRI_card_pcm_fromspan(xpd_t *xpd, xpacket_t *pack)
1833 struct PRI_priv_data *priv;
1835 unsigned long flags;
1837 xpp_line_t wanted_lines;
1839 int physical_mask = 0;
1845 pcm = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, pcm);
1846 spin_lock_irqsave(&xpd->lock, flags);
1847 wanted_lines = PHONEDEV(xpd).wanted_pcm_mask;
1849 for_each_line(xpd, i) {
1850 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
1852 if (priv->pri_protocol == PRI_PROTO_E1) {
1853 /* In E1 - Only 0'th channel is unused */
1856 } else if (priv->pri_protocol == PRI_PROTO_T1) {
1857 /* In T1 - Every 4'th channel is unused */
1861 if (IS_SET(wanted_lines, i)) {
1862 physical_mask |= BIT(physical_chan);
1863 if (SPAN_REGISTERED(xpd)) {
1865 int channo = XPD_CHAN(xpd, i)->channo;
1867 if (pcmtx >= 0 && pcmtx_chan == channo)
1868 memset((u_char *)pcm, pcmtx,
1872 memcpy((u_char *)pcm, chan->writechunk,
1874 if (i == PRI_DCHAN_IDX(priv)) {
1875 if (priv->dchan_tx_sample !=
1876 chan->writechunk[0]) {
1877 priv->dchan_tx_sample =
1878 chan->writechunk[0];
1879 priv->dchan_tx_counter++;
1880 } else if (chan->writechunk[0] == 0xFF)
1881 dchan_state(xpd, 0);
1883 /* Clobber for next tick */
1884 chan->writechunk[0] = 0xFF;
1887 memset((u_char *)pcm, DAHDI_XLAW(0, chan),
1889 pcm += DAHDI_CHUNKSIZE;
1893 RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines) = physical_mask;
1894 XPD_COUNTER(xpd, PCM_WRITE)++;
1895 spin_unlock_irqrestore(&xpd->lock, flags);
1898 /*! Copy PCM chunks from the packet we received to the xpd struct.
1899 * \param xbus xbus of target xpd.
1900 * \param xpd target xpd.
1901 * \param pack Source packet.
1903 * On PRI this function is should also shift the lines back mask one bit, as
1904 * channel 0 on the wire is an internal chip control channel.
1906 * \see PRI_card_pcm_fromspan
1908 static void PRI_card_pcm_tospan(xpd_t *xpd, xpacket_t *pack)
1910 struct PRI_priv_data *priv;
1912 xpp_line_t physical_mask;
1913 unsigned long flags;
1917 if (!SPAN_REGISTERED(xpd))
1921 pcm = RPACKET_FIELD(pack, GLOBAL, PCM_READ, pcm);
1922 physical_mask = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines);
1923 spin_lock_irqsave(&xpd->lock, flags);
1925 for (i = 0; i < CHANNELS_PERXPD; i++) {
1928 if (priv->pri_protocol == PRI_PROTO_E1) {
1929 /* In E1 - Only 0'th channel is unused */
1932 } else if (priv->pri_protocol == PRI_PROTO_T1) {
1933 /* In T1 - Every 4'th channel is unused */
1937 if (logical_chan == PRI_DCHAN_IDX(priv) && !priv->is_cas) {
1938 if (priv->dchan_rx_sample != pcm[0]) {
1939 if (debug & DBG_PCM) {
1941 "RX-D-Chan: prev=0x%X now=0x%X\n",
1942 priv->dchan_rx_sample, pcm[0]);
1943 dump_packet("RX-D-Chan", pack, 1);
1945 priv->dchan_rx_sample = pcm[0];
1946 priv->dchan_rx_counter++;
1947 } else if (pcm[0] == 0xFF)
1948 dchan_state(xpd, 0);
1950 if (IS_SET(physical_mask, i)) {
1951 struct dahdi_chan *chan = XPD_CHAN(xpd, logical_chan);
1952 r = chan->readchunk;
1953 // memset((u_char *)r, 0x5A, DAHDI_CHUNKSIZE); // DEBUG
1954 memcpy((u_char *)r, pcm, DAHDI_CHUNKSIZE);
1955 pcm += DAHDI_CHUNKSIZE;
1959 XPD_COUNTER(xpd, PCM_READ)++;
1960 spin_unlock_irqrestore(&xpd->lock, flags);
1963 static int PRI_timing_priority(xpd_t *xpd)
1965 struct PRI_priv_data *priv;
1969 if (priv->layer1_up)
1970 return PHONEDEV(xpd).timing_priority;
1971 XPD_DBG(SYNC, xpd, "No timing priority (no layer1)\n");
1975 static int PRI_echocancel_timeslot(xpd_t *xpd, int pos)
1978 * Skip ts=0 (used for PRI sync)
1980 return (1 + pos) * 4 + xpd->addr.subunit;
1983 static int PRI_echocancel_setmask(xpd_t *xpd, xpp_line_t ec_mask)
1985 struct PRI_priv_data *priv;
1991 XPD_DBG(GENERAL, xpd, "0x%8X\n", ec_mask);
1992 if (!ECHOOPS(xpd->xbus)) {
1993 XPD_DBG(GENERAL, xpd,
1994 "No echo canceller in XBUS: Doing nothing.\n");
1997 for (i = 0; i < PHONEDEV(xpd).channels; i++) {
1998 int on = BIT(i) & ec_mask;
2000 if (i == PRI_DCHAN_IDX(priv))
2002 CALL_EC_METHOD(ec_set, xpd->xbus, xpd, i, on);
2004 CALL_EC_METHOD(ec_update, xpd->xbus, xpd->xbus);
2008 /*---------------- PRI: HOST COMMANDS -------------------------------------*/
2010 static /* 0x33 */ HOSTCMD(PRI, SET_LED, enum pri_led_selectors led_sel,
2011 enum pri_led_state to_led_state)
2016 struct pri_leds *pri_leds;
2017 struct PRI_priv_data *priv;
2023 XPD_DBG(LEDS, xpd, "led_sel=%d to_state=%d\n", led_sel, to_led_state);
2024 XFRAME_NEW_CMD(xframe, pack, xbus, PRI, SET_LED, xpd->xbus_idx);
2025 pri_leds = &RPACKET_FIELD(pack, PRI, SET_LED, pri_leds);
2026 pri_leds->state = to_led_state;
2027 pri_leds->led_sel = led_sel;
2028 pri_leds->reserved = 0;
2029 XPACKET_LEN(pack) = RPACKET_SIZE(PRI, SET_LED);
2030 ret = send_cmd_frame(xbus, xframe);
2031 priv->ledstate[led_sel] = to_led_state;
2035 /*---------------- PRI: Astribank Reply Handlers --------------------------*/
2036 static void layer1_state(xpd_t *xpd, __u8 data_low)
2038 struct PRI_priv_data *priv;
2039 int alarms = DAHDI_ALARM_NONE;
2045 priv->poll_noreplies = 0;
2046 if (data_low & REG_FRS0_LOS)
2047 alarms |= DAHDI_ALARM_RED;
2048 if (data_low & REG_FRS0_AIS)
2049 alarms |= DAHDI_ALARM_BLUE;
2050 if (data_low & REG_FRS0_RRA)
2051 alarms |= DAHDI_ALARM_YELLOW;
2052 layer1_up_prev = priv->layer1_up;
2053 priv->layer1_up = alarms == DAHDI_ALARM_NONE;
2056 * Some bad bits (e.g: LMFA and NMF have no alarm "colors"
2057 * associated. However, layer1 is still not working if they are set.
2058 * FIXME: These behave differently in E1/T1, so ignore them for while.
2060 if (data_low & (REG_FRS0_LMFA | REG_FRS0_E1_NMF))
2061 priv->layer1_up = 0;
2063 priv->alarms = alarms;
2064 if (!priv->layer1_up) {
2065 dchan_state(xpd, 0);
2066 } else if (priv->is_cas && !layer1_up_prev) {
2067 int regbase = cas_regbase(xpd);
2070 XPD_DBG(SIGNAL, xpd,
2071 "Returning From Alarm Refreshing Rx register data \n");
2072 for (i = 0; i < cas_numregs(xpd); i++)
2073 query_subunit(xpd, regbase + i);
2076 if (SPAN_REGISTERED(xpd) && PHONEDEV(xpd).span.alarms != alarms) {
2077 char str1[MAX_PROC_WRITE];
2078 char str2[MAX_PROC_WRITE];
2080 alarm2str(PHONEDEV(xpd).span.alarms, str1, sizeof(str1));
2081 alarm2str(alarms, str2, sizeof(str2));
2082 XPD_NOTICE(xpd, "Alarms: 0x%X (%s) => 0x%X (%s)\n",
2083 PHONEDEV(xpd).span.alarms, str1, alarms, str2);
2085 if (alarms == DAHDI_ALARM_NONE)
2087 else if (PHONEDEV(xpd).span.alarms == DAHDI_ALARM_NONE)
2088 send_idlebits(xpd, 1);
2090 PHONEDEV(xpd).span.alarms = alarms;
2091 elect_syncer("LAYER1");
2092 dahdi_alarm_notify(&PHONEDEV(xpd).span);
2095 priv->reg_frs0 = data_low;
2096 priv->layer1_replies++;
2097 XPD_DBG(REGS, xpd, "subunit=%d data_low=0x%02X\n", xpd->addr.subunit,
2101 static int decode_cas_e1(xpd_t *xpd, __u8 regnum, __u8 data_low)
2103 struct PRI_priv_data *priv;
2104 uint pos = regnum - REG_RS2_E;
2105 int rsnum = pos + 2;
2107 int chan2 = pos + 16;
2108 int val1 = (data_low >> 4) & 0xF;
2109 int val2 = data_low & 0xF;
2112 BUG_ON(!priv->is_cas);
2113 BUG_ON(priv->pri_protocol != PRI_PROTO_E1);
2114 XPD_DBG(SIGNAL, xpd, "RBS: RX: data_low=0x%02X\n", data_low);
2115 if (pos >= NUM_CAS_RS_E) {
2116 XPD_ERR(xpd, "%s: got bad pos=%d [0-%d]\n", __func__, pos,
2120 if (chan1 < 0 || chan1 > PHONEDEV(xpd).channels) {
2121 XPD_NOTICE(xpd, "%s: %s CAS: Bad chan1 number (%d)\n", __func__,
2122 pri_protocol_name(priv->pri_protocol), chan1);
2125 if (chan2 < 0 || chan2 > PHONEDEV(xpd).channels) {
2126 XPD_NOTICE(xpd, "%s: %s CAS: Bad chan2 number (%d)\n", __func__,
2127 pri_protocol_name(priv->pri_protocol), chan2);
2130 XPD_DBG(SIGNAL, xpd,
2131 "RBS: RX: RS%02d (channel %2d, channel %2d): 0x%02X -> 0x%02X\n",
2132 rsnum, chan1 + 1, chan2 + 1, priv->cas_rs_e[pos], data_low);
2133 if (SPAN_REGISTERED(xpd)) {
2134 dahdi_rbsbits(XPD_CHAN(xpd, chan1), val1);
2135 dahdi_rbsbits(XPD_CHAN(xpd, chan2), val2);
2137 priv->dchan_rx_counter++;
2138 priv->cas_rs_e[pos] = data_low;
2142 static int decode_cas_t1(xpd_t *xpd, __u8 regnum, __u8 data_low)
2144 struct PRI_priv_data *priv;
2151 BUG_ON(!priv->is_cas);
2152 BUG_ON(priv->pri_protocol != PRI_PROTO_T1);
2153 rsnum = regnum - REG_RS1_E;
2155 XPD_ERR(xpd, "Bad rsnum=%d\n", rsnum);
2159 rsnum = rsnum % 6; /* 2 identical banks of 6 registers */
2160 chan_per_reg = CHAN_PER_REGS(priv);
2161 width = 8 / chan_per_reg;
2162 XPD_DBG(SIGNAL, xpd, "RBS: RX(%s,%d): RS%02d data_low=0x%02X\n",
2163 (priv->is_esf) ? "esf" : "d4", chan_per_reg, rsnum + 1,
2165 for (i = 0; i < chan_per_reg; i++) {
2166 uint rxsig = (data_low >> (i * width)) & BITMASK(width);
2168 struct dahdi_chan *chan;
2172 pos = rsnum * chan_per_reg + chan_per_reg - i - 1;
2173 if (pos < 0 || pos >= PHONEDEV(xpd).channels) {
2174 XPD_ERR(xpd, "%s: Bad pos=%d\n", __func__, pos);
2177 chan = XPD_CHAN(xpd, pos);
2179 XPD_ERR(xpd, "%s: Null channel in pos=%d\n", __func__,
2183 if (chan->rxsig != rxsig) {
2184 LINE_DBG(SIGNAL, xpd, pos, "i=%d rxsig=0x%02X\n", i,
2186 dahdi_rbsbits(chan, rxsig);
2189 priv->cas_rs_e[rsnum] = data_low;
2193 static void process_cas_dchan(xpd_t *xpd, __u8 regnum, __u8 data_low)
2195 struct PRI_priv_data *priv;
2198 if (!priv->is_cas) {
2199 static int rate_limit;
2201 if ((rate_limit++ % 10003) == 0)
2202 XPD_NOTICE(xpd, "RBS: RX: not in CAS mode. Ignore.\n");
2205 if (!priv->layer1_up) {
2206 static int rate_limit;
2208 if ((rate_limit++ % 10003) == 0)
2209 XPD_DBG(SIGNAL, xpd, "RBS: RX: No layer1.\n");
2211 if (!SPAN_REGISTERED(xpd)) {
2212 static int rate_limit;
2214 if ((rate_limit++ % 10003) == 0)
2215 XPD_DBG(SIGNAL, xpd,
2216 "RBS: RX: Span not registered. Ignore.\n");
2219 if (priv->pri_protocol == PRI_PROTO_E1) {
2220 if (regnum == REG_RS1_E)
2221 return; /* Time slot 0: Ignored for E1 */
2222 if (regnum < REG_RS2_E) {
2223 /* Should not happen, but harmless. Ignore */
2224 if (regnum == REG_RS1_E)
2228 "%s: received register 0x%X in protocol %s. "
2231 pri_protocol_name(priv->pri_protocol));
2234 if (decode_cas_e1(xpd, regnum, data_low) < 0)
2236 } else if (priv->pri_protocol == PRI_PROTO_T1) {
2237 if (regnum > REG_RS12_E) {
2239 "%s: received register 0x%X in protocol %s. "
2242 pri_protocol_name(priv->pri_protocol));
2245 if (decode_cas_t1(xpd, regnum, data_low) < 0)
2249 "%s: protocol %s is not supported yet with CAS\n",
2250 __func__, pri_protocol_name(priv->pri_protocol));
2252 priv->cas_replies++;
2255 static int PRI_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
2257 unsigned long flags;
2258 struct PRI_priv_data *priv;
2259 struct xpd_addr addr;
2264 /* Map UNIT + PORTNUM to XPD */
2266 addr.unit = orig_xpd->addr.unit;
2267 addr.subunit = info->h.portnum;
2268 regnum = REG_FIELD(info, regnum);
2269 data_low = REG_FIELD(info, data_low);
2270 xpd = xpd_byaddr(xbus, addr.unit, addr.subunit);
2272 static int rate_limit;
2274 if ((rate_limit++ % 1003) < 5)
2275 notify_bad_xpd(__func__, xbus, addr, orig_xpd->xpdname);
2278 spin_lock_irqsave(&xpd->lock, flags);
2281 if (info->h.is_multibyte) {
2282 XPD_NOTICE(xpd, "Got Multibyte: %d bytes, eoframe: %d\n",
2283 info->h.bytes, info->h.eoframe);
2286 if (regnum == REG_FRS0 && !REG_FIELD(info, do_subreg))
2287 layer1_state(xpd, data_low);
2288 else if (regnum == REG_FRS1 && !REG_FIELD(info, do_subreg))
2289 priv->reg_frs1 = data_low;
2290 if (priv->is_cas && !REG_FIELD(info, do_subreg)) {
2291 if (regnum >= REG_RS1_E && regnum <= REG_RS16_E)
2292 process_cas_dchan(xpd, regnum, data_low);
2295 * Update /proc info only if reply relate to the
2296 * last slic read request
2298 if (REG_FIELD(&xpd->requested_reply, regnum) ==
2299 REG_FIELD(info, regnum)
2300 && REG_FIELD(&xpd->requested_reply, do_subreg) ==
2301 REG_FIELD(info, do_subreg)
2302 && REG_FIELD(&xpd->requested_reply, subreg) ==
2303 REG_FIELD(info, subreg)) {
2304 xpd->last_reply = *info;
2308 spin_unlock_irqrestore(&xpd->lock, flags);
2312 static int PRI_card_state(xpd_t *xpd, bool on)
2315 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
2319 static const struct xops pri_xops = {
2320 .card_new = PRI_card_new,
2321 .card_init = PRI_card_init,
2322 .card_remove = PRI_card_remove,
2323 .card_tick = PRI_card_tick,
2324 .card_register_reply = PRI_card_register_reply,
2327 static const struct phoneops pri_phoneops = {
2328 .card_dahdi_preregistration = PRI_card_dahdi_preregistration,
2329 .card_dahdi_postregistration = PRI_card_dahdi_postregistration,
2330 .card_pcm_recompute = PRI_card_pcm_recompute,
2331 .card_pcm_fromspan = PRI_card_pcm_fromspan,
2332 .card_pcm_tospan = PRI_card_pcm_tospan,
2333 .echocancel_timeslot = PRI_echocancel_timeslot,
2334 .echocancel_setmask = PRI_echocancel_setmask,
2335 .card_timing_priority = PRI_timing_priority,
2336 .card_ioctl = PRI_card_ioctl,
2337 .card_open = PRI_card_open,
2338 .card_close = PRI_card_close,
2339 .card_state = PRI_card_state,
2342 static xproto_table_t PROTO_TABLE(PRI) = {
2343 .owner = THIS_MODULE,
2345 /* Table Card Opcode */
2347 .name = "PRI", /* protocol name */
2348 .ports_per_subunit = 1,
2349 .type = XPD_TYPE_PRI,
2351 .phoneops = &pri_phoneops,
2352 .packet_is_valid = pri_packet_is_valid,
2353 .packet_dump = pri_packet_dump,
2356 static bool pri_packet_is_valid(xpacket_t *pack)
2358 const xproto_entry_t *xe = NULL;
2359 // DBG(GENERAL, "\n");
2360 xe = xproto_card_entry(&PROTO_TABLE(PRI), XPACKET_OP(pack));
2364 static void pri_packet_dump(const char *msg, xpacket_t *pack)
2366 DBG(GENERAL, "%s\n", msg);
2369 /*------------------------- REGISTER Handling --------------------------*/
2371 /*------------------------- sysfs stuff --------------------------------*/
2372 static DEVICE_ATTR_READER(pri_protocol_show, dev, buf)
2375 struct PRI_priv_data *priv;
2376 unsigned long flags;
2380 xpd = dev_to_xpd(dev);
2385 spin_lock_irqsave(&xpd->lock, flags);
2386 len += sprintf(buf, "%s\n", pri_protocol_name(priv->pri_protocol));
2387 spin_unlock_irqrestore(&xpd->lock, flags);
2391 static DEVICE_ATTR_WRITER(pri_protocol_store, dev, buf, count)
2394 enum pri_protocol new_protocol = PRI_PROTO_0;
2399 xpd = dev_to_xpd(dev);
2400 XPD_DBG(GENERAL, xpd, "%s\n", buf);
2403 i = strcspn(buf, " \r\n");
2406 "Protocol name '%s' has %d characters (should be 2). "
2411 if (strncasecmp(buf, "E1", 2) == 0)
2412 new_protocol = PRI_PROTO_E1;
2413 else if (strncasecmp(buf, "T1", 2) == 0)
2414 new_protocol = PRI_PROTO_T1;
2415 else if (strncasecmp(buf, "J1", 2) == 0)
2416 new_protocol = PRI_PROTO_J1;
2419 "Unknown PRI protocol '%s' (should be E1|T1|J1). "
2424 ret = set_pri_proto(xpd, new_protocol);
2425 return (ret < 0) ? ret : count;
2428 static DEVICE_ATTR(pri_protocol, S_IRUGO | S_IWUSR, pri_protocol_show,
2429 pri_protocol_store);
2431 static DEVICE_ATTR_READER(pri_localloop_show, dev, buf)
2434 struct PRI_priv_data *priv;
2435 unsigned long flags;
2439 xpd = dev_to_xpd(dev);
2444 spin_lock_irqsave(&xpd->lock, flags);
2445 len += sprintf(buf, "%c\n", (priv->local_loopback) ? 'Y' : 'N');
2446 spin_unlock_irqrestore(&xpd->lock, flags);
2450 static DEVICE_ATTR_WRITER(pri_localloop_store, dev, buf, count)
2458 xpd = dev_to_xpd(dev);
2459 XPD_DBG(GENERAL, xpd, "%s\n", buf);
2462 if ((i = strcspn(buf, " \r\n")) != 1) {
2464 "Value '%s' has %d characters (should be 1). Ignored\n",
2468 if (strchr("1Yy", buf[0]) != NULL)
2470 else if (strchr("0Nn", buf[0]) != NULL)
2474 "Unknown value '%s' (should be [1Yy]|[0Nn]). Ignored\n",
2478 ret = set_localloop(xpd, ll);
2479 return (ret < 0) ? ret : count;
2482 static DEVICE_ATTR(pri_localloop, S_IRUGO | S_IWUSR, pri_localloop_show,
2483 pri_localloop_store);
2485 static DEVICE_ATTR_READER(pri_layer1_show, dev, buf)
2488 struct PRI_priv_data *priv;
2489 unsigned long flags;
2493 xpd = dev_to_xpd(dev);
2498 spin_lock_irqsave(&xpd->lock, flags);
2499 if (priv->poll_noreplies > 1)
2500 len += sprintf(buf + len, "Unknown[%d]", priv->poll_noreplies);
2503 sprintf(buf + len, "%-10s",
2504 ((priv->layer1_up) ? "UP" : "DOWN"));
2505 len += sprintf(buf + len, "%d\n", priv->layer1_replies);
2506 spin_unlock_irqrestore(&xpd->lock, flags);
2510 static DEVICE_ATTR(pri_layer1, S_IRUGO, pri_layer1_show, NULL);
2512 static DEVICE_ATTR_READER(pri_alarms_show, dev, buf)
2515 struct PRI_priv_data *priv;
2516 unsigned long flags;
2518 static const struct {
2523 REG_FRS0_LOS, "RED"}, {
2524 REG_FRS0_AIS, "BLUE"}, {
2525 REG_FRS0_RRA, "YELLOW"},};
2528 xpd = dev_to_xpd(dev);
2533 spin_lock_irqsave(&xpd->lock, flags);
2534 if (priv->poll_noreplies > 1)
2535 len += sprintf(buf + len, "Unknown[%d]", priv->poll_noreplies);
2539 for (i = 0; i < ARRAY_SIZE(alarm_types); i++) {
2540 if (priv->reg_frs0 & alarm_types[i].bits)
2542 sprintf(buf + len, "%s ",
2543 alarm_types[i].name);
2546 len += sprintf(buf + len, "\n");
2547 spin_unlock_irqrestore(&xpd->lock, flags);
2551 static DEVICE_ATTR(pri_alarms, S_IRUGO, pri_alarms_show, NULL);
2553 static DEVICE_ATTR_READER(pri_cas_show, dev, buf)
2556 struct PRI_priv_data *priv;
2557 unsigned long flags;
2561 xpd = dev_to_xpd(dev);
2566 spin_lock_irqsave(&xpd->lock, flags);
2571 sprintf(buf + len, "CAS: replies=%d\n", priv->cas_replies);
2572 len += sprintf(buf + len, " CAS-TS: ");
2573 for (i = 0; i < NUM_CAS_RS_E; i++)
2574 len += sprintf(buf + len, " %02X", priv->cas_ts_e[i]);
2575 len += sprintf(buf + len, "\n");
2576 len += sprintf(buf + len, " CAS-RS: ");
2577 for (i = 0; i < NUM_CAS_RS_E; i++)
2578 len += sprintf(buf + len, " %02X", priv->cas_rs_e[i]);
2579 len += sprintf(buf + len, "\n");
2581 spin_unlock_irqrestore(&xpd->lock, flags);
2585 static DEVICE_ATTR(pri_cas, S_IRUGO, pri_cas_show, NULL);
2587 static DEVICE_ATTR_READER(pri_dchan_show, dev, buf)
2590 struct PRI_priv_data *priv;
2591 unsigned long flags;
2595 xpd = dev_to_xpd(dev);
2600 spin_lock_irqsave(&xpd->lock, flags);
2603 "D-Channel: TX=[%5d] (0x%02X) RX=[%5d] (0x%02X) ",
2604 priv->dchan_tx_counter, priv->dchan_tx_sample,
2605 priv->dchan_rx_counter, priv->dchan_rx_sample);
2606 if (priv->dchan_alive) {
2608 sprintf(buf + len, "(alive %d K-ticks)\n",
2609 priv->dchan_alive_ticks / 1000);
2611 len += sprintf(buf + len, "(dead)\n");
2613 spin_unlock_irqrestore(&xpd->lock, flags);
2617 static DEVICE_ATTR(pri_dchan, S_IRUGO, pri_dchan_show, NULL);
2619 static DEVICE_ATTR_READER(pri_clocking_show, dev, buf)
2622 struct PRI_priv_data *priv;
2623 unsigned long flags;
2627 xpd = dev_to_xpd(dev);
2632 spin_lock_irqsave(&xpd->lock, flags);
2634 sprintf(buf + len, "%s\n",
2635 (priv->clock_source) ? "MASTER" : "SLAVE");
2636 spin_unlock_irqrestore(&xpd->lock, flags);
2640 static DEVICE_ATTR(pri_clocking, S_IRUGO, pri_clocking_show, NULL);
2642 static int pri_xpd_probe(struct device *dev)
2647 xpd = dev_to_xpd(dev);
2648 /* Is it our device? */
2649 if (xpd->xpd_type != XPD_TYPE_PRI) {
2650 XPD_ERR(xpd, "drop suggestion for %s (%d)\n", dev_name(dev),
2654 XPD_DBG(DEVICES, xpd, "SYSFS\n");
2655 ret = device_create_file(dev, &dev_attr_pri_protocol);
2658 "%s: device_create_file(pri_protocol) failed: %d\n",
2660 goto fail_pri_protocol;
2662 ret = device_create_file(dev, &dev_attr_pri_localloop);
2665 "%s: device_create_file(pri_localloop) failed: %d\n",
2667 goto fail_pri_localloop;
2669 ret = device_create_file(dev, &dev_attr_pri_layer1);
2671 XPD_ERR(xpd, "%s: device_create_file(pri_layer1) failed: %d\n",
2673 goto fail_pri_layer1;
2675 ret = device_create_file(dev, &dev_attr_pri_alarms);
2677 XPD_ERR(xpd, "%s: device_create_file(pri_alarms) failed: %d\n",
2679 goto fail_pri_alarms;
2681 ret = device_create_file(dev, &dev_attr_pri_cas);
2683 XPD_ERR(xpd, "%s: device_create_file(pri_cas) failed: %d\n",
2687 ret = device_create_file(dev, &dev_attr_pri_dchan);
2689 XPD_ERR(xpd, "%s: device_create_file(pri_dchan) failed: %d\n",
2691 goto fail_pri_dchan;
2693 ret = device_create_file(dev, &dev_attr_pri_clocking);
2696 "%s: device_create_file(pri_clocking) failed: %d\n",
2698 goto fail_pri_clocking;
2702 device_remove_file(dev, &dev_attr_pri_dchan);
2704 device_remove_file(dev, &dev_attr_pri_cas);
2706 device_remove_file(dev, &dev_attr_pri_alarms);
2708 device_remove_file(dev, &dev_attr_pri_layer1);
2710 device_remove_file(dev, &dev_attr_pri_localloop);
2712 device_remove_file(dev, &dev_attr_pri_protocol);
2717 static int pri_xpd_remove(struct device *dev)
2721 xpd = dev_to_xpd(dev);
2722 XPD_DBG(DEVICES, xpd, "SYSFS\n");
2723 device_remove_file(dev, &dev_attr_pri_clocking);
2724 device_remove_file(dev, &dev_attr_pri_dchan);
2725 device_remove_file(dev, &dev_attr_pri_cas);
2726 device_remove_file(dev, &dev_attr_pri_alarms);
2727 device_remove_file(dev, &dev_attr_pri_layer1);
2728 device_remove_file(dev, &dev_attr_pri_localloop);
2729 device_remove_file(dev, &dev_attr_pri_protocol);
2733 static struct xpd_driver pri_driver = {
2734 .xpd_type = XPD_TYPE_PRI,
2737 .owner = THIS_MODULE,
2738 .probe = pri_xpd_probe,
2739 .remove = pri_xpd_remove}
2742 static int __init card_pri_startup(void)
2746 if ((ret = xpd_driver_register(&pri_driver.driver)) < 0)
2748 #ifdef DAHDI_AUDIO_NOTIFY
2749 INFO("FEATURE: WITH DAHDI_AUDIO_NOTIFY\n");
2751 INFO("FEATURE: WITHOUT DAHDI_AUDIO_NOTIFY\n");
2753 xproto_register(&PROTO_TABLE(PRI));
2757 static void __exit card_pri_cleanup(void)
2760 xproto_unregister(&PROTO_TABLE(PRI));
2761 xpd_driver_unregister(&pri_driver.driver);
2764 MODULE_DESCRIPTION("XPP PRI Card Driver");
2765 MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
2766 MODULE_LICENSE("GPL");
2767 MODULE_ALIAS_XPD(XPD_TYPE_PRI);
2769 module_init(card_pri_startup);
2770 module_exit(card_pri_cleanup);