Debian Codegraph @

drivers/dahdi/xpp/card_fxo.c

raw symbols SCIP
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2004-2006, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/fs.h>
26 #include <linux/delay.h>
27 #include <linux/proc_fs.h>
28 #include <linux/seq_file.h>
29 #include "xpd.h"
30 #include "xproto.h"
31 #include "xpp_dahdi.h"
32 #include "card_fxo.h"
33 #include "dahdi_debug.h"
34 #include "xbus-core.h"
35
36 static const char rcsid[] = "$Id$";
37
38 static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
39 static DEF_PARM(uint, poll_battery_interval, 500, 0644,
40 "Poll battery interval in milliseconds (0 - disable)");
41 static DEF_PARM_BOOL(use_polrev_firmware, 1, 0444,
42 "Use firmware reports of polarity reversal");
43 static DEF_PARM_BOOL(squelch_polrev, 0, 0644,
44 "Never report polarity reversal");
45 #ifdef WITH_METERING
46 static DEF_PARM(uint, poll_metering_interval, 500, 0644,
47 "Poll metering interval in milliseconds (0 - disable)");
48 #endif
49 static DEF_PARM(int, ring_debounce, 50, 0644,
50 "Number of ticks to debounce a false RING indication");
51 static DEF_PARM(int, caller_id_style, 0, 0444,
52 "Caller-Id detection style: "
53 "0 - [BELL], "
54 "1 - [ETSI_FSK], "
55 "2 - [ETSI_DTMF], "
56 "3 - [PASSTHROUGH]");
57 static DEF_PARM(int, power_denial_safezone, 650, 0644,
58 "msec after offhook to ignore power-denial ( (0 - disable power-denial)");
59 static DEF_PARM(int, power_denial_minlen, 80, 0644,
60 "Minimal detected power-denial length (msec) (0 - disable power-denial)");
61 static DEF_PARM(uint, battery_threshold, 3, 0644,
62 "Minimum voltage that shows there is battery");
63 static DEF_PARM(uint, battery_debounce, 1000, 0644,
64 "Minimum interval (msec) for detection of battery off");
65
66 enum cid_style {
67 CID_STYLE_BELL = 0, /* E.g: US (Bellcore) */
68 CID_STYLE_ETSI_FSK = 1, /* E.g: UK (British Telecom) */
69 CID_STYLE_ETSI_DTMF = 2, /* E.g: DK, Russia */
70 CID_STYLE_PASSTHROUGH = 3, /* No change: Let asterisk */
71 /* (>= 1.8) DSP handle this */
72 };
73
74 /* Signaling is opposite (fxs signalling for fxo card) */
75 #if 1
76 #define FXO_DEFAULT_SIGCAP (DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS)
77 #else
78 #define FXO_DEFAULT_SIGCAP (DAHDI_SIG_SF)
79 #endif
80
81 enum fxo_leds {
82 LED_GREEN,
83 LED_RED,
84 };
85
86 #define NUM_LEDS 2
87 #define DELAY_UNTIL_DIALTONE 3000
88
89 /*
90 * Minimum duration for polarity reversal detection (in ticks)
91 * Should be longer than the time to detect a ring, so voltage
92 * fluctuation during ring won't trigger false detection.
93 */
94 #define POLREV_THRESHOLD 200
95 #define POWER_DENIAL_CURRENT 3
96 #define POWER_DENIAL_DELAY 2500 /* ticks */
97
98 /* Shortcuts */
99 #define DAA_WRITE 1
100 #define DAA_READ 0
101 #define DAA_DIRECT_REQUEST(xbus, xpd, port, writing, reg, dL) \
102 xpp_register_request((xbus), (xpd), (port), \
103 (writing), (reg), 0, 0, (dL), 0, 0, 0, 0)
104
105 /*---------------- FXO Protocol Commands ----------------------------------*/
106
107 static bool fxo_packet_is_valid(xpacket_t *pack);
108 static void fxo_packet_dump(const char *msg, xpacket_t *pack);
109 #ifdef CONFIG_PROC_FS
110 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
111 static const struct proc_ops proc_fxo_info_ops;
112 #else
113 static const struct file_operations proc_fxo_info_ops;
114 #endif
115 #ifdef WITH_METERING
116 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
117 static const struct proc_ops proc_xpd_metering_ops;
118 #else
119 static const struct file_operations proc_xpd_metering_ops;
120 #endif
121 #endif
122 #endif
123 static void dahdi_report_battery(xpd_t *xpd, lineno_t chan);
124 static void report_polarity_reversal(xpd_t *xpd, xportno_t portno, char *msg);
125
126 #define PROC_FXO_INFO_FNAME "fxo_info"
127 #ifdef WITH_METERING
128 #define PROC_METERING_FNAME "metering_read"
129 #endif
130
131 #define REG_INTERRUPT_SRC 0x04 /* 4 - Interrupt Source */
132 #define REG_INTERRUPT_SRC_POLI BIT(0) /* Polarity Reversal Detect Interrupt*/
133 #define REG_INTERRUPT_SRC_RING BIT(7) /* Ring Detect Interrupt */
134
135 #define REG_DAA_CONTROL1 0x05 /* 5 - DAA Control 1 */
136 #define REG_DAA_CONTROL1_OH BIT(0) /* Off-Hook. */
137 #define REG_DAA_CONTROL1_ONHM BIT(3) /* On-Hook Line Monitor */
138
139 #define DAA_REG_METERING 0x11 /* 17 */
140 #define DAA_REG_CURRENT 0x1C /* 28 */
141 #define DAA_REG_VBAT 0x1D /* 29 */
142
143 enum battery_state {
144 BATTERY_UNKNOWN = 0,
145 BATTERY_ON = 1,
146 BATTERY_OFF = -1
147 };
148
149 enum polarity_state {
150 POL_UNKNOWN = 0,
151 POL_POSITIVE = 1,
152 POL_NEGATIVE = -1
153 };
154
155 enum power_state {
156 POWER_UNKNOWN = 0,
157 POWER_ON = 1,
158 POWER_OFF = -1
159 };
160
161 struct FXO_priv_data {
162 #ifdef WITH_METERING
163 struct proc_dir_entry *meteringfile;
164 #endif
165 struct proc_dir_entry *fxo_info;
166 uint poll_counter;
167 signed char battery_voltage[CHANNELS_PERXPD];
168 signed char battery_current[CHANNELS_PERXPD];
169 enum battery_state battery[CHANNELS_PERXPD];
170 ushort nobattery_debounce[CHANNELS_PERXPD];
171 enum polarity_state polarity[CHANNELS_PERXPD];
172 ushort polarity_debounce[CHANNELS_PERXPD];
173 int polarity_last_interval[CHANNELS_PERXPD];
174 #define POLARITY_LAST_INTERVAL_NONE (-1)
175 #define POLARITY_LAST_INTERVAL_MAX 40
176 enum power_state power[CHANNELS_PERXPD];
177 ushort power_denial_delay[CHANNELS_PERXPD];
178 ushort power_denial_length[CHANNELS_PERXPD];
179 ushort power_denial_safezone[CHANNELS_PERXPD];
180 xpp_line_t cidfound; /* 0 - OFF, 1 - ON */
181 unsigned int cidtimer[CHANNELS_PERXPD];
182 xpp_line_t ledstate[NUM_LEDS]; /* 0 - OFF, 1 - ON */
183 xpp_line_t ledcontrol[NUM_LEDS]; /* 0 - OFF, 1 - ON */
184 int led_counter[NUM_LEDS][CHANNELS_PERXPD];
185 atomic_t ring_debounce[CHANNELS_PERXPD];
186 #ifdef WITH_METERING
187 uint metering_count[CHANNELS_PERXPD];
188 xpp_line_t metering_tone_state;
189 #endif
190 };
191
192 /*
193 * LED counter values:
194 * n>1 : BLINK every n'th tick
195 */
196 #define LED_COUNTER(priv, pos, color) ((priv)->led_counter[color][pos])
197 #define IS_BLINKING(priv, pos, color) (LED_COUNTER(priv, pos, color) > 0)
198 #define MARK_BLINK(priv, pos, color, t) \
199 ((priv)->led_counter[color][pos] = (t))
200 #define MARK_OFF(priv, pos, color) \
201 do { \
202 BIT_CLR((priv)->ledcontrol[color], (pos)); \
203 MARK_BLINK((priv), (pos), (color), 0); \
204 } while (0)
205 #define MARK_ON(priv, pos, color) \
206 do { \
207 BIT_SET((priv)->ledcontrol[color], (pos)); \
208 MARK_BLINK((priv), (pos), (color), 0); \
209 } while (0)
210
211 #define LED_BLINK_RING (1000/8) /* in ticks */
212
213 /*---------------- FXO: Static functions ----------------------------------*/
214
215 static const char *power2str(enum power_state pw)
216 {
217 switch (pw) {
218 case POWER_UNKNOWN:
219 return "UNKNOWN";
220 case POWER_OFF:
221 return "OFF";
222 case POWER_ON:
223 return "ON";
224 }
225 return NULL;
226 }
227
228 static void power_change(xpd_t *xpd, int portno, enum power_state pw)
229 {
230 struct FXO_priv_data *priv;
231
232 priv = xpd->priv;
233 LINE_DBG(SIGNAL, xpd, portno, "power: %s -> %s\n",
234 power2str(priv->power[portno]), power2str(pw));
235 priv->power[portno] = pw;
236 }
237
238 static void reset_battery_readings(xpd_t *xpd, lineno_t pos)
239 {
240 struct FXO_priv_data *priv = xpd->priv;
241
242 priv->nobattery_debounce[pos] = 0;
243 priv->power_denial_delay[pos] = 0;
244 power_change(xpd, pos, POWER_UNKNOWN);
245 }
246
247 static const int led_register_mask[] = { BIT(7), BIT(6), BIT(5) };
248
249 /*
250 * LED control is done via DAA register 0x20
251 */
252 static int do_led(xpd_t *xpd, lineno_t chan, __u8 which, bool on)
253 {
254 int ret = 0;
255 struct FXO_priv_data *priv;
256 xbus_t *xbus;
257 __u8 value;
258
259 BUG_ON(!xpd);
260 xbus = xpd->xbus;
261 priv = xpd->priv;
262 which = which % NUM_LEDS;
263 if (IS_SET(PHONEDEV(xpd).digital_outputs, chan)
264 || IS_SET(PHONEDEV(xpd).digital_inputs, chan))
265 goto out;
266 if (chan == PORT_BROADCAST) {
267 priv->ledstate[which] = (on) ? ~0 : 0;
268 } else {
269 if (on)
270 BIT_SET(priv->ledstate[which], chan);
271 else
272 BIT_CLR(priv->ledstate[which], chan);
273 }
274 value = 0;
275 value |= ((BIT(5) | BIT(6) | BIT(7)) & ~led_register_mask[which]);
276 value |= (on) ? BIT(0) : 0;
277 value |= (on) ? BIT(1) : 0;
278 LINE_DBG(LEDS, xpd, chan, "LED: which=%d -- %s\n", which,
279 (on) ? "on" : "off");
280 ret = DAA_DIRECT_REQUEST(xbus, xpd, chan, DAA_WRITE, 0x20, value);
281 out:
282 return ret;
283 }
284
285 static void handle_fxo_leds(xpd_t *xpd)
286 {
287 int i;
288 unsigned long flags;
289 const enum fxo_leds colors[] = { LED_GREEN, LED_RED };
290 enum fxo_leds color;
291 unsigned int timer_count;
292 struct FXO_priv_data *priv;
293
294 BUG_ON(!xpd);
295 spin_lock_irqsave(&xpd->lock, flags);
296 priv = xpd->priv;
297 timer_count = xpd->timer_count;
298 for (color = 0; color < ARRAY_SIZE(colors); color++) {
299 for_each_line(xpd, i) {
300 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)
301 || IS_SET(PHONEDEV(xpd).digital_inputs, i))
302 continue;
303 /* Blinking? */
304 if ((xpd->blink_mode & BIT(i)) || IS_BLINKING(priv, i, color)) {
305 int mod_value = LED_COUNTER(priv, i, color);
306
307 if (!mod_value)
308 /* safety value */
309 mod_value = DEFAULT_LED_PERIOD;
310 // led state is toggled
311 if ((timer_count % mod_value) == 0) {
312 LINE_DBG(LEDS, xpd, i, "ledstate=%s\n",
313 (IS_SET
314 (priv->ledstate[color],
315 i)) ? "ON" : "OFF");
316 if (!IS_SET(priv->ledstate[color], i))
317 do_led(xpd, i, color, 1);
318 else
319 do_led(xpd, i, color, 0);
320 }
321 } else if (IS_SET(priv->ledcontrol[color], i)
322 && !IS_SET(priv->ledstate[color], i)) {
323 do_led(xpd, i, color, 1);
324 } else if (!IS_SET(priv->ledcontrol[color], i)
325 && IS_SET(priv->ledstate[color], i)) {
326 do_led(xpd, i, color, 0);
327 }
328 }
329 }
330 spin_unlock_irqrestore(&xpd->lock, flags);
331 }
332
333 static void update_dahdi_ring(xpd_t *xpd, int pos, bool on)
334 {
335 BUG_ON(!xpd);
336 if (caller_id_style == CID_STYLE_BELL)
337 oht_pcm(xpd, pos, !on);
338 /*
339 * We should not spinlock before calling dahdi_hooksig() as
340 * it may call back into our xpp_hooksig() and cause
341 * a nested spinlock scenario
342 */
343 notify_rxsig(xpd, pos, (on) ? DAHDI_RXSIG_RING : DAHDI_RXSIG_OFFHOOK);
344 }
345
346 static void mark_ring(xpd_t *xpd, lineno_t pos, bool on, bool update_dahdi)
347 {
348 struct FXO_priv_data *priv;
349
350 priv = xpd->priv;
351 BUG_ON(!priv);
352 atomic_set(&priv->ring_debounce[pos], 0); /* Stop debouncing */
353 /*
354 * We don't want to check battery during ringing
355 * due to voltage fluctuations.
356 */
357 reset_battery_readings(xpd, pos);
358 if (on && !PHONEDEV(xpd).ringing[pos]) {
359 LINE_DBG(SIGNAL, xpd, pos, "START\n");
360 PHONEDEV(xpd).ringing[pos] = 1;
361 priv->cidtimer[pos] = xpd->timer_count;
362 MARK_BLINK(priv, pos, LED_GREEN, LED_BLINK_RING);
363 if (update_dahdi)
364 update_dahdi_ring(xpd, pos, on);
365 } else if (!on && PHONEDEV(xpd).ringing[pos]) {
366 LINE_DBG(SIGNAL, xpd, pos, "STOP\n");
367 PHONEDEV(xpd).ringing[pos] = 0;
368 priv->cidtimer[pos] = xpd->timer_count;
369 if (IS_BLINKING(priv, pos, LED_GREEN))
370 MARK_BLINK(priv, pos, LED_GREEN, 0);
371 if (update_dahdi)
372 update_dahdi_ring(xpd, pos, on);
373 priv->polarity_last_interval[pos] = POLARITY_LAST_INTERVAL_NONE;
374 }
375 }
376
377 static int do_sethook(xpd_t *xpd, int pos, bool to_offhook)
378 {
379 unsigned long flags;
380 xbus_t *xbus;
381 struct FXO_priv_data *priv;
382 int ret = 0;
383 __u8 value;
384
385 BUG_ON(!xpd);
386 /* We can SETHOOK state only on PSTN */
387 BUG_ON(PHONEDEV(xpd).direction == TO_PHONE);
388 xbus = xpd->xbus;
389 priv = xpd->priv;
390 BUG_ON(!priv);
391 if (priv->battery[pos] != BATTERY_ON && to_offhook) {
392 LINE_NOTICE(xpd, pos,
393 "Cannot take offhook while battery is off!\n");
394 return -EINVAL;
395 }
396 spin_lock_irqsave(&xpd->lock, flags);
397 mark_ring(xpd, pos, 0, 0); // No more rings
398 value = REG_DAA_CONTROL1_ONHM; /* Bit 3 is for CID */
399 if (to_offhook)
400 value |= REG_DAA_CONTROL1_OH;
401 LINE_DBG(SIGNAL, xpd, pos, "SETHOOK: value=0x%02X %s\n", value,
402 (to_offhook) ? "OFFHOOK" : "ONHOOK");
403 if (to_offhook)
404 MARK_ON(priv, pos, LED_GREEN);
405 else
406 MARK_OFF(priv, pos, LED_GREEN);
407 ret =
408 DAA_DIRECT_REQUEST(xbus, xpd, pos, DAA_WRITE, REG_DAA_CONTROL1,
409 value);
410 mark_offhook(xpd, pos, to_offhook);
411 switch (caller_id_style) {
412 case CID_STYLE_ETSI_DTMF:
413 case CID_STYLE_PASSTHROUGH:
414 break;
415 default:
416 oht_pcm(xpd, pos, 0);
417 break;
418 }
419 #ifdef WITH_METERING
420 priv->metering_count[pos] = 0;
421 priv->metering_tone_state = 0L;
422 DAA_DIRECT_REQUEST(xbus, xpd, pos, DAA_WRITE, DAA_REG_METERING, 0x2D);
423 #endif
424 /* unstable during hook changes */
425 reset_battery_readings(xpd, pos);
426 if (to_offhook) {
427 priv->power_denial_safezone[pos] = power_denial_safezone;
428 } else {
429 priv->power_denial_length[pos] = 0;
430 priv->power_denial_safezone[pos] = 0;
431 }
432 priv->cidtimer[pos] = xpd->timer_count;
433 spin_unlock_irqrestore(&xpd->lock, flags);
434 return ret;
435 }
436
437 /*---------------- FXO: Methods -------------------------------------------*/
438
439 static void fxo_proc_remove(xbus_t *xbus, xpd_t *xpd)
440 {
441 struct FXO_priv_data *priv;
442
443 BUG_ON(!xpd);
444 priv = xpd->priv;
445 XPD_DBG(PROC, xpd, "\n");
446 #ifdef CONFIG_PROC_FS
447 #ifdef WITH_METERING
448 if (priv->meteringfile) {
449 XPD_DBG(PROC, xpd, "Removing xpd metering tone file\n");
450 remove_proc_entry(PROC_METERING_FNAME, xpd->proc_xpd_dir);
451 priv->meteringfile = NULL;
452 }
453 #endif
454 if (priv->fxo_info) {
455 XPD_DBG(PROC, xpd, "Removing xpd FXO_INFO file\n");
456 remove_proc_entry(PROC_FXO_INFO_FNAME, xpd->proc_xpd_dir);
457 priv->fxo_info = NULL;
458 }
459 #endif
460 }
461
462 static int fxo_proc_create(xbus_t *xbus, xpd_t *xpd)
463 {
464 struct FXO_priv_data *priv;
465
466 BUG_ON(!xpd);
467 priv = xpd->priv;
468 #ifdef CONFIG_PROC_FS
469 XPD_DBG(PROC, xpd, "Creating FXO_INFO file\n");
470 priv->fxo_info = proc_create_data(PROC_FXO_INFO_FNAME, 0444,
471 xpd->proc_xpd_dir,
472 &proc_fxo_info_ops, xpd);
473 if (!priv->fxo_info) {
474 XPD_ERR(xpd, "Failed to create proc file '%s'\n",
475 PROC_FXO_INFO_FNAME);
476 fxo_proc_remove(xbus, xpd);
477 return -EINVAL;
478 }
479 SET_PROC_DIRENTRY_OWNER(priv->fxo_info);
480 #ifdef WITH_METERING
481 XPD_DBG(PROC, xpd, "Creating Metering tone file\n");
482 priv->meteringfile = proc_create_data(PROC_METERING_FNAME, 0444,
483 xpd->proc_xpd_dir,
484 &proc_xpd_metering_ops, xpd);
485 if (!priv->meteringfile) {
486 XPD_ERR(xpd, "Failed to create proc file '%s'\n",
487 PROC_METERING_FNAME);
488 fxo_proc_remove(xbus, xpd);
489 return -EINVAL;
490 }
491 SET_PROC_DIRENTRY_OWNER(priv->meteringfile);
492 #endif
493 #endif
494 return 0;
495 }
496
497 static xpd_t *FXO_card_new(xbus_t *xbus, int unit, int subunit,
498 const xproto_table_t *proto_table,
499 const struct unit_descriptor *unit_descriptor,
500 bool to_phone)
501 {
502 xpd_t *xpd = NULL;
503 int channels;
504 int subunit_ports;
505
506 if (to_phone) {
507 XBUS_NOTICE(xbus,
508 "XPD=%d%d: try to instanciate FXO with "
509 "reverse direction\n",
510 unit, subunit);
511 return NULL;
512 }
513 subunit_ports = unit_descriptor->numchips * unit_descriptor->ports_per_chip;
514 if (unit_descriptor->subtype == 2)
515 channels = min(2, subunit_ports);
516 else
517 channels = min(8, subunit_ports);
518 xpd =
519 xpd_alloc(xbus, unit, subunit,
520 sizeof(struct FXO_priv_data), proto_table, unit_descriptor, channels);
521 if (!xpd)
522 return NULL;
523 PHONEDEV(xpd).direction = TO_PSTN;
524 xpd->type_name = "FXO";
525 if (fxo_proc_create(xbus, xpd) < 0)
526 goto err;
527 return xpd;
528 err:
529 xpd_free(xpd);
530 return NULL;
531 }
532
533 static int FXO_card_init(xbus_t *xbus, xpd_t *xpd)
534 {
535 struct FXO_priv_data *priv;
536 int i;
537
538 BUG_ON(!xpd);
539 priv = xpd->priv;
540 // Hanghup all lines
541 for_each_line(xpd, i) {
542 do_sethook(xpd, i, 0);
543 /* will be updated on next battery sample */
544 priv->polarity[i] = POL_UNKNOWN;
545 priv->polarity_debounce[i] = 0;
546 /* will be updated on next battery sample */
547 priv->battery[i] = BATTERY_UNKNOWN;
548 /* will be updated on next battery sample */
549 priv->power[i] = POWER_UNKNOWN;
550 switch (caller_id_style) {
551 case CID_STYLE_ETSI_DTMF:
552 case CID_STYLE_PASSTHROUGH:
553 oht_pcm(xpd, i, 1);
554 break;
555 }
556 priv->polarity_last_interval[i] = POLARITY_LAST_INTERVAL_NONE;
557 }
558 XPD_DBG(GENERAL, xpd, "done\n");
559 for_each_line(xpd, i) {
560 do_led(xpd, i, LED_GREEN, 0);
561 }
562 for_each_line(xpd, i) {
563 do_led(xpd, i, LED_GREEN, 1);
564 msleep(50);
565 }
566 for_each_line(xpd, i) {
567 do_led(xpd, i, LED_GREEN, 0);
568 msleep(50);
569 }
570 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
571 return 0;
572 }
573
574 static int FXO_card_remove(xbus_t *xbus, xpd_t *xpd)
575 {
576 BUG_ON(!xpd);
577 XPD_DBG(GENERAL, xpd, "\n");
578 fxo_proc_remove(xbus, xpd);
579 return 0;
580 }
581
582 static int FXO_card_dahdi_preregistration(xpd_t *xpd, bool on)
583 {
584 xbus_t *xbus;
585 struct FXO_priv_data *priv;
586 int i;
587 unsigned int timer_count;
588
589 BUG_ON(!xpd);
590 xbus = xpd->xbus;
591 BUG_ON(!xbus);
592 priv = xpd->priv;
593 BUG_ON(!priv);
594 timer_count = xpd->timer_count;
595 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "ON" : "OFF");
596 PHONEDEV(xpd).span.spantype = SPANTYPE_ANALOG_FXO;
597 for_each_line(xpd, i) {
598 struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
599
600 XPD_DBG(GENERAL, xpd, "setting FXO channel %d\n", i);
601 snprintf(cur_chan->name, MAX_CHANNAME, "XPP_FXO/%02d/%1d%1d/%d",
602 xbus->num, xpd->addr.unit, xpd->addr.subunit, i);
603 cur_chan->chanpos = i + 1;
604 cur_chan->pvt = xpd;
605 cur_chan->sigcap = FXO_DEFAULT_SIGCAP;
606 }
607 for_each_line(xpd, i) {
608 MARK_ON(priv, i, LED_GREEN);
609 msleep(4);
610 MARK_ON(priv, i, LED_RED);
611 }
612 for_each_line(xpd, i) {
613 priv->cidtimer[i] = timer_count;
614 }
615 return 0;
616 }
617
618 static int FXO_card_dahdi_postregistration(xpd_t *xpd, bool on)
619 {
620 xbus_t *xbus;
621 struct FXO_priv_data *priv;
622 int i;
623
624 BUG_ON(!xpd);
625 xbus = xpd->xbus;
626 BUG_ON(!xbus);
627 priv = xpd->priv;
628 BUG_ON(!priv);
629 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "ON" : "OFF");
630 for_each_line(xpd, i) {
631 MARK_OFF(priv, i, LED_GREEN);
632 msleep(2);
633 MARK_OFF(priv, i, LED_RED);
634 msleep(2);
635 }
636 return 0;
637 }
638
639 static int FXO_span_assigned(xpd_t *xpd)
640 {
641 xbus_t *xbus;
642 struct FXO_priv_data *priv;
643 int i;
644
645 BUG_ON(!xpd);
646 xbus = xpd->xbus;
647 BUG_ON(!xbus);
648 priv = xpd->priv;
649 BUG_ON(!priv);
650 XPD_DBG(GENERAL, xpd, "\n");
651 for_each_line(xpd, i)
652 dahdi_report_battery(xpd, i);
653 return 0;
654 }
655
656 static int FXO_card_hooksig(xpd_t *xpd, int pos, enum dahdi_txsig txsig)
657 {
658 struct FXO_priv_data *priv;
659 int ret = 0;
660
661 priv = xpd->priv;
662 BUG_ON(!priv);
663 LINE_DBG(SIGNAL, xpd, pos, "%s\n", txsig2str(txsig));
664 BUG_ON(PHONEDEV(xpd).direction != TO_PSTN);
665 /* XXX Enable hooksig for FXO XXX */
666 switch (txsig) {
667 case DAHDI_TXSIG_START:
668 case DAHDI_TXSIG_OFFHOOK:
669 ret = do_sethook(xpd, pos, 1);
670 break;
671 case DAHDI_TXSIG_ONHOOK:
672 ret = do_sethook(xpd, pos, 0);
673 break;
674 default:
675 XPD_NOTICE(xpd, "Can't set tx state to %s (%d)\n",
676 txsig2str(txsig), txsig);
677 return -EINVAL;
678 }
679 return ret;
680 }
681
682 static void dahdi_report_battery(xpd_t *xpd, lineno_t chan)
683 {
684 struct FXO_priv_data *priv;
685
686 BUG_ON(!xpd);
687 priv = xpd->priv;
688 if (SPAN_REGISTERED(xpd)) {
689 switch (priv->battery[chan]) {
690 case BATTERY_UNKNOWN:
691 /* no-op */
692 break;
693 case BATTERY_OFF:
694 LINE_DBG(SIGNAL, xpd, chan, "Send DAHDI_ALARM_RED\n");
695 dahdi_alarm_channel(XPD_CHAN(xpd, chan),
696 DAHDI_ALARM_RED);
697 break;
698 case BATTERY_ON:
699 LINE_DBG(SIGNAL, xpd, chan, "Send DAHDI_ALARM_NONE\n");
700 dahdi_alarm_channel(XPD_CHAN(xpd, chan),
701 DAHDI_ALARM_NONE);
702 break;
703 }
704 }
705 }
706
707 static int FXO_card_open(xpd_t *xpd, lineno_t chan)
708 {
709 BUG_ON(!xpd);
710 return 0;
711 }
712
713 static void poll_battery(xbus_t *xbus, xpd_t *xpd)
714 {
715 int i;
716
717 for_each_line(xpd, i) {
718 DAA_DIRECT_REQUEST(xbus, xpd, i, DAA_READ, DAA_REG_VBAT, 0);
719 }
720 }
721
722 #ifdef WITH_METERING
723 static void poll_metering(xbus_t *xbus, xpd_t *xpd)
724 {
725 int i;
726
727 for_each_line(xpd, i) {
728 if (IS_OFFHOOK(xpd, i))
729 DAA_DIRECT_REQUEST(xbus, xpd, i, DAA_READ,
730 DAA_REG_METERING, 0);
731 }
732 }
733 #endif
734
735 static void handle_fxo_ring(xpd_t *xpd)
736 {
737 struct FXO_priv_data *priv;
738 int i;
739
740 priv = xpd->priv;
741 for_each_line(xpd, i) {
742 if (likely(use_polrev_firmware)) {
743 int *t = &priv->polarity_last_interval[i];
744 if (*t != POLARITY_LAST_INTERVAL_NONE) {
745 (*t)++;
746 if (*t > POLARITY_LAST_INTERVAL_MAX) {
747 LINE_DBG(SIGNAL, xpd, i,
748 "polrev(GOOD): %d msec\n", *t);
749 *t = POLARITY_LAST_INTERVAL_NONE;
750 report_polarity_reversal(xpd,
751 i, "firmware");
752 }
753 }
754 }
755 if (atomic_read(&priv->ring_debounce[i]) > 0) {
756 /* Maybe start ring */
757 if (atomic_dec_and_test(&priv->ring_debounce[i]))
758 mark_ring(xpd, i, 1, 1);
759 } else if (atomic_read(&priv->ring_debounce[i]) < 0) {
760 /* Maybe stop ring */
761 if (atomic_inc_and_test(&priv->ring_debounce[i]))
762 mark_ring(xpd, i, 0, 1);
763 }
764 }
765 }
766
767 static void handle_fxo_power_denial(xpd_t *xpd)
768 {
769 struct FXO_priv_data *priv;
770 int i;
771
772 if (!power_denial_safezone)
773 return; /* Ignore power denials */
774 priv = xpd->priv;
775 for_each_line(xpd, i) {
776 if (PHONEDEV(xpd).ringing[i] || !IS_OFFHOOK(xpd, i)) {
777 priv->power_denial_delay[i] = 0;
778 continue;
779 }
780 if (priv->power_denial_safezone[i] > 0) {
781 if (--priv->power_denial_safezone[i] == 0) {
782 /*
783 * Poll current, prev answers are meaningless
784 */
785 DAA_DIRECT_REQUEST(xpd->xbus, xpd, i, DAA_READ,
786 DAA_REG_CURRENT, 0);
787 }
788 continue;
789 }
790 if (priv->power_denial_length[i] > 0) {
791 priv->power_denial_length[i]--;
792 if (priv->power_denial_length[i] <= 0) {
793 /*
794 * But maybe the FXS started to ring (and
795 * the firmware haven't detected it yet).
796 * This would cause false power denials so
797 * we just flag it and schedule more ticks
798 * to wait.
799 */
800 LINE_DBG(SIGNAL, xpd, i,
801 "Possible Power Denial Hangup\n");
802 priv->power_denial_delay[i] =
803 POWER_DENIAL_DELAY;
804 }
805 continue;
806 }
807 if (priv->power_denial_delay[i] > 0) {
808 /*
809 * Ring detection by the firmware takes some time.
810 * Therefore we delay our decision until we are
811 * sure that no ring has started during this time.
812 */
813 priv->power_denial_delay[i]--;
814 if (priv->power_denial_delay[i] <= 0) {
815 LINE_DBG(SIGNAL, xpd, i,
816 "Power Denial Hangup\n");
817 priv->power_denial_delay[i] = 0;
818 /*
819 * Let Asterisk decide what to do
820 */
821 notify_rxsig(xpd, i, DAHDI_RXSIG_ONHOOK);
822 }
823 }
824 }
825 }
826
827 /*
828 * For caller-id CID_STYLE_ETSI_DTMF:
829 * - No indication is passed before the CID
830 * - We try to detect it and send "fake" polarity reversal.
831 * - The chan_dahdi.conf should have cidstart=polarity
832 * - Based on an idea in http://bugs.digium.com/view.php?id=9096
833 */
834 static void check_etsi_dtmf(xpd_t *xpd)
835 {
836 struct FXO_priv_data *priv;
837 int portno;
838 unsigned int timer_count;
839
840 if (!SPAN_REGISTERED(xpd))
841 return;
842 priv = xpd->priv;
843 BUG_ON(!priv);
844 timer_count = xpd->timer_count;
845 for_each_line(xpd, portno) {
846 /* Skip offhook and ringing ports */
847 if (IS_OFFHOOK(xpd, portno) || PHONEDEV(xpd).ringing[portno])
848 continue;
849 if (IS_SET(priv->cidfound, portno)) {
850 if (timer_count > priv->cidtimer[portno] + 4000) {
851 /* reset flags if it's been a while */
852 priv->cidtimer[portno] = timer_count;
853 BIT_CLR(priv->cidfound, portno);
854 LINE_DBG(SIGNAL, xpd, portno,
855 "Reset CID flag\n");
856 }
857 continue;
858 }
859 if (timer_count > priv->cidtimer[portno] + 400) {
860 struct dahdi_chan *chan = XPD_CHAN(xpd, portno);
861 int sample;
862 int i;
863
864 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
865 sample = DAHDI_XLAW(chan->readchunk[i], chan);
866 if (sample > 16000 || sample < -16000) {
867 priv->cidtimer[portno] = timer_count;
868 BIT_SET(priv->cidfound, portno);
869 LINE_DBG(SIGNAL, xpd, portno,
870 "Found DTMF CLIP (%d)\n", i);
871 report_polarity_reversal(xpd, portno,
872 "fake");
873 break;
874 }
875 }
876 }
877 }
878 }
879
880 static int FXO_card_tick(xbus_t *xbus, xpd_t *xpd)
881 {
882 struct FXO_priv_data *priv;
883
884 BUG_ON(!xpd);
885 priv = xpd->priv;
886 BUG_ON(!priv);
887 if (poll_battery_interval != 0
888 && (priv->poll_counter % poll_battery_interval) == 0)
889 poll_battery(xbus, xpd);
890 #ifdef WITH_METERING
891 if (poll_metering_interval != 0
892 && (priv->poll_counter % poll_metering_interval) == 0)
893 poll_metering(xbus, xpd);
894 #endif
895 handle_fxo_leds(xpd);
896 handle_fxo_ring(xpd);
897 handle_fxo_power_denial(xpd);
898 if (caller_id_style == CID_STYLE_ETSI_DTMF && likely(xpd->card_present))
899 check_etsi_dtmf(xpd);
900 priv->poll_counter++;
901 return 0;
902 }
903
904 #include <dahdi/wctdm_user.h>
905 /*
906 * The first register is the ACIM, the other are coefficient registers.
907 * We define the array size explicitly to track possible inconsistencies
908 * if the struct is modified.
909 */
910 static const char echotune_regs[sizeof(struct wctdm_echo_coefs)] =
911 { 30, 45, 46, 47, 48, 49, 50, 51, 52 };
912
913 static int FXO_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd,
914 unsigned long arg)
915 {
916 int i, ret;
917 unsigned char echotune_data[ARRAY_SIZE(echotune_regs)];
918
919 BUG_ON(!xpd);
920 if (!XBUS_IS(xpd->xbus, READY))
921 return -ENODEV;
922 switch (cmd) {
923 case WCTDM_SET_ECHOTUNE:
924 XPD_DBG(GENERAL, xpd, "-- Setting echo registers: \n");
925 /* first off: check if this span is fxs. If not: -EINVALID */
926 if (copy_from_user
927 (&echotune_data, (void __user *)arg, sizeof(echotune_data)))
928 return -EFAULT;
929
930 for (i = 0; i < ARRAY_SIZE(echotune_regs); i++) {
931 XPD_DBG(REGS, xpd, "Reg=0x%02X, data=0x%02X\n",
932 echotune_regs[i], echotune_data[i]);
933 ret =
934 DAA_DIRECT_REQUEST(xpd->xbus, xpd, pos, DAA_WRITE,
935 echotune_regs[i],
936 echotune_data[i]);
937 if (ret < 0) {
938 LINE_NOTICE(xpd, pos,
939 "Couldn't write %0x02X to "
940 "register %0x02X\n",
941 echotune_data[i], echotune_regs[i]);
942 return ret;
943 }
944 msleep(1);
945 }
946
947 XPD_DBG(GENERAL, xpd, "-- Set echo registers successfully\n");
948 break;
949 case DAHDI_TONEDETECT:
950 /*
951 * Asterisk call all span types with this (FXS specific)
952 * call. Silently ignore it.
953 */
954 LINE_DBG(GENERAL, xpd, pos,
955 "DAHDI_TONEDETECT (FXO: NOTIMPLEMENTED)\n");
956 return -ENOTTY;
957 default:
958 report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
959 return -ENOTTY;
960 }
961 return 0;
962 }
963
964 /*---------------- FXO: HOST COMMANDS -------------------------------------*/
965
966 /*---------------- FXO: Astribank Reply Handlers --------------------------*/
967
968 HANDLER_DEF(FXO, SIG_CHANGED)
969 {
970 xpp_line_t sig_status =
971 RPACKET_FIELD(pack, FXO, SIG_CHANGED, sig_status);
972 xpp_line_t sig_toggles =
973 RPACKET_FIELD(pack, FXO, SIG_CHANGED, sig_toggles);
974 unsigned long flags;
975 int i;
976 struct FXO_priv_data *priv;
977
978 if (!xpd) {
979 notify_bad_xpd(__func__, xbus, XPACKET_ADDR(pack), cmd->name);
980 return -EPROTO;
981 }
982 priv = xpd->priv;
983 BUG_ON(!priv);
984 XPD_DBG(SIGNAL, xpd, "(PSTN) sig_toggles=0x%04X sig_status=0x%04X\n",
985 sig_toggles, sig_status);
986 spin_lock_irqsave(&xpd->lock, flags);
987 for_each_line(xpd, i) {
988 int debounce;
989
990 if (IS_SET(sig_toggles, i)) {
991 if (priv->battery[i] == BATTERY_OFF) {
992 /*
993 * With poll_battery_interval==0 we cannot
994 * have BATTERY_OFF so we won't get here
995 */
996 LINE_NOTICE(xpd, i,
997 "SIG_CHANGED while battery is off. "
998 "Ignored.\n");
999 continue;
1000 }
1001 /* First report false ring alarms */
1002 debounce = atomic_read(&priv->ring_debounce[i]);
1003 if (debounce)
1004 LINE_NOTICE(xpd, i,
1005 "Ignored a false short ring "
1006 "(lasted only %dms)\n",
1007 ring_debounce - debounce);
1008 /*
1009 * Now set a new ring alarm.
1010 * It will be checked in handle_fxo_ring()
1011 */
1012 debounce =
1013 (IS_SET(sig_status, i)) ? ring_debounce :
1014 -ring_debounce;
1015 atomic_set(&priv->ring_debounce[i], debounce);
1016 }
1017 }
1018 spin_unlock_irqrestore(&xpd->lock, flags);
1019 return 0;
1020 }
1021
1022 static void report_polarity_reversal(xpd_t *xpd, xportno_t portno, char *msg)
1023 {
1024 /*
1025 * Inform dahdi/Asterisk:
1026 * 1. Maybe used for hangup detection during offhook
1027 * 2. In some countries used to report caller-id
1028 * during onhook but before first ring.
1029 */
1030 if (caller_id_style == CID_STYLE_ETSI_FSK)
1031 /* will be cleared on ring/offhook */
1032 oht_pcm(xpd, portno, 1);
1033 if (SPAN_REGISTERED(xpd)) {
1034 LINE_DBG(SIGNAL, xpd, portno,
1035 "%s DAHDI_EVENT_POLARITY (%s)\n",
1036 (squelch_polrev) ? "Squelch" : "Send",
1037 msg);
1038 if (!squelch_polrev)
1039 dahdi_qevent_lock(XPD_CHAN(xpd, portno),
1040 DAHDI_EVENT_POLARITY);
1041 }
1042 }
1043
1044 static void update_battery_voltage(xpd_t *xpd, __u8 data_low,
1045 xportno_t portno)
1046 {
1047 struct FXO_priv_data *priv;
1048 enum polarity_state pol;
1049 int msec;
1050 signed char volts = (signed char)data_low;
1051
1052 priv = xpd->priv;
1053 BUG_ON(!priv);
1054 priv->battery_voltage[portno] = volts;
1055 if (PHONEDEV(xpd).ringing[portno])
1056 goto ignore_reading; /* ring voltage create false alarms */
1057 if (abs(volts) < battery_threshold) {
1058 /*
1059 * Check for battery voltage fluctuations
1060 */
1061 if (priv->battery[portno] != BATTERY_OFF) {
1062 int milliseconds;
1063
1064 milliseconds =
1065 priv->nobattery_debounce[portno]++ *
1066 poll_battery_interval;
1067 if (milliseconds > battery_debounce) {
1068 LINE_DBG(SIGNAL, xpd, portno,
1069 "BATTERY OFF voltage=%d\n", volts);
1070 priv->battery[portno] = BATTERY_OFF;
1071 dahdi_report_battery(xpd, portno);
1072 /* What's the polarity ? */
1073 priv->polarity[portno] = POL_UNKNOWN;
1074 priv->polarity_debounce[portno] = 0;
1075 /* What's the current ? */
1076 power_change(xpd, portno, POWER_UNKNOWN);
1077 /*
1078 * Stop further processing for now
1079 */
1080 goto ignore_reading;
1081 }
1082
1083 }
1084 } else {
1085 priv->nobattery_debounce[portno] = 0;
1086 if (priv->battery[portno] != BATTERY_ON) {
1087 LINE_DBG(SIGNAL, xpd, portno, "BATTERY ON voltage=%d\n",
1088 volts);
1089 priv->battery[portno] = BATTERY_ON;
1090 dahdi_report_battery(xpd, portno);
1091 }
1092 }
1093 #if 0
1094 /*
1095 * Mark FXO ports without battery!
1096 */
1097 if (priv->battery[portno] != BATTERY_ON)
1098 MARK_ON(priv, portno, LED_RED);
1099 else
1100 MARK_OFF(priv, portno, LED_RED);
1101 #endif
1102 if (priv->battery[portno] != BATTERY_ON) {
1103 /* What's the polarity ? */
1104 priv->polarity[portno] = POL_UNKNOWN;
1105 return;
1106 }
1107 /*
1108 * Handle reverse polarity
1109 */
1110 if (volts == 0)
1111 pol = POL_UNKNOWN;
1112 else if (volts < 0)
1113 pol = POL_NEGATIVE;
1114 else
1115 pol = POL_POSITIVE;
1116 if (priv->polarity[portno] == pol) {
1117 /*
1118 * Same polarity, reset debounce counter
1119 */
1120 priv->polarity_debounce[portno] = 0;
1121 return;
1122 }
1123 /*
1124 * Track polarity reversals and debounce spikes.
1125 * Only reversals with long duration count.
1126 */
1127 msec = priv->polarity_debounce[portno]++ * poll_battery_interval;
1128 if (msec >= POLREV_THRESHOLD) {
1129 priv->polarity_debounce[portno] = 0;
1130 if (pol != POL_UNKNOWN && priv->polarity[portno] != POL_UNKNOWN) {
1131 char *polname = NULL;
1132
1133 if (pol == POL_POSITIVE)
1134 polname = "Positive";
1135 else if (pol == POL_NEGATIVE)
1136 polname = "Negative";
1137 else
1138 BUG();
1139 LINE_DBG(SIGNAL, xpd, portno,
1140 "Polarity changed to %s\n", polname);
1141 if (!use_polrev_firmware)
1142 report_polarity_reversal(xpd, portno, polname);
1143 }
1144 priv->polarity[portno] = pol;
1145 }
1146 return;
1147 ignore_reading:
1148 /*
1149 * Reset debounce counters to prevent false alarms
1150 */
1151 /* unstable during hook changes */
1152 reset_battery_readings(xpd, portno);
1153 }
1154
1155 static void update_battery_current(xpd_t *xpd, __u8 data_low,
1156 xportno_t portno)
1157 {
1158 struct FXO_priv_data *priv;
1159
1160 priv = xpd->priv;
1161 BUG_ON(!priv);
1162 priv->battery_current[portno] = data_low;
1163 /*
1164 * During ringing, current is not stable.
1165 * During onhook there should not be current anyway.
1166 */
1167 if (PHONEDEV(xpd).ringing[portno] || !IS_OFFHOOK(xpd, portno))
1168 goto ignore_it;
1169 /*
1170 * Power denial with no battery voltage is meaningless
1171 */
1172 if (priv->battery[portno] != BATTERY_ON)
1173 goto ignore_it;
1174 /* Safe zone after offhook */
1175 if (priv->power_denial_safezone[portno] > 0)
1176 goto ignore_it;
1177 if (data_low < POWER_DENIAL_CURRENT) {
1178 if (priv->power[portno] == POWER_ON) {
1179 power_change(xpd, portno, POWER_OFF);
1180 priv->power_denial_length[portno] = power_denial_minlen;
1181 }
1182 } else {
1183 if (priv->power[portno] != POWER_ON) {
1184 power_change(xpd, portno, POWER_ON);
1185 priv->power_denial_length[portno] = 0;
1186 /* We are now OFFHOOK */
1187 hookstate_changed(xpd, portno, 1);
1188 }
1189 }
1190 return;
1191 ignore_it:
1192 priv->power_denial_delay[portno] = 0;
1193 }
1194
1195 #ifdef WITH_METERING
1196 #define BTD_BIT BIT(0)
1197
1198 static void update_metering_state(xpd_t *xpd, __u8 data_low, lineno_t portno)
1199 {
1200 struct FXO_priv_data *priv;
1201 bool metering_tone = data_low & BTD_BIT;
1202 bool old_metering_tone;
1203
1204 priv = xpd->priv;
1205 BUG_ON(!priv);
1206 old_metering_tone = IS_SET(priv->metering_tone_state, portno);
1207 LINE_DBG(SIGNAL, xpd, portno, "METERING: %s [dL=0x%X] (%d)\n",
1208 (metering_tone) ? "ON" : "OFF", data_low,
1209 priv->metering_count[portno]);
1210 if (metering_tone && !old_metering_tone) {
1211 /* Rising edge */
1212 priv->metering_count[portno]++;
1213 BIT_SET(priv->metering_tone_state, portno);
1214 } else if (!metering_tone && old_metering_tone)
1215 BIT_CLR(priv->metering_tone_state, portno);
1216 if (metering_tone) {
1217 /* Clear the BTD bit */
1218 data_low &= ~BTD_BIT;
1219 DAA_DIRECT_REQUEST(xpd->xbus, xpd, portno, DAA_WRITE,
1220 DAA_REG_METERING, data_low);
1221 }
1222 }
1223 #endif
1224
1225 static void got_chip_interrupt(xpd_t *xpd, __u8 data_low,
1226 xportno_t portno)
1227 {
1228 struct FXO_priv_data *priv;
1229 int t;
1230
1231 if (!use_polrev_firmware)
1232 return;
1233 priv = xpd->priv;
1234 LINE_DBG(SIGNAL, xpd, portno, "mask=0x%X\n", data_low);
1235 if (!(data_low & REG_INTERRUPT_SRC_POLI))
1236 return;
1237 t = priv->polarity_last_interval[portno];
1238 if (PHONEDEV(xpd).ringing[portno]) {
1239 priv->polarity_last_interval[portno] =
1240 POLARITY_LAST_INTERVAL_NONE;
1241 LINE_DBG(SIGNAL, xpd, portno,
1242 "polrev(false): %d msec (while ringing)\n", t);
1243 } else if (data_low & REG_INTERRUPT_SRC_RING) {
1244 priv->polarity_last_interval[portno] =
1245 POLARITY_LAST_INTERVAL_NONE;
1246 LINE_DBG(SIGNAL, xpd, portno,
1247 "polrev(false): %d msec (with chip-interrupt ring)\n",
1248 t);
1249 } else if (t == POLARITY_LAST_INTERVAL_NONE) {
1250 priv->polarity_last_interval[portno] = 0;
1251 LINE_DBG(SIGNAL, xpd, portno,
1252 "polrev(start)\n");
1253 } else if (t < POLARITY_LAST_INTERVAL_MAX) {
1254 /*
1255 * Start counting upward from -POLARITY_LAST_INTERVAL_MAX
1256 * Until we reach POLARITY_LAST_INTERVAL_NONE.
1257 * This way we filter bursts of false reports we get
1258 * during ringing.
1259 */
1260 priv->polarity_last_interval[portno] =
1261 POLARITY_LAST_INTERVAL_NONE -
1262 POLARITY_LAST_INTERVAL_MAX;
1263 LINE_DBG(SIGNAL, xpd, portno,
1264 "polrev(false): %d msec (interval shorter than %d)\n",
1265 t, POLARITY_LAST_INTERVAL_MAX);
1266 }
1267 }
1268
1269 static int FXO_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
1270 {
1271 struct FXO_priv_data *priv;
1272 lineno_t portno;
1273
1274 priv = xpd->priv;
1275 BUG_ON(!priv);
1276 portno = info->h.portnum;
1277 switch (REG_FIELD(info, regnum)) {
1278 case REG_INTERRUPT_SRC:
1279 got_chip_interrupt(xpd, REG_FIELD(info, data_low), portno);
1280 break;
1281 case DAA_REG_VBAT:
1282 update_battery_voltage(xpd, REG_FIELD(info, data_low), portno);
1283 break;
1284 case DAA_REG_CURRENT:
1285 update_battery_current(xpd, REG_FIELD(info, data_low), portno);
1286 break;
1287 #ifdef WITH_METERING
1288 case DAA_REG_METERING:
1289 update_metering_state(xpd, REG_FIELD(info, data_low), portno);
1290 break;
1291 #endif
1292 }
1293 LINE_DBG(REGS, xpd, portno, "%c reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
1294 ((info->h.bytes == 3) ? 'I' : 'D'), REG_FIELD(info, regnum),
1295 REG_FIELD(info, data_low), REG_FIELD(info, data_high));
1296 /* Update /proc info only if reply relate to the last slic read request */
1297 if (REG_FIELD(&xpd->requested_reply, regnum) ==
1298 REG_FIELD(info, regnum)
1299 && REG_FIELD(&xpd->requested_reply, do_subreg) ==
1300 REG_FIELD(info, do_subreg)
1301 && REG_FIELD(&xpd->requested_reply, subreg) ==
1302 REG_FIELD(info, subreg)) {
1303 xpd->last_reply = *info;
1304 }
1305 return 0;
1306 }
1307
1308 static int FXO_card_state(xpd_t *xpd, bool on)
1309 {
1310 int ret = 0;
1311 struct FXO_priv_data *priv;
1312
1313 BUG_ON(!xpd);
1314 priv = xpd->priv;
1315 BUG_ON(!priv);
1316 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
1317 return ret;
1318 }
1319
1320 static const struct xops fxo_xops = {
1321 .card_new = FXO_card_new,
1322 .card_init = FXO_card_init,
1323 .card_remove = FXO_card_remove,
1324 .card_tick = FXO_card_tick,
1325 .card_register_reply = FXO_card_register_reply,
1326 };
1327
1328 static const struct phoneops fxo_phoneops = {
1329 .card_dahdi_preregistration = FXO_card_dahdi_preregistration,
1330 .card_dahdi_postregistration = FXO_card_dahdi_postregistration,
1331 .card_hooksig = FXO_card_hooksig,
1332 .card_pcm_recompute = generic_card_pcm_recompute,
1333 .card_pcm_fromspan = generic_card_pcm_fromspan,
1334 .card_pcm_tospan = generic_card_pcm_tospan,
1335 .card_timing_priority = generic_timing_priority,
1336 .echocancel_timeslot = generic_echocancel_timeslot,
1337 .echocancel_setmask = generic_echocancel_setmask,
1338 .card_ioctl = FXO_card_ioctl,
1339 .card_open = FXO_card_open,
1340 .card_state = FXO_card_state,
1341 .span_assigned = FXO_span_assigned,
1342 };
1343
1344 static xproto_table_t PROTO_TABLE(FXO) = {
1345 .owner = THIS_MODULE,
1346 .entries = {
1347 /* Prototable Card Opcode */
1348 XENTRY( FXO, FXO, SIG_CHANGED ),
1349 },
1350 .name = "FXO", /* protocol name */
1351 .ports_per_subunit = 8,
1352 .type = XPD_TYPE_FXO,
1353 .xops = &fxo_xops,
1354 .phoneops = &fxo_phoneops,
1355 .packet_is_valid = fxo_packet_is_valid,
1356 .packet_dump = fxo_packet_dump,
1357 };
1358
1359 static bool fxo_packet_is_valid(xpacket_t *pack)
1360 {
1361 const xproto_entry_t *xe;
1362
1363 //DBG(GENERAL, "\n");
1364 xe = xproto_card_entry(&PROTO_TABLE(FXO), XPACKET_OP(pack));
1365 return xe != NULL;
1366 }
1367
1368 static void fxo_packet_dump(const char *msg, xpacket_t *pack)
1369 {
1370 DBG(GENERAL, "%s\n", msg);
1371 }
1372
1373 /*------------------------- DAA Handling --------------------------*/
1374
1375 #ifdef CONFIG_PROC_FS
1376 static int proc_fxo_info_show(struct seq_file *sfile, void *not_used)
1377 {
1378 unsigned long flags;
1379 xpd_t *xpd = sfile->private;
1380 struct FXO_priv_data *priv;
1381 int i;
1382
1383 if (!xpd)
1384 return -ENODEV;
1385 spin_lock_irqsave(&xpd->lock, flags);
1386 priv = xpd->priv;
1387 BUG_ON(!priv);
1388 seq_printf(sfile, "\t%-17s: ", "Channel");
1389 for_each_line(xpd, i) {
1390 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
1391 && !IS_SET(PHONEDEV(xpd).digital_inputs, i)) {
1392 seq_printf(sfile, "%4d ", i % 10);
1393 }
1394 }
1395 seq_printf(sfile, "\nLeds:");
1396 seq_printf(sfile, "\n\t%-17s: ", "state");
1397 for_each_line(xpd, i) {
1398 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
1399 && !IS_SET(PHONEDEV(xpd).digital_inputs, i)) {
1400 seq_printf(sfile, " %d%d ",
1401 IS_SET(priv->ledstate[LED_GREEN], i),
1402 IS_SET(priv->ledstate[LED_RED], i));
1403 }
1404 }
1405 seq_printf(sfile, "\n\t%-17s: ", "blinking");
1406 for_each_line(xpd, i) {
1407 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
1408 && !IS_SET(PHONEDEV(xpd).digital_inputs, i)) {
1409 seq_printf(sfile, " %d%d ",
1410 IS_BLINKING(priv, i, LED_GREEN),
1411 IS_BLINKING(priv, i, LED_RED));
1412 }
1413 }
1414 seq_printf(sfile, "\nBattery-Data:");
1415 seq_printf(sfile, "\n\t%-17s: ", "voltage");
1416 for_each_line(xpd, i) {
1417 seq_printf(sfile, "%4d ", priv->battery_voltage[i]);
1418 }
1419 seq_printf(sfile, "\n\t%-17s: ", "current");
1420 for_each_line(xpd, i) {
1421 seq_printf(sfile, "%4d ", priv->battery_current[i]);
1422 }
1423 seq_printf(sfile, "\nBattery:");
1424 seq_printf(sfile, "\n\t%-17s: ", "on");
1425 for_each_line(xpd, i) {
1426 char *bat;
1427
1428 if (priv->battery[i] == BATTERY_ON)
1429 bat = "+";
1430 else if (priv->battery[i] == BATTERY_OFF)
1431 bat = "-";
1432 else
1433 bat = ".";
1434 seq_printf(sfile, "%4s ", bat);
1435 }
1436 seq_printf(sfile, "\n\t%-17s: ", "debounce");
1437 for_each_line(xpd, i) {
1438 seq_printf(sfile, "%4d ", priv->nobattery_debounce[i]);
1439 }
1440 seq_printf(sfile, "\nPolarity-Reverse:");
1441 seq_printf(sfile, "\n\t%-17s: ", "polarity");
1442 for_each_line(xpd, i) {
1443 char *polname;
1444
1445 if (priv->polarity[i] == POL_POSITIVE)
1446 polname = "+";
1447 else if (priv->polarity[i] == POL_NEGATIVE)
1448 polname = "-";
1449 else
1450 polname = ".";
1451 seq_printf(sfile, "%4s ", polname);
1452 }
1453 seq_printf(sfile, "\n\t%-17s: ", "debounce");
1454 for_each_line(xpd, i) {
1455 seq_printf(sfile, "%4d ", priv->polarity_debounce[i]);
1456 }
1457 seq_printf(sfile, "\nPower-Denial:");
1458 seq_printf(sfile, "\n\t%-17s: ", "power");
1459 for_each_line(xpd, i) {
1460 char *curr;
1461
1462 if (priv->power[i] == POWER_ON)
1463 curr = "+";
1464 else if (priv->power[i] == POWER_OFF)
1465 curr = "-";
1466 else
1467 curr = ".";
1468 seq_printf(sfile, "%4s ", curr);
1469 }
1470 seq_printf(sfile, "\n\t%-17s: ", "safezone");
1471 for_each_line(xpd, i) {
1472 seq_printf(sfile, "%4d ", priv->power_denial_safezone[i]);
1473 }
1474 seq_printf(sfile, "\n\t%-17s: ", "delay");
1475 for_each_line(xpd, i) {
1476 seq_printf(sfile, "%4d ", priv->power_denial_delay[i]);
1477 }
1478 #ifdef WITH_METERING
1479 seq_printf(sfile, "\nMetering:");
1480 seq_printf(sfile, "\n\t%-17s: ", "count");
1481 for_each_line(xpd, i) {
1482 seq_printf(sfile, "%4d ", priv->metering_count[i]);
1483 }
1484 #endif
1485 seq_printf(sfile, "\n");
1486 spin_unlock_irqrestore(&xpd->lock, flags);
1487 return 0;
1488 }
1489
1490 static int proc_fxo_info_open(struct inode *inode, struct file *file)
1491 {
1492 return single_open(file, proc_fxo_info_show, PDE_DATA(inode));
1493 }
1494
1495 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
1496 static const struct proc_ops proc_fxo_info_ops = {
1497 .proc_open = proc_fxo_info_open,
1498 .proc_read = seq_read,
1499 .proc_lseek = seq_lseek,
1500 .proc_release = single_release,
1501 };
1502 #else
1503 static const struct file_operations proc_fxo_info_ops = {
1504 .owner = THIS_MODULE,
1505 .open = proc_fxo_info_open,
1506 .read = seq_read,
1507 .llseek = seq_lseek,
1508 .release = single_release,
1509 };
1510 #endif
1511
1512 #ifdef WITH_METERING
1513 static int proc_xpd_metering_show(struct seq_file *sfile, void *not_used)
1514 {
1515 unsigned long flags;
1516 xpd_t *xpd = sfile->private;
1517 struct FXO_priv_data *priv;
1518 int i;
1519
1520 if (!xpd)
1521 return -ENODEV;
1522 priv = xpd->priv;
1523 BUG_ON(!priv);
1524 spin_lock_irqsave(&xpd->lock, flags);
1525 seq_printf(sfile, "# Chan\tMeter (since last read)\n");
1526 for_each_line(xpd, i) {
1527 seq_printf(sfile, "%d\t%d\n", i, priv->metering_count[i]);
1528 }
1529 spin_unlock_irqrestore(&xpd->lock, flags);
1530 /* Zero meters */
1531 for_each_line(xpd, i)
1532 priv->metering_count[i] = 0;
1533 return 0;
1534 }
1535
1536 static int proc_xpd_metering_open(struct inode *inode, struct file *file)
1537 {
1538 return single_open(file, proc_xpd_metering_show, PDE_DATA(inode));
1539 }
1540
1541 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
1542 static const struct proc_ops proc_xpd_metering_ops = {
1543 .proc_open = proc_xpd_metering_open,
1544 .proc_read = seq_read,
1545 .proc_lseek = seq_lseek,
1546 .proc_release = single_release,
1547 };
1548 #else
1549 static const struct file_operations proc_xpd_metering_ops = {
1550 .owner = THIS_MODULE,
1551 .open = proc_xpd_metering_open,
1552 .read = seq_read,
1553 .llseek = seq_lseek,
1554 .release = single_release,
1555 };
1556 #endif
1557
1558 #endif
1559 #endif
1560
1561 static DEVICE_ATTR_READER(fxo_battery_show, dev, buf)
1562 {
1563 xpd_t *xpd;
1564 struct FXO_priv_data *priv;
1565 unsigned long flags;
1566 int len = 0;
1567 int i;
1568
1569 BUG_ON(!dev);
1570 xpd = dev_to_xpd(dev);
1571 if (!xpd)
1572 return -ENODEV;
1573 priv = xpd->priv;
1574 BUG_ON(!priv);
1575 spin_lock_irqsave(&xpd->lock, flags);
1576 for_each_line(xpd, i) {
1577 char bat;
1578
1579 if (priv->battery[i] == BATTERY_ON)
1580 bat = '+';
1581 else if (priv->battery[i] == BATTERY_OFF)
1582 bat = '-';
1583 else
1584 bat = '.';
1585 len += sprintf(buf + len, "%c ", bat);
1586 }
1587 len += sprintf(buf + len, "\n");
1588 spin_unlock_irqrestore(&xpd->lock, flags);
1589 return len;
1590 }
1591
1592 static DEVICE_ATTR(fxo_battery, S_IRUGO, fxo_battery_show, NULL);
1593
1594 static int fxo_xpd_probe(struct device *dev)
1595 {
1596 xpd_t *xpd;
1597 int ret;
1598
1599 xpd = dev_to_xpd(dev);
1600 /* Is it our device? */
1601 if (xpd->xpd_type != XPD_TYPE_FXO) {
1602 XPD_ERR(xpd, "drop suggestion for %s (%d)\n", dev_name(dev),
1603 xpd->xpd_type);
1604 return -EINVAL;
1605 }
1606 XPD_DBG(DEVICES, xpd, "SYSFS\n");
1607 ret = device_create_file(dev, &dev_attr_fxo_battery);
1608 if (ret) {
1609 XPD_ERR(xpd, "%s: device_create_file(fxo_battery) failed: %d\n",
1610 __func__, ret);
1611 goto fail_fxo_battery;
1612 }
1613 return 0;
1614 fail_fxo_battery:
1615 return ret;
1616 }
1617
1618 static int fxo_xpd_remove(struct device *dev)
1619 {
1620 xpd_t *xpd;
1621
1622 xpd = dev_to_xpd(dev);
1623 XPD_DBG(DEVICES, xpd, "SYSFS\n");
1624 device_remove_file(dev, &dev_attr_fxo_battery);
1625 return 0;
1626 }
1627
1628 static struct xpd_driver fxo_driver = {
1629 .xpd_type = XPD_TYPE_FXO,
1630 .driver = {
1631 .name = "fxo",
1632 .owner = THIS_MODULE,
1633 .probe = fxo_xpd_probe,
1634 .remove = fxo_xpd_remove}
1635 };
1636
1637 static int __init card_fxo_startup(void)
1638 {
1639 int ret;
1640
1641 if (ring_debounce <= 0) {
1642 ERR("ring_debounce=%d. Must be positive number of ticks\n",
1643 ring_debounce);
1644 return -EINVAL;
1645 }
1646 if ((ret = xpd_driver_register(&fxo_driver.driver)) < 0)
1647 return ret;
1648 #ifdef WITH_METERING
1649 INFO("FEATURE: WITH METERING Detection\n");
1650 #else
1651 INFO("FEATURE: NO METERING Detection\n");
1652 #endif
1653 xproto_register(&PROTO_TABLE(FXO));
1654 return 0;
1655 }
1656
1657 static void __exit card_fxo_cleanup(void)
1658 {
1659 xproto_unregister(&PROTO_TABLE(FXO));
1660 xpd_driver_unregister(&fxo_driver.driver);
1661 }
1662
1663 MODULE_DESCRIPTION("XPP FXO Card Driver");
1664 MODULE_AUTHOR("Oron Peled <[email protected]>");
1665 MODULE_LICENSE("GPL");
1666 MODULE_ALIAS_XPD(XPD_TYPE_FXO);
1667
1668 module_init(card_fxo_startup);
1669 module_exit(card_fxo_cleanup);