Revert "Remove support for xpp drivers."
[dahdi/linux.git] / drivers / dahdi / xpp / card_fxs.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il>
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 && unit_descriptor->numchips != 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                                 notified = true;
1346                                 LINE_NOTICE(xpd, pos,
1347                                         "Got DAHDI_VMWI notification "
1348                                         "but vmwi_ioctl parameter is off. "
1349                                         "Ignoring.\n");
1350                         }
1351                         return 0;
1352                 }
1353                 /* Digital inputs/outputs don't have VM leds */
1354                 if (IS_SET
1355                     (PHONEDEV(xpd).digital_inputs | PHONEDEV(xpd).
1356                      digital_outputs, pos))
1357                         return 0;
1358                 PHONEDEV(xpd).msg_waiting[pos] = val;
1359                 LINE_DBG(SIGNAL, xpd, pos, "DAHDI_VMWI: %s\n",
1360                          (val) ? "yes" : "no");
1361                 return 0;
1362         default:
1363                 report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
1364         }
1365         return -ENOTTY;
1366 }
1367
1368 static int FXS_card_open(xpd_t *xpd, lineno_t chan)
1369 {
1370         struct FXS_priv_data *priv;
1371
1372         BUG_ON(!xpd);
1373         priv = xpd->priv;
1374         if (IS_OFFHOOK(xpd, chan))
1375                 LINE_NOTICE(xpd, chan, "Already offhook during open. OK.\n");
1376         else
1377                 LINE_DBG(SIGNAL, xpd, chan, "is onhook\n");
1378         /*
1379          * Delegate updating dahdi to FXS_card_tick():
1380          *   The problem is that dahdi_hooksig() is spinlocking the channel and
1381          *   we are called by dahdi with the spinlock already held on the
1382          *   same channel.
1383          */
1384         BIT_SET(priv->update_offhook_state, chan);
1385         return 0;
1386 }
1387
1388 static int FXS_card_close(xpd_t *xpd, lineno_t chan)
1389 {
1390         struct FXS_priv_data *priv;
1391
1392         BUG_ON(!xpd);
1393         LINE_DBG(GENERAL, xpd, chan, "\n");
1394         priv = xpd->priv;
1395         priv->idletxhookstate[chan] = FXS_LINE_POL_ACTIVE;
1396         return 0;
1397 }
1398
1399 #ifdef  POLL_DIGITAL_INPUTS
1400 /*
1401  * INPUT polling is done via SLIC register 0x06 (same as LEDS):
1402  *         7     6     5     4     3     2     1     0
1403  *      +-----+-----+-----+-----+-----+-----+-----+-----+
1404  *      | I1  | I3  |     |     | I2  | I4  |     |     |
1405  *      +-----+-----+-----+-----+-----+-----+-----+-----+
1406  *
1407  */
1408 static int input_ports_type1[] = {
1409         /* slic = input_port */
1410         [0]     = -1,
1411         [1]     = -1,
1412         [2]     = 2,
1413         [3]     = 3,
1414         [4]     = -1,
1415         [5]     = -1,
1416         [6]     = 0,
1417         [7]     = 1,
1418         };
1419
1420 static void poll_inputs(xpd_t *xpd)
1421 {
1422         int i;
1423
1424         BUG_ON(xpd->xbus_idx != 0);     // Only unit #0 has digital inputs
1425         if (XPD_HW(xpd).type == 6) {
1426                 EXP_REQUEST(xpd->xbus, xpd, SLIC_READ,
1427                         REG_TYPE6_EXP_GPIOB, 0, 0);
1428         } else {
1429                 for (i = 0; i < ARRAY_SIZE(input_ports_type1); i++) {
1430                         int pos = input_ports_type1[i];
1431                         if (pos >= 0) {
1432                                 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, i, SLIC_READ, 0x06, 0);
1433                         }
1434                 }
1435         }
1436 }
1437 #endif
1438
1439 static void poll_linefeed(xpd_t *xpd)
1440 {
1441         struct FXS_priv_data *priv;
1442         int i;
1443
1444         if (XPD_HW(xpd).type != 6)
1445                 return;
1446         if (xpd->xpd_state != XPD_STATE_READY)
1447                 return;
1448         priv = xpd->priv;
1449         BUG_ON(!priv);
1450         BUG_ON(!xpd->xbus);
1451
1452         XPD_DBG(GENERAL, xpd, "periodic poll");
1453         for_each_line(xpd, i) {
1454                 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)
1455                     || IS_SET(PHONEDEV(xpd).digital_inputs, i))
1456                         continue;
1457                 if (priv->polledhook[i] == FXS_LINE_OPEN &&
1458                                 priv->lasttxhook[i] != FXS_LINE_OPEN) {
1459                         LINE_NOTICE(xpd, i, "Overheat detected, resetting.");
1460                         priv->overheat_reset_counter[i]++;
1461                         linefeed_control(xpd->xbus, xpd, i,
1462                                         priv->lasttxhook[i]);
1463                 }
1464                 SLIC_DIRECT_REQUEST(xpd->xbus, xpd, i, SLIC_READ,
1465                                 REG_TYPE6_LINEFEED, 0);
1466         }
1467 }
1468
1469 static void handle_linefeed(xpd_t *xpd)
1470 {
1471         struct FXS_priv_data *priv;
1472         int i;
1473
1474         BUG_ON(!xpd);
1475         priv = xpd->priv;
1476         BUG_ON(!priv);
1477         for_each_line(xpd, i) {
1478                 if (priv->lasttxhook[i] == FXS_LINE_RING
1479                     && !IS_SET(priv->neon_blinking, i)) {
1480                         /* RINGing, prepare for OHT */
1481                         priv->ohttimer[i] = OHT_TIMER;
1482                         priv->idletxhookstate[i] = FXS_LINE_POL_OHTRANS;
1483                 } else {
1484                         if (priv->ohttimer[i]) {
1485                                 priv->ohttimer[i]--;
1486                                 if (!priv->ohttimer[i]) {
1487                                         LINE_DBG(SIGNAL, xpd, i,
1488                                                  "ohttimer expired\n");
1489                                         priv->idletxhookstate[i] =
1490                                             FXS_LINE_POL_ACTIVE;
1491                                         oht_pcm(xpd, i, 0);
1492                                         vmwi_search(xpd, i, 0);
1493                                         if (priv->lasttxhook[i] ==
1494                                             FXS_LINE_POL_OHTRANS) {
1495                                                 /* Apply the change if appropriate */
1496                                                 linefeed_control(xpd->xbus, xpd,
1497                                                                  i,
1498                                                                  FXS_LINE_POL_ACTIVE);
1499                                         }
1500                                 }
1501                         }
1502                 }
1503         }
1504 }
1505
1506 /*
1507  * Optimized memcmp() like function. Only test for equality (true/false).
1508  * This optimization reduced the detect_vmwi() runtime by a factor of 3.
1509  */
1510 static inline bool mem_equal(const char a[], const char b[], size_t len)
1511 {
1512         int i;
1513
1514         for (i = 0; i < len; i++)
1515                 if (a[i] != b[i])
1516                         return 0;
1517         return 1;
1518 }
1519
1520 /*
1521  * Detect Voice Mail Waiting Indication
1522  */
1523 static void detect_vmwi(xpd_t *xpd)
1524 {
1525         struct FXS_priv_data *priv;
1526         xbus_t *xbus;
1527         static const __u8 FSK_COMMON_PATTERN[] =
1528             { 0xA8, 0x49, 0x22, 0x3B, 0x9F, 0xFF, 0x1F, 0xBB };
1529         static const __u8 FSK_ON_PATTERN[] =
1530             { 0xA2, 0x2C, 0x1F, 0x2C, 0xBB, 0xA1, 0xA5, 0xFF };
1531         static const __u8 FSK_OFF_PATTERN[] =
1532             { 0xA2, 0x2C, 0x28, 0xA5, 0xB1, 0x21, 0x49, 0x9F };
1533         int i;
1534         xpp_line_t ignore_mask;
1535
1536         BUG_ON(!xpd);
1537         xbus = xpd->xbus;
1538         priv = xpd->priv;
1539         BUG_ON(!priv);
1540         ignore_mask =
1541                 PHONEDEV(xpd).offhook_state |
1542                 ~(PHONEDEV(xpd).oht_pcm_pass) |
1543                 ~(priv->search_fsk_pattern) |
1544                 PHONEDEV(xpd).digital_inputs |
1545                 PHONEDEV(xpd).digital_outputs;
1546         for_each_line(xpd, i) {
1547                 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
1548                 __u8 *writechunk = chan->writechunk;
1549
1550                 if (IS_SET(ignore_mask, i))
1551                         continue;
1552 #if 0
1553                 if (writechunk[0] != 0x7F && writechunk[0] != 0) {
1554                         int j;
1555
1556                         LINE_DBG(GENERAL, xpd, i, "MSG:");
1557                         for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
1558                                 if (debug)
1559                                         printk(" %02X", writechunk[j]);
1560                         }
1561                         if (debug)
1562                                 printk("\n");
1563                 }
1564 #endif
1565                 if (unlikely
1566                     (mem_equal
1567                      (writechunk, FSK_COMMON_PATTERN, DAHDI_CHUNKSIZE))) {
1568                         LINE_DBG(SIGNAL, xpd, i,
1569                                 "Found common FSK pattern. "
1570                                 "Start looking for ON/OFF patterns.\n");
1571                         BIT_SET(priv->found_fsk_pattern, i);
1572                 } else if (unlikely(IS_SET(priv->found_fsk_pattern, i))) {
1573                         BIT_CLR(priv->found_fsk_pattern, i);
1574                         oht_pcm(xpd, i, 0);
1575                         if (unlikely
1576                             (mem_equal
1577                              (writechunk, FSK_ON_PATTERN, DAHDI_CHUNKSIZE))) {
1578                                 LINE_DBG(SIGNAL, xpd, i, "MSG WAITING ON\n");
1579                                 PHONEDEV(xpd).msg_waiting[i] = 1;
1580                                 start_stop_vm_led(xbus, xpd, i);
1581                         } else
1582                             if (unlikely
1583                                 (mem_equal
1584                                  (writechunk, FSK_OFF_PATTERN,
1585                                   DAHDI_CHUNKSIZE))) {
1586                                 LINE_DBG(SIGNAL, xpd, i, "MSG WAITING OFF\n");
1587                                 PHONEDEV(xpd).msg_waiting[i] = 0;
1588                                 start_stop_vm_led(xbus, xpd, i);
1589                         } else {
1590                                 int j;
1591
1592                                 LINE_NOTICE(xpd, i, "MSG WAITING Unexpected:");
1593                                 for (j = 0; j < DAHDI_CHUNKSIZE; j++)
1594                                         printk(" %02X", writechunk[j]);
1595                                 printk("\n");
1596                         }
1597                 }
1598         }
1599 }
1600
1601 static int FXS_card_tick(xbus_t *xbus, xpd_t *xpd)
1602 {
1603         struct FXS_priv_data *priv;
1604
1605         BUG_ON(!xpd);
1606         priv = xpd->priv;
1607         BUG_ON(!priv);
1608 #ifdef  POLL_DIGITAL_INPUTS
1609         if (poll_digital_inputs && PHONEDEV(xpd).digital_inputs) {
1610                 if ((xpd->timer_count % poll_digital_inputs) == 0)
1611                         poll_inputs(xpd);
1612         }
1613 #endif
1614         if ((xpd->timer_count % poll_chan_linefeed) == 0)
1615                 poll_linefeed(xpd);
1616         handle_fxs_leds(xpd);
1617         handle_linefeed(xpd);
1618         if (XPD_HW(xpd).type == 6)
1619                 blink_mwi(xpd);
1620         /*
1621          * Hack alert (FIXME):
1622          *   Asterisk did FXS_card_open() and we wanted to report
1623          *   offhook state. However, the channel is spinlocked by dahdi
1624          *   so we marked it in the priv->update_offhook_state mask and
1625          *   now we take care of notification to dahdi and Asterisk
1626          */
1627         if (priv->update_offhook_state) {
1628                 enum dahdi_rxsig rxsig;
1629                 int i;
1630
1631                 for_each_line(xpd, i) {
1632                         if (!IS_SET(priv->update_offhook_state, i))
1633                                 continue;
1634                         rxsig =
1635                             IS_OFFHOOK(xpd,
1636                                        i) ? DAHDI_RXSIG_OFFHOOK :
1637                             DAHDI_RXSIG_ONHOOK;
1638                         /* Notify after open() */
1639                         notify_rxsig(xpd, i, rxsig);
1640                         BIT_CLR(priv->update_offhook_state, i);
1641                 }
1642         }
1643         if (SPAN_REGISTERED(xpd)) {
1644                 if (!vmwi_ioctl && priv->search_fsk_pattern)
1645                         detect_vmwi(xpd);       /* Detect via FSK modulation */
1646         }
1647         return 0;
1648 }
1649
1650 /*---------------- FXS: HOST COMMANDS -------------------------------------*/
1651
1652 /*---------------- FXS: Astribank Reply Handlers --------------------------*/
1653
1654 /*
1655  * Should be called with spinlocked XPD
1656  */
1657 static void process_hookstate(xpd_t *xpd, xpp_line_t offhook,
1658                               xpp_line_t change_mask)
1659 {
1660         xbus_t *xbus;
1661         struct FXS_priv_data *priv;
1662         int i;
1663
1664         BUG_ON(!xpd);
1665         BUG_ON(PHONEDEV(xpd).direction != TO_PHONE);
1666         xbus = xpd->xbus;
1667         priv = xpd->priv;
1668         XPD_DBG(SIGNAL, xpd, "offhook=0x%X change_mask=0x%X\n", offhook,
1669                 change_mask);
1670         for_each_line(xpd, i) {
1671                 if (IS_SET(PHONEDEV(xpd).digital_outputs, i)
1672                     || IS_SET(PHONEDEV(xpd).digital_inputs, i))
1673                         continue;
1674                 if (IS_SET(change_mask, i)) {
1675                         PHONEDEV(xpd).ringing[i] = 0;   /* No more ringing... */
1676 #ifdef  WITH_METERING
1677                         metering_gen(xpd, i, 0);        /* Stop metering... */
1678 #endif
1679                         MARK_BLINK(priv, i, LED_GREEN, 0);
1680                         /*
1681                          * Reset our previous DTMF memories...
1682                          */
1683                         BIT_CLR(priv->prev_key_down, i);
1684                         priv->prev_key_time[i].tv_sec =
1685                             priv->prev_key_time[i].tv_usec = 0L;
1686                         if (IS_SET(offhook, i)) {
1687                                 LINE_DBG(SIGNAL, xpd, i, "OFFHOOK\n");
1688                                 MARK_ON(priv, i, LED_GREEN);
1689                                 hookstate_changed(xpd, i, 1);
1690                         } else {
1691                                 LINE_DBG(SIGNAL, xpd, i, "ONHOOK\n");
1692                                 MARK_OFF(priv, i, LED_GREEN);
1693                                 hookstate_changed(xpd, i, 0);
1694                         }
1695                         /*
1696                          * Must switch to low power. In high power, an ONHOOK
1697                          * won't be detected.
1698                          */
1699                         do_chan_power(xbus, xpd, i, 0);
1700                 }
1701         }
1702 }
1703
1704 HANDLER_DEF(FXS, SIG_CHANGED)
1705 {
1706         xpp_line_t sig_status =
1707             RPACKET_FIELD(pack, FXS, SIG_CHANGED, sig_status);
1708         xpp_line_t sig_toggles =
1709             RPACKET_FIELD(pack, FXS, SIG_CHANGED, sig_toggles);
1710         unsigned long flags;
1711
1712         BUG_ON(!xpd);
1713         BUG_ON(PHONEDEV(xpd).direction != TO_PHONE);
1714         XPD_DBG(SIGNAL, xpd, "(PHONE) sig_toggles=0x%04X sig_status=0x%04X\n",
1715                 sig_toggles, sig_status);
1716 #if 0
1717         Is this needed ? for_each_line(xpd, i) {
1718                 // Power down (prevent overheating!!!)
1719                 if (IS_SET(sig_toggles, i))
1720                         do_chan_power(xpd->xbus, xpd, BIT(i), 0);
1721         }
1722 #endif
1723         spin_lock_irqsave(&xpd->lock, flags);
1724         process_hookstate(xpd, sig_status, sig_toggles);
1725         spin_unlock_irqrestore(&xpd->lock, flags);
1726         return 0;
1727 }
1728
1729 #ifdef  POLL_DIGITAL_INPUTS
1730 static inline void notify_digital_input(xpd_t *xpd, int input_port, int offhook)
1731 {
1732         int channo = PHONEDEV(xpd).channels - LINES_DIGI_INP + input_port;
1733
1734         /* Stop ringing. No leds for digital inputs. */
1735         PHONEDEV(xpd).ringing[channo] = 0;
1736         if (offhook && !IS_OFFHOOK(xpd, channo)) {
1737                 LINE_DBG(SIGNAL, xpd, channo, "OFFHOOK\n");
1738                 hookstate_changed(xpd, channo, 1);
1739         } else if (!offhook && IS_OFFHOOK(xpd, channo)) {
1740                 LINE_DBG(SIGNAL, xpd, channo, "ONHOOK\n");
1741                 hookstate_changed(xpd, channo, 0);
1742         }
1743 }
1744
1745 static void process_digital_inputs(xpd_t *xpd, const reg_cmd_t *info)
1746 {
1747         bool offhook;
1748         /* Sanity check */
1749         if (!PHONEDEV(xpd).digital_inputs) {
1750                 XPD_NOTICE(xpd, "%s called without digital inputs. Ignored\n",
1751                            __func__);
1752                 return;
1753         }
1754         if (XPD_HW(xpd).type == 6) {
1755                 static int input_values_type6[] = { 0x80, 0x20, 0x08, 0x02 };   /* I/O Expander values of input relays */
1756                 int i;
1757
1758                 /* Map I/O Expander GPIO into line number */
1759                 for (i = 0; i < ARRAY_SIZE(input_values_type6); i++) {
1760                         int chanmask = input_values_type6[i];
1761
1762                         offhook = (REG_FIELD(info, data_low) & chanmask) == 0;
1763                         notify_digital_input(xpd, i, offhook);
1764                 }
1765         } else {
1766                 int channo = info->h.portnum;
1767                 int input_port;
1768                 offhook = (REG_FIELD(info, data_low) & 0x1) == 0;
1769                 if (channo < 0 || channo >= ARRAY_SIZE(input_ports_type1)) {
1770                         XPD_ERR(xpd, "%s: got bad portnum=%d\n", __func__, channo);
1771                         return;
1772                 }
1773                 input_port = input_ports_type1[channo];
1774                 if (input_port < 0) {
1775                         XPD_ERR(xpd, "%s: portnum=%d is not input port\n", __func__, channo);
1776                         return;
1777                 }
1778                 notify_digital_input(xpd, input_port, offhook);
1779         }
1780 }
1781 #endif
1782
1783 static const char dtmf_digits[] = {
1784         'D', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '#',
1785         'A', 'B', 'C'
1786 };
1787
1788 /*
1789  * This function is called with spinlocked XPD
1790  */
1791 static void process_dtmf(xpd_t *xpd, uint portnum, __u8 val)
1792 {
1793         __u8 digit;
1794         bool key_down = val & 0x10;
1795         bool want_mute;
1796         bool want_event;
1797         struct FXS_priv_data *priv;
1798         struct timeval now;
1799         int msec = 0;
1800
1801         if (!dtmf_detection)
1802                 return;
1803         if (!SPAN_REGISTERED(xpd))
1804                 return;
1805         priv = xpd->priv;
1806         val &= 0xF;
1807         digit = dtmf_digits[val];
1808         want_mute = IS_SET(priv->want_dtmf_mute, portnum);
1809         want_event = IS_SET(priv->want_dtmf_events, portnum);
1810         if (!IS_SET(priv->prev_key_down, portnum) && !key_down)
1811                 LINE_NOTICE(xpd, portnum, "DTMF: duplicate UP (%c)\n", digit);
1812         if (key_down)
1813                 BIT_SET(priv->prev_key_down, portnum);
1814         else
1815                 BIT_CLR(priv->prev_key_down, portnum);
1816         do_gettimeofday(&now);
1817         if (priv->prev_key_time[portnum].tv_sec != 0)
1818                 msec = usec_diff(&now, &priv->prev_key_time[portnum]) / 1000;
1819         priv->prev_key_time[portnum] = now;
1820         LINE_DBG(SIGNAL, xpd, portnum,
1821                 "[%lu.%06lu] DTMF digit %-4s '%c' "
1822                 "(val=%d, want_mute=%s want_event=%s, delta=%d msec)\n",
1823                 now.tv_sec, now.tv_usec, (key_down) ? "DOWN" : "UP", digit,
1824                 val, (want_mute) ? "yes" : "no", (want_event) ? "yes" : "no",
1825                 msec);
1826         /*
1827          * FIXME: we currently don't use the want_dtmf_mute until
1828          * we are sure about the logic in Asterisk native bridging.
1829          * Meanwhile, simply mute it on button press.
1830          */
1831         if (key_down && want_mute)
1832                 __do_mute_dtmf(xpd, portnum, 1);
1833         else
1834                 __do_mute_dtmf(xpd, portnum, 0);
1835         if (want_event) {
1836                 int event =
1837                     (key_down) ? DAHDI_EVENT_DTMFDOWN : DAHDI_EVENT_DTMFUP;
1838
1839                 dahdi_qevent_lock(XPD_CHAN(xpd, portnum), event | digit);
1840         }
1841 }
1842
1843 static int FXS_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
1844 {
1845         unsigned long flags;
1846         struct FXS_priv_data *priv;
1847         __u8 regnum = 0;
1848         bool indirect = 0;
1849
1850         spin_lock_irqsave(&xpd->lock, flags);
1851         priv = xpd->priv;
1852         BUG_ON(!priv);
1853         if (info->h.bytes == REG_CMD_SIZE(REG)) {
1854                 if ((XPD_HW(xpd).type == 1) && (REG_FIELD(info, regnum) == 0x1E))
1855                         indirect = 1;
1856                 regnum = (indirect) ? REG_FIELD(info, subreg) : REG_FIELD(info, regnum);
1857                 XPD_DBG(REGS, xpd, "%s reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
1858                         (indirect) ? "I" : "D", regnum, REG_FIELD(info, data_low),
1859                         REG_FIELD(info, data_high));
1860         }
1861         if (info->h.bytes == REG_CMD_SIZE(RAM)) {
1862                 uint addr;
1863                 unsigned long val;
1864                 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",
1865                         info->h.portnum,
1866                         REG_FIELD_RAM(info, addr_low),
1867                         REG_FIELD_RAM(info, addr_high),
1868                         REG_FIELD_RAM(info, data_0),
1869                         REG_FIELD_RAM(info, data_1),
1870                         REG_FIELD_RAM(info, data_2),
1871                         REG_FIELD_RAM(info, data_3));
1872                 addr = (REG_FIELD_RAM(info, addr_high) << 8) | REG_FIELD_RAM(info, addr_low);
1873                 val = (REG_FIELD_RAM(info, data_3) << 24) |
1874                       (REG_FIELD_RAM(info, data_2) << 16) |
1875                       (REG_FIELD_RAM(info, data_1) << 8) |
1876                        REG_FIELD_RAM(info, data_0);
1877         } else if ((XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_DTMF_DECODE) ||
1878             (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_TONDTMF)) {
1879                 __u8 val = REG_FIELD(info, data_low);
1880
1881                 process_dtmf(xpd, info->h.portnum, val);
1882         } else if ((XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_LINEFEED)) {
1883                 __u8 val = REG_FIELD(info, data_low);
1884
1885                 LINE_DBG(SIGNAL, xpd, info->h.portnum,
1886                         "REG_TYPE6_LINEFEED: dataL=0x%X \n", val);
1887                 priv->polledhook[info->h.portnum] = val;
1888         }
1889 #ifdef  POLL_DIGITAL_INPUTS
1890         /*
1891          * Process digital inputs polling results
1892          */
1893         else if ( (XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_DIGITAL_IOCTRL) ||
1894                   (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_EXP_GPIOB && 
1895                    REG_FIELD(info, do_expander)))
1896                 process_digital_inputs(xpd, info);
1897 #endif
1898         else if (XPD_HW(xpd).type == 1 && !indirect && regnum == REG_TYPE1_LOOPCLOSURE) { /* OFFHOOK ? */
1899                 __u8 val = REG_FIELD(info, data_low);
1900                 xpp_line_t mask = BIT(info->h.portnum);
1901                 xpp_line_t offhook;
1902
1903                 /*
1904                  * Validate reply. Non-existing/disabled ports
1905                  * will reply with 0xFF. Ignore these.
1906                  */
1907                 if ((val & REG_TYPE1_LOOPCLOSURE_ZERO) == 0) {
1908                         offhook = (val & REG_TYPE1_LOOPCLOSURE_LCR) ? mask : 0;
1909                         LINE_DBG(SIGNAL, xpd, info->h.portnum,
1910                                 "REG_TYPE1_LOOPCLOSURE: dataL=0x%X "
1911                                 "(offhook=0x%X mask=0x%X)\n",
1912                                 val, offhook, mask);
1913                         process_hookstate(xpd, offhook, mask);
1914                 }
1915         } else if (XPD_HW(xpd).type == 6 && !indirect && regnum == REG_TYPE6_LCRRTP) { /* OFFHOOK ? */
1916                 __u8 val = REG_FIELD(info, data_low);
1917                 xpp_line_t mask = BIT(info->h.portnum);
1918                 xpp_line_t offhook;
1919
1920                 /*
1921                  * Validate reply. Non-existing/disabled ports
1922                  * will reply with 0xFF. Ignore these.
1923                  */
1924                 if ((val & REG_TYPE6_LCRRTP_ZERO) == 0) {
1925                         offhook = (val & REG_TYPE6_LCRRTP_LCR) ? mask : 0;
1926                         LINE_DBG(SIGNAL, xpd, info->h.portnum,
1927                                 "REG_TYPE6_LCRRTP: dataL=0x%X "
1928                                 "(offhook=0x%X mask=0x%X)\n",
1929                                 val, offhook, mask);
1930                         process_hookstate(xpd, offhook, mask);
1931                 }
1932         } else {
1933 #if 0
1934                 XPD_NOTICE(xpd,
1935                         "Spurious register reply(ignored): "
1936                         "%s reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
1937                         (indirect) ? "I" : "D",
1938                         regnum, REG_FIELD(info, data_low),
1939                         REG_FIELD(info, data_high));
1940 #endif
1941         }
1942         /*
1943          * Update /proc info only if reply relate to the last slic
1944          * read request
1945          */
1946         if (REG_FIELD(&xpd->requested_reply, regnum) ==
1947                         REG_FIELD(info, regnum)
1948                 && REG_FIELD(&xpd->requested_reply, do_subreg) ==
1949                         REG_FIELD(info, do_subreg)
1950                 && REG_FIELD(&xpd->requested_reply, subreg) ==
1951                         REG_FIELD(info, subreg)) {
1952                 xpd->last_reply = *info;
1953         }
1954         spin_unlock_irqrestore(&xpd->lock, flags);
1955         return 0;
1956 }
1957
1958 static int FXS_card_state(xpd_t *xpd, bool on)
1959 {
1960         BUG_ON(!xpd);
1961         XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
1962         return 0;
1963 }
1964
1965 static const struct xops fxs_xops = {
1966         .card_new = FXS_card_new,
1967         .card_init = FXS_card_init,
1968         .card_remove = FXS_card_remove,
1969         .card_tick = FXS_card_tick,
1970         .card_register_reply = FXS_card_register_reply,
1971 };
1972
1973 static const struct phoneops fxs_phoneops = {
1974         .card_dahdi_preregistration = FXS_card_dahdi_preregistration,
1975         .card_dahdi_postregistration = FXS_card_dahdi_postregistration,
1976         .card_hooksig = FXS_card_hooksig,
1977         .card_pcm_recompute = generic_card_pcm_recompute,
1978         .card_pcm_fromspan = generic_card_pcm_fromspan,
1979         .card_pcm_tospan = generic_card_pcm_tospan,
1980         .card_timing_priority = generic_timing_priority,
1981         .echocancel_timeslot = generic_echocancel_timeslot,
1982         .echocancel_setmask = generic_echocancel_setmask,
1983         .card_open = FXS_card_open,
1984         .card_close = FXS_card_close,
1985         .card_ioctl = FXS_card_ioctl,
1986         .card_state = FXS_card_state,
1987 };
1988
1989 static xproto_table_t PROTO_TABLE(FXS) = {
1990         .owner = THIS_MODULE,
1991         .entries = {
1992                 /*      Prototable      Card    Opcode          */
1993                 XENTRY( FXS,            FXS,    SIG_CHANGED     ),
1994         },
1995         .name = "FXS",  /* protocol name */
1996         .ports_per_subunit = 8,
1997         .type = XPD_TYPE_FXS,
1998         .xops = &fxs_xops,
1999         .phoneops = &fxs_phoneops,
2000         .packet_is_valid = fxs_packet_is_valid,
2001         .packet_dump = fxs_packet_dump,
2002 };
2003
2004 static bool fxs_packet_is_valid(xpacket_t *pack)
2005 {
2006         const xproto_entry_t *xe;
2007
2008         // DBG(GENERAL, "\n");
2009         xe = xproto_card_entry(&PROTO_TABLE(FXS), XPACKET_OP(pack));
2010         return xe != NULL;
2011 }
2012
2013 static void fxs_packet_dump(const char *msg, xpacket_t *pack)
2014 {
2015         DBG(GENERAL, "%s\n", msg);
2016 }
2017
2018 /*------------------------- SLIC Handling --------------------------*/
2019
2020 #ifdef  CONFIG_PROC_FS
2021 static int proc_fxs_info_show(struct seq_file *sfile, void *not_used)
2022 {
2023         unsigned long flags;
2024         xpd_t *xpd = sfile->private;
2025         struct FXS_priv_data *priv;
2026         int i;
2027         int led;
2028
2029         if (!xpd)
2030                 return -ENODEV;
2031         spin_lock_irqsave(&xpd->lock, flags);
2032         priv = xpd->priv;
2033         BUG_ON(!priv);
2034         seq_printf(sfile, "%-12s", "Channel:");
2035         for_each_line(xpd, i) {
2036                 seq_printf(sfile, "%4d", i);
2037         }
2038         seq_printf(sfile, "\n%-12s", "");
2039         for_each_line(xpd, i) {
2040                 char *chan_type;
2041
2042                 if (IS_SET(PHONEDEV(xpd).digital_outputs, i))
2043                         chan_type = "out";
2044                 else if (IS_SET(PHONEDEV(xpd).digital_inputs, i))
2045                         chan_type = "in";
2046                 else
2047                         chan_type = "";
2048                 seq_printf(sfile, "%4s", chan_type);
2049         }
2050         seq_printf(sfile, "\n%-12s", "idletxhook:");
2051         for_each_line(xpd, i) {
2052                 seq_printf(sfile, "%4d", priv->idletxhookstate[i]);
2053         }
2054         seq_printf(sfile, "\n%-12s", "lasttxhook:");
2055         for_each_line(xpd, i) {
2056                 seq_printf(sfile, "%4d", priv->lasttxhook[i]);
2057         }
2058         seq_printf(sfile, "\n%-12s", "ohttimer:");
2059         for_each_line(xpd, i) {
2060                 seq_printf(sfile, "%4d", priv->ohttimer[i]);
2061         }
2062         seq_printf(sfile, "\n%-12s", "neon_blink:");
2063         for_each_line(xpd, i) {
2064                 seq_printf(sfile, "%4d",
2065                             IS_SET(priv->neon_blinking, i));
2066         }
2067         seq_printf(sfile, "\n%-12s", "search_fsk:");
2068         for_each_line(xpd, i) {
2069                 seq_printf(sfile, "%4d",
2070                             IS_SET(priv->search_fsk_pattern, i));
2071         }
2072         seq_printf(sfile, "\n%-12s", "vbat_h:");
2073         for_each_line(xpd, i) {
2074                 seq_printf(sfile, "%4d",
2075                         test_bit(i, (unsigned long *)&priv->vbat_h));
2076         }
2077         seq_printf(sfile, "\n");
2078         for (led = 0; led < NUM_LEDS; led++) {
2079                 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2080                         led, "ledstate");
2081                 for_each_line(xpd, i) {
2082                         if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2083                             && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2084                                 seq_printf(sfile, "%d ",
2085                                             IS_SET(priv->ledstate[led], i));
2086                 }
2087                 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2088                         led, "ledcontrol");
2089                 for_each_line(xpd, i) {
2090                         if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2091                             && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2092                                 seq_printf(sfile, "%d ",
2093                                             IS_SET(priv->ledcontrol[led], i));
2094                 }
2095                 seq_printf(sfile, "\nLED #%d\t%-12s: ",
2096                         led, "led_counter");
2097                 for_each_line(xpd, i) {
2098                         if (!IS_SET(PHONEDEV(xpd).digital_outputs, i)
2099                             && !IS_SET(PHONEDEV(xpd).digital_inputs, i))
2100                                 seq_printf(sfile, "%d ",
2101                                             LED_COUNTER(priv, i, led));
2102                 }
2103         }
2104         seq_printf(sfile, "\n%-12s", "overheats:");
2105         for_each_line(xpd, i) {
2106                 seq_printf(sfile, "%4d", priv->overheat_reset_counter[i]);
2107         }
2108         seq_printf(sfile, "\n");
2109         spin_unlock_irqrestore(&xpd->lock, flags);
2110         return 0;
2111 }
2112
2113 static int proc_fxs_info_open(struct inode *inode, struct file *file)
2114 {
2115         return single_open(file, proc_fxs_info_show, PDE_DATA(inode));
2116 }
2117
2118 static const struct file_operations proc_fxs_info_ops = {
2119         .owner          = THIS_MODULE,
2120         .open           = proc_fxs_info_open,
2121         .read           = seq_read,
2122         .llseek         = seq_lseek,
2123         .release        = single_release,
2124 };
2125
2126 #ifdef  WITH_METERING
2127 static ssize_t proc_xpd_metering_write(struct file *file,
2128                 const char __user *buffer, size_t count, loff_t *offset)
2129 {
2130         xpd_t *xpd = file->private_data;
2131         char buf[MAX_PROC_WRITE];
2132         lineno_t chan;
2133         int num;
2134         int ret;
2135
2136         if (!xpd)
2137                 return -ENODEV;
2138         if (count >= MAX_PROC_WRITE - 1) {
2139                 XPD_ERR(xpd, "Metering string too long (%zu)\n", count);
2140                 return -EINVAL;
2141         }
2142         if (copy_from_user(&buf, buffer, count))
2143                 return -EFAULT;
2144         buf[count] = '\0';
2145         ret = sscanf(buf, "%d", &num);
2146         if (ret != 1) {
2147                 XPD_ERR(xpd, "Metering value should be number. Got '%s'\n",
2148                         buf);
2149                 return -EINVAL;
2150         }
2151         chan = num;
2152         if (chan != PORT_BROADCAST && chan > xpd->channels) {
2153                 XPD_ERR(xpd, "Metering tone: bad channel number %d\n", chan);
2154                 return -EINVAL;
2155         }
2156         if ((ret = metering_gen(xpd, chan, 1)) < 0) {
2157                 XPD_ERR(xpd, "Failed sending metering tone\n");
2158                 return ret;
2159         }
2160         return count;
2161 }
2162
2163 static int proc_xpd_metering_open(struct inode *inode, struct file *file)
2164 {
2165         file->private_data = PDE_DATA(inode);
2166 }
2167
2168 static const struct file_operations proc_xpd_metering_ops = {
2169         .owner          = THIS_MODULE,
2170         .open           = proc_xpd_metering_open,
2171         .write          = proc_xpd_metering_write,
2172         .release        = single_release,
2173 };
2174 #endif
2175 #endif
2176
2177 static DEVICE_ATTR_READER(fxs_ring_registers_show, dev, buf)
2178 {
2179         xpd_t *xpd;
2180         struct FXS_priv_data *priv;
2181         unsigned long flags;
2182         const struct ring_reg_params *p;
2183         const struct byte_pair *v;
2184         enum ring_types rtype;
2185         int len = 0;
2186         int i;
2187
2188         BUG_ON(!dev);
2189         xpd = dev_to_xpd(dev);
2190         if (!xpd)
2191                 return -ENODEV;
2192         priv = xpd->priv;
2193         BUG_ON(!priv);
2194         spin_lock_irqsave(&xpd->lock, flags);
2195         len += sprintf(buf + len, "#   Reg#: D/I\tNEON     \tTRAPEZ   \tNORMAL   \n");
2196         for (i = 0; i < ARRAY_SIZE(ring_parameters); i++) {
2197                 p = &ring_parameters[i];
2198                 len += sprintf(buf + len, "[%d] 0x%02X: %c",
2199                         i, p->regno, (p->is_indirect) ? 'I' : 'D');
2200                 for (rtype = RING_TYPE_NEON; rtype <= RING_TYPE_NORMAL; rtype++) {
2201                         v = &(p->values[rtype]);
2202                         if (p->is_indirect)
2203                                 len += sprintf(buf + len, "\t0x%02X 0x%02X",
2204                                         v->h_val, v->l_val);
2205                         else
2206                                 len += sprintf(buf + len, "\t0x%02X ----",
2207                                         v->l_val);
2208                 }
2209                 len += sprintf(buf + len, "\n");
2210         }
2211         spin_unlock_irqrestore(&xpd->lock, flags);
2212         return len;
2213 }
2214
2215 static DEVICE_ATTR_WRITER(fxs_ring_registers_store, dev, buf, count)
2216 {
2217         xpd_t *xpd;
2218         struct FXS_priv_data *priv;
2219         unsigned long flags;
2220         char rtype_name[MAX_PROC_WRITE];
2221         enum ring_types rtype;
2222         struct ring_reg_params *params;
2223         struct byte_pair *v;
2224         int regno;
2225         int h_val;
2226         int l_val;
2227         int ret;
2228         int i;
2229
2230         BUG_ON(!dev);
2231         xpd = dev_to_xpd(dev);
2232         if (!xpd)
2233                 return -ENODEV;
2234         priv = xpd->priv;
2235         BUG_ON(!priv);
2236         ret = sscanf(buf, "%10s %X %X %X\n",
2237                 rtype_name, &regno, &h_val, &l_val);
2238         if (ret < 3 || ret > 4) {
2239                 XPD_ERR(xpd, "Bad input: '%s'\n", buf);
2240                 XPD_ERR(xpd, "# Correct input\n");
2241                 XPD_ERR(xpd, "{NEON|TRAPEZ|NORMAL} <regno> <byte> [<byte>]\n");
2242                 goto invalid_input;
2243         }
2244         if (strcasecmp("NEON", rtype_name) == 0)
2245                 rtype = RING_TYPE_NEON;
2246         else if (strcasecmp("TRAPEZ", rtype_name) == 0)
2247                 rtype = RING_TYPE_TRAPEZ;
2248         else if (strcasecmp("NORMAL", rtype_name) == 0)
2249                 rtype = RING_TYPE_NORMAL;
2250         else {
2251                 XPD_ERR(xpd, "Unknown ring type '%s' (NEON/TRAPEZ/NORMAL)\n",
2252                         rtype_name);
2253                 goto invalid_input;
2254         }
2255         params = NULL;
2256         for (i = 0; i < ARRAY_SIZE(ring_parameters); i++) {
2257                 if (ring_parameters[i].regno == regno) {
2258                         params = &ring_parameters[i];
2259                         break;
2260                 }
2261         }
2262         if (!params) {
2263                 XPD_ERR(xpd, "Bad register 0x%X\n", regno);
2264                 goto invalid_input;
2265         }
2266         if (params->is_indirect) {
2267                 if (ret != 4) {
2268                         XPD_ERR(xpd,
2269                                 "Missing low-byte (0x%X is indirect register)\n",
2270                                 regno);
2271                         goto invalid_input;
2272                 }
2273                 XPD_DBG(SIGNAL, xpd, "%s Indirect 0x%X <=== 0x%X 0x%X\n",
2274                         rtype_name, regno, h_val, l_val);
2275         } else {
2276                 if (ret != 3) {
2277                         XPD_ERR(xpd,
2278                                 "Should give exactly one value (0x%X is direct register)\n",
2279                                 regno);
2280                         goto invalid_input;
2281                 }
2282                 l_val = h_val;
2283                 h_val = 0;
2284                 XPD_DBG(SIGNAL, xpd, "%s Direct 0x%X <=== 0x%X\n",
2285                         rtype_name, regno, h_val);
2286         }
2287         spin_lock_irqsave(&xpd->lock, flags);
2288         v = &(params->values[rtype]);
2289         v->h_val = h_val;
2290         v->l_val = l_val;
2291         spin_unlock_irqrestore(&xpd->lock, flags);
2292         return count;
2293 invalid_input:
2294         return -EINVAL;
2295 }
2296
2297 static DEVICE_ATTR(fxs_ring_registers, S_IRUGO | S_IWUSR,
2298         fxs_ring_registers_show,
2299         fxs_ring_registers_store);
2300
2301 static int fxs_xpd_probe(struct device *dev)
2302 {
2303         xpd_t *xpd;
2304         int ret;
2305
2306         xpd = dev_to_xpd(dev);
2307         /* Is it our device? */
2308         if (xpd->xpd_type != XPD_TYPE_FXS) {
2309                 XPD_ERR(xpd, "drop suggestion for %s (%d)\n", dev_name(dev),
2310                         xpd->xpd_type);
2311                 return -EINVAL;
2312         }
2313         XPD_DBG(DEVICES, xpd, "SYSFS\n");
2314         ret = device_create_file(dev, &dev_attr_fxs_ring_registers);
2315         if (ret) {
2316                 XPD_ERR(xpd, "%s: device_create_file(fxs_ring_registers) failed: %d\n",
2317                         __func__, ret);
2318                 goto fail_fxs_ring_registers;
2319         }
2320         return 0;
2321 fail_fxs_ring_registers:
2322         return ret;
2323 }
2324
2325 static int fxs_xpd_remove(struct device *dev)
2326 {
2327         xpd_t *xpd;
2328
2329         xpd = dev_to_xpd(dev);
2330         XPD_DBG(DEVICES, xpd, "SYSFS\n");
2331         device_remove_file(dev, &dev_attr_fxs_ring_registers);
2332         return 0;
2333 }
2334
2335 static struct xpd_driver fxs_driver = {
2336         .xpd_type = XPD_TYPE_FXS,
2337         .driver = {
2338                    .name = "fxs",
2339                    .owner = THIS_MODULE,
2340                    .probe = fxs_xpd_probe,
2341                    .remove = fxs_xpd_remove}
2342 };
2343
2344 static int __init card_fxs_startup(void)
2345 {
2346         int ret;
2347
2348         if ((ret = xpd_driver_register(&fxs_driver.driver)) < 0)
2349                 return ret;
2350
2351 #ifdef  POLL_DIGITAL_INPUTS
2352         INFO("FEATURE: with DIGITAL INPUTS support (polled every %d msec)\n",
2353              poll_digital_inputs);
2354 #else
2355         INFO("FEATURE: without DIGITAL INPUTS support\n");
2356 #endif
2357         INFO("FEATURE: DAHDI_VMWI (HVAC only)\n");
2358 #ifdef  WITH_METERING
2359         INFO("FEATURE: WITH METERING Generation\n");
2360 #else
2361         INFO("FEATURE: NO METERING Generation\n");
2362 #endif
2363         xproto_register(&PROTO_TABLE(FXS));
2364         return 0;
2365 }
2366
2367 static void __exit card_fxs_cleanup(void)
2368 {
2369         xproto_unregister(&PROTO_TABLE(FXS));
2370         xpd_driver_unregister(&fxs_driver.driver);
2371 }
2372
2373 MODULE_DESCRIPTION("XPP FXS Card Driver");
2374 MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
2375 MODULE_LICENSE("GPL");
2376 MODULE_ALIAS_XPD(XPD_TYPE_FXS);
2377
2378 module_init(card_fxs_startup);
2379 module_exit(card_fxs_cleanup);