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