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