Debian Codegraph @

drivers/dahdi/xpp/card_fxs.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/seq_file.h>
28 #include "xpd.h"
29 #include "xproto.h"
30 #include "xpp_dahdi.h"
31 #include "card_fxs.h"
32 #include "dahdi_debug.h"
33 #include "xbus-core.h"
34
35 static const char rcsid[] = "$Id$";
36
37 /* must be before dahdi_debug.h */
38 static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
39 static DEF_PARM_BOOL(reversepolarity, 0, 0644, "Reverse Line Polarity");
40 static DEF_PARM_BOOL(dtmf_detection, 1, 0644, "Do DTMF detection in hardware");
41 #ifdef POLL_DIGITAL_INPUTS
42 static DEF_PARM(uint, poll_digital_inputs, 1000, 0644, "Poll Digital Inputs");
43 #endif
44 static DEF_PARM(uint, poll_chan_linefeed, 30000, 0644, "Poll Channel Linefeed");
45
46 static DEF_PARM_BOOL(vmwi_ioctl, 1, 0644,
47 "Asterisk support VMWI notification via ioctl");
48 static DEF_PARM_BOOL(ring_trapez, 0, 0664, "Use trapezoid ring type");
49 static DEF_PARM_BOOL(lower_ringing_noise, 0, 0664,
50 "Lower ringing noise (may loose CallerID)");
51
52 /* Signaling is opposite (fxo signalling for fxs card) */
53 #if 1
54 #define FXS_DEFAULT_SIGCAP \
55 (DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS)
56 #else
57 #define FXS_DEFAULT_SIGCAP \
58 (DAHDI_SIG_SF | DAHDI_SIG_EM)
59 #endif
60
61 #define VMWI_TYPE(priv, pos, type) \
62 ((priv)->vmwisetting[pos].vmwi_type & DAHDI_VMWI_ ## type)
63 #define VMWI_NEON(priv, pos) VMWI_TYPE(priv, pos, HVAC)
64
65 #define LINES_DIGI_OUT 2
66 #define LINES_DIGI_INP 4
67
68 enum fxs_leds {
69 LED_GREEN,
70 LED_RED,
71 OUTPUT_RELAY,
72 };
73
74 #define NUM_LEDS 2
75
76 /* Shortcuts */
77 #define SLIC_WRITE 1
78 #define SLIC_READ 0
79 #define SLIC_DIRECT_REQUEST(xbus, xpd, port, writing, reg, dL) \
80 xpp_register_request((xbus), (xpd), (port), \
81 (writing), (reg), 0, 0, (dL), 0, 0, 0, 0)
82 #define SLIC_INDIRECT_REQUEST(xbus, xpd, port, writing, reg, dL, dH) \
83 xpp_register_request((xbus), (xpd), (port), \
84 (writing), 0x1E, 1, (reg), (dL), 1, (dH), 0, 0)
85 #define EXP_REQUEST(xbus, xpd, writing, reg, dL, dH) \
86 xpp_register_request((xbus), (xpd), 0, \
87 (writing), (reg), 1, 0, (dL), 1, (dH), 0, 1)
88 #define RAM_REQUEST(xbus, xpd, port, writing, addr, data) \
89 xpp_ram_request((xbus), (xpd), (port), \
90 (writing), (__u8)(addr), (__u8)((addr) >> 8), (__u8)(data), (__u8)((data) >> 8), (__u8)((data) >> 16), (__u8)((data) >> 24), 0)
91
92 #define VALID_PORT(port) \
93 (((port) >= 0 && (port) <= 7) || (port) == PORT_BROADCAST)
94
95 #define REG_TYPE1_DIGITAL_IOCTRL 0x06 /* LED and RELAY control */
96
97 /* Values of SLIC linefeed control register (0x40) */
98 enum fxs_state {
99 FXS_LINE_OPEN = 0x00, /* Open */
100 FXS_LINE_ACTIVE = 0x01, /* Forward active */
101 FXS_LINE_OHTRANS = 0x02, /* Forward on-hook transmission */
102 FXS_LINE_TIPOPEN = 0x03, /* TIP open */
103 FXS_LINE_RING = 0x04, /* Ringing */
104 FXS_LINE_REV_ACTIVE = 0x05, /* Reverse active */
105 FXS_LINE_REV_OHTRANS = 0x06, /* Reverse on-hook transmission */
106 FXS_LINE_RING_OPEN = 0x07 /* RING open */
107 };
108
109 enum neon_state {
110 END_NEON = 0,
111 INIT_NEON = 1,
112 };
113
114 #define FXS_LINE_POL_ACTIVE \
115 ((reversepolarity) ? FXS_LINE_REV_ACTIVE : FXS_LINE_ACTIVE)
116 #define FXS_LINE_POL_OHTRANS \
117 ((reversepolarity) ? FXS_LINE_REV_OHTRANS : FXS_LINE_OHTRANS)
118
119 /* FXS type 1 registers */
120 #define REG_TYPE1_RINGCON 0x22 /* 34 - Ringing Oscillator Control */
121
122 /*
123 * DTMF detection
124 */
125 #define REG_TYPE1_DTMF_DECODE 0x18 /* 24 - DTMF Decode Status */
126 #define REG_TYPE1_BATTERY 0x42 /* 66 - Battery Feed Control */
127 #define REG_TYPE1_BATTERY_BATSL BIT(1) /* Battery Feed Select */
128
129 /* 68 - Loop Closure/Ring Trip Detect Status */
130 #define REG_TYPE1_LOOPCLOSURE 0x44
131 #define REG_TYPE1_LOOPCLOSURE_ZERO 0xF8 /* Loop Closure zero bits. */
132 #define REG_TYPE1_LOOPCLOSURE_LCR BIT(0) /* Loop Closure Detect Indicator. */
133
134 /* FXS type 6 registers */
135 #define REG_TYPE6_RINGCON 0x26 /* 38 - Ringing Oscillator Control */
136
137 /* 34 - Loop Closure/Ring Trip Detect Status */
138 #define REG_TYPE6_LCRRTP 0x22
139 #define REG_TYPE6_LCRRTP_ZERO 0xF0 /* Loop Closure zero bits. */
140 #define REG_TYPE6_LCRRTP_LCR BIT(1) /* Loop Closure Detect Indicator. */
141
142 #define REG_TYPE6_TONEN 0x3E /* 62 - Hardware DTMF detection */
143 #define REG_TYPE6_TONEN_DTMF_DIS BIT(2) /* DTMF Disable */
144 #define REG_TYPE6_LINEFEED 0x1E /* 30 - Linefeed */
145 #define REG_TYPE6_TONDTMF 0x3C /* 60 - DTMF Decode Status */
146 #define REG_TYPE6_EXP_GPIOA 0x12 /* I/O Expander GPIOA */
147 #define REG_TYPE6_EXP_GPIOB 0x13 /* I/O Expander GPIOB */
148 #define REG_TYPE6_ENHANCE 0x2F /* 47 - Enhance */
149 #define REG_TYPE6_USERSTAT 0x42 /* 66 - Userstat */
150 #define REG_TYPE6_DIAG1 0x47 /* 71 - Diag1 */
151 #define RAM_TYPE6_SLOPE_VLIM 634
152 #define SLOPE_VLIM_DFLT 0x1E655196L
153 #define SLOPE_VLIM_MWI 0x8000000L
154 #define RAM_TYPE6_VBATH_EXPECT 767
155 #define VBATH_EXPECT_DFLT 0x2B10A20L
156 #define VBATH_EXPECT_MWI 0x6147AB2L
157
158 /*---------------- FXS Protocol Commands ----------------------------------*/
159
160 static bool fxs_packet_is_valid(xpacket_t *pack);
161 static void fxs_packet_dump(const char *msg, xpacket_t *pack);
162 #ifdef CONFIG_PROC_FS
163 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
164 static const struct proc_ops proc_fxs_info_ops;
165 #else
166 static const struct file_operations proc_fxs_info_ops;
167 #endif
168 #ifdef WITH_METERING
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
170 static const struct proc_ops proc_xpd_metering_ops;
171 #else
172 static const struct file_operations proc_xpd_metering_ops;
173 #endif
174 #endif
175 #endif
176 static void start_stop_vm_led(xbus_t *xbus, xpd_t *xpd, lineno_t pos);
177
178 #define PROC_FXS_INFO_FNAME "fxs_info"
179 #ifdef WITH_METERING
180 #define PROC_METERING_FNAME "metering_gen"
181 #endif
182
183 struct FXS_priv_data {
184 #ifdef WITH_METERING
185 struct proc_dir_entry *meteringfile;
186 #endif
187 struct proc_dir_entry *fxs_info;
188 xpp_line_t ledstate[NUM_LEDS]; /* 0 - OFF, 1 - ON */
189 xpp_line_t ledcontrol[NUM_LEDS]; /* 0 - OFF, 1 - ON */
190 xpp_line_t search_fsk_pattern;
191 xpp_line_t found_fsk_pattern;
192 xpp_line_t update_offhook_state;
193 xpp_line_t want_dtmf_events; /* what dahdi want */
194 xpp_line_t want_dtmf_mute; /* what dahdi want */
195 xpp_line_t prev_key_down; /* DTMF down sets the bit */
196 xpp_line_t neon_blinking;
197 xpp_line_t neonstate;
198 xpp_line_t vbat_h; /* High voltage */
199 ktime_t prev_key_time[CHANNELS_PERXPD];
200 int led_counter[NUM_LEDS][CHANNELS_PERXPD];
201 int overheat_reset_counter[CHANNELS_PERXPD];
202 int ohttimer[CHANNELS_PERXPD];
203 #define OHT_TIMER 6000 /* How long after RING to retain OHT */
204 /* IDLE changing hook state */
205 enum fxs_state idletxhookstate[CHANNELS_PERXPD];
206 enum fxs_state lasttxhook[CHANNELS_PERXPD];
207 enum fxs_state polledhook[CHANNELS_PERXPD];
208 struct dahdi_vmwi_info vmwisetting[CHANNELS_PERXPD];
209 };
210
211 /*
212 * LED counter values:
213 * n>1 : BLINK every n'th tick
214 */
215 #define LED_COUNTER(priv, pos, color) ((priv)->led_counter[color][pos])
216 #define IS_BLINKING(priv, pos, color) (LED_COUNTER(priv, pos, color) > 0)
217 #define MARK_BLINK(priv, pos, color, t) \
218 ((priv)->led_counter[color][pos] = (t))
219 #define MARK_OFF(priv, pos, color) \
220 do { \
221 BIT_CLR((priv)->ledcontrol[color], (pos)); \
222 MARK_BLINK((priv), (pos), (color), 0); \
223 } while (0)
224 #define MARK_ON(priv, pos, color) \
225 do { \
226 BIT_SET((priv)->ledcontrol[color], (pos)); \
227 MARK_BLINK((priv), (pos), (color), 0); \
228 } while (0)
229
230 #define LED_BLINK_RING (1000/8) /* in ticks */
231
232 /*---------------- FXS: Static functions ----------------------------------*/
233 static int set_vm_led_mode(xbus_t *xbus, xpd_t *xpd, int pos,
234 unsigned int msg_waiting);
235
236 static int do_chan_power(xbus_t *xbus, xpd_t *xpd, lineno_t chan, bool on)
237 {
238 struct FXS_priv_data *priv;
239 unsigned long *p;
240 int was;
241
242 BUG_ON(!xbus);
243 BUG_ON(!xpd);
244 if (XPD_HW(xpd).type == 6) {
245 LINE_DBG(SIGNAL, xpd, chan, "is ignored in Si32260\n");
246 return 0;
247 }
248 priv = xpd->priv;
249 p = (unsigned long *)&priv->vbat_h;
250 if (on)
251 was = test_and_set_bit(chan, p) != 0;
252 else
253 was = test_and_clear_bit(chan, p) != 0;
254 if (was == on) {
255 LINE_DBG(SIGNAL, xpd, chan,
256 "%s (same, ignored)\n", (on) ? "up" : "down");
257 return 0;
258 }
259 LINE_DBG(SIGNAL, xpd, chan, "%s\n", (on) ? "up" : "down");
260 return SLIC_DIRECT_REQUEST(xbus, xpd, chan, SLIC_WRITE, REG_TYPE1_BATTERY,
261 (on) ? (int)REG_TYPE1_BATTERY_BATSL : 0x00);
262 }
263
264 static int linefeed_control(xbus_t *xbus, xpd_t *xpd, lineno_t chan,
265 enum fxs_state value)
266 {
267 struct FXS_priv_data *priv;
268 bool want_vbat_h;
269
270 priv = xpd->priv;
271 /*
272 * Should we drop vbat_h only during actuall ring?
273 * - It would lower the noise caused to other channels by
274 * group ringing
275 * - But it may also stop CallerID from passing through the SLIC
276 */
277 want_vbat_h = value == FXS_LINE_RING;
278 if (lower_ringing_noise || want_vbat_h)
279 do_chan_power(xbus, xpd, chan, want_vbat_h);
280 LINE_DBG(SIGNAL, xpd, chan, "value=0x%02X\n", value);
281 priv->lasttxhook[chan] = value;
282 if (XPD_HW(xpd).type == 6) {
283 int ret;
284
285 /* Make sure NEON state is off for */
286 if (value == FXS_LINE_POL_OHTRANS && IS_SET(priv->neon_blinking, chan))
287 set_vm_led_mode(xpd->xbus, xpd, chan, 0);
288 ret = SLIC_DIRECT_REQUEST(xbus, xpd, chan, SLIC_WRITE, REG_TYPE6_LINEFEED, value);
289 if (value == FXS_LINE_POL_ACTIVE && PHONEDEV(xpd).msg_waiting[chan])
290 set_vm_led_mode(xpd->xbus, xpd, chan, PHONEDEV(xpd).msg_waiting[chan]);
291 return ret;
292 } else {
293 return SLIC_DIRECT_REQUEST(xbus, xpd, chan, SLIC_WRITE, 0x40, value);
294 }
295 return 0;
296 }
297
298 static void vmwi_search(xpd_t *xpd, lineno_t pos, bool on)
299 {
300 struct FXS_priv_data *priv;
301
302 priv = xpd->priv;
303 BUG_ON(!xpd);
304 if (VMWI_NEON(priv, pos) && on) {
305 LINE_DBG(SIGNAL, xpd, pos, "START\n");
306 BIT_SET(priv->search_fsk_pattern, pos);
307 } else {
308 LINE_DBG(SIGNAL, xpd, pos, "STOP\n");
309 BIT_CLR(priv->search_fsk_pattern, pos);
310 }
311 }
312
313 /*
314 * LED and RELAY control is done via SLIC register 0x06:
315 * 7 6 5 4 3 2 1 0
316 * +-----+-----+-----+-----+-----+-----+-----+-----+
317 * | M2 | M1 | M3 | C2 | O1 | O3 | C1 | C3 |
318 * +-----+-----+-----+-----+-----+-----+-----+-----+
319 *
320 * Cn - Control bit (control one digital line)
321 * On - Output bit (program a digital line for output)
322 * Mn - Mask bit (only the matching output control bit is affected)
323 *
324 * C3 - OUTPUT RELAY (0 - OFF, 1 - ON)
325 * C1 - GREEN LED (0 - OFF, 1 - ON)
326 * O3 - Output RELAY (this line is output)
327 * O1 - Output GREEN (this line is output)
328 * C2 - RED LED (0 - OFF, 1 - ON)
329 * M3 - Mask RELAY. (1 - C3 effect the OUTPUT RELAY)
330 * M2 - Mask RED. (1 - C2 effect the RED LED)
331 * M1 - Mask GREEN. (1 - C1 effect the GREEN LED)
332 *
333 * The OUTPUT RELAY (actually a relay out) is connected to line 0 and 4 only.
334 */
335
336 // GREEN RED OUTPUT RELAY
337 static const int led_register_mask[] = { BIT(7), BIT(6), BIT(5) };
338 static const int led_register_vals[] = { BIT(4), BIT(1), BIT(0) };
339
340 /*
341 * pos can be:
342 * - A line number
343 * - ALL_LINES. This is not valid anymore since 8-Jan-2007.
344 */
345 static int do_led(xpd_t *xpd, lineno_t chan, __u8 which, bool on)
346 {
347 int ret = 0;
348 struct FXS_priv_data *priv;
349 int value;
350 xbus_t *xbus;
351
352 BUG_ON(!xpd);
353 BUG_ON(chan == ALL_LINES);
354 xbus = xpd->xbus;
355 priv = xpd->priv;
356 which = which % NUM_LEDS;
357 if (IS_SET(PHONEDEV(xpd).digital_outputs, chan)
358 || IS_SET(PHONEDEV(xpd).digital_inputs, chan))
359 goto out;
360 if (chan == PORT_BROADCAST) {
361 priv->ledstate[which] = (on) ? ~0 : 0;
362 } else {
363 if (on)
364 BIT_SET(priv->ledstate[which], chan);
365 else
366 BIT_CLR(priv->ledstate[which], chan);
367 }
368 LINE_DBG(LEDS, xpd, chan, "LED: (type=%d) which=%d -- %s\n", XPD_HW(xpd).type, which,
369 (on) ? "on" : "off");
370 if (XPD_HW(xpd).type == 6) {
371 int mask = 1 << chan;
372 value = (on) << chan;
373 XPD_DBG(LEDS, xpd, "LED(%d): 0x%0X (mask: 0x%0X)\n", chan,
374 value, mask);
375 if (which == LED_GREEN) { /* other leds ignored */
376 ret = EXP_REQUEST(xbus, xpd, SLIC_WRITE,
377 REG_TYPE6_EXP_GPIOA, value, mask);
378 }
379 } else {
380 value = BIT(2) | BIT(3);
381 value |= ((BIT(5) | BIT(6) | BIT(7)) & ~led_register_mask[which]);
382 if (on)
383 value |= led_register_vals[which];
384 ret = SLIC_DIRECT_REQUEST(xbus, xpd, chan, SLIC_WRITE,
385 REG_TYPE1_DIGITAL_IOCTRL, value);
386 }
387 return 0;
388 out:
389 return ret;
390 }
391
392 static inline void set_mwi_led(xpd_t *xpd, int pos, int on)
393 {
394 struct FXS_priv_data *priv;
395 BUG_ON(!xpd);
396 priv = xpd->priv;
397
398 if (XPD_HW(xpd).type != 6)
399 return;
400 if (on) {
401 if (! IS_SET(priv->neonstate, pos)) {
402 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_ENHANCE, 0x00);
403 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_USERSTAT, 0x04);
404 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_DIAG1, 0x0F);
405 BIT_SET(priv->neonstate, pos);
406 }
407 } else {
408 if (IS_SET(priv->neonstate, pos)) {
409 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_DIAG1, 0x00);
410 BIT_CLR(priv->neonstate, pos);
411 }
412 }
413 }
414
415 static void blink_mwi(xpd_t *xpd)
416 {
417 struct FXS_priv_data *priv;
418 unsigned int timer_count;
419 int i;
420
421 BUG_ON(!xpd);
422 priv = xpd->priv;
423 timer_count = xpd->timer_count;
424 for_each_line(xpd, i) {
425 unsigned int msgs = PHONEDEV(xpd).msg_waiting[i];
426 /* LED duty cycle: 300ms on, 700ms off */
427 unsigned int in_range = (timer_count % 1000) >= 0 && (timer_count % 1000) <= 300;
428
429 if (!IS_OFFHOOK(xpd, i) && msgs && in_range &&
430 IS_SET(priv->neon_blinking,i) && priv->ohttimer[i] == 0)
431 set_mwi_led(xpd, i, 1);
432 else
433 set_mwi_led(xpd, i, 0);
434 }
435 }
436
437 static void handle_fxs_leds(xpd_t *xpd)
438 {
439 int i;
440 const enum fxs_leds colors[] = { LED_GREEN, LED_RED };
441 enum fxs_leds color;
442 unsigned int timer_count;
443 struct FXS_priv_data *priv;
444
445 BUG_ON(!xpd);
446 priv = xpd->priv;
447 timer_count = xpd->timer_count;
448 for (color = 0; color < ARRAY_SIZE(colors); color++) {
449 for_each_line(xpd, i) {
450 if (IS_SET
451 (PHONEDEV(xpd).digital_outputs | PHONEDEV(xpd).
452 digital_inputs, i))
453 continue;
454 /* Blinking? */
455 if ((xpd->blink_mode & BIT(i)) || IS_BLINKING(priv, i, color)) {
456 int mod_value = LED_COUNTER(priv, i, color);
457
458 if (!mod_value)
459 /* safety value */
460 mod_value = DEFAULT_LED_PERIOD;
461 /* led state is toggled */
462 if ((timer_count % mod_value) == 0) {
463 LINE_DBG(LEDS, xpd, i, "ledstate=%s\n",
464 (IS_SET
465 (priv->ledstate[color],
466 i)) ? "ON" : "OFF");
467 if (!IS_SET(priv->ledstate[color], i))
468 do_led(xpd, i, color, 1);
469 else
470 do_led(xpd, i, color, 0);
471 }
472 } else
473 if (IS_SET
474 (priv->ledcontrol[color] & ~priv->
475 ledstate[color], i)) {
476 do_led(xpd, i, color, 1);
477 } else
478 if (IS_SET
479 (~priv->ledcontrol[color] & priv->
480 ledstate[color], i)) {
481 do_led(xpd, i, color, 0);
482 }
483
484 }
485 }
486 }
487
488 static void restore_leds(xpd_t *xpd)
489 {
490 struct FXS_priv_data *priv;
491 int i;
492
493 priv = xpd->priv;
494 for_each_line(xpd, i) {
495 if (IS_OFFHOOK(xpd, i))
496 MARK_ON(priv, i, LED_GREEN);
497 else
498 MARK_OFF(priv, i, LED_GREEN);
499 }
500 }
501
502 #ifdef WITH_METERING
503 static int metering_gen(xpd_t *xpd, lineno_t chan, bool on)
504 {
505 __u8 value = (on) ? 0x94 : 0x00;
506
507 if (XPD_HW(xpd).type == 6) {
508 XBUS_NOTICE("Metering not supported with FXS type 6");
509 return 0;
510 }
511 LINE_DBG(SIGNAL, xpd, chan, "METERING Generate: %s\n",
512 (on) ? "ON" : "OFF");
513 return SLIC_DIRECT_REQUEST(xpd->xbus, xpd, chan, SLIC_WRITE, 0x23,
514 value);
515 }
516 #endif
517
518 /*---------------- FXS: Methods -------------------------------------------*/
519
520 static void fxs_proc_remove(xbus_t *xbus, xpd_t *xpd)
521 {
522 struct FXS_priv_data *priv;
523
524 BUG_ON(!xpd);
525 priv = xpd->priv;
526 #ifdef CONFIG_PROC_FS
527 #ifdef WITH_METERING
528 if (priv->meteringfile) {
529 XPD_DBG(PROC, xpd, "Removing xpd metering tone file\n");
530 remove_proc_entry(PROC_METERING_FNAME, xpd->proc_xpd_dir);
531 priv->meteringfile = NULL;
532 }
533 #endif
534 if (priv->fxs_info) {
535 XPD_DBG(PROC, xpd, "Removing xpd FXS_INFO file\n");
536 remove_proc_entry(PROC_FXS_INFO_FNAME, xpd->proc_xpd_dir);
537 priv->fxs_info = NULL;
538 }
539 #endif
540 }
541
542 static int fxs_proc_create(xbus_t *xbus, xpd_t *xpd)
543 {
544 struct FXS_priv_data *priv;
545
546 BUG_ON(!xpd);
547 priv = xpd->priv;
548
549 #ifdef CONFIG_PROC_FS
550 XPD_DBG(PROC, xpd, "Creating FXS_INFO file\n");
551 priv->fxs_info = proc_create_data(PROC_FXS_INFO_FNAME, 0444,
552 xpd->proc_xpd_dir,
553 &proc_fxs_info_ops, xpd);
554 if (!priv->fxs_info) {
555 XPD_ERR(xpd, "Failed to create proc file '%s'\n",
556 PROC_FXS_INFO_FNAME);
557 fxs_proc_remove(xbus, xpd);
558 return -EINVAL;
559 }
560 SET_PROC_DIRENTRY_OWNER(priv->fxs_info);
561 #ifdef WITH_METERING
562 XPD_DBG(PROC, xpd, "Creating Metering tone file\n");
563 priv->meteringfile = proc_create_data(PROC_METERING_FNAME, 0200,
564 xpd->proc_xpd_dir,
565 &proc_xpd_metering_ops, xpd);
566 if (!priv->meteringfile) {
567 XPD_ERR(xpd, "Failed to create proc file '%s'\n",
568 PROC_METERING_FNAME);
569 fxs_proc_remove(xbus, xpd);
570 return -EINVAL;
571 }
572 #endif
573 #endif
574 return 0;
575 }
576
577 static xpd_t *FXS_card_new(xbus_t *xbus, int unit, int subunit,
578 const xproto_table_t *proto_table,
579 const struct unit_descriptor *unit_descriptor,
580 bool to_phone)
581 {
582 xpd_t *xpd = NULL;
583 int channels;
584 int subunit_ports;
585 int regular_channels;
586 struct FXS_priv_data *priv;
587 int i;
588 int d_inputs = 0;
589 int d_outputs = 0;
590
591 if (!to_phone) {
592 XBUS_NOTICE(xbus,
593 "XPD=%d%d: try to instanciate FXS with reverse direction\n",
594 unit, subunit);
595 return NULL;
596 }
597 subunit_ports = unit_descriptor->numchips * unit_descriptor->ports_per_chip;
598 if (unit_descriptor->subtype == 2)
599 regular_channels = min(6, subunit_ports);
600 else
601 regular_channels = min(8, subunit_ports);
602 channels = regular_channels;
603 /* Calculate digital inputs/outputs */
604 if (unit == 0 && unit_descriptor->subtype != 4 && unit_descriptor->numchips != 4) {
605 channels += 6; /* 2 DIGITAL OUTPUTS, 4 DIGITAL INPUTS */
606 d_inputs = LINES_DIGI_INP;
607 d_outputs = LINES_DIGI_OUT;
608 }
609 xpd =
610 xpd_alloc(xbus, unit, subunit,
611 sizeof(struct FXS_priv_data), proto_table, unit_descriptor, channels);
612 if (!xpd)
613 return NULL;
614 /* Initialize digital inputs/outputs */
615 if (d_inputs) {
616 XBUS_DBG(GENERAL, xbus, "Initialize %d digital inputs\n",
617 d_inputs);
618 PHONEDEV(xpd).digital_inputs =
619 BITMASK(d_inputs) << (regular_channels + d_outputs);
620 } else
621 XBUS_DBG(GENERAL, xbus, "No digital inputs\n");
622 if (d_outputs) {
623 XBUS_DBG(GENERAL, xbus, "Initialize %d digital outputs\n",
624 d_outputs);
625 PHONEDEV(xpd).digital_outputs =
626 BITMASK(d_outputs) << regular_channels;
627 } else
628 XBUS_DBG(GENERAL, xbus, "No digital outputs\n");
629 PHONEDEV(xpd).direction = TO_PHONE;
630 xpd->type_name = "FXS";
631 if (fxs_proc_create(xbus, xpd) < 0)
632 goto err;
633 priv = xpd->priv;
634 for_each_line(xpd, i) {
635 priv->idletxhookstate[i] = FXS_LINE_POL_ACTIVE;
636 }
637 return xpd;
638 err:
639 xpd_free(xpd);
640 return NULL;
641 }
642
643 static int FXS_card_init(xbus_t *xbus, xpd_t *xpd)
644 {
645 struct FXS_priv_data *priv;
646 int ret = 0;
647 int i;
648
649 BUG_ON(!xpd);
650 priv = xpd->priv;
651 /*
652 * Setup ring timers
653 */
654 /* Software controled ringing (for CID) */
655 /* Ringing Oscilator Control */
656 if (XPD_HW(xpd).type == 6) {
657 ret = SLIC_DIRECT_REQUEST(xbus, xpd, PORT_BROADCAST, SLIC_WRITE,
658 REG_TYPE6_RINGCON, 0x00);
659 } else {
660 ret = SLIC_DIRECT_REQUEST(xbus, xpd, PORT_BROADCAST, SLIC_WRITE,
661 REG_TYPE1_RINGCON, 0x00);
662 }
663 if (ret < 0)
664 goto err;
665 for_each_line(xpd, i) {
666 if (XPD_HW(xpd).type == 6)
667 /* An arbitrary value that is not FXS_LINE_OPEN */
668 priv->polledhook[i] = FXS_LINE_ACTIVE;
669 linefeed_control(xbus, xpd, i, FXS_LINE_POL_ACTIVE);
670 }
671 XPD_DBG(GENERAL, xpd, "done\n");
672 for_each_line(xpd, i) {
673 do_led(xpd, i, LED_GREEN, 0);
674 do_led(xpd, i, LED_RED, 0);
675 }
676 for_each_line(xpd, i) {
677 do_led(xpd, i, LED_GREEN, 1);
678 msleep(50);
679 }
680 for_each_line(xpd, i) {
681 do_led(xpd, i, LED_GREEN, 0);
682 msleep(50);
683 }
684 restore_leds(xpd);
685 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
686 /*
687 * We should query our offhook state long enough time after we
688 * set the linefeed_control()
689 * So we do this after the LEDs
690 */
691 for_each_line(xpd, i) {
692 if (IS_SET
693 (PHONEDEV(xpd).digital_outputs | PHONEDEV(xpd).
694 digital_inputs, i))
695 continue;
696 if (XPD_HW(xpd).type == 6) {
697 SLIC_DIRECT_REQUEST(xbus, xpd, i, SLIC_READ, REG_TYPE6_LCRRTP,
698 0);
699 } else {
700 SLIC_DIRECT_REQUEST(xbus, xpd, i, SLIC_READ, REG_TYPE1_LOOPCLOSURE,
701 0);
702 }
703 }
704 return 0;
705 err:
706 fxs_proc_remove(xbus, xpd);
707 XPD_ERR(xpd, "Failed initializing registers (%d)\n", ret);
708 return ret;
709 }
710
711 static int FXS_card_remove(xbus_t *xbus, xpd_t *xpd)
712 {
713 BUG_ON(!xpd);
714 XPD_DBG(GENERAL, xpd, "\n");
715 fxs_proc_remove(xbus, xpd);
716 return 0;
717 }
718
719 static int FXS_card_dahdi_preregistration(xpd_t *xpd, bool on)
720 {
721 xbus_t *xbus;
722 struct FXS_priv_data *priv;
723 int i;
724
725 BUG_ON(!xpd);
726 xbus = xpd->xbus;
727 BUG_ON(!xbus);
728 priv = xpd->priv;
729 BUG_ON(!priv);
730 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
731 PHONEDEV(xpd).span.spantype = SPANTYPE_ANALOG_FXS;
732 for_each_line(xpd, i) {
733 struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
734
735 XPD_DBG(GENERAL, xpd, "setting FXS channel %d\n", i);
736 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)) {
737 snprintf(cur_chan->name, MAX_CHANNAME,
738 "XPP_OUT/%02d/%1d%1d/%d", xbus->num,
739 xpd->addr.unit, xpd->addr.subunit, i);
740 } else if (IS_SET(PHONEDEV(xpd).digital_inputs, i)) {
741 snprintf(cur_chan->name, MAX_CHANNAME,
742 "XPP_IN/%02d/%1d%1d/%d", xbus->num,
743 xpd->addr.unit, xpd->addr.subunit, i);
744 } else {
745 snprintf(cur_chan->name, MAX_CHANNAME,
746 "XPP_FXS/%02d/%1d%1d/%d", xbus->num,
747 xpd->addr.unit, xpd->addr.subunit, i);
748 }
749 cur_chan->chanpos = i + 1;
750 cur_chan->pvt = xpd;
751 cur_chan->sigcap = FXS_DEFAULT_SIGCAP;
752 if (!vmwi_ioctl) {
753 /* Old asterisk, assume default VMWI type */
754 priv->vmwisetting[i].vmwi_type = DAHDI_VMWI_HVAC;
755 }
756 }
757 for_each_line(xpd, i) {
758 MARK_ON(priv, i, LED_GREEN);
759 msleep(4);
760 MARK_ON(priv, i, LED_RED);
761 }
762 return 0;
763 }
764
765 static int FXS_card_dahdi_postregistration(xpd_t *xpd, bool on)
766 {
767 xbus_t *xbus;
768 struct FXS_priv_data *priv;
769 int i;
770
771 BUG_ON(!xpd);
772 xbus = xpd->xbus;
773 BUG_ON(!xbus);
774 priv = xpd->priv;
775 BUG_ON(!priv);
776 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
777 for_each_line(xpd, i) {
778 MARK_OFF(priv, i, LED_GREEN);
779 msleep(2);
780 MARK_OFF(priv, i, LED_RED);
781 msleep(2);
782 }
783 restore_leds(xpd);
784 return 0;
785 }
786
787 /*
788 * Called with XPD spinlocked
789 */
790 static void __do_mute_dtmf(xpd_t *xpd, int pos, bool muteit)
791 {
792 struct FXS_priv_data *priv;
793
794 priv = xpd->priv;
795 LINE_DBG(SIGNAL, xpd, pos, "%s\n", (muteit) ? "MUTE" : "UNMUTE");
796 if (muteit)
797 BIT_SET(PHONEDEV(xpd).mute_dtmf, pos);
798 else
799 BIT_CLR(PHONEDEV(xpd).mute_dtmf, pos);
800 /* already spinlocked */
801 CALL_PHONE_METHOD(card_pcm_recompute, xpd, priv->search_fsk_pattern);
802 }
803
804 struct ring_reg_param {
805 int is_indirect;
806 int regno;
807 uint8_t h_val;
808 uint8_t l_val;
809 };
810
811 enum ring_types {
812 RING_TYPE_NEON = 0,
813 RING_TYPE_TRAPEZ,
814 RING_TYPE_NORMAL,
815 };
816
817 struct byte_pair {
818 uint8_t h_val;
819 uint8_t l_val;
820 };
821
822 struct ring_reg_params {
823 const int is_indirect;
824 const int regno;
825 struct byte_pair values[1 + RING_TYPE_NORMAL - RING_TYPE_NEON];
826 };
827
828 #define REG_ENTRY(di, reg, vh1, vl1, vh2, vl2, vh3, vl3) \
829 { (di), (reg), .values = { \
830 [RING_TYPE_NEON] = { .h_val = (vh1), .l_val = (vl1) }, \
831 [RING_TYPE_TRAPEZ] = { .h_val = (vh2), .l_val = (vl2) }, \
832 [RING_TYPE_NORMAL] = { .h_val = (vh3), .l_val = (vl3) }, \
833 }, \
834 }
835
836 static struct ring_reg_params ring_parameters[] = {
837 /* INDIR REG NEON TRAPEZ NORMAL */
838 REG_ENTRY(1, 0x16, 0xE8, 0x03, 0xC8, 0x00, 0x00, 0x00),
839 REG_ENTRY(1, 0x15, 0xEF, 0x7B, 0xAB, 0x5E, 0x77, 0x01),
840 REG_ENTRY(1, 0x14, 0x9F, 0x00, 0x8C, 0x01, 0xFD, 0x7E),
841
842 REG_ENTRY(0, 0x22, 0x00, 0x19, 0x00, 0x01, 0x00, 0x00),
843
844 REG_ENTRY(0, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00),
845 REG_ENTRY(0, 0x31, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00),
846 REG_ENTRY(0, 0x32, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00),
847 REG_ENTRY(0, 0x33, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00),
848
849 REG_ENTRY(1, 0x1D, 0x00, 0x46, 0x00, 0x36, 0x00, 0x36),
850 };
851
852 static void set_neon_state(xbus_t *xbus, xpd_t *xpd, int pos,
853 enum neon_state ns)
854 {
855 struct FXS_priv_data *priv;
856
857 LINE_DBG(SIGNAL, xpd, pos, "set NEON -> %d\n", ns);
858 priv = xpd->priv;
859 if (ns == INIT_NEON)
860 BIT_SET(priv->neon_blinking, pos);
861 else
862 BIT_CLR(priv->neon_blinking, pos);
863 if (XPD_HW(xpd).type == 6) {
864 switch (ns) {
865 case INIT_NEON:
866 RAM_REQUEST(xbus, xpd, pos, SLIC_WRITE, RAM_TYPE6_VBATH_EXPECT, VBATH_EXPECT_MWI << 3);
867 //RAM_REQUEST(xbus, xpd, pos, SLIC_WRITE, RAM_TYPE6_SLOPE_VLIM, SLOPE_VLIM_MWI << 3);
868 break;
869 default:
870 LINE_DBG(REGS, xpd, pos, "0x%04X: R 0x\n", RAM_TYPE6_SLOPE_VLIM);
871 set_mwi_led(xpd, pos, 0); /* Cannot have NEON LED during OHT (type == 6) */
872 SLIC_DIRECT_REQUEST(xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_USERSTAT, 0x00);
873 SLIC_DIRECT_REQUEST(xbus, xpd, pos, SLIC_WRITE, REG_TYPE6_ENHANCE, 0x10);
874 RAM_REQUEST(xbus, xpd, pos, SLIC_WRITE, RAM_TYPE6_VBATH_EXPECT, VBATH_EXPECT_DFLT << 3);
875 RAM_REQUEST(xbus, xpd, pos, SLIC_WRITE, RAM_TYPE6_SLOPE_VLIM, SLOPE_VLIM_DFLT << 3);
876 break;
877 }
878 }
879 }
880 static int send_ring_parameters(xbus_t *xbus, xpd_t *xpd, int pos,
881 enum ring_types rtype)
882 {
883 const struct ring_reg_params *p;
884 const struct byte_pair *v;
885 int ret = 0;
886 int i;
887
888 if (XPD_HW(xpd).type == 6)
889 return 0;
890 if (rtype < RING_TYPE_NEON || rtype > RING_TYPE_NORMAL)
891 return -EINVAL;
892 for (i = 0; i < ARRAY_SIZE(ring_parameters); i++) {
893 p = &ring_parameters[i];
894 v = &(p->values[rtype]);
895 if (p->is_indirect) {
896 LINE_DBG(REGS, xpd, pos,
897 "[%d] 0x%02X: I 0x%02X 0x%02X\n",
898 i, p->regno, v->h_val, v->l_val);
899 ret = SLIC_INDIRECT_REQUEST(xbus, xpd, pos, SLIC_WRITE,
900 p->regno, v->h_val, v->l_val);
901 if (ret < 0) {
902 LINE_ERR(xpd, pos,
903 "Failed: 0x%02X: I 0x%02X, 0x%02X\n",
904 p->regno, v->h_val, v->l_val);
905 break;
906 }
907 } else {
908 LINE_DBG(REGS, xpd, pos, "[%d] 0x%02X: D 0x%02X\n",
909 i, p->regno, v->l_val);
910 ret = SLIC_DIRECT_REQUEST(xbus, xpd, pos, SLIC_WRITE,
911 p->regno, v->l_val);
912 if (ret < 0) {
913 LINE_ERR(xpd, pos,
914 "Failed: 0x%02X: D 0x%02X\n",
915 p->regno, v->l_val);
916 break;
917 }
918 }
919 }
920 return ret;
921 }
922
923 static int set_vm_led_mode(xbus_t *xbus, xpd_t *xpd, int pos,
924 unsigned int msg_waiting)
925 {
926 int ret = 0;
927 struct FXS_priv_data *priv;
928 BUG_ON(!xbus);
929 BUG_ON(!xpd);
930
931 priv = xpd->priv;
932 if (VMWI_NEON(priv, pos) && msg_waiting) {
933 /* A write to register 0x40 will now turn on/off the VM led */
934 LINE_DBG(SIGNAL, xpd, pos, "NEON\n");
935 set_neon_state(xbus, xpd, pos, INIT_NEON);
936 ret = send_ring_parameters(xbus, xpd, pos, RING_TYPE_NEON);
937 } else if (ring_trapez) {
938 LINE_DBG(SIGNAL, xpd, pos, "RINGER: Trapez ring\n");
939 set_neon_state(xbus, xpd, pos, END_NEON);
940 ret = send_ring_parameters(xbus, xpd, pos, RING_TYPE_TRAPEZ);
941 } else {
942 /* A write to register 0x40 will now turn on/off the ringer */
943 LINE_DBG(SIGNAL, xpd, pos, "RINGER\n");
944 set_neon_state(xbus, xpd, pos, END_NEON);
945 ret = send_ring_parameters(xbus, xpd, pos, RING_TYPE_NORMAL);
946 }
947 return (ret ? -EPROTO : 0);
948 }
949
950 static void start_stop_vm_led(xbus_t *xbus, xpd_t *xpd, lineno_t pos)
951 {
952 struct FXS_priv_data *priv;
953 unsigned int msgs;
954
955 BUG_ON(!xpd);
956 if (IS_SET
957 (PHONEDEV(xpd).digital_outputs | PHONEDEV(xpd).digital_inputs, pos))
958 return;
959 priv = xpd->priv;
960 msgs = PHONEDEV(xpd).msg_waiting[pos];
961 LINE_DBG(SIGNAL, xpd, pos, "%s\n", (msgs) ? "ON" : "OFF");
962 set_vm_led_mode(xbus, xpd, pos, msgs);
963 if (XPD_HW(xpd).type == 1) {
964 do_chan_power(xbus, xpd, pos, msgs > 0);
965 linefeed_control(xbus, xpd, pos,
966 (msgs > 0) ? FXS_LINE_RING : priv->idletxhookstate[pos]);
967 }
968 }
969
970 static int relay_out(xpd_t *xpd, int pos, bool on)
971 {
972 int ret = 0;
973 int value = 0;
974 int which = pos;
975
976 BUG_ON(!xpd);
977 /* map logical position to output port number (0/1) */
978 which -= (XPD_HW(xpd).subtype == 2) ? 6 : 8;
979 LINE_DBG(SIGNAL, xpd, pos, "which=%d -- %s\n", which,
980 (on) ? "on" : "off");
981 if (XPD_HW(xpd).type == 6) {
982 int relay_values_type6[] = { 0x01, 0x40 };
983 which = which % ARRAY_SIZE(relay_values_type6);
984 if (on)
985 value |= relay_values_type6[which];
986 ret = EXP_REQUEST(xpd->xbus, xpd, SLIC_WRITE,
987 REG_TYPE6_EXP_GPIOB, value, relay_values_type6[which]);
988 } else {
989 int relay_channels_type1[] = { 0, 4 };
990 which = which % ARRAY_SIZE(relay_channels_type1);
991 value = BIT(2) | BIT(3);
992 value |=
993 ((BIT(5) | BIT(6) | BIT(7)) & ~led_register_mask[OUTPUT_RELAY]);
994 if (on)
995 value |= led_register_vals[OUTPUT_RELAY];
996 ret = SLIC_DIRECT_REQUEST(xpd->xbus, xpd, relay_channels_type1[which],
997 SLIC_WRITE, REG_TYPE1_DIGITAL_IOCTRL, value);
998 }
999 return ret;
1000 }
1001
1002 static int send_ring(xpd_t *xpd, lineno_t chan, bool on)
1003 {
1004 int ret = 0;
1005 xbus_t *xbus;
1006 struct FXS_priv_data *priv;
1007 enum fxs_state value = (on) ? FXS_LINE_RING : FXS_LINE_POL_ACTIVE;
1008
1009 BUG_ON(!xpd);
1010 xbus = xpd->xbus;
1011 BUG_ON(!xbus);
1012 LINE_DBG(SIGNAL, xpd, chan, "%s\n", (on) ? "on" : "off");
1013 priv = xpd->priv;
1014 set_vm_led_mode(xbus, xpd, chan, 0);
1015 do_chan_power(xbus, xpd, chan, on); /* Power up (for ring) */
1016 ret = linefeed_control(xbus, xpd, chan, value);
1017 if (on) {
1018 MARK_BLINK(priv, chan, LED_GREEN, LED_BLINK_RING);
1019 } else {
1020 if (IS_BLINKING(priv, chan, LED_GREEN))
1021 MARK_BLINK(priv, chan, LED_GREEN, 0);
1022 }
1023 return ret;
1024 }
1025
1026 static int FXS_card_hooksig(xpd_t *xpd, int pos, enum dahdi_txsig txsig)
1027 {
1028 struct FXS_priv_data *priv;
1029 int ret = 0;
1030 struct dahdi_chan *chan = NULL;
1031 enum fxs_state txhook;
1032 unsigned long flags;
1033
1034 LINE_DBG(SIGNAL, xpd, pos, "%s\n", txsig2str(txsig));
1035 priv = xpd->priv;
1036 BUG_ON(PHONEDEV(xpd).direction != TO_PHONE);
1037 if (IS_SET(PHONEDEV(xpd).digital_inputs, pos)) {
1038 LINE_DBG(SIGNAL, xpd, pos,
1039 "Ignoring signal sent to digital input line\n");
1040 return 0;
1041 }
1042 if (SPAN_REGISTERED(xpd))
1043 chan = XPD_CHAN(xpd, pos);
1044 switch (txsig) {
1045 case DAHDI_TXSIG_ONHOOK:
1046 spin_lock_irqsave(&xpd->lock, flags);
1047 PHONEDEV(xpd).ringing[pos] = 0;
1048 oht_pcm(xpd, pos, 0);
1049 vmwi_search(xpd, pos, 0);
1050 BIT_CLR(priv->want_dtmf_events, pos);
1051 BIT_CLR(priv->want_dtmf_mute, pos);
1052 __do_mute_dtmf(xpd, pos, 0);
1053 spin_unlock_irqrestore(&xpd->lock, flags);
1054 if (IS_SET(PHONEDEV(xpd).digital_outputs, pos)) {
1055 LINE_DBG(SIGNAL, xpd, pos, "%s -> digital output OFF\n",
1056 txsig2str(txsig));
1057 ret = relay_out(xpd, pos, 0);
1058 return ret;
1059 }
1060 if (priv->lasttxhook[pos] == FXS_LINE_OPEN) {
1061 /*
1062 * Restore state after KEWL hangup.
1063 */
1064 LINE_DBG(SIGNAL, xpd, pos, "KEWL STOP\n");
1065 linefeed_control(xpd->xbus, xpd, pos,
1066 FXS_LINE_POL_ACTIVE);
1067 if (IS_OFFHOOK(xpd, pos))
1068 MARK_ON(priv, pos, LED_GREEN);
1069 }
1070 ret = send_ring(xpd, pos, 0); // RING off
1071 if (!IS_OFFHOOK(xpd, pos))
1072 start_stop_vm_led(xpd->xbus, xpd, pos);
1073 txhook = priv->lasttxhook[pos];
1074 if (chan) {
1075 switch (chan->sig) {
1076 case DAHDI_SIG_EM:
1077 case DAHDI_SIG_FXOKS:
1078 case DAHDI_SIG_FXOLS:
1079 txhook = priv->idletxhookstate[pos];
1080 break;
1081 case DAHDI_SIG_FXOGS:
1082 txhook = FXS_LINE_TIPOPEN;
1083 break;
1084 }
1085 }
1086 ret = linefeed_control(xpd->xbus, xpd, pos, txhook);
1087 break;
1088 case DAHDI_TXSIG_OFFHOOK:
1089 if (IS_SET(PHONEDEV(xpd).digital_outputs, pos)) {
1090 LINE_NOTICE(xpd, pos,
1091 "%s -> Is digital output. Ignored\n",
1092 txsig2str(txsig));
1093 return -EINVAL;
1094 }
1095 txhook = priv->lasttxhook[pos];
1096 if (PHONEDEV(xpd).ringing[pos]) {
1097 oht_pcm(xpd, pos, 1);
1098 txhook = FXS_LINE_OHTRANS;
1099 }
1100 PHONEDEV(xpd).ringing[pos] = 0;
1101 if (chan) {
1102 switch (chan->sig) {
1103 case DAHDI_SIG_EM:
1104 txhook = FXS_LINE_POL_ACTIVE;
1105 break;
1106 default:
1107 txhook = priv->idletxhookstate[pos];
1108 break;
1109 }
1110 }
1111 ret = linefeed_control(xpd->xbus, xpd, pos, txhook);
1112 break;
1113 case DAHDI_TXSIG_START:
1114 PHONEDEV(xpd).ringing[pos] = 1;
1115 oht_pcm(xpd, pos, 0);
1116 vmwi_search(xpd, pos, 0);
1117 if (IS_SET(PHONEDEV(xpd).digital_outputs, pos)) {
1118 LINE_DBG(SIGNAL, xpd, pos, "%s -> digital output ON\n",
1119 txsig2str(txsig));
1120 ret = relay_out(xpd, pos, 1);
1121 return ret;
1122 }
1123 ret = send_ring(xpd, pos, 1); // RING on
1124 break;
1125 case DAHDI_TXSIG_KEWL:
1126 if (IS_SET(PHONEDEV(xpd).digital_outputs, pos)) {
1127 LINE_DBG(SIGNAL, xpd, pos,
1128 "%s -> Is digital output. Ignored\n",
1129 txsig2str(txsig));
1130 return -EINVAL;
1131 }
1132 linefeed_control(xpd->xbus, xpd, pos, FXS_LINE_OPEN);
1133 MARK_OFF(priv, pos, LED_GREEN);
1134 break;
1135 default:
1136 XPD_NOTICE(xpd, "%s: Can't set tx state to %s (%d)\n", __func__,
1137 txsig2str(txsig), txsig);
1138 ret = -EINVAL;
1139 }
1140 return ret;
1141 }
1142
1143 static int set_vmwi(xpd_t *xpd, int pos, unsigned long arg)
1144 {
1145 struct FXS_priv_data *priv;
1146 struct dahdi_vmwi_info vmwisetting;
1147 const int vmwi_flags =
1148 DAHDI_VMWI_LREV | DAHDI_VMWI_HVDC | DAHDI_VMWI_HVAC;
1149
1150 priv = xpd->priv;
1151 BUG_ON(!priv);
1152 if (copy_from_user
1153 (&vmwisetting, (__user void *)arg, sizeof(vmwisetting)))
1154 return -EFAULT;
1155 if ((vmwisetting.vmwi_type & ~vmwi_flags) != 0) {
1156 LINE_NOTICE(xpd, pos, "Bad DAHDI_VMWI_CONFIG: 0x%X\n",
1157 vmwisetting.vmwi_type);
1158 return -EINVAL;
1159 }
1160 LINE_DBG(SIGNAL, xpd, pos, "DAHDI_VMWI_CONFIG: 0x%X\n",
1161 vmwisetting.vmwi_type);
1162 if (VMWI_TYPE(priv, pos, LREV)) {
1163 LINE_NOTICE(xpd, pos,
1164 "%s: VMWI(lrev) is not implemented yet. Ignored.\n",
1165 __func__);
1166 }
1167 if (VMWI_TYPE(priv, pos, HVDC)) {
1168 LINE_NOTICE(xpd, pos,
1169 "%s: VMWI(hvdc) is not implemented yet. Ignored.\n",
1170 __func__);
1171 }
1172 if (VMWI_TYPE(priv, pos, HVAC)) {
1173 ; /* VMWI_NEON */
1174 }
1175 if (priv->vmwisetting[pos].vmwi_type == 0) {
1176 ; /* Disable VMWI */
1177 }
1178 priv->vmwisetting[pos] = vmwisetting;
1179 set_vm_led_mode(xpd->xbus, xpd, pos, PHONEDEV(xpd).msg_waiting[pos]);
1180 return 0;
1181 }
1182
1183 static int hardware_dtmf_control(xpd_t *xpd, int pos, bool on)
1184 {
1185 int ret = 0;
1186
1187 LINE_DBG(SIGNAL, xpd, pos, "%s: %s\n", __func__, (on) ? "on" : "off");
1188 if (XPD_HW(xpd).type == 6) {
1189 int value = (on) ? 0xE0 : REG_TYPE6_TONEN_DTMF_DIS;
1190 ret = SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE,
1191 REG_TYPE6_TONEN, value);
1192 } else {
1193 ret = SLIC_DIRECT_REQUEST(xpd->xbus, xpd, pos, SLIC_WRITE, 0x17, on);
1194 }
1195 return ret;
1196 }
1197
1198 /*
1199 * Private ioctl()
1200 * We don't need it now, since we detect vmwi via FSK patterns
1201 */
1202 static int FXS_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd,
1203 unsigned long arg)
1204 {
1205 struct FXS_priv_data *priv;
1206 xbus_t *xbus;
1207 int val;
1208 unsigned long flags;
1209
1210 BUG_ON(!xpd);
1211 priv = xpd->priv;
1212 BUG_ON(!priv);
1213 xbus = xpd->xbus;
1214 BUG_ON(!xbus);
1215 if (!XBUS_IS(xbus, READY))
1216 return -ENODEV;
1217 if (pos < 0 || pos >= PHONEDEV(xpd).channels) {
1218 XPD_NOTICE(xpd, "Bad channel number %d in %s(), cmd=%u\n", pos,
1219 __func__, cmd);
1220 return -EINVAL;
1221 }
1222
1223 switch (cmd) {
1224 case DAHDI_ONHOOKTRANSFER:
1225 if (get_user(val, (int __user *)arg))
1226 return -EFAULT;
1227 LINE_DBG(SIGNAL, xpd, pos, "DAHDI_ONHOOKTRANSFER (%d millis)\n",
1228 val);
1229 if (IS_SET
1230 (PHONEDEV(xpd).digital_inputs | PHONEDEV(xpd).
1231 digital_outputs, pos))
1232 return 0; /* Nothing to do */
1233 oht_pcm(xpd, pos, 1); /* Get ready of VMWI FSK tones */
1234 if (priv->lasttxhook[pos] == FXS_LINE_POL_ACTIVE
1235 || IS_SET(priv->neon_blinking, pos)) {
1236 priv->ohttimer[pos] = val;
1237 priv->idletxhookstate[pos] = FXS_LINE_POL_OHTRANS;
1238 vmwi_search(xpd, pos, 1);
1239 CALL_PHONE_METHOD(card_pcm_recompute, xpd,
1240 priv->search_fsk_pattern);
1241 LINE_DBG(SIGNAL, xpd, pos,
1242 "Start OHT_TIMER. wanted_pcm_mask=0x%X\n",
1243 PHONEDEV(xpd).wanted_pcm_mask);
1244 }
1245 if (VMWI_NEON(priv, pos) && !IS_OFFHOOK(xpd, pos))
1246 start_stop_vm_led(xbus, xpd, pos);
1247 return 0;
1248 case DAHDI_TONEDETECT:
1249 if (get_user(val, (int __user *)arg))
1250 return -EFAULT;
1251 LINE_DBG(SIGNAL, xpd, pos,
1252 "DAHDI_TONEDETECT: %s %s (dtmf_detection=%s)\n",
1253 (val & DAHDI_TONEDETECT_ON) ? "ON" : "OFF",
1254 (val & DAHDI_TONEDETECT_MUTE) ? "MUTE" : "NO-MUTE",
1255 (dtmf_detection ? "YES" : "NO"));
1256 if (!dtmf_detection) {
1257 spin_lock_irqsave(&xpd->lock, flags);
1258 if (IS_SET(priv->want_dtmf_events, pos)) {
1259 /*
1260 * Detection mode changed:
1261 * Disable DTMF interrupts
1262 */
1263 }
1264 hardware_dtmf_control(xpd, pos, 0);
1265 BIT_CLR(priv->want_dtmf_events, pos);
1266 BIT_CLR(priv->want_dtmf_mute, pos);
1267 __do_mute_dtmf(xpd, pos, 0);
1268 spin_unlock_irqrestore(&xpd->lock, flags);
1269 return -ENOTTY;
1270 }
1271 /*
1272 * During natively bridged calls, Asterisk
1273 * will request one of the sides to stop sending
1274 * dtmf events. Check the requested state.
1275 */
1276 spin_lock_irqsave(&xpd->lock, flags);
1277 if (val & DAHDI_TONEDETECT_ON) {
1278 if (!IS_SET(priv->want_dtmf_events, pos)) {
1279 /*
1280 * Detection mode changed:
1281 * Enable DTMF interrupts
1282 */
1283 LINE_DBG(SIGNAL, xpd, pos,
1284 "DAHDI_TONEDETECT: "
1285 "Enable Hardware DTMF\n");
1286 hardware_dtmf_control(xpd, pos, 1);
1287 }
1288 BIT_SET(priv->want_dtmf_events, pos);
1289 } else {
1290 if (IS_SET(priv->want_dtmf_events, pos)) {
1291 /*
1292 * Detection mode changed:
1293 * Disable DTMF interrupts
1294 */
1295 LINE_DBG(SIGNAL, xpd, pos,
1296 "DAHDI_TONEDETECT: "
1297 "Disable Hardware DTMF\n");
1298 hardware_dtmf_control(xpd, pos, 0);
1299 }
1300 BIT_CLR(priv->want_dtmf_events, pos);
1301 }
1302 if (val & DAHDI_TONEDETECT_MUTE) {
1303 BIT_SET(priv->want_dtmf_mute, pos);
1304 } else {
1305 BIT_CLR(priv->want_dtmf_mute, pos);
1306 __do_mute_dtmf(xpd, pos, 0);
1307 }
1308 spin_unlock_irqrestore(&xpd->lock, flags);
1309 return 0;
1310 case DAHDI_SETPOLARITY:
1311 if (get_user(val, (int __user *)arg))
1312 return -EFAULT;
1313 /*
1314 * Asterisk may send us this if chan_dahdi config
1315 * has "hanguponpolarityswitch=yes" to notify
1316 * that the other side has hanged up.
1317 *
1318 * This has no effect on normal phone (but we may
1319 * be connected to another FXO equipment).
1320 * note that this chan_dahdi settings has different
1321 * meaning for FXO, where it signals polarity
1322 * reversal *detection* logic.
1323 *
1324 * It seems that sometimes we get this from
1325 * asterisk in wrong state (e.g: while ringing).
1326 * In these cases, silently ignore it.
1327 */
1328 if (priv->lasttxhook[pos] == FXS_LINE_RING
1329 || priv->lasttxhook[pos] == FXS_LINE_OPEN) {
1330 LINE_DBG(SIGNAL, xpd, pos,
1331 "DAHDI_SETPOLARITY: %s Cannot change "
1332 "when lasttxhook=0x%X\n",
1333 (val) ? "ON" : "OFF", priv->lasttxhook[pos]);
1334 return -EINVAL;
1335 }
1336 LINE_DBG(SIGNAL, xpd, pos, "DAHDI_SETPOLARITY: %s\n",
1337 (val) ? "ON" : "OFF");
1338 if ((val && !reversepolarity) || (!val && reversepolarity))
1339 priv->lasttxhook[pos] |= FXS_LINE_RING;
1340 else
1341 priv->lasttxhook[pos] &= ~FXS_LINE_RING;
1342 linefeed_control(xbus, xpd, pos, priv->lasttxhook[pos]);
1343 return 0;
1344 case DAHDI_VMWI_CONFIG:
1345 if (set_vmwi(xpd, pos, arg) < 0)
1346 return -EINVAL;
1347 return 0;
1348 case DAHDI_VMWI: /* message-waiting led control */
1349 if (get_user(val, (int __user *)arg))
1350 return -EFAULT;
1351 if (!vmwi_ioctl) {
1352 static bool notified;
1353
1354 if (!notified) {
1355 notified = true;
1356 LINE_NOTICE(xpd, pos,
1357 "Got DAHDI_VMWI notification "
1358 "but vmwi_ioctl parameter is off. "
1359 "Ignoring.\n");
1360 }
1361 return 0;
1362 }
1363 /* Digital inputs/outputs don't have VM leds */
1364 if (IS_SET
1365 (PHONEDEV(xpd).digital_inputs | PHONEDEV(xpd).
1366 digital_outputs, pos))
1367 return 0;
1368 PHONEDEV(xpd).msg_waiting[pos] = val;
1369 LINE_DBG(SIGNAL, xpd, pos, "DAHDI_VMWI: %s\n",
1370 (val) ? "yes" : "no");
1371 return 0;
1372 default:
1373 report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
1374 }
1375 return -ENOTTY;
1376 }
1377
1378 static int FXS_card_open(xpd_t *xpd, lineno_t chan)
1379 {
1380 struct FXS_priv_data *priv;
1381
1382 BUG_ON(!xpd);
1383 priv = xpd->priv;
1384 if (IS_OFFHOOK(xpd, chan))
1385 LINE_NOTICE(xpd, chan, "Already offhook during open. OK.\n");
1386 else
1387 LINE_DBG(SIGNAL, xpd, chan, "is onhook\n");
1388 /*
1389 * Delegate updating dahdi to FXS_card_tick():
1390 * The problem is that dahdi_hooksig() is spinlocking the channel and
1391 * we are called by dahdi with the spinlock already held on the
1392 * same channel.
1393 */
1394 BIT_SET(priv->update_offhook_state, chan);
1395 return 0;
1396 }
1397
1398 static int FXS_card_close(xpd_t *xpd, lineno_t chan)
1399 {
1400 struct FXS_priv_data *priv;
1401
1402 BUG_ON(!xpd);
1403 LINE_DBG(GENERAL, xpd, chan, "\n");
1404 priv = xpd->priv;
1405 priv->idletxhookstate[chan] = FXS_LINE_POL_ACTIVE;
1406 return 0;
1407 }
1408
1409 #ifdef POLL_DIGITAL_INPUTS
1410 /*
1411 * INPUT polling is done via SLIC register 0x06 (same as LEDS):
1412 * 7 6 5 4 3 2 1 0
1413 * +-----+-----+-----+-----+-----+-----+-----+-----+
1414 * | I1 | I3 | | | I2 | I4 | | |
1415 * +-----+-----+-----+-----+-----+-----+-----+-----+
1416 *
1417 */
1418 static int input_ports_type1[] = {
1419 /* slic = input_port */
1420 [0] = -1,
1421 [1] = -1,
1422 [2] = 2,
1423 [3] = 3,
1424 [4] = -1,
1425 [5] = -1,
1426 [6] = 0,
1427 [7] = 1,
1428 };
1429
1430 static void poll_inputs(xpd_t *xpd)
1431 {
1432 int i;
1433
1434 BUG_ON(xpd->xbus_idx != 0); // Only unit #0 has digital inputs
1435 if (XPD_HW(xpd).type == 6) {
1436 EXP_REQUEST(xpd->xbus, xpd, SLIC_READ,
1437 REG_TYPE6_EXP_GPIOB, 0, 0);
1438 } else {
1439 for (i = 0; i < ARRAY_SIZE(input_ports_type1); i++) {
1440 int pos = input_ports_type1[i];
1441 if (pos >= 0) {
1442 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, i, SLIC_READ, 0x06, 0);
1443 }
1444 }
1445 }
1446 }
1447 #endif
1448
1449 static void poll_linefeed(xpd_t *xpd)
1450 {
1451 struct FXS_priv_data *priv;
1452 int i;
1453
1454 if (XPD_HW(xpd).type != 6)
1455 return;
1456 if (xpd->xpd_state != XPD_STATE_READY)
1457 return;
1458 priv = xpd->priv;
1459 BUG_ON(!priv);
1460 BUG_ON(!xpd->xbus);
1461
1462 XPD_DBG(GENERAL, xpd, "periodic poll");
1463 for_each_line(xpd, i) {
1464 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)
1465 || IS_SET(PHONEDEV(xpd).digital_inputs, i))
1466 continue;
1467 if (priv->polledhook[i] == FXS_LINE_OPEN &&
1468 priv->lasttxhook[i] != FXS_LINE_OPEN) {
1469 LINE_NOTICE(xpd, i, "Overheat detected, resetting.");
1470 priv->overheat_reset_counter[i]++;
1471 linefeed_control(xpd->xbus, xpd, i,
1472 priv->lasttxhook[i]);
1473 }
1474 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, i, SLIC_READ,
1475 REG_TYPE6_LINEFEED, 0);
1476 }
1477 }
1478
1479 static void handle_linefeed(xpd_t *xpd)
1480 {
1481 struct FXS_priv_data *priv;
1482 int i;
1483
1484 BUG_ON(!xpd);
1485 priv = xpd->priv;
1486 BUG_ON(!priv);
1487 for_each_line(xpd, i) {
1488 if (priv->lasttxhook[i] == FXS_LINE_RING
1489 && !IS_SET(priv->neon_blinking, i)) {
1490 /* RINGing, prepare for OHT */
1491 priv->ohttimer[i] = OHT_TIMER;
1492 priv->idletxhookstate[i] = FXS_LINE_POL_OHTRANS;
1493 } else {
1494 if (priv->ohttimer[i]) {
1495 priv->ohttimer[i]--;
1496 if (!priv->ohttimer[i]) {
1497 LINE_DBG(SIGNAL, xpd, i,
1498 "ohttimer expired\n");
1499 priv->idletxhookstate[i] =
1500 FXS_LINE_POL_ACTIVE;
1501 oht_pcm(xpd, i, 0);
1502 vmwi_search(xpd, i, 0);
1503 if (priv->lasttxhook[i] ==
1504 FXS_LINE_POL_OHTRANS) {
1505 /* Apply the change if appropriate */
1506 linefeed_control(xpd->xbus, xpd,
1507 i,
1508 FXS_LINE_POL_ACTIVE);
1509 }
1510 }
1511 }
1512 }
1513 }
1514 }
1515
1516 /*
1517 * Optimized memcmp() like function. Only test for equality (true/false).
1518 * This optimization reduced the detect_vmwi() runtime by a factor of 3.
1519 */
1520 static inline bool mem_equal(const char a[], const char b[], size_t len)
1521 {
1522 int i;
1523
1524 for (i = 0; i < len; i++)
1525 if (a[i] != b[i])
1526 return 0;
1527 return 1;
1528 }
1529
1530 /*
1531 * Detect Voice Mail Waiting Indication
1532 */
1533 static void detect_vmwi(xpd_t *xpd)
1534 {
1535 struct FXS_priv_data *priv;
1536 xbus_t *xbus;
1537 static const __u8 FSK_COMMON_PATTERN[] =
1538 { 0xA8, 0x49, 0x22, 0x3B, 0x9F, 0xFF, 0x1F, 0xBB };
1539 static const __u8 FSK_ON_PATTERN[] =
1540 { 0xA2, 0x2C, 0x1F, 0x2C, 0xBB, 0xA1, 0xA5, 0xFF };
1541 static const __u8 FSK_OFF_PATTERN[] =
1542 { 0xA2, 0x2C, 0x28, 0xA5, 0xB1, 0x21, 0x49, 0x9F };
1543 int i;
1544 xpp_line_t ignore_mask;
1545
1546 BUG_ON(!xpd);
1547 xbus = xpd->xbus;
1548 priv = xpd->priv;
1549 BUG_ON(!priv);
1550 ignore_mask =
1551 PHONEDEV(xpd).offhook_state |
1552 ~(PHONEDEV(xpd).oht_pcm_pass) |
1553 ~(priv->search_fsk_pattern) |
1554 PHONEDEV(xpd).digital_inputs |
1555 PHONEDEV(xpd).digital_outputs;
1556 for_each_line(xpd, i) {
1557 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
1558 __u8 *writechunk = chan->writechunk;
1559
1560 if (IS_SET(ignore_mask, i))
1561 continue;
1562 #if 0
1563 if (writechunk[0] != 0x7F && writechunk[0] != 0) {
1564 int j;
1565
1566 LINE_DBG(GENERAL, xpd, i, "MSG:");
1567 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
1568 if (debug)
1569 printk(" %02X", writechunk[j]);
1570 }
1571 if (debug)
1572 printk("\n");
1573 }
1574 #endif
1575 if (unlikely
1576 (mem_equal
1577 (writechunk, FSK_COMMON_PATTERN, DAHDI_CHUNKSIZE))) {
1578 LINE_DBG(SIGNAL, xpd, i,
1579 "Found common FSK pattern. "
1580 "Start looking for ON/OFF patterns.\n");
1581 BIT_SET(priv->found_fsk_pattern, i);
1582 } else if (unlikely(IS_SET(priv->found_fsk_pattern, i))) {
1583 BIT_CLR(priv->found_fsk_pattern, i);
1584 oht_pcm(xpd, i, 0);
1585 if (unlikely
1586 (mem_equal
1587 (writechunk, FSK_ON_PATTERN, DAHDI_CHUNKSIZE))) {
1588 LINE_DBG(SIGNAL, xpd, i, "MSG WAITING ON\n");
1589 PHONEDEV(xpd).msg_waiting[i] = 1;
1590 start_stop_vm_led(xbus, xpd, i);
1591 } else
1592 if (unlikely
1593 (mem_equal
1594 (writechunk, FSK_OFF_PATTERN,
1595 DAHDI_CHUNKSIZE))) {
1596 LINE_DBG(SIGNAL, xpd, i, "MSG WAITING OFF\n");
1597 PHONEDEV(xpd).msg_waiting[i] = 0;
1598 start_stop_vm_led(xbus, xpd, i);
1599 } else {
1600 int j;
1601
1602 LINE_NOTICE(xpd, i, "MSG WAITING Unexpected:");
1603 for (j = 0; j < DAHDI_CHUNKSIZE; j++)
1604 printk(" %02X", writechunk[j]);
1605 printk("\n");
1606 }
1607 }
1608 }
1609 }
1610
1611 static int FXS_card_tick(xbus_t *xbus, xpd_t *xpd)
1612 {
1613 struct FXS_priv_data *priv;
1614
1615 BUG_ON(!xpd);
1616 priv = xpd->priv;
1617 BUG_ON(!priv);
1618 #ifdef POLL_DIGITAL_INPUTS
1619 if (poll_digital_inputs && PHONEDEV(xpd).digital_inputs) {
1620 if ((xpd->timer_count % poll_digital_inputs) == 0)
1621 poll_inputs(xpd);
1622 }
1623 #endif
1624 if ((xpd->timer_count % poll_chan_linefeed) == 0)
1625 poll_linefeed(xpd);
1626 handle_fxs_leds(xpd);
1627 handle_linefeed(xpd);
1628 if (XPD_HW(xpd).type == 6)
1629 blink_mwi(xpd);
1630 /*
1631 * Hack alert (FIXME):
1632 * Asterisk did FXS_card_open() and we wanted to report
1633 * offhook state. However, the channel is spinlocked by dahdi
1634 * so we marked it in the priv->update_offhook_state mask and
1635 * now we take care of notification to dahdi and Asterisk
1636 */
1637 if (priv->update_offhook_state) {
1638 enum dahdi_rxsig rxsig;
1639 int i;
1640
1641 for_each_line(xpd, i) {
1642 if (!IS_SET(priv->update_offhook_state, i))
1643 continue;
1644 rxsig =
1645 IS_OFFHOOK(xpd,
1646 i) ? DAHDI_RXSIG_OFFHOOK :
1647 DAHDI_RXSIG_ONHOOK;
1648 /* Notify after open() */
1649 notify_rxsig(xpd, i, rxsig);
1650 BIT_CLR(priv->update_offhook_state, i);
1651 }
1652 }
1653 if (SPAN_REGISTERED(xpd)) {
1654 if (!vmwi_ioctl && priv->search_fsk_pattern)
1655 detect_vmwi(xpd); /* Detect via FSK modulation */
1656 }
1657 return 0;
1658 }
1659
1660 /*---------------- FXS: HOST COMMANDS -------------------------------------*/
1661
1662 /*---------------- FXS: Astribank Reply Handlers --------------------------*/
1663
1664 /*
1665 * Should be called with spinlocked XPD
1666 */
1667 static void process_hookstate(xpd_t *xpd, xpp_line_t offhook,
1668 xpp_line_t change_mask)
1669 {
1670 xbus_t *xbus;
1671 struct FXS_priv_data *priv;
1672 int i;
1673
1674 BUG_ON(!xpd);
1675 BUG_ON(PHONEDEV(xpd).direction != TO_PHONE);
1676 xbus = xpd->xbus;
1677 priv = xpd->priv;
1678 XPD_DBG(SIGNAL, xpd, "offhook=0x%X change_mask=0x%X\n", offhook,
1679 change_mask);
1680 for_each_line(xpd, i) {
1681 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)
1682 || IS_SET(PHONEDEV(xpd).digital_inputs, i))
1683 continue;
1684 if (IS_SET(change_mask, i)) {
1685 PHONEDEV(xpd).ringing[i] = 0; /* No more ringing... */
1686 #ifdef WITH_METERING
1687 metering_gen(xpd, i, 0); /* Stop metering... */
1688 #endif
1689 MARK_BLINK(priv, i, LED_GREEN, 0);
1690 /*
1691 * Reset our previous DTMF memories...
1692 */
1693 BIT_CLR(priv->prev_key_down, i);
1694 priv->prev_key_time[i] = ktime_set(0L, 0UL);
1695 if (IS_SET(offhook, i)) {
1696 LINE_DBG(SIGNAL, xpd, i, "OFFHOOK\n");
1697 MARK_ON(priv, i, LED_GREEN);
1698 hookstate_changed(xpd, i, 1);
1699 } else {
1700 LINE_DBG(SIGNAL, xpd, i, "ONHOOK\n");
1701 MARK_OFF(priv, i, LED_GREEN);
1702 hookstate_changed(xpd, i, 0);
1703 }
1704 /*
1705 * Must switch to low power. In high power, an ONHOOK
1706 * won't be detected.
1707 */
1708 do_chan_power(xbus, xpd, i, 0);
1709 }
1710 }
1711 }
1712
1713 HANDLER_DEF(FXS, SIG_CHANGED)
1714 {
1715 xpp_line_t sig_status =
1716 RPACKET_FIELD(pack, FXS, SIG_CHANGED, sig_status);
1717 xpp_line_t sig_toggles =
1718 RPACKET_FIELD(pack, FXS, SIG_CHANGED, sig_toggles);
1719 unsigned long flags;
1720
1721 BUG_ON(!xpd);
1722 BUG_ON(PHONEDEV(xpd).direction != TO_PHONE);
1723 XPD_DBG(SIGNAL, xpd, "(PHONE) sig_toggles=0x%04X sig_status=0x%04X\n",
1724 sig_toggles, sig_status);
1725 #if 0
1726 Is this needed ? for_each_line(xpd, i) {
1727 // Power down (prevent overheating!!!)
1728 if (IS_SET(sig_toggles, i))
1729 do_chan_power(xpd->xbus, xpd, BIT(i), 0);
1730 }
1731 #endif
1732 spin_lock_irqsave(&xpd->lock, flags);
1733 process_hookstate(xpd, sig_status, sig_toggles);
1734 spin_unlock_irqrestore(&xpd->lock, flags);
1735 return 0;
1736 }
1737
1738 #ifdef POLL_DIGITAL_INPUTS
1739 static inline void notify_digital_input(xpd_t *xpd, int input_port, int offhook)
1740 {
1741 int channo = PHONEDEV(xpd).channels - LINES_DIGI_INP + input_port;
1742
1743 /* Stop ringing. No leds for digital inputs. */
1744 PHONEDEV(xpd).ringing[channo] = 0;
1745 if (offhook && !IS_OFFHOOK(xpd, channo)) {
1746 LINE_DBG(SIGNAL, xpd, channo, "OFFHOOK\n");
1747 hookstate_changed(xpd, channo, 1);
1748 } else if (!offhook && IS_OFFHOOK(xpd, channo)) {
1749 LINE_DBG(SIGNAL, xpd, channo, "ONHOOK\n");
1750 hookstate_changed(xpd, channo, 0);
1751 }
1752 }
1753
1754 static void process_digital_inputs(xpd_t *xpd, const reg_cmd_t *info)
1755 {
1756 bool offhook;
1757 /* Sanity check */
1758 if (!PHONEDEV(xpd).digital_inputs) {
1759 XPD_NOTICE(xpd, "%s called without digital inputs. Ignored\n",
1760 __func__);
1761 return;
1762 }
1763 if (XPD_HW(xpd).type == 6) {
1764 static int input_values_type6[] = { 0x80, 0x20, 0x08, 0x02 }; /* I/O Expander values of input relays */
1765 int i;
1766
1767 /* Map I/O Expander GPIO into line number */
1768 for (i = 0; i < ARRAY_SIZE(input_values_type6); i++) {
1769 int chanmask = input_values_type6[i];
1770
1771 offhook = (REG_FIELD(info, data_low) & chanmask) == 0;
1772 notify_digital_input(xpd, i, offhook);
1773 }
1774 } else {
1775 int channo = info->h.portnum;
1776 int input_port;
1777 offhook = (REG_FIELD(info, data_low) & 0x1) == 0;
1778 if (channo < 0 || channo >= ARRAY_SIZE(input_ports_type1)) {
1779 XPD_ERR(xpd, "%s: got bad portnum=%d\n", __func__, channo);
1780 return;
1781 }
1782 input_port = input_ports_type1[channo];
1783 if (input_port < 0) {
1784 XPD_ERR(xpd, "%s: portnum=%d is not input port\n", __func__, channo);
1785 return;
1786 }
1787 notify_digital_input(xpd, input_port, offhook);
1788 }
1789 }
1790 #endif
1791
1792 static const char dtmf_digits[] = {
1793 'D', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '#',
1794 'A', 'B', 'C'
1795 };
1796
1797 /*
1798 * This function is called with spinlocked XPD
1799 */
1800 static void process_dtmf(xpd_t *xpd, uint portnum, __u8 val)
1801 {
1802 __u8 digit;
1803 bool key_down = val & 0x10;
1804 bool want_mute;
1805 bool want_event;
1806 struct FXS_priv_data *priv;
1807 ktime_t now;
1808 s64 msec = 0;
1809 struct timespec64 ts;
1810
1811 if (!dtmf_detection)
1812 return;
1813 if (!SPAN_REGISTERED(xpd))
1814 return;
1815 priv = xpd->priv;
1816 val &= 0xF;
1817 digit = dtmf_digits[val];
1818 want_mute = IS_SET(priv->want_dtmf_mute, portnum);
1819 want_event = IS_SET(priv->want_dtmf_events, portnum);
1820 if (!IS_SET(priv->prev_key_down, portnum) && !key_down)
1821 LINE_NOTICE(xpd, portnum, "DTMF: duplicate UP (%c)\n", digit);
1822 if (key_down)
1823 BIT_SET(priv->prev_key_down, portnum);
1824 else
1825 BIT_CLR(priv->prev_key_down, portnum);
1826 now = ktime_get();
1827 if (!dahdi_ktime_equal(priv->prev_key_time[portnum], ktime_set(0, 0)))
1828 msec = ktime_ms_delta(now, priv->prev_key_time[portnum]);
1829 priv->prev_key_time[portnum] = now;
1830 ts = ktime_to_timespec64(now);
1831 LINE_DBG(SIGNAL, xpd, portnum,
1832 "[%lld.%06ld] DTMF digit %-4s '%c' (val=%d, want_mute=%s want_event=%s, delta=%lld msec)\n",
1833 (s64)ts.tv_sec, ts.tv_nsec * NSEC_PER_USEC,
1834 (key_down) ? "DOWN" : "UP", digit, val,
1835 (want_mute) ? "yes" : "no", (want_event) ? "yes" : "no", msec);
1836 /*
1837 * FIXME: we currently don't use the want_dtmf_mute until
1838 * we are sure about the logic in Asterisk native bridging.
1839 * Meanwhile, simply mute it on button press.
1840 */
1841 if (key_down && want_mute)
1842 __do_mute_dtmf(xpd, portnum, 1);
1843 else
1844 __do_mute_dtmf(xpd, portnum, 0);
1845 if (want_event) {
1846 int event =
1847 (key_down) ? DAHDI_EVENT_DTMFDOWN : DAHDI_EVENT_DTMFUP;
1848
1849 dahdi_qevent_lock(XPD_CHAN(xpd, portnum), event | digit);
1850 }
1851 }
1852
1853 static int FXS_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
1854 {
1855 unsigned long flags;
1856 struct FXS_priv_data *priv;
1857 __u8 regnum = 0;
1858 bool indirect = 0;
1859
1860 spin_lock_irqsave(&xpd->lock, flags);
1861 priv = xpd->priv;
1862 BUG_ON(!priv);
1863 if (info->h.bytes == REG_CMD_SIZE(REG)) {
1864 if ((XPD_HW(xpd).type == 1) && (REG_FIELD(info, regnum) == 0x1E))
1865 indirect = 1;
1866 regnum = (indirect) ? REG_FIELD(info, subreg) : REG_FIELD(info, regnum);
1867 XPD_DBG(REGS, xpd, "%s reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
1868 (indirect) ? "I" : "D", regnum, REG_FIELD(info, data_low),
1869 REG_FIELD(info, data_high));
1870 }
1871 if (info->h.bytes == REG_CMD_SIZE(RAM)) {
1872 uint addr;
1873 unsigned long val;
1874 XPD_DBG(REGS, xpd, "port=%d, addr_low=0x%X, addr_high=0x%X, data_0=0x%X data_1=0x%X data_2=0x%X data_3=0x%X\n",
1875 info->h.portnum,
1876 REG_FIELD_RAM(info, addr_low),
1877 REG_FIELD_RAM(info, addr_high),
1878 REG_FIELD_RAM(info, data_0),
1879 REG_FIELD_RAM(info, data_1),
1880 REG_FIELD_RAM(info, data_2),
1881 REG_FIELD_RAM(info, data_3));
1882 addr = (REG_FIELD_RAM(info, addr_high) << 8) | REG_FIELD_RAM(info, addr_low);
1883 val = (REG_FIELD_RAM(info, data_3) << 24) |
1884 (REG_FIELD_RAM(info, data_2) << 16) |
1885 (REG_FIELD_RAM(info, data_1) << 8) |
1886 REG_FIELD_RAM(info, data_0);
1887 } else if ((XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_DTMF_DECODE) ||
1888 (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_TONDTMF)) {
1889 __u8 val = REG_FIELD(info, data_low);
1890
1891 process_dtmf(xpd, info->h.portnum, val);
1892 } else if ((XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_LINEFEED)) {
1893 __u8 val = REG_FIELD(info, data_low);
1894
1895 LINE_DBG(SIGNAL, xpd, info->h.portnum,
1896 "REG_TYPE6_LINEFEED: dataL=0x%X \n", val);
1897 priv->polledhook[info->h.portnum] = val;
1898 }
1899 #ifdef POLL_DIGITAL_INPUTS
1900 /*
1901 * Process digital inputs polling results
1902 */
1903 else if ( (XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_DIGITAL_IOCTRL) ||
1904 (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_EXP_GPIOB &&
1905 REG_FIELD(info, do_expander)))
1906 process_digital_inputs(xpd, info);
1907 #endif
1908 else if (XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_LOOPCLOSURE) { /* OFFHOOK ? */
1909 __u8 val = REG_FIELD(info, data_low);
1910 xpp_line_t mask = BIT(info->h.portnum);
1911 xpp_line_t offhook;
1912
1913 /*
1914 * Validate reply. Non-existing/disabled ports
1915 * will reply with 0xFF. Ignore these.
1916 */
1917 if ((val & REG_TYPE1_LOOPCLOSURE_ZERO) == 0) {
1918 offhook = (val & REG_TYPE1_LOOPCLOSURE_LCR) ? mask : 0;
1919 LINE_DBG(SIGNAL, xpd, info->h.portnum,
1920 "REG_TYPE1_LOOPCLOSURE: dataL=0x%X "
1921 "(offhook=0x%X mask=0x%X)\n",
1922 val, offhook, mask);
1923 process_hookstate(xpd, offhook, mask);
1924 }
1925 } else if (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_LCRRTP) { /* OFFHOOK ? */
1926 __u8 val = REG_FIELD(info, data_low);
1927 xpp_line_t mask = BIT(info->h.portnum);
1928 xpp_line_t offhook;
1929
1930 /*
1931 * Validate reply. Non-existing/disabled ports
1932 * will reply with 0xFF. Ignore these.
1933 */
1934 if ((val & REG_TYPE6_LCRRTP_ZERO) == 0) {
1935 offhook = (val & REG_TYPE6_LCRRTP_LCR) ? mask : 0;
1936 LINE_DBG(SIGNAL, xpd, info->h.portnum,
1937 "REG_TYPE6_LCRRTP: dataL=0x%X "
1938 "(offhook=0x%X mask=0x%X)\n",
1939 val, offhook, mask);
1940 process_hookstate(xpd, offhook, mask);
1941 }
1942 } else {
1943 #if 0
1944 XPD_NOTICE(xpd,
1945 "Spurious register reply(ignored): "
1946 "%s reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
1947 (indirect) ? "I" : "D",
1948 regnum, REG_FIELD(info, data_low),
1949 REG_FIELD(info, data_high));
1950 #endif
1951 }
1952 /*
1953 * Update /proc info only if reply relate to the last slic
1954 * read request
1955 */
1956 if (REG_FIELD(&xpd->requested_reply, regnum) ==
1957 REG_FIELD(info, regnum)
1958 && REG_FIELD(&xpd->requested_reply, do_subreg) ==
1959 REG_FIELD(info, do_subreg)
1960 && REG_FIELD(&xpd->requested_reply, subreg) ==
1961 REG_FIELD(info, subreg)) {
1962 xpd->last_reply = *info;
1963 }
1964 spin_unlock_irqrestore(&xpd->lock, flags);
1965 return 0;
1966 }
1967
1968 static int FXS_card_state(xpd_t *xpd, bool on)
1969 {
1970 BUG_ON(!xpd);
1971 XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
1972 return 0;
1973 }
1974
1975 static const struct xops fxs_xops = {
1976 .card_new = FXS_card_new,
1977 .card_init = FXS_card_init,
1978 .card_remove = FXS_card_remove,
1979 .card_tick = FXS_card_tick,
1980 .card_register_reply = FXS_card_register_reply,
1981 };
1982
1983 static const struct phoneops fxs_phoneops = {
1984 .card_dahdi_preregistration = FXS_card_dahdi_preregistration,
1985 .card_dahdi_postregistration = FXS_card_dahdi_postregistration,
1986 .card_hooksig = FXS_card_hooksig,
1987 .card_pcm_recompute = generic_card_pcm_recompute,
1988 .card_pcm_fromspan = generic_card_pcm_fromspan,
1989 .card_pcm_tospan = generic_card_pcm_tospan,
1990 .card_timing_priority = generic_timing_priority,
1991 .echocancel_timeslot = generic_echocancel_timeslot,
1992 .echocancel_setmask = generic_echocancel_setmask,
1993 .card_open = FXS_card_open,
1994 .card_close = FXS_card_close,
1995 .card_ioctl = FXS_card_ioctl,
1996 .card_state = FXS_card_state,
1997 };
1998
1999 static xproto_table_t PROTO_TABLE(FXS) = {
2000 .owner = THIS_MODULE,
2001 .entries = {
2002 /* Prototable Card Opcode */
2003 XENTRY( FXS, FXS, SIG_CHANGED ),
2004 },
2005 .name = "FXS", /* protocol name */
2006 .ports_per_subunit = 8,
2007 .type = XPD_TYPE_FXS,
2008 .xops = &fxs_xops,
2009 .phoneops = &fxs_phoneops,
2010 .packet_is_valid = fxs_packet_is_valid,
2011 .packet_dump = fxs_packet_dump,
2012 };
2013
2014 static bool fxs_packet_is_valid(xpacket_t *pack)
2015 {
2016 const xproto_entry_t *xe;
2017
2018 // DBG(GENERAL, "\n");
2019 xe = xproto_card_entry(&PROTO_TABLE(FXS), XPACKET_OP(pack));
2020 return xe != NULL;
2021 }
2022
2023 static void fxs_packet_dump(const char *msg, xpacket_t *pack)
2024 {
2025 DBG(GENERAL, "%s\n", msg);
2026 }
2027
2028 /*------------------------- SLIC Handling --------------------------*/
2029
2030 #ifdef CONFIG_PROC_FS
2031 static int proc_fxs_info_show(struct seq_file *sfile, void *not_used)
2032 {
2033 unsigned long flags;
2034 xpd_t *xpd = sfile->private;
2035 struct FXS_priv_data *priv;
2036 int i;
2037 int led;
2038
2039 if (!xpd)
2040 return -ENODEV;
2041 spin_lock_irqsave(&xpd->lock, flags);
2042 priv = xpd->priv;
2043 BUG_ON(!priv);
2044 seq_printf(sfile, "%-12s", "Channel:");
2045 for_each_line(xpd, i) {
2046 seq_printf(sfile, "%4d", i);
2047 }
2048 seq_printf(sfile, "\n%-12s", "");
2049 for_each_line(xpd, i) {
2050 char *chan_type;
2051
2052 if (IS_SET(PHONEDEV(xpd).digital_outputs, i))
2053 chan_type = "out";
2054 else if (IS_SET(PHONEDEV(xpd).digital_inputs, i))
2055 chan_type = "in";
2056 else
2057 chan_type = "";
2058 seq_printf(sfile, "%4s", chan_type);
2059 }
2060 seq_printf(sfile, "\n%-12s", "idletxhook:");
2061 for_each_line(xpd, i) {
2062 seq_printf(sfile, "%4d", priv->idletxhookstate[i]);
2063 }
2064 seq_printf(sfile, "\n%-12s", "lasttxhook:");
2065 for_each_line(xpd, i) {
2066 seq_printf(sfile, "%4d", priv->lasttxhook[i]);
2067 }
2068 seq_printf(sfile, "\n%-12s", "ohttimer:");
2069 for_each_line(xpd, i) {
2070 seq_printf(sfile, "%4d", priv->ohttimer[i]);
2071 }
2072 seq_printf(sfile, "\n%-12s", "neon_blink:");
2073 for_each_line(xpd, i) {
2074 seq_printf(sfile, "%4d",
2075 IS_SET(priv->neon_blinking, i));
2076 }
2077 seq_printf(sfile, "\n%-12s", "search_fsk:");
2078 for_each_line(xpd, i) {
2079 seq_printf(sfile, "%4d",
2080 IS_SET(priv->search_fsk_pattern, i));
2081 }
2082 seq_printf(sfile, "\n%-12s", "vbat_h:");
2083 for_each_line(xpd, i) {
2084 seq_printf(sfile, "%4d",
2085 test_bit(i, (unsigned long *)&priv->vbat_h));
2086 }
2087 seq_printf(sfile, "\n");
2088 for (led = 0; led < NUM_LEDS; led++) {
2089 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2090 led, "ledstate");
2091 for_each_line(xpd, i) {
2092 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2093 && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2094 seq_printf(sfile, "%d ",
2095 IS_SET(priv->ledstate[led], i));
2096 }
2097 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2098 led, "ledcontrol");
2099 for_each_line(xpd, i) {
2100 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2101 && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2102 seq_printf(sfile, "%d ",
2103 IS_SET(priv->ledcontrol[led], i));
2104 }
2105 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2106 led, "led_counter");
2107 for_each_line(xpd, i) {
2108 if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2109 && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2110 seq_printf(sfile, "%d ",
2111 LED_COUNTER(priv, i, led));
2112 }
2113 }
2114 seq_printf(sfile, "\n%-12s", "overheats:");
2115 for_each_line(xpd, i) {
2116 seq_printf(sfile, "%4d", priv->overheat_reset_counter[i]);
2117 }
2118 seq_printf(sfile, "\n");
2119 spin_unlock_irqrestore(&xpd->lock, flags);
2120 return 0;
2121 }
2122
2123 static int proc_fxs_info_open(struct inode *inode, struct file *file)
2124 {
2125 return single_open(file, proc_fxs_info_show, PDE_DATA(inode));
2126 }
2127
2128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2129 static const struct proc_ops proc_fxs_info_ops = {
2130 .proc_open = proc_fxs_info_open,
2131 .proc_read = seq_read,
2132 .proc_lseek = seq_lseek,
2133 .proc_release = single_release,
2134 };
2135 #else
2136 static const struct file_operations proc_fxs_info_ops = {
2137 .owner = THIS_MODULE,
2138 .open = proc_fxs_info_open,
2139 .read = seq_read,
2140 .llseek = seq_lseek,
2141 .release = single_release,
2142 };
2143 #endif
2144
2145
2146 #ifdef WITH_METERING
2147 static ssize_t proc_xpd_metering_write(struct file *file,
2148 const char __user *buffer, size_t count, loff_t *offset)
2149 {
2150 xpd_t *xpd = file->private_data;
2151 char buf[MAX_PROC_WRITE];
2152 lineno_t chan;
2153 int num;
2154 int ret;
2155
2156 if (!xpd)
2157 return -ENODEV;
2158 if (count >= MAX_PROC_WRITE - 1) {
2159 XPD_ERR(xpd, "Metering string too long (%zu)\n", count);
2160 return -EINVAL;
2161 }
2162 if (copy_from_user(&buf, buffer, count))
2163 return -EFAULT;
2164 buf[count] = '\0';
2165 ret = sscanf(buf, "%d", &num);
2166 if (ret != 1) {
2167 XPD_ERR(xpd, "Metering value should be number. Got '%s'\n",
2168 buf);
2169 return -EINVAL;
2170 }
2171 chan = num;
2172 if (chan != PORT_BROADCAST && chan > xpd->channels) {
2173 XPD_ERR(xpd, "Metering tone: bad channel number %d\n", chan);
2174 return -EINVAL;
2175 }
2176 if ((ret = metering_gen(xpd, chan, 1)) < 0) {
2177 XPD_ERR(xpd, "Failed sending metering tone\n");
2178 return ret;
2179 }
2180 return count;
2181 }
2182
2183 static int proc_xpd_metering_open(struct inode *inode, struct file *file)
2184 {
2185 file->private_data = PDE_DATA(inode);
2186 }
2187
2188 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
2189 static const struct proc_ops proc_xpd_metering_ops = {
2190 .proc_open = proc_xpd_metering_open,
2191 .proc_write = proc_xpd_metering_write,
2192 .proc_release = single_release,
2193 };
2194 #else
2195 static const struct file_operations proc_xpd_metering_ops = {
2196 .owner = THIS_MODULE,
2197 .open = proc_xpd_metering_open,
2198 .write = proc_xpd_metering_write,
2199 .release = single_release,
2200 };
2201 #endif
2202 #endif
2203 #endif
2204
2205 static DEVICE_ATTR_READER(fxs_ring_registers_show, dev, buf)
2206 {
2207 xpd_t *xpd;
2208 struct FXS_priv_data *priv;
2209 unsigned long flags;
2210 const struct ring_reg_params *p;
2211 const struct byte_pair *v;
2212 enum ring_types rtype;
2213 int len = 0;
2214 int i;
2215
2216 BUG_ON(!dev);
2217 xpd = dev_to_xpd(dev);
2218 if (!xpd)
2219 return -ENODEV;
2220 priv = xpd->priv;
2221 BUG_ON(!priv);
2222 spin_lock_irqsave(&xpd->lock, flags);
2223 len += sprintf(buf + len, "# Reg#: D/I\tNEON \tTRAPEZ \tNORMAL \n");
2224 for (i = 0; i < ARRAY_SIZE(ring_parameters); i++) {
2225 p = &ring_parameters[i];
2226 len += sprintf(buf + len, "[%d] 0x%02X: %c",
2227 i, p->regno, (p->is_indirect) ? 'I' : 'D');
2228 for (rtype = RING_TYPE_NEON; rtype <= RING_TYPE_NORMAL; rtype++) {
2229 v = &(p->values[rtype]);
2230 if (p->is_indirect)
2231 len += sprintf(buf + len, "\t0x%02X 0x%02X",
2232 v->h_val, v->l_val);
2233 else
2234 len += sprintf(buf + len, "\t0x%02X ----",
2235 v->l_val);
2236 }
2237 len += sprintf(buf + len, "\n");
2238 }
2239 spin_unlock_irqrestore(&xpd->lock, flags);
2240 return len;
2241 }
2242
2243 static DEVICE_ATTR_WRITER(fxs_ring_registers_store, dev, buf, count)
2244 {
2245 xpd_t *xpd;
2246 struct FXS_priv_data *priv;
2247 unsigned long flags;
2248 char rtype_name[MAX_PROC_WRITE];
2249 enum ring_types rtype;
2250 struct ring_reg_params *params;
2251 struct byte_pair *v;
2252 int regno;
2253 int h_val;
2254 int l_val;
2255 int ret;
2256 int i;
2257
2258 BUG_ON(!dev);
2259 xpd = dev_to_xpd(dev);
2260 if (!xpd)
2261 return -ENODEV;
2262 priv = xpd->priv;
2263 BUG_ON(!priv);
2264 ret = sscanf(buf, "%10s %X %X %X\n",
2265 rtype_name, &regno, &h_val, &l_val);
2266 if (ret < 3 || ret > 4) {
2267 XPD_ERR(xpd, "Bad input: '%s'\n", buf);
2268 XPD_ERR(xpd, "# Correct input\n");
2269 XPD_ERR(xpd, "{NEON|TRAPEZ|NORMAL} <regno> <byte> [<byte>]\n");
2270 goto invalid_input;
2271 }
2272 if (strcasecmp("NEON", rtype_name) == 0)
2273 rtype = RING_TYPE_NEON;
2274 else if (strcasecmp("TRAPEZ", rtype_name) == 0)
2275 rtype = RING_TYPE_TRAPEZ;
2276 else if (strcasecmp("NORMAL", rtype_name) == 0)
2277 rtype = RING_TYPE_NORMAL;
2278 else {
2279 XPD_ERR(xpd, "Unknown ring type '%s' (NEON/TRAPEZ/NORMAL)\n",
2280 rtype_name);
2281 goto invalid_input;
2282 }
2283 params = NULL;
2284 for (i = 0; i < ARRAY_SIZE(ring_parameters); i++) {
2285 if (ring_parameters[i].regno == regno) {
2286 params = &ring_parameters[i];
2287 break;
2288 }
2289 }
2290 if (!params) {
2291 XPD_ERR(xpd, "Bad register 0x%X\n", regno);
2292 goto invalid_input;
2293 }
2294 if (params->is_indirect) {
2295 if (ret != 4) {
2296 XPD_ERR(xpd,
2297 "Missing low-byte (0x%X is indirect register)\n",
2298 regno);
2299 goto invalid_input;
2300 }
2301 XPD_DBG(SIGNAL, xpd, "%s Indirect 0x%X <=== 0x%X 0x%X\n",
2302 rtype_name, regno, h_val, l_val);
2303 } else {
2304 if (ret != 3) {
2305 XPD_ERR(xpd,
2306 "Should give exactly one value (0x%X is direct register)\n",
2307 regno);
2308 goto invalid_input;
2309 }
2310 l_val = h_val;
2311 h_val = 0;
2312 XPD_DBG(SIGNAL, xpd, "%s Direct 0x%X <=== 0x%X\n",
2313 rtype_name, regno, h_val);
2314 }
2315 spin_lock_irqsave(&xpd->lock, flags);
2316 v = &(params->values[rtype]);
2317 v->h_val = h_val;
2318 v->l_val = l_val;
2319 spin_unlock_irqrestore(&xpd->lock, flags);
2320 return count;
2321 invalid_input:
2322 return -EINVAL;
2323 }
2324
2325 static DEVICE_ATTR(fxs_ring_registers, S_IRUGO | S_IWUSR,
2326 fxs_ring_registers_show,
2327 fxs_ring_registers_store);
2328
2329 static int fxs_xpd_probe(struct device *dev)
2330 {
2331 xpd_t *xpd;
2332 int ret;
2333
2334 xpd = dev_to_xpd(dev);
2335 /* Is it our device? */
2336 if (xpd->xpd_type != XPD_TYPE_FXS) {
2337 XPD_ERR(xpd, "drop suggestion for %s (%d)\n", dev_name(dev),
2338 xpd->xpd_type);
2339 return -EINVAL;
2340 }
2341 XPD_DBG(DEVICES, xpd, "SYSFS\n");
2342 ret = device_create_file(dev, &dev_attr_fxs_ring_registers);
2343 if (ret) {
2344 XPD_ERR(xpd, "%s: device_create_file(fxs_ring_registers) failed: %d\n",
2345 __func__, ret);
2346 goto fail_fxs_ring_registers;
2347 }
2348 return 0;
2349 fail_fxs_ring_registers:
2350 return ret;
2351 }
2352
2353 static int fxs_xpd_remove(struct device *dev)
2354 {
2355 xpd_t *xpd;
2356
2357 xpd = dev_to_xpd(dev);
2358 XPD_DBG(DEVICES, xpd, "SYSFS\n");
2359 device_remove_file(dev, &dev_attr_fxs_ring_registers);
2360 return 0;
2361 }
2362
2363 static struct xpd_driver fxs_driver = {
2364 .xpd_type = XPD_TYPE_FXS,
2365 .driver = {
2366 .name = "fxs",
2367 .owner = THIS_MODULE,
2368 .probe = fxs_xpd_probe,
2369 .remove = fxs_xpd_remove}
2370 };
2371
2372 static int __init card_fxs_startup(void)
2373 {
2374 int ret;
2375
2376 if ((ret = xpd_driver_register(&fxs_driver.driver)) < 0)
2377 return ret;
2378
2379 #ifdef POLL_DIGITAL_INPUTS
2380 INFO("FEATURE: with DIGITAL INPUTS support (polled every %d msec)\n",
2381 poll_digital_inputs);
2382 #else
2383 INFO("FEATURE: without DIGITAL INPUTS support\n");
2384 #endif
2385 INFO("FEATURE: DAHDI_VMWI (HVAC only)\n");
2386 #ifdef WITH_METERING
2387 INFO("FEATURE: WITH METERING Generation\n");
2388 #else
2389 INFO("FEATURE: NO METERING Generation\n");
2390 #endif
2391 xproto_register(&PROTO_TABLE(FXS));
2392 return 0;
2393 }
2394
2395 static void __exit card_fxs_cleanup(void)
2396 {
2397 xproto_unregister(&PROTO_TABLE(FXS));
2398 xpd_driver_unregister(&fxs_driver.driver);
2399 }
2400
2401 MODULE_DESCRIPTION("XPP FXS Card Driver");
2402 MODULE_AUTHOR("Oron Peled <[email protected]>");
2403 MODULE_LICENSE("GPL");
2404 MODULE_ALIAS_XPD(XPD_TYPE_FXS);
2405
2406 module_init(card_fxs_startup);
2407 module_exit(card_fxs_cleanup);