Debian Codegraph @

drivers/dahdi/wctdm24xxp/base.c

raw symbols SCIP
1 /*
2 * Wildcard TDM2400P TDM FXS/FXO Interface Driver for DAHDI Telephony interface
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Support for TDM800P and VPM150M by Matthew Fredrickson <[email protected]>
6 *
7 * Support for Hx8 by Andrew Kohlsmith <[email protected]> and Matthew
8 * Fredrickson <[email protected]>
9 *
10 * Copyright (C) 2005 - 2012 Digium, Inc.
11 * All rights reserved.
12 *
13 * Sections for QRV cards written by Jim Dixon <[email protected]>
14 * Copyright (C) 2006, Jim Dixon and QRV Communications
15 * All rights reserved.
16 *
17 */
18
19 /*
20 * See http://www.asterisk.org for more information about
21 * the Asterisk project. Please do not directly contact
22 * any of the maintainers of this project for assistance;
23 * the project provides a web site, mailing lists and IRC
24 * channels for your use.
25 *
26 * This program is free software, distributed under the terms of
27 * the GNU General Public License Version 2 as published by the
28 * Free Software Foundation. See the LICENSE file included with
29 * this program for more details.
30 */
31
32 /* For QRV DRI cards, gain is signed short, expressed in hundredths of
33 db (in reference to 1v Peak @ 1000Hz) , as follows:
34
35 Rx Gain: -11.99 to 15.52 db
36 Tx Gain - No Pre-Emphasis: -35.99 to 12.00 db
37 Tx Gain - W/Pre-Emphasis: -23.99 to 0.00 db
38 */
39
40 #include <linux/version.h>
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/pci.h>
46 #include <linux/sched.h>
47
48 #include <linux/interrupt.h>
49 #include <linux/workqueue.h>
50 #include <linux/delay.h>
51 #include <linux/moduleparam.h>
52 #include <linux/firmware.h>
53 #include <linux/semaphore.h>
54 #include <linux/crc32.h>
55 #include <linux/slab.h>
56
57
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
59 /* Define this if you would like to load the modules in parallel. While this
60 * can speed up loads when multiple cards handled by this driver are installed,
61 * it also makes it impossible to abort module loads with ctrl-c */
62 #undef USE_ASYNC_INIT
63 #include <linux/async.h>
64 #else
65 #undef USE_ASYNC_INIT
66 #endif
67
68 #include <dahdi/kernel.h>
69 #include <dahdi/wctdm_user.h>
70
71 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
72 #include <linux/sched/signal.h>
73 #endif /* 4.11.0 */
74
75 #include "proslic.h"
76
77 #include "wctdm24xxp.h"
78 #include "xhfc.h"
79
80 #include "adt_lec.h"
81
82 #include "voicebus/GpakCust.h"
83 #include "voicebus/GpakApi.h"
84
85 #if VOICEBUS_SFRAME_SIZE != SFRAME_SIZE
86 #error SFRAME_SIZE must match the VOICEBUS_SFRAME_SIZE
87 #endif
88
89 /*
90 Experimental max loop current limit for the proslic
91 Loop current limit is from 20 mA to 41 mA in steps of 3
92 (according to datasheet)
93 So set the value below to:
94 0x00 : 20mA (default)
95 0x01 : 23mA
96 0x02 : 26mA
97 0x03 : 29mA
98 0x04 : 32mA
99 0x05 : 35mA
100 0x06 : 37mA
101 0x07 : 41mA
102 */
103 static int loopcurrent = 20;
104
105 /* Following define is a logical exclusive OR to determine if the polarity of an fxs line is to be reversed.
106 * The items taken into account are:
107 * overall polarity reversal for the module,
108 * polarity reversal for the port,
109 * and the state of the line reversal MWI indicator
110 */
111 #define POLARITY_XOR(fxs) \
112 ((reversepolarity != 0) ^ ((fxs)->reversepolarity != 0) ^ \
113 ((fxs)->vmwi_linereverse != 0))
114
115 static int reversepolarity = 0;
116
117 static alpha indirect_regs[] =
118 {
119 {0,255,"DTMF_ROW_0_PEAK",0x55C2},
120 {1,255,"DTMF_ROW_1_PEAK",0x51E6},
121 {2,255,"DTMF_ROW2_PEAK",0x4B85},
122 {3,255,"DTMF_ROW3_PEAK",0x4937},
123 {4,255,"DTMF_COL1_PEAK",0x3333},
124 {5,255,"DTMF_FWD_TWIST",0x0202},
125 {6,255,"DTMF_RVS_TWIST",0x0202},
126 {7,255,"DTMF_ROW_RATIO_TRES",0x0198},
127 {8,255,"DTMF_COL_RATIO_TRES",0x0198},
128 {9,255,"DTMF_ROW_2ND_ARM",0x0611},
129 {10,255,"DTMF_COL_2ND_ARM",0x0202},
130 {11,255,"DTMF_PWR_MIN_TRES",0x00E5},
131 {12,255,"DTMF_OT_LIM_TRES",0x0A1C},
132 {13,0,"OSC1_COEF",0x7B30},
133 {14,1,"OSC1X",0x0063},
134 {15,2,"OSC1Y",0x0000},
135 {16,3,"OSC2_COEF",0x7870},
136 {17,4,"OSC2X",0x007D},
137 {18,5,"OSC2Y",0x0000},
138 {19,6,"RING_V_OFF",0x0000},
139 {20,7,"RING_OSC",0x7EF0},
140 {21,8,"RING_X",0x0160},
141 {22,9,"RING_Y",0x0000},
142 {23,255,"PULSE_ENVEL",0x2000},
143 {24,255,"PULSE_X",0x2000},
144 {25,255,"PULSE_Y",0x0000},
145 //{26,13,"RECV_DIGITAL_GAIN",0x4000}, // playback volume set lower
146 {26,13,"RECV_DIGITAL_GAIN",0x2000}, // playback volume set lower
147 {27,14,"XMIT_DIGITAL_GAIN",0x4000},
148 //{27,14,"XMIT_DIGITAL_GAIN",0x2000},
149 {28,15,"LOOP_CLOSE_TRES",0x1000},
150 {29,16,"RING_TRIP_TRES",0x3600},
151 {30,17,"COMMON_MIN_TRES",0x1000},
152 {31,18,"COMMON_MAX_TRES",0x0200},
153 {32,19,"PWR_ALARM_Q1Q2",0x07C0},
154 {33,20,"PWR_ALARM_Q3Q4", 0x4C00 /* 0x2600 */},
155 {34,21,"PWR_ALARM_Q5Q6",0x1B80},
156 {35,22,"LOOP_CLOSURE_FILTER",0x8000},
157 {36,23,"RING_TRIP_FILTER",0x0320},
158 {37,24,"TERM_LP_POLE_Q1Q2",0x008C},
159 {38,25,"TERM_LP_POLE_Q3Q4",0x0100},
160 {39,26,"TERM_LP_POLE_Q5Q6",0x0010},
161 {40,27,"CM_BIAS_RINGING",0x0C00},
162 {41,64,"DCDC_MIN_V",0x0C00},
163 {42,255,"DCDC_XTRA",0x1000},
164 {43,66,"LOOP_CLOSE_TRES_LOW",0x1000},
165 };
166
167 /* names of HWEC modules */
168 static const char *vpmadt032_name = "VPMADT032";
169 static const char *vpmoct_name = "VPMOCT032";
170
171 /* Undefine to enable Power alarm / Transistor debug -- note: do not
172 enable for normal operation! */
173 /* #define PAQ_DEBUG */
174
175 #define DEBUG_CARD (1 << 0)
176 #define DEBUG_ECHOCAN (1 << 1)
177
178 #include "fxo_modes.h"
179
180 struct wctdm_desc {
181 const char *name;
182 const int flags;
183 const int ports;
184 };
185
186 static const struct wctdm_desc wctdm2400 = { "Wildcard TDM2400P", 0, 24 };
187 static const struct wctdm_desc wctdm800 = { "Wildcard TDM800P", 0, 8 };
188 static const struct wctdm_desc wctdm410 = { "Wildcard TDM410P", 0, 4 };
189 static const struct wctdm_desc wcaex2400 = { "Wildcard AEX2400", FLAG_EXPRESS, 24 };
190 static const struct wctdm_desc wcaex800 = { "Wildcard AEX800", FLAG_EXPRESS, 8 };
191 static const struct wctdm_desc wcaex410 = { "Wildcard AEX410", FLAG_EXPRESS, 4 };
192 static const struct wctdm_desc wcha80000 = { "HA8-0000", 0, 8 };
193 static const struct wctdm_desc wchb80000 = { "HB8-0000", FLAG_EXPRESS, 8 };
194
195 static inline bool is_pcie(const struct wctdm *wc)
196 {
197 return (wc->desc->flags & FLAG_EXPRESS) > 0;
198 }
199
200 /**
201 * Returns true if the card is one of the Hybrid Digital Analog Cards.
202 */
203 static inline bool is_hx8(const struct wctdm *wc)
204 {
205 return (&wcha80000 == wc->desc) || (&wchb80000 == wc->desc);
206 }
207
208 static inline struct dahdi_chan *
209 get_dahdi_chan(const struct wctdm *wc, struct wctdm_module *const mod)
210 {
211 return wc->aspan->span.chans[mod->card];
212 }
213
214 static inline void
215 mod_hooksig(struct wctdm *wc, struct wctdm_module *mod, enum dahdi_rxsig rxsig)
216 {
217 dahdi_hooksig(get_dahdi_chan(wc, mod), rxsig);
218 }
219
220 struct wctdm *ifaces[WC_MAX_IFACES];
221 DEFINE_SEMAPHORE(ifacelock);
222
223 static void wctdm_release(struct wctdm *wc);
224
225 static int fxovoltage = 0;
226 static unsigned int battdebounce;
227 static unsigned int battalarm;
228 static unsigned int battthresh;
229 static int debug = 0;
230 #ifdef DEBUG
231 static int robust = 0;
232 static int digitalloopback;
233 #endif
234 static int lowpower = 0;
235 static int boostringer = 0;
236 static int fastringer = 0;
237 static int _opermode = 0;
238 static char *opermode = "FCC";
239 static int fxshonormode = 0;
240 static int alawoverride = 0;
241 static char *companding = "auto";
242 static int fastpickup = -1; /* -1 auto, 0 no, 1 yes */
243 static int fxotxgain = 0;
244 static int fxorxgain = 0;
245 static int fxstxgain = 0;
246 static int fxsrxgain = 0;
247 static int nativebridge = 0;
248 static int ringdebounce = DEFAULT_RING_DEBOUNCE;
249 static int latency = VOICEBUS_DEFAULT_LATENCY;
250 static unsigned int max_latency = VOICEBUS_DEFAULT_MAXLATENCY;
251 static int forceload;
252
253 #define MS_PER_HOOKCHECK (1)
254 #define NEONMWI_ON_DEBOUNCE (100/MS_PER_HOOKCHECK)
255 static int neonmwi_monitor = 0; /* Note: this causes use of full wave ring detect */
256 static int neonmwi_level = 75; /* neon mwi trip voltage */
257 static int neonmwi_envelope = 10;
258 static int neonmwi_offlimit = 16000; /* Time in milliseconds the monitor is checked before saying no message is waiting */
259 static int neonmwi_offlimit_cycles; /* Time in milliseconds the monitor is checked before saying no message is waiting */
260
261 static int vpmsupport = 1;
262
263 static int vpmnlptype = DEFAULT_NLPTYPE;
264 static int vpmnlpthresh = DEFAULT_NLPTHRESH;
265 static int vpmnlpmaxsupp = DEFAULT_NLPMAXSUPP;
266
267 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
268
269 static const struct dahdi_echocan_features vpm_ec_features = {
270 .NLP_automatic = 1,
271 .CED_tx_detect = 1,
272 .CED_rx_detect = 1,
273 };
274
275 static const struct dahdi_echocan_ops vpm_ec_ops = {
276 .echocan_free = echocan_free,
277 };
278
279 static int
280 wctdm_init_proslic(struct wctdm *wc, struct wctdm_module *const mod, int fast,
281 int manual, int sane);
282
283 static void set_offsets(struct wctdm_module *const mod, int altcs)
284 {
285 int card = mod->card;
286 int bit;
287
288 mod->subaddr = (altcs) ? 0 : (mod->card & 0x3);
289
290 for (bit = 0; bit < ARRAY_SIZE(mod->offsets); ++bit) {
291 /* Let's add some trickery to make the TDM410 work */
292 if (altcs == 3) {
293 if (card == 2) {
294 card = 4;
295 altcs = 0;
296 } else if (card == 3) {
297 card = 5;
298 altcs = 2;
299 }
300 }
301 mod->offsets[bit] = ((((card & 0x3) * 3 + bit) * 7) +
302 (card >> 2) + altcs +
303 ((altcs) ? -21 : 0));
304 }
305 }
306
307 static inline __attribute_const__ int
308 CMD_BYTE(const struct wctdm_module *const mod, const int bit)
309 {
310 return mod->offsets[bit];
311 }
312
313 static inline __attribute_const__ int VPM_CMD_BYTE(int timeslot, int bit)
314 {
315 return ((((timeslot) & 0x3) * 3 + (bit)) * 7) + ((timeslot) >> 2);
316 }
317
318 typedef int (*bg_work_func_t)(struct wctdm *wc, unsigned long data);
319
320 struct bg {
321 struct workqueue_struct *wq;
322 struct work_struct work;
323 struct completion complete;
324 struct wctdm *wc;
325 bg_work_func_t fn;
326 unsigned long param;
327 int ret;
328 };
329
330 static void bg_work_func(struct work_struct *work)
331 {
332 struct bg *bg = container_of(work, struct bg, work);
333 bg->ret = bg->fn(bg->wc, bg->param);
334 complete(&bg->complete);
335 }
336
337 /**
338 * bg_create - Call a function running in a background thread.
339 * @wc: The board structure passed to fn
340 * @fn: The function to run in it's own thread.
341 * @parma: An extra parameter to pass to the fn.
342 *
343 * Returns NULL if the thread could not be created, otherwise a pointer to be
344 * passed to bg_join in order to get the return value.
345 *
346 * The function 'fn' will be run in a new thread. The return value is the
347 * return from the bg_join function.
348 *
349 * This would probably be best served by concurrency managed workqueues before
350 * merging, but this will at least work on the older kernels tht DAHDI
351 * supports.
352 */
353 static struct bg *
354 bg_create(struct wctdm *wc, bg_work_func_t fn, unsigned long param)
355 {
356 struct bg *bg;
357
358 bg = kzalloc(sizeof(*bg), GFP_KERNEL);
359 if (!bg)
360 return NULL;
361
362 bg->wq = create_singlethread_workqueue("wctdm_bg");
363 if (!bg->wq) {
364 kfree(bg);
365 return NULL;
366 }
367
368 init_completion(&bg->complete);
369 INIT_WORK(&bg->work, bg_work_func);
370
371 bg->wc = wc;
372 bg->fn = fn;
373 bg->param = param;
374
375 queue_work(bg->wq, &bg->work);
376
377 return bg;
378 }
379
380 /**
381 * bg_join - Wait for a background function to complete and get the result.
382 * @bg: Pointer returned from the bg_create call.
383 *
384 * Returns the result of the function passed to bg_create.
385 */
386 static int bg_join(struct bg *bg)
387 {
388 int ret = -ERESTARTSYS;
389
390 if (unlikely(!bg))
391 return -EINVAL;
392
393 while (ret)
394 ret = wait_for_completion_interruptible(&bg->complete);
395
396 ret = bg->ret;
397 destroy_workqueue(bg->wq);
398 kfree(bg);
399
400 return ret;
401 }
402
403 static void
404 setchanconfig_from_state(struct vpmadt032 *vpm, int channel,
405 GpakChannelConfig_t *chanconfig)
406 {
407 GpakEcanParms_t *p;
408
409 BUG_ON(!vpm);
410
411 chanconfig->PcmInPortA = 3;
412 chanconfig->PcmInSlotA = channel;
413 chanconfig->PcmOutPortA = SerialPortNull;
414 chanconfig->PcmOutSlotA = channel;
415 chanconfig->PcmInPortB = 2;
416 chanconfig->PcmInSlotB = channel;
417 chanconfig->PcmOutPortB = 3;
418 chanconfig->PcmOutSlotB = channel;
419 chanconfig->ToneTypesA = Null_tone;
420 chanconfig->MuteToneA = Disabled;
421 chanconfig->FaxCngDetA = Disabled;
422 chanconfig->ToneTypesB = Null_tone;
423 chanconfig->EcanEnableA = Enabled;
424 chanconfig->EcanEnableB = Disabled;
425 chanconfig->MuteToneB = Disabled;
426 chanconfig->FaxCngDetB = Disabled;
427
428 /* The software companding will be overridden on a channel by channel
429 * basis when the channel is enabled. */
430 chanconfig->SoftwareCompand = cmpPCMU;
431
432 chanconfig->FrameRate = rate2ms;
433 p = &chanconfig->EcanParametersA;
434
435 vpmadt032_get_default_parameters(p);
436
437 p->EcanNlpType = vpm->curecstate[channel].nlp_type;
438 p->EcanNlpThreshold = vpm->curecstate[channel].nlp_threshold;
439 p->EcanNlpMaxSuppress = vpm->curecstate[channel].nlp_max_suppress;
440
441 memcpy(&chanconfig->EcanParametersB,
442 &chanconfig->EcanParametersA,
443 sizeof(chanconfig->EcanParametersB));
444 }
445
446 struct vpmadt032_channel_setup {
447 struct work_struct work;
448 struct wctdm *wc;
449 };
450
451 static void vpm_setup_work_func(struct work_struct *work)
452 {
453 struct vpmadt032_channel_setup *setup =
454 container_of(work, struct vpmadt032_channel_setup, work);
455 int i;
456 int res;
457 GpakChannelConfig_t chanconfig;
458 GPAK_ChannelConfigStat_t cstatus;
459 GPAK_AlgControlStat_t algstatus;
460 GpakPortConfig_t portconfig = {0};
461 gpakConfigPortStatus_t configportstatus;
462 GPAK_PortConfigStat_t pstatus;
463 struct vpmadt032 *vpm;
464 struct wctdm *const wc = setup->wc;
465
466 WARN_ON(!wc);
467 WARN_ON(!wc->vpmadt032);
468 if (unlikely(!wc || !wc->vpmadt032))
469 return;
470 vpm = wc->vpmadt032;
471
472 /* First Serial Port config */
473 portconfig.SlotsSelect1 = SlotCfgNone;
474 portconfig.FirstBlockNum1 = 0;
475 portconfig.FirstSlotMask1 = 0x0000;
476 portconfig.SecBlockNum1 = 1;
477 portconfig.SecSlotMask1 = 0x0000;
478 portconfig.SerialWordSize1 = SerWordSize8;
479 portconfig.CompandingMode1 = cmpNone;
480 portconfig.TxFrameSyncPolarity1 = FrameSyncActHigh;
481 portconfig.RxFrameSyncPolarity1 = FrameSyncActHigh;
482 portconfig.TxClockPolarity1 = SerClockActHigh;
483 portconfig.RxClockPolarity1 = SerClockActHigh;
484 portconfig.TxDataDelay1 = DataDelay0;
485 portconfig.RxDataDelay1 = DataDelay0;
486 portconfig.DxDelay1 = Disabled;
487 portconfig.ThirdSlotMask1 = 0x0000;
488 portconfig.FouthSlotMask1 = 0x0000;
489 portconfig.FifthSlotMask1 = 0x0000;
490 portconfig.SixthSlotMask1 = 0x0000;
491 portconfig.SevenSlotMask1 = 0x0000;
492 portconfig.EightSlotMask1 = 0x0000;
493
494 /* Second Serial Port config */
495 portconfig.SlotsSelect2 = SlotCfg2Groups;
496 portconfig.FirstBlockNum2 = 0;
497 portconfig.FirstSlotMask2 = 0xffff;
498 portconfig.SecBlockNum2 = 1;
499 portconfig.SecSlotMask2 = 0xffff;
500 portconfig.SerialWordSize2 = SerWordSize8;
501 portconfig.CompandingMode2 = cmpNone;
502 portconfig.TxFrameSyncPolarity2 = FrameSyncActHigh;
503 portconfig.RxFrameSyncPolarity2 = FrameSyncActHigh;
504 portconfig.TxClockPolarity2 = SerClockActHigh;
505 portconfig.RxClockPolarity2 = SerClockActLow;
506 portconfig.TxDataDelay2 = DataDelay0;
507 portconfig.RxDataDelay2 = DataDelay0;
508 portconfig.DxDelay2 = Disabled;
509 portconfig.ThirdSlotMask2 = 0x0000;
510 portconfig.FouthSlotMask2 = 0x0000;
511 portconfig.FifthSlotMask2 = 0x0000;
512 portconfig.SixthSlotMask2 = 0x0000;
513 portconfig.SevenSlotMask2 = 0x0000;
514 portconfig.EightSlotMask2 = 0x0000;
515
516 /* Third Serial Port Config */
517 portconfig.SlotsSelect3 = SlotCfg2Groups;
518 portconfig.FirstBlockNum3 = 0;
519 portconfig.FirstSlotMask3 = 0xffff;
520 portconfig.SecBlockNum3 = 1;
521 portconfig.SecSlotMask3 = 0xffff;
522 portconfig.SerialWordSize3 = SerWordSize8;
523 portconfig.CompandingMode3 = cmpNone;
524 portconfig.TxFrameSyncPolarity3 = FrameSyncActHigh;
525 portconfig.RxFrameSyncPolarity3 = FrameSyncActHigh;
526 portconfig.TxClockPolarity3 = SerClockActHigh;
527 portconfig.RxClockPolarity3 = SerClockActLow;
528 portconfig.TxDataDelay3 = DataDelay0;
529 portconfig.RxDataDelay3 = DataDelay0;
530 portconfig.DxDelay3 = Disabled;
531 portconfig.ThirdSlotMask3 = 0x0000;
532 portconfig.FouthSlotMask3 = 0x0000;
533 portconfig.FifthSlotMask3 = 0x0000;
534 portconfig.SixthSlotMask3 = 0x0000;
535 portconfig.SevenSlotMask3 = 0x0000;
536 portconfig.EightSlotMask3 = 0x0000;
537
538 if ((configportstatus = gpakConfigurePorts(vpm->dspid, &portconfig, &pstatus))) {
539 dev_notice(&wc->vb.pdev->dev, "Configuration of ports failed (%d)!\n", configportstatus);
540 return;
541 } else {
542 if (vpm->options.debug & DEBUG_ECHOCAN)
543 dev_info(&wc->vb.pdev->dev, "Configured McBSP ports successfully\n");
544 }
545
546 res = gpakPingDsp(vpm->dspid, &vpm->version);
547 if (res) {
548 dev_notice(&wc->vb.pdev->dev, "Error pinging DSP (%d)\n", res);
549 return;
550 }
551
552 for (i = 0; i < vpm->options.channels; ++i) {
553 vpm->curecstate[i].tap_length = 0;
554 vpm->curecstate[i].nlp_type = vpm->options.vpmnlptype;
555 vpm->curecstate[i].nlp_threshold = vpm->options.vpmnlpthresh;
556 vpm->curecstate[i].nlp_max_suppress =
557 vpm->options.vpmnlpmaxsupp;
558 vpm->curecstate[i].companding = ADT_COMP_ULAW;
559 vpm->setchanconfig_from_state(vpm, i, &chanconfig);
560
561 res = gpakConfigureChannel(vpm->dspid, i, tdmToTdm,
562 &chanconfig, &cstatus);
563 if (res) {
564 dev_notice(&wc->vb.pdev->dev,
565 "Unable to configure channel #%d (%d)",
566 i, res);
567 if (res == 1)
568 printk(KERN_CONT ", reason %d", cstatus);
569 printk(KERN_CONT "\n");
570 goto exit;
571 }
572
573 res = gpakAlgControl(vpm->dspid, i, BypassEcanA, &algstatus);
574 if (res) {
575 dev_notice(&wc->vb.pdev->dev,
576 "Unable to disable echo can on channel %d "
577 "(reason %d:%d)\n", i + 1, res, algstatus);
578 goto exit;
579 }
580
581 res = gpakAlgControl(vpm->dspid, i,
582 BypassSwCompanding, &algstatus);
583 if (res) {
584 dev_notice(&wc->vb.pdev->dev,
585 "Unable to disable echo can on channel %d "
586 "(reason %d:%d)\n", i + 1, res, algstatus);
587 goto exit;
588 }
589 }
590
591 res = gpakPingDsp(vpm->dspid, &vpm->version);
592 if (res) {
593 dev_notice(&wc->vb.pdev->dev, "Error pinging DSP (%d)\n", res);
594 goto exit;
595 }
596
597 set_bit(VPM150M_ACTIVE, &vpm->control);
598
599 exit:
600 kfree(setup);
601 }
602
603 static int config_vpmadt032(struct vpmadt032 *vpm, struct wctdm *wc)
604 {
605 struct vpmadt032_channel_setup *setup;
606
607 /* Because the channel configuration can take such a long time, let's
608 * move this out onto the VPM workqueue so the system can proceeded
609 * with startup. */
610
611 setup = kzalloc(sizeof(*setup), GFP_KERNEL);
612 if (!setup)
613 return -ENOMEM;
614
615 setup->wc = wc;
616 INIT_WORK(&setup->work, vpm_setup_work_func);
617 queue_work(vpm->wq, &setup->work);
618
619 return 0;
620 }
621
622 /**
623 * is_good_frame() - Whether the SFRAME received was one sent.
624 *
625 */
626 static inline bool is_good_frame(const u8 *sframe)
627 {
628 const u8 a = sframe[0*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
629 const u8 b = sframe[1*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
630 return a != b;
631 }
632
633 static inline void cmd_dequeue_vpmoct(struct wctdm *wc, u8 *eframe)
634 {
635 struct vpmoct *vpm = wc->vpmoct;
636 struct vpmoct_cmd *cmd;
637 u8 i;
638
639 /* Pop a command off pending list */
640 spin_lock(&vpm->list_lock);
641 if (list_empty(&vpm->pending_list)) {
642 spin_unlock(&vpm->list_lock);
643 return;
644 }
645
646 cmd = list_entry(vpm->pending_list.next, struct vpmoct_cmd, node);
647 if (is_vpmoct_cmd_read(cmd))
648 list_move_tail(&cmd->node, &vpm->active_list);
649 else
650 list_del_init(&cmd->node);
651
652 /* Skip audio (24 bytes) and ignore first 6 timeslots */
653 eframe += 30;
654
655 /* Save ident so we can match the return eframe */
656 cmd->txident = wc->txident;
657
658 /* We have four timeslots to work with for a regular spi packet */
659 /* TODO: Create debug flag for this in dev */
660
661 /* The vpmoct requires a "sync" spi command as the first three bytes
662 * of an eframe */
663 eframe[7*0] = 0x12;
664 eframe[7*1] = 0x34;
665 eframe[7*2] = 0x56;
666 eframe[7*3] = cmd->command;
667 eframe[7*4] = cmd->address;
668 eframe[7*5] = cmd->data[0];
669 for (i = 1; i < cmd->chunksize; i++)
670 eframe[(7*5)+7*i] = cmd->data[i];
671
672 /* Clean up fire-and-forget messages from memory */
673 if (list_empty(&cmd->node))
674 kfree(cmd);
675
676 spin_unlock(&vpm->list_lock);
677 }
678
679 static inline void cmd_dequeue_vpmadt032(struct wctdm *wc, u8 *eframe)
680 {
681 struct vpmadt032_cmd *curcmd = NULL;
682 struct vpmadt032 *vpmadt032 = wc->vpmadt032;
683 int x;
684 unsigned char leds = ~((wc->framecount / 1000) % 8) & 0x7;
685
686 /* Skip audio */
687 eframe += 24;
688
689 if (test_bit(VPM150M_HPIRESET, &vpmadt032->control)) {
690 if (debug & DEBUG_ECHOCAN)
691 dev_info(&wc->vb.pdev->dev, "HW Resetting VPMADT032...\n");
692 for (x = 24; x < 28; x++) {
693 if (x == 24) {
694 if (test_and_clear_bit(VPM150M_HPIRESET,
695 &vpmadt032->control)) {
696 eframe[VPM_CMD_BYTE(x, 0)] = 0x0b;
697 } else {
698 eframe[VPM_CMD_BYTE(x, 0)] = leds;
699 }
700 } else {
701 eframe[VPM_CMD_BYTE(x, 0)] = leds;
702 }
703 eframe[VPM_CMD_BYTE(x, 1)] = 0;
704 eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
705 }
706 return;
707 }
708
709 if ((curcmd = vpmadt032_get_ready_cmd(vpmadt032))) {
710 curcmd->txident = wc->txident;
711 #if 0
712 // if (printk_ratelimit())
713 dev_info(&wc->vb.pdev->dev, "Transmitting txident = %d, desc = 0x%x, addr = 0x%x, data = 0x%x\n", curcmd->txident, curcmd->desc, curcmd->address, curcmd->data);
714 #endif
715 if (curcmd->desc & __VPM150M_RWPAGE) {
716 /* Set CTRL access to page*/
717 eframe[VPM_CMD_BYTE(24, 0)] = (0x8 << 4);
718 eframe[VPM_CMD_BYTE(24, 1)] = 0;
719 eframe[VPM_CMD_BYTE(24, 2)] = 0x20;
720
721 /* Do a page write */
722 if (curcmd->desc & __VPM150M_WR)
723 eframe[VPM_CMD_BYTE(25, 0)] = ((0x8 | 0x4) << 4);
724 else
725 eframe[VPM_CMD_BYTE(25, 0)] = ((0x8 | 0x4 | 0x1) << 4);
726 eframe[VPM_CMD_BYTE(25, 1)] = 0;
727 if (curcmd->desc & __VPM150M_WR)
728 eframe[VPM_CMD_BYTE(25, 2)] = curcmd->data & 0xf;
729 else
730 eframe[VPM_CMD_BYTE(25, 2)] = 0;
731
732 /* Clear XADD */
733 eframe[VPM_CMD_BYTE(26, 0)] = (0x8 << 4);
734 eframe[VPM_CMD_BYTE(26, 1)] = 0;
735 eframe[VPM_CMD_BYTE(26, 2)] = 0;
736
737 /* Fill in to buffer to size */
738 eframe[VPM_CMD_BYTE(27, 0)] = 0;
739 eframe[VPM_CMD_BYTE(27, 1)] = 0;
740 eframe[VPM_CMD_BYTE(27, 2)] = 0;
741
742 } else {
743 /* Set address */
744 eframe[VPM_CMD_BYTE(24, 0)] = ((0x8 | 0x4) << 4);
745 eframe[VPM_CMD_BYTE(24, 1)] = (curcmd->address >> 8) & 0xff;
746 eframe[VPM_CMD_BYTE(24, 2)] = curcmd->address & 0xff;
747
748 /* Send/Get our data */
749 eframe[VPM_CMD_BYTE(25, 0)] = (curcmd->desc & __VPM150M_WR) ?
750 ((0x8 | (0x3 << 1)) << 4) : ((0x8 | (0x3 << 1) | 0x1) << 4);
751 eframe[VPM_CMD_BYTE(25, 1)] = (curcmd->data >> 8) & 0xff;
752 eframe[VPM_CMD_BYTE(25, 2)] = curcmd->data & 0xff;
753
754 eframe[VPM_CMD_BYTE(26, 0)] = 0;
755 eframe[VPM_CMD_BYTE(26, 1)] = 0;
756 eframe[VPM_CMD_BYTE(26, 2)] = 0;
757
758 /* Fill in the rest */
759 eframe[VPM_CMD_BYTE(27, 0)] = 0;
760 eframe[VPM_CMD_BYTE(27, 1)] = 0;
761 eframe[VPM_CMD_BYTE(27, 2)] = 0;
762 }
763 } else if (test_and_clear_bit(VPM150M_SWRESET, &vpmadt032->control)) {
764 for (x = 24; x < 28; x++) {
765 if (x == 24)
766 eframe[VPM_CMD_BYTE(x, 0)] = (0x8 << 4);
767 else
768 eframe[VPM_CMD_BYTE(x, 0)] = 0x00;
769 eframe[VPM_CMD_BYTE(x, 1)] = 0;
770 if (x == 24)
771 eframe[VPM_CMD_BYTE(x, 2)] = 0x01;
772 else
773 eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
774 }
775 } else {
776 for (x = 24; x < 28; x++) {
777 eframe[VPM_CMD_BYTE(x, 0)] = 0x00;
778 eframe[VPM_CMD_BYTE(x, 1)] = 0x00;
779 eframe[VPM_CMD_BYTE(x, 2)] = 0x00;
780 }
781 }
782
783 /* Add our leds in */
784 for (x = 24; x < 28; x++) {
785 eframe[VPM_CMD_BYTE(x, 0)] |= leds;
786 }
787 }
788
789 /* Call with wc->reglock held and local interrupts disabled */
790 static void _cmd_dequeue(struct wctdm *wc, u8 *eframe, int card, int pos)
791 {
792 struct wctdm_module *const mod = &wc->mods[card];
793 unsigned int curcmd=0;
794
795 /* QRV only use commands relating to the first channel */
796 if ((card & 0x03) && (mod->type == QRV))
797 return;
798
799 /* Skip audio */
800 eframe += 24;
801 /* Search for something waiting to transmit */
802 if (pos) {
803 if (!list_empty(&mod->pending_cmds)) {
804 struct wctdm_cmd *const cmd =
805 list_entry(mod->pending_cmds.next,
806 struct wctdm_cmd, node);
807 curcmd = cmd->cmd;
808 cmd->ident = wc->txident;
809 list_move_tail(&cmd->node, &mod->active_cmds);
810 }
811 }
812
813 if (!curcmd) {
814 /* If nothing else, use filler */
815 switch (mod->type) {
816 case FXS:
817 curcmd = CMD_RD(LINE_STATE);
818 break;
819 case FXO:
820 curcmd = CMD_RD(12);
821 break;
822 case BRI:
823 curcmd = 0x101010;
824 break;
825 case QRV:
826 curcmd = CMD_RD(3);
827 break;
828 default:
829 break;
830 }
831 }
832
833 switch (mod->type) {
834 case FXS:
835 eframe[CMD_BYTE(mod, 0)] = (1 << (mod->subaddr));
836 if (curcmd & __CMD_WR)
837 eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0x7f;
838 else
839 eframe[CMD_BYTE(mod, 1)] = 0x80 | ((curcmd >> 8) & 0x7f);
840 eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
841 break;
842
843 case FXO:
844 {
845 static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
846 if (curcmd & __CMD_WR)
847 eframe[CMD_BYTE(mod, 0)] = 0x20 | ADDRS[mod->subaddr];
848 else
849 eframe[CMD_BYTE(mod, 0)] = 0x60 | ADDRS[mod->subaddr];
850 eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0xff;
851 eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
852 break;
853 }
854 case FXSINIT:
855 /* Special case, we initialize the FXS's into the three-byte command mode then
856 switch to the regular mode. To send it into thee byte mode, treat the path as
857 6 two-byte commands and in the last one we initialize register 0 to 0x80. All modules
858 read this as the command to switch to daisy chain mode and we're done. */
859 eframe[CMD_BYTE(mod, 0)] = 0x00;
860 eframe[CMD_BYTE(mod, 1)] = 0x00;
861 if ((card & 0x1) == 0x1)
862 eframe[CMD_BYTE(mod, 2)] = 0x80;
863 else
864 eframe[CMD_BYTE(mod, 2)] = 0x00;
865 break;
866
867 case BRI:
868 if (unlikely((curcmd != 0x101010) && (curcmd & 0x1010) == 0x1010)) /* b400m CPLD */
869 eframe[CMD_BYTE(mod, 0)] = 0x55;
870 else /* xhfc */
871 eframe[CMD_BYTE(mod, 0)] = 0x10;
872 eframe[CMD_BYTE(mod, 1)] = (curcmd >> 8) & 0xff;
873 eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
874 break;
875
876 case QRV:
877 eframe[CMD_BYTE(mod, 0)] = 0x00;
878 if (!curcmd) {
879 eframe[CMD_BYTE(mod, 1)] = 0x00;
880 eframe[CMD_BYTE(mod, 2)] = 0x00;
881 } else {
882 if (curcmd & __CMD_WR)
883 eframe[CMD_BYTE(mod, 1)] = 0x40 | ((curcmd >> 8) & 0x3f);
884 else
885 eframe[CMD_BYTE(mod, 1)] = 0xc0 | ((curcmd >> 8) & 0x3f);
886 eframe[CMD_BYTE(mod, 2)] = curcmd & 0xff;
887 }
888 break;
889
890 case NONE:
891 eframe[CMD_BYTE(mod, 0)] = 0x10;
892 eframe[CMD_BYTE(mod, 1)] = 0x10;
893 eframe[CMD_BYTE(mod, 2)] = 0x10;
894 break;
895 }
896 }
897
898 static inline void cmd_decipher_vpmoct(struct wctdm *wc, const u8 *eframe)
899 {
900 struct vpmoct *vpm = wc->vpmoct;
901 struct vpmoct_cmd *cmd;
902 int i;
903
904 /* Skip audio and first 6 timeslots */
905 eframe += 30;
906
907 spin_lock(&vpm->list_lock);
908 /* No command to handle, just exit */
909 if (list_empty(&vpm->active_list)) {
910 spin_unlock(&vpm->list_lock);
911 return;
912 }
913
914 cmd = list_entry(vpm->active_list.next, struct vpmoct_cmd, node);
915 if (wc->rxident == cmd->txident)
916 list_del_init(&cmd->node);
917 else
918 cmd = NULL;
919 spin_unlock(&vpm->list_lock);
920
921 if (!cmd)
922 return;
923
924 /* Store result, Ignoring the first "sync spi command" bytes */
925 cmd->command = eframe[7*3];
926 cmd->address = eframe[7*4];
927 for (i = 0; i < cmd->chunksize; ++i)
928 cmd->data[i] = eframe[7*(5+i)];
929 complete(&cmd->complete);
930 }
931
932 static inline void cmd_decipher_vpmadt032(struct wctdm *wc, const u8 *eframe)
933 {
934 struct vpmadt032 *const vpm = wc->vpmadt032;
935 struct vpmadt032_cmd *cmd;
936
937 BUG_ON(!vpm);
938
939 /* If the hardware is not processing any commands currently, then
940 * there is nothing for us to do here. */
941 if (list_empty(&vpm->active_cmds)) {
942 return;
943 }
944
945 spin_lock(&vpm->list_lock);
946 cmd = list_entry(vpm->active_cmds.next, struct vpmadt032_cmd, node);
947 if (wc->rxident == cmd->txident) {
948 list_del_init(&cmd->node);
949 } else {
950 cmd = NULL;
951 }
952 spin_unlock(&vpm->list_lock);
953
954 if (!cmd)
955 return;
956
957 /* Skip audio */
958 eframe += 24;
959
960 /* Store result */
961 cmd->data = (0xff & eframe[VPM_CMD_BYTE(25, 1)]) << 8;
962 cmd->data |= eframe[VPM_CMD_BYTE(25, 2)];
963 if (cmd->desc & __VPM150M_WR) {
964 kfree(cmd);
965 } else {
966 cmd->desc |= __VPM150M_FIN;
967 complete(&cmd->complete);
968 }
969 }
970
971 /**
972 * Call with the reglock held and local interrupts disabled
973 */
974 static void _cmd_decipher(struct wctdm *wc, const u8 *eframe, int card)
975 {
976 enum { TDM_BYTES = 24, };
977 struct wctdm_module *const mod = &wc->mods[card];
978 struct wctdm_cmd *cmd;
979 u8 address;
980 u8 value;
981
982 if (list_empty(&mod->active_cmds))
983 return;
984
985 cmd = list_entry(mod->active_cmds.next, struct wctdm_cmd, node);
986 if (cmd->ident != wc->rxident)
987 return;
988
989 list_del(&cmd->node);
990
991 if (cmd->cmd & __CMD_WR) {
992 kfree(cmd);
993 return;
994 }
995
996 address = (cmd->cmd >> 8) & 0xff;
997
998 cmd->cmd = eframe[TDM_BYTES + CMD_BYTE(mod, 2)];
999
1000 value = (cmd->cmd & 0xff);
1001
1002 if (cmd->complete) {
1003 complete(cmd->complete);
1004 return;
1005 }
1006
1007 list_add(&cmd->node, &wc->free_isr_commands);
1008
1009 switch (mod->type) {
1010 case FXS:
1011 if (68 == address)
1012 mod->mod.fxs.hook_state_shadow = value;
1013 else
1014 mod->mod.fxs.linefeed_control_shadow = value;
1015 break;
1016 case FXO:
1017 if (5 == address)
1018 mod->mod.fxo.hook_ring_shadow = value;
1019 else
1020 mod->mod.fxo.line_voltage_status = value;
1021 break;
1022 case QRV:
1023 /* wctdm_isr_getreg(wc, mod, 3); */ /* COR/CTCSS state */
1024 /* TODO: This looks broken to me, but I have no way to
1025 * resolved it. */
1026 /* wc->mods[card & 0xfc].cmds[USER_COMMANDS + 1] = CMD_RD(3); */
1027 break;
1028 default:
1029 break;
1030 }
1031 }
1032
1033 /* Call with wc.reglock held and local interrupts disabled. */
1034 static void
1035 wctdm_isr_getreg(struct wctdm *wc, struct wctdm_module *const mod, u8 address)
1036 {
1037 struct wctdm_cmd *cmd;
1038
1039 if (!list_empty(&wc->free_isr_commands)) {
1040 cmd = list_entry(wc->free_isr_commands.next,
1041 struct wctdm_cmd, node);
1042 list_del(&cmd->node);
1043 } else {
1044 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
1045 if (unlikely(!cmd))
1046 return;
1047 }
1048
1049 cmd->cmd = CMD_RD(address);
1050 cmd->complete = NULL;
1051
1052 list_add(&cmd->node, &mod->pending_cmds);
1053 }
1054
1055 /* Must be called with wc.reglock held and local interrupts disabled */
1056 static inline void
1057 wctdm_setreg_intr(struct wctdm *wc, struct wctdm_module *mod, int addr, int val)
1058 {
1059 struct wctdm_cmd *cmd;
1060
1061 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
1062 if (unlikely(!cmd))
1063 return;
1064
1065 cmd->complete = NULL;
1066 cmd->cmd = CMD_WR(addr, val);
1067
1068 list_add_tail(&cmd->node, &mod->pending_cmds);
1069 }
1070
1071
1072 static void cmd_checkisr(struct wctdm *wc, struct wctdm_module *const mod)
1073 {
1074 if (mod->sethook) {
1075 wctdm_setreg_intr(wc, mod, ((mod->sethook >> 8) & 0xff),
1076 mod->sethook & 0xff);
1077 mod->sethook = 0;
1078 return;
1079 }
1080
1081 switch (mod->type) {
1082 case FXS:
1083 wctdm_isr_getreg(wc, mod, 68); /* Hook state */
1084 #ifdef PAQ_DEBUG
1085 wctdm_isr_getreg(wc, mod, 19); /* Transistor interrupts */
1086 #else
1087 wctdm_isr_getreg(wc, mod, LINE_STATE);
1088 #endif
1089 break;
1090 case FXO:
1091 wctdm_isr_getreg(wc, mod, 5); /* Hook/Ring state */
1092 wctdm_isr_getreg(wc, mod, 29); /* Battery */
1093 break;
1094 case QRV:
1095 wctdm_isr_getreg(wc, mod, 3); /* COR/CTCSS state */
1096 /* TODO: This looks broken to me, but I have no way to
1097 * resolved it. */
1098 /* wc->mods[card & 0xfc].cmds[USER_COMMANDS + 1] = CMD_RD(3); */
1099 break;
1100 case BRI:
1101 /* TODO: Two calls needed here? */
1102 wctdm_bri_checkisr(wc, mod, 0);
1103 wctdm_bri_checkisr(wc, mod, 1);
1104 break;
1105 default:
1106 break;
1107 }
1108 }
1109
1110 /**
1111 * insert_tdm_data() - Move TDM data from channels to sframe.
1112 *
1113 */
1114 static void insert_tdm_data(const struct wctdm *wc, u8 *sframe)
1115 {
1116 int i;
1117 register u8 *chanchunk;
1118
1119 for (i = 0; i < wc->avchannels; i += 4) {
1120 chanchunk = &wc->chans[0 + i]->chan.writechunk[0];
1121 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
1122 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
1123 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
1124 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
1125 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
1126 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
1127 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
1128 sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
1129
1130 chanchunk = &wc->chans[1 + i]->chan.writechunk[0];
1131 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
1132 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
1133 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
1134 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
1135 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
1136 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
1137 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
1138 sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
1139
1140 chanchunk = &wc->chans[2 + i]->chan.writechunk[0];
1141 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
1142 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
1143 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
1144 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
1145 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
1146 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
1147 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
1148 sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
1149
1150 chanchunk = &wc->chans[3 + i]->chan.writechunk[0];
1151 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
1152 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
1153 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
1154 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
1155 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
1156 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
1157 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
1158 sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
1159 }
1160 }
1161
1162 static inline void wctdm_transmitprep(struct wctdm *wc, unsigned char *sframe)
1163 {
1164 int x, y;
1165 struct dahdi_span *s;
1166 unsigned char *eframe = sframe;
1167
1168 /* Calculate Transmission */
1169 if (likely(is_initialized(wc))) {
1170 for (x = 0; x < MAX_SPANS; x++) {
1171 if (wc->spans[x]) {
1172 s = &wc->spans[x]->span;
1173 _dahdi_transmit(s);
1174 }
1175 }
1176 insert_tdm_data(wc, sframe);
1177 #ifdef CONFIG_VOICEBUS_ECREFERENCE
1178 for (x = 0; x < wc->avchannels; ++x) {
1179 __dahdi_fifo_put(wc->ec_reference[x],
1180 wc->chans[x]->chan.writechunk,
1181 DAHDI_CHUNKSIZE);
1182 }
1183 #endif
1184 }
1185
1186 spin_lock(&wc->reglock);
1187 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
1188 /* Send a sample, as a 32-bit word */
1189
1190 /* TODO: ABK: hmm, this was originally mods_per_board, but we
1191 * need to worry about all the active "voice" timeslots, since
1192 * BRI modules have a different number of TDM channels than
1193 * installed modules. */
1194 for (y = 0; y < wc->avchannels; y++) {
1195 if (y < wc->mods_per_board)
1196 _cmd_dequeue(wc, eframe, y, x);
1197 }
1198
1199 if (wc->vpmadt032)
1200 cmd_dequeue_vpmadt032(wc, eframe);
1201 else if (wc->vpmoct)
1202 cmd_dequeue_vpmoct(wc, eframe);
1203
1204 if (x < DAHDI_CHUNKSIZE - 1) {
1205 eframe[EFRAME_SIZE] = wc->ctlreg;
1206 eframe[EFRAME_SIZE + 1] = wc->txident++;
1207 if (4 == wc->desc->ports)
1208 eframe[EFRAME_SIZE + 2] = wc->tdm410leds;
1209 }
1210 eframe += (EFRAME_SIZE + EFRAME_GAP);
1211 }
1212 spin_unlock(&wc->reglock);
1213 }
1214
1215 int wctdm_setreg(struct wctdm *wc, struct wctdm_module *mod, int addr, int val)
1216 {
1217 struct wctdm_cmd *cmd;
1218 unsigned long flags;
1219
1220 #if 0 /* TODO */
1221 /* QRV and BRI cards are only addressed at their first "port" */
1222 if ((card & 0x03) && ((wc->mods[card].type == QRV) ||
1223 (wc->mods[card].type == BRI)))
1224 return 0;
1225 #endif
1226
1227 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1228 if (unlikely(!cmd))
1229 return -ENOMEM;
1230
1231 cmd->complete = NULL;
1232 cmd->cmd = CMD_WR(addr, val);
1233
1234 spin_lock_irqsave(&wc->reglock, flags);
1235 list_add_tail(&cmd->node, &mod->pending_cmds);
1236 spin_unlock_irqrestore(&wc->reglock, flags);
1237
1238 return 0;
1239 }
1240
1241 int wctdm_getreg(struct wctdm *wc, struct wctdm_module *const mod, int addr)
1242 {
1243 unsigned long flags;
1244 struct wctdm_cmd *cmd;
1245 int val;
1246
1247 #if 0 /* TODO */
1248 /* if a QRV card, use only its first channel */
1249 if (wc->mods[card].type == QRV) {
1250 if (card & 3)
1251 return 0;
1252 }
1253 #endif
1254
1255 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1256 if (!cmd)
1257 return -ENOMEM;
1258
1259 cmd->complete = kmalloc(sizeof(*cmd->complete), GFP_KERNEL);
1260 if (!cmd->complete) {
1261 kfree(cmd);
1262 return -ENOMEM;
1263 }
1264
1265 init_completion(cmd->complete);
1266
1267 cmd->cmd = CMD_RD(addr);
1268
1269 spin_lock_irqsave(&wc->reglock, flags);
1270 list_add_tail(&cmd->node, &mod->pending_cmds);
1271 spin_unlock_irqrestore(&wc->reglock, flags);
1272
1273 wait_for_completion(cmd->complete);
1274 val = cmd->cmd & 0xff;
1275
1276 kfree(cmd->complete);
1277 kfree(cmd);
1278
1279 return val;
1280 }
1281
1282 static int wctdm_getregs(struct wctdm *wc, struct wctdm_module *const mod,
1283 int *const addresses, const size_t count)
1284 {
1285 int x;
1286 unsigned long flags;
1287 struct wctdm_cmd *cmd;
1288 struct wctdm_cmd **cmds = kmalloc(sizeof(cmd) * count, GFP_KERNEL);
1289
1290 if (!cmds)
1291 return -ENOMEM;
1292
1293 for (x = 0; x < count; ++x) {
1294 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1295 if (!cmd) {
1296 kfree(cmds);
1297 return -ENOMEM;
1298 }
1299
1300 cmd->complete = kmalloc(sizeof(*cmd->complete), GFP_KERNEL);
1301 if (!cmd->complete) {
1302 kfree(cmd);
1303 kfree(cmds);
1304 return -ENOMEM;
1305 }
1306
1307 init_completion(cmd->complete);
1308
1309 cmd->cmd = CMD_RD(addresses[x]);
1310
1311 spin_lock_irqsave(&wc->reglock, flags);
1312 list_add_tail(&cmd->node, &mod->pending_cmds);
1313 spin_unlock_irqrestore(&wc->reglock, flags);
1314
1315 cmds[x] = cmd;
1316 }
1317
1318 for (x = count - 1; x >= 0; --x) {
1319 cmd = cmds[x];
1320 wait_for_completion(cmd->complete);
1321 addresses[x] = cmd->cmd & 0xff;
1322 kfree(cmd->complete);
1323 kfree(cmd);
1324 }
1325
1326 kfree(cmds);
1327 return 0;
1328 }
1329
1330 /**
1331 * call with wc->reglock held and interrupts disabled.
1332 */
1333 static void cmd_retransmit(struct wctdm *wc)
1334 {
1335 int x;
1336
1337 for (x = 0; x < wc->mods_per_board; x++) {
1338 struct wctdm_module *const mod = &wc->mods[x];
1339 if (mod->type == BRI)
1340 continue;
1341 list_splice_init(&mod->active_cmds, &mod->pending_cmds);
1342 }
1343
1344 #ifdef VPM_SUPPORT
1345 if (wc->vpmadt032)
1346 vpmadt032_resend(wc->vpmadt032);
1347 #endif
1348 }
1349
1350 /**
1351 * extract_tdm_data() - Move TDM data from sframe to channels.
1352 *
1353 */
1354 static void extract_tdm_data(struct wctdm *wc, const u8 *sframe)
1355 {
1356 int i;
1357 register u8 *chanchunk;
1358
1359 for (i = 0; i < wc->avchannels; i += 4) {
1360 chanchunk = &wc->chans[0 + i]->chan.readchunk[0];
1361 chanchunk[0] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
1362 chanchunk[1] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
1363 chanchunk[2] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
1364 chanchunk[3] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
1365 chanchunk[4] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
1366 chanchunk[5] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
1367 chanchunk[6] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
1368 chanchunk[7] = sframe[0 + i + (EFRAME_SIZE + EFRAME_GAP)*7];
1369
1370 chanchunk = &wc->chans[1 + i]->chan.readchunk[0];
1371 chanchunk[0] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
1372 chanchunk[1] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
1373 chanchunk[2] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
1374 chanchunk[3] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
1375 chanchunk[4] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
1376 chanchunk[5] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
1377 chanchunk[6] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
1378 chanchunk[7] = sframe[1 + i + (EFRAME_SIZE + EFRAME_GAP)*7];
1379
1380 chanchunk = &wc->chans[2 + i]->chan.readchunk[0];
1381 chanchunk[0] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
1382 chanchunk[1] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
1383 chanchunk[2] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
1384 chanchunk[3] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
1385 chanchunk[4] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
1386 chanchunk[5] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
1387 chanchunk[6] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
1388 chanchunk[7] = sframe[2 + i + (EFRAME_SIZE + EFRAME_GAP)*7];
1389
1390 chanchunk = &wc->chans[3 + i]->chan.readchunk[0];
1391 chanchunk[0] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*0];
1392 chanchunk[1] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*1];
1393 chanchunk[2] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*2];
1394 chanchunk[3] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*3];
1395 chanchunk[4] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*4];
1396 chanchunk[5] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*5];
1397 chanchunk[6] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*6];
1398 chanchunk[7] = sframe[3 + i + (EFRAME_SIZE + EFRAME_GAP)*7];
1399 }
1400
1401 /* Pre-echo with the vpmoct overwrites the 24th timeslot with the
1402 * specified channel's pre-echo audio stream. This data is ignored
1403 * on all but the 24xx card, so we store it in a temporary buffer.
1404 */
1405 if (wc->vpmoct && wc->vpmoct->preecho_enabled) {
1406 chanchunk = &wc->vpmoct->preecho_buf[0];
1407 chanchunk[0] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*0];
1408 chanchunk[1] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*1];
1409 chanchunk[2] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*2];
1410 chanchunk[3] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*3];
1411 chanchunk[4] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*4];
1412 chanchunk[5] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*5];
1413 chanchunk[6] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*6];
1414 chanchunk[7] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*7];
1415 }
1416 }
1417
1418 static inline void wctdm_receiveprep(struct wctdm *wc, const u8 *sframe)
1419 {
1420 int x, y;
1421 bool irqmiss = false;
1422 unsigned char expected;
1423 const u8 *eframe = sframe;
1424
1425 if (unlikely(!is_good_frame(sframe)))
1426 return;
1427
1428 spin_lock(&wc->reglock);
1429
1430 if (likely(is_initialized(wc)))
1431 extract_tdm_data(wc, sframe);
1432
1433 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
1434 if (x < DAHDI_CHUNKSIZE - 1) {
1435 expected = wc->rxident + 1;
1436 wc->rxident = eframe[EFRAME_SIZE + 1];
1437 if (wc->rxident != expected) {
1438 irqmiss = true;
1439 cmd_retransmit(wc);
1440 }
1441 }
1442
1443 for (y = 0; y < wc->avchannels; y++)
1444 _cmd_decipher(wc, eframe, y);
1445
1446 if (wc->vpmadt032)
1447 cmd_decipher_vpmadt032(wc, eframe);
1448 else if (wc->vpmoct)
1449 cmd_decipher_vpmoct(wc, eframe);
1450
1451 eframe += (EFRAME_SIZE + EFRAME_GAP);
1452 }
1453 spin_unlock(&wc->reglock);
1454
1455 /* XXX We're wasting 8 taps. We should get closer :( */
1456 if (likely(is_initialized(wc))) {
1457 for (x = 0; x < wc->avchannels; x++) {
1458 struct wctdm_chan *const wchan = wc->chans[x];
1459 struct dahdi_chan *const c = &wchan->chan;
1460 #ifdef CONFIG_VOICEBUS_ECREFERENCE
1461 unsigned char buffer[DAHDI_CHUNKSIZE];
1462 __dahdi_fifo_get(wc->ec_reference[x], buffer,
1463 ARRAY_SIZE(buffer));
1464 _dahdi_ec_chunk(c, c->readchunk, buffer);
1465 #else
1466 if ((wc->vpmoct) &&
1467 (wchan->timeslot == wc->vpmoct->preecho_timeslot) &&
1468 (wc->vpmoct->preecho_enabled)) {
1469 __dahdi_ec_chunk(c, c->readchunk,
1470 wc->vpmoct->preecho_buf,
1471 c->writechunk);
1472 } else {
1473 __dahdi_ec_chunk(c, c->readchunk, c->readchunk,
1474 c->writechunk);
1475 }
1476 #endif
1477 }
1478
1479 for (x = 0; x < MAX_SPANS; x++) {
1480 if (wc->spans[x]) {
1481 struct dahdi_span *s = &wc->spans[x]->span;
1482 #if 1
1483 /* Check for digital spans */
1484 if (s->ops->chanconfig == b400m_chanconfig) {
1485 BUG_ON(!is_hx8(wc));
1486 if (s->flags & DAHDI_FLAG_RUNNING)
1487 b400m_dchan(s);
1488
1489 }
1490 #endif
1491 _dahdi_receive(s);
1492 if (unlikely(irqmiss))
1493 wc->ddev->irqmisses++;
1494 }
1495 }
1496 }
1497 }
1498
1499 static int wait_access(struct wctdm *wc, struct wctdm_module *const mod)
1500 {
1501 unsigned char data = 0;
1502 int count = 0;
1503
1504 #define MAX_ATTEMPTS 10
1505
1506 /* Wait for indirect access */
1507 while (count++ < MAX_ATTEMPTS) {
1508 data = wctdm_getreg(wc, mod, I_STATUS);
1509 if (!data)
1510 return 0;
1511 }
1512
1513 if (count > (MAX_ATTEMPTS - 1)) {
1514 dev_notice(&wc->vb.pdev->dev,
1515 " ##### Loop error (%02x) #####\n", data);
1516 }
1517
1518 return 0;
1519 }
1520
1521 static unsigned char translate_3215(unsigned char address)
1522 {
1523 int x;
1524 for (x = 0; x < ARRAY_SIZE(indirect_regs); x++) {
1525 if (indirect_regs[x].address == address) {
1526 address = indirect_regs[x].altaddr;
1527 break;
1528 }
1529 }
1530 return address;
1531 }
1532
1533 static int
1534 wctdm_proslic_setreg_indirect(struct wctdm *wc, struct wctdm_module *const mod,
1535 unsigned char address, unsigned short data)
1536 {
1537 int res = -1;
1538
1539 address = translate_3215(address);
1540 if (address == 255)
1541 return 0;
1542
1543 if (!wait_access(wc, mod)) {
1544 wctdm_setreg(wc, mod, IDA_LO, (u8)(data & 0xFF));
1545 wctdm_setreg(wc, mod, IDA_HI, (u8)((data & 0xFF00)>>8));
1546 wctdm_setreg(wc, mod, IAA, address);
1547 res = 0;
1548 };
1549 return res;
1550 }
1551
1552 static int
1553 wctdm_proslic_getreg_indirect(struct wctdm *wc, struct wctdm_module *const mod,
1554 unsigned char address)
1555 {
1556 int res = -1;
1557 char *p=NULL;
1558
1559 address = translate_3215(address);
1560 if (address == 255)
1561 return 0;
1562
1563 if (!wait_access(wc, mod)) {
1564 wctdm_setreg(wc, mod, IAA, address);
1565 if (!wait_access(wc, mod)) {
1566 int addresses[2] = {IDA_LO, IDA_HI};
1567 wctdm_getregs(wc, mod, addresses,
1568 ARRAY_SIZE(addresses));
1569 res = addresses[0] | (addresses[1] << 8);
1570 } else
1571 p = "Failed to wait inside\n";
1572 } else
1573 p = "failed to wait\n";
1574 if (p)
1575 dev_notice(&wc->vb.pdev->dev, "%s", p);
1576 return res;
1577 }
1578
1579 static int
1580 wctdm_proslic_init_indirect_regs(struct wctdm *wc, struct wctdm_module *mod)
1581 {
1582 unsigned char i;
1583
1584 for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
1585 if (wctdm_proslic_setreg_indirect(wc, mod,
1586 indirect_regs[i].address,
1587 indirect_regs[i].initial))
1588 return -1;
1589 }
1590
1591 return 0;
1592 }
1593
1594 static int
1595 wctdm_proslic_verify_indirect_regs(struct wctdm *wc, struct wctdm_module *mod)
1596 {
1597 int passed = 1;
1598 unsigned short i, initial;
1599 int j;
1600
1601 for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
1602 j = wctdm_proslic_getreg_indirect(wc, mod,
1603 (u8)indirect_regs[i].address);
1604 if (j < 0) {
1605 dev_notice(&wc->vb.pdev->dev, "Failed to read indirect register %d\n", i);
1606 return -1;
1607 }
1608 initial = indirect_regs[i].initial;
1609
1610 if ((j != initial) && (indirect_regs[i].altaddr != 255)) {
1611 dev_notice(&wc->vb.pdev->dev,
1612 "!!!!!!! %s iREG %X = %X should be %X\n",
1613 indirect_regs[i].name,
1614 indirect_regs[i].address, j, initial);
1615 passed = 0;
1616 }
1617 }
1618
1619 if (passed) {
1620 if (debug & DEBUG_CARD) {
1621 dev_info(&wc->vb.pdev->dev,
1622 "Init Indirect Registers completed successfully.\n");
1623 }
1624 } else {
1625 dev_notice(&wc->vb.pdev->dev,
1626 " !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
1627 return -1;
1628 }
1629 return 0;
1630 }
1631
1632 /**
1633 * wctdm_proslic_check_oppending -
1634 *
1635 * Ensures that a write to the line feed register on the SLIC has been
1636 * processed. If it hasn't after the timeout value, then it will resend the
1637 * command and wait for another timeout period.
1638 *
1639 */
1640 static void
1641 wctdm_proslic_check_oppending(struct wctdm *wc, struct wctdm_module *const mod)
1642 {
1643 struct fxs *const fxs = &mod->mod.fxs;
1644
1645 if (!(fxs->lasttxhook & SLIC_LF_OPPENDING))
1646 return;
1647
1648 /* Monitor the Pending LF state change, for the next 100ms */
1649 spin_lock(&wc->reglock);
1650
1651
1652 if (!(fxs->lasttxhook & SLIC_LF_OPPENDING)) {
1653 spin_unlock(&wc->reglock);
1654 return;
1655 }
1656
1657 if ((fxs->linefeed_control_shadow & SLIC_LF_SETMASK) ==
1658 (fxs->lasttxhook & SLIC_LF_SETMASK)) {
1659 fxs->lasttxhook &= SLIC_LF_SETMASK;
1660 if (debug & DEBUG_CARD) {
1661 dev_info(&wc->vb.pdev->dev,
1662 "SLIC_LF OK: card=%d shadow=%02x "
1663 "lasttxhook=%02x framecount=%ld\n", mod->card,
1664 fxs->linefeed_control_shadow,
1665 fxs->lasttxhook, wc->framecount);
1666 }
1667 } else if (time_after(wc->framecount, fxs->oppending_timeout)) {
1668 /* Check again in 100 ms */
1669 fxs->oppending_timeout = wc->framecount + 100;
1670
1671 wctdm_setreg_intr(wc, mod, LINE_STATE, fxs->lasttxhook);
1672 if (debug & DEBUG_CARD) {
1673 dev_info(&wc->vb.pdev->dev,
1674 "SLIC_LF RETRY: card=%d shadow=%02x "
1675 "lasttxhook=%02x framecount=%ld\n", mod->card,
1676 fxs->linefeed_control_shadow,
1677 fxs->lasttxhook, wc->framecount);
1678 }
1679 }
1680
1681 spin_unlock(&wc->reglock);
1682 }
1683
1684 /* 256ms interrupt */
1685 static void
1686 wctdm_proslic_recheck_sanity(struct wctdm *wc, struct wctdm_module *const mod)
1687 {
1688 struct fxs *const fxs = &mod->mod.fxs;
1689 int res;
1690 #ifdef PAQ_DEBUG
1691 res = mod->isrshadow[1];
1692 res &= ~0x3;
1693 if (res) {
1694 mod->isrshadow[1] = 0;
1695 fxs->palarms++;
1696 if (fxs->palarms < MAX_ALARMS) {
1697 dev_notice(&wc->vb.pdev->dev, "Power alarm (%02x) on module %d, resetting!\n", res, card + 1);
1698 mod->sethook = CMD_WR(19, res);
1699 /* Update shadow register to avoid extra power alarms until next read */
1700 mod->isrshadow[1] = 0;
1701 } else {
1702 if (fxs->palarms == MAX_ALARMS)
1703 dev_notice(&wc->vb.pdev->dev, "Too many power alarms on card %d, NOT resetting!\n", card + 1);
1704 }
1705 }
1706 #else
1707 spin_lock(&wc->reglock);
1708
1709 /* reg 64 has to be zero at last isr read */
1710 res = !fxs->linefeed_control_shadow &&
1711 !(fxs->lasttxhook & SLIC_LF_OPPENDING) && /* not a transition */
1712 fxs->lasttxhook; /* not an intended zero */
1713
1714 if (res) {
1715 fxs->palarms++;
1716 if (fxs->palarms < MAX_ALARMS) {
1717 dev_notice(&wc->vb.pdev->dev,
1718 "Power alarm on module %d, resetting!\n",
1719 mod->card + 1);
1720 if (fxs->lasttxhook == SLIC_LF_RINGING) {
1721 fxs->lasttxhook = POLARITY_XOR(fxs) ?
1722 SLIC_LF_ACTIVE_REV :
1723 SLIC_LF_ACTIVE_FWD;;
1724 }
1725 fxs->lasttxhook |= SLIC_LF_OPPENDING;
1726 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
1727 fxs->oppending_timeout = wc->framecount + 100;
1728
1729 /* Update shadow register to avoid extra power alarms
1730 * until next read */
1731 fxs->linefeed_control_shadow = fxs->lasttxhook;
1732 } else {
1733 if (fxs->palarms == MAX_ALARMS) {
1734 dev_notice(&wc->vb.pdev->dev,
1735 "Too many power alarms on card %d, "
1736 "NOT resetting!\n", mod->card + 1);
1737 }
1738 }
1739 }
1740 spin_unlock(&wc->reglock);
1741 #endif
1742 }
1743
1744 static void wctdm_qrvdri_check_hook(struct wctdm *wc, int card)
1745 {
1746 signed char b,b1;
1747 int qrvcard = card & 0xfc;
1748
1749 if (wc->mods[card].mod.qrv.debtime >= 2)
1750 wc->mods[card].mod.qrv.debtime--;
1751 b = wc->mods[qrvcard].mod.qrv.isrshadow[0]; /* Hook/Ring state */
1752 b &= 0xcc; /* use bits 3-4 and 6-7 only */
1753
1754 if (wc->mods[qrvcard].mod.qrv.radmode & RADMODE_IGNORECOR)
1755 b &= ~4;
1756 else if (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_INVERTCOR))
1757 b ^= 4;
1758 if (wc->mods[qrvcard + 1].mod.qrv.radmode | RADMODE_IGNORECOR)
1759 b &= ~0x40;
1760 else if (!(wc->mods[qrvcard + 1].mod.qrv.radmode | RADMODE_INVERTCOR))
1761 b ^= 0x40;
1762
1763 if ((wc->mods[qrvcard].mod.qrv.radmode & RADMODE_IGNORECT) ||
1764 (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_EXTTONE)))
1765 b &= ~8;
1766 else if (!(wc->mods[qrvcard].mod.qrv.radmode & RADMODE_EXTINVERT))
1767 b ^= 8;
1768 if ((wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_IGNORECT) ||
1769 (!(wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_EXTTONE)))
1770 b &= ~0x80;
1771 else if (!(wc->mods[qrvcard + 1].mod.qrv.radmode & RADMODE_EXTINVERT))
1772 b ^= 0x80;
1773 /* now b & MASK should be zero, if its active */
1774 /* check for change in chan 0 */
1775 if ((!(b & 0xc)) != wc->mods[qrvcard + 2].mod.qrv.hook)
1776 {
1777 wc->mods[qrvcard].mod.qrv.debtime = wc->mods[qrvcard].mod.qrv.debouncetime;
1778 wc->mods[qrvcard + 2].mod.qrv.hook = !(b & 0xc);
1779 }
1780 /* if timed-out and ready */
1781 if (wc->mods[qrvcard].mod.qrv.debtime == 1) {
1782 b1 = wc->mods[qrvcard + 2].mod.qrv.hook;
1783 if (debug) {
1784 dev_info(&wc->vb.pdev->dev,
1785 "QRV channel %d rx state changed to %d\n",
1786 qrvcard, wc->mods[qrvcard + 2].mod.qrv.hook);
1787 }
1788 dahdi_hooksig(wc->aspan->span.chans[qrvcard],
1789 (b1) ? DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
1790 wc->mods[card].mod.qrv.debtime = 0;
1791 }
1792 /* check for change in chan 1 */
1793 if ((!(b & 0xc0)) != wc->mods[qrvcard + 3].mod.qrv.hook)
1794 {
1795 wc->mods[qrvcard + 1].mod.qrv.debtime = QRV_DEBOUNCETIME;
1796 wc->mods[qrvcard + 3].mod.qrv.hook = !(b & 0xc0);
1797 }
1798 if (wc->mods[qrvcard + 1].mod.qrv.debtime == 1) {
1799 b1 = wc->mods[qrvcard + 3].mod.qrv.hook;
1800 if (debug) {
1801 dev_info(&wc->vb.pdev->dev,
1802 "QRV channel %d rx state changed to %d\n",
1803 qrvcard + 1, wc->mods[qrvcard + 3].mod.qrv.hook);
1804 }
1805 dahdi_hooksig(wc->aspan->span.chans[qrvcard + 1],
1806 (b1) ? DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
1807 wc->mods[card].mod.qrv.debtime = 0;
1808 }
1809 return;
1810 }
1811
1812 static inline bool is_fxo_ringing(const struct fxo *const fxo)
1813 {
1814 return ((fxo->hook_ring_shadow & 0x60) &&
1815 ((fxo->battery_state == BATTERY_PRESENT) ||
1816 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1817 }
1818
1819 static inline bool is_fxo_ringing_positive(const struct fxo *const fxo)
1820 {
1821 return (((fxo->hook_ring_shadow & 0x60) == 0x20) &&
1822 ((fxo->battery_state == BATTERY_PRESENT) ||
1823 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1824 }
1825
1826 static inline bool is_fxo_ringing_negative(const struct fxo *const fxo)
1827 {
1828 return (((fxo->hook_ring_shadow & 0x60) == 0x40) &&
1829 ((fxo->battery_state == BATTERY_PRESENT) ||
1830 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1831 }
1832
1833 static inline void set_ring(struct fxo *fxo, enum ring_detector_state new)
1834 {
1835 fxo->ring_state = new;
1836 }
1837
1838 static void wctdm_fxo_ring_detect(struct wctdm *wc, struct wctdm_module *mod)
1839 {
1840 struct fxo *const fxo = &mod->mod.fxo;
1841 static const unsigned int POLARITY_CHANGES_NEEDED = 2;
1842
1843 /* Look for ring status bits (Ring Detect Signal Negative and Ring
1844 * Detect Signal Positive) to transition back and forth
1845 * POLARITY_CHANGES_NEEDED times to indicate that a ring is occurring.
1846 * Provide some number of samples to allow for the transitions to occur
1847 * before giving up. NOTE: neon mwi voltages will trigger one of these
1848 * bits to go active but not to have transitions between the two bits
1849 * (i.e. no negative to positive or positive to negative traversals) */
1850
1851 switch (fxo->ring_state) {
1852 case DEBOUNCING_RINGING_POSITIVE:
1853 if (is_fxo_ringing_negative(fxo)) {
1854 if (++fxo->ring_polarity_change_count >
1855 POLARITY_CHANGES_NEEDED) {
1856 mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
1857 set_ring(fxo, RINGING);
1858 if (debug) {
1859 dev_info(&wc->vb.pdev->dev,
1860 "RING on %s!\n",
1861 get_dahdi_chan(wc, mod)->name);
1862 }
1863 } else {
1864 set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
1865 }
1866 } else if (time_after(wc->framecount,
1867 fxo->ringdebounce_timer)) {
1868 set_ring(fxo, RINGOFF);
1869 }
1870 break;
1871 case DEBOUNCING_RINGING_NEGATIVE:
1872 if (is_fxo_ringing_positive(fxo)) {
1873 if (++fxo->ring_polarity_change_count >
1874 POLARITY_CHANGES_NEEDED) {
1875 mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
1876 set_ring(fxo, RINGING);
1877 if (debug) {
1878 dev_info(&wc->vb.pdev->dev,
1879 "RING on %s!\n",
1880 get_dahdi_chan(wc, mod)->name);
1881 }
1882 } else {
1883 set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
1884 }
1885 } else if (time_after(wc->framecount,
1886 fxo->ringdebounce_timer)) {
1887 set_ring(fxo, RINGOFF);
1888 }
1889 break;
1890 case RINGING:
1891 if (!is_fxo_ringing(fxo)) {
1892 set_ring(fxo, DEBOUNCING_RINGOFF);
1893 fxo->ringdebounce_timer =
1894 wc->framecount + ringdebounce / 8;
1895 }
1896 break;
1897 case DEBOUNCING_RINGOFF:
1898 if (!is_fxo_ringing(fxo)) {
1899 if (time_after(wc->framecount,
1900 fxo->ringdebounce_timer)) {
1901 if (debug) {
1902 dev_info(&wc->vb.pdev->dev,
1903 "NO RING on %s!\n",
1904 get_dahdi_chan(wc, mod)->name);
1905 }
1906 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
1907 set_ring(fxo, RINGOFF);
1908 }
1909 } else {
1910 set_ring(fxo, RINGING);
1911 }
1912 break;
1913 case RINGOFF:
1914 if (is_fxo_ringing(fxo)) {
1915 /* Look for positive/negative crossings in ring status
1916 * reg */
1917 if (is_fxo_ringing_positive(fxo))
1918 set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
1919 else
1920 set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
1921 fxo->ringdebounce_timer =
1922 wc->framecount + ringdebounce / 8;
1923 fxo->ring_polarity_change_count = 0;
1924 }
1925 break;
1926 }
1927 }
1928
1929 #define MS_PER_CHECK_HOOK 1
1930
1931 static void
1932 wctdm_check_battery_lost(struct wctdm *wc, struct wctdm_module *const mod)
1933 {
1934 struct fxo *const fxo = &mod->mod.fxo;
1935
1936 /* possible existing states:
1937 battery lost, no debounce timer
1938 battery lost, debounce timer (going to battery present)
1939 battery present or unknown, no debounce timer
1940 battery present or unknown, debounce timer (going to battery lost)
1941 */
1942 switch (fxo->battery_state) {
1943 case BATTERY_DEBOUNCING_PRESENT_ALARM:
1944 fxo->battery_state = BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM;
1945 fxo->battdebounce_timer = wc->framecount + battdebounce;
1946 break;
1947 case BATTERY_DEBOUNCING_PRESENT:
1948 fxo->battery_state = BATTERY_LOST;
1949 break;
1950 case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
1951 fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
1952 fxo->battdebounce_timer = wc->framecount +
1953 battalarm - battdebounce;
1954 break;
1955 case BATTERY_UNKNOWN:
1956 mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
1957 fallthrough;
1958 case BATTERY_PRESENT:
1959 fxo->battery_state = BATTERY_DEBOUNCING_LOST;
1960 fxo->battdebounce_timer = wc->framecount + battdebounce;
1961 break;
1962 case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
1963 case BATTERY_DEBOUNCING_LOST: /* Intentional drop through */
1964 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1965 if (debug) {
1966 dev_info(&wc->vb.pdev->dev,
1967 "NO BATTERY on %d/%d!\n",
1968 wc->aspan->span.spanno,
1969 mod->card + 1);
1970 }
1971 #ifdef JAPAN
1972 if (!wc->ohdebounce && wc->offhook) {
1973 dahdi_hooksig(wc->aspan->chans[card],
1974 DAHDI_RXSIG_ONHOOK);
1975 if (debug) {
1976 dev_info(&wc->vb.pdev->dev,
1977 "Signalled On Hook\n");
1978 }
1979 #ifdef ZERO_BATT_RING
1980 wc->onhook++;
1981 #endif
1982 }
1983 #else
1984 mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
1985 #endif
1986 /* set the alarm timer, taking into account that part
1987 * of its time period has already passed while
1988 * debouncing occurred */
1989 fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
1990 fxo->battdebounce_timer = wc->framecount +
1991 battalarm - battdebounce;
1992 }
1993 break;
1994 case BATTERY_DEBOUNCING_LOST_ALARM:
1995 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1996 fxo->battery_state = BATTERY_LOST;
1997 dahdi_alarm_channel(get_dahdi_chan(wc, mod),
1998 DAHDI_ALARM_RED);
1999 }
2000 break;
2001 case BATTERY_LOST:
2002 break;
2003 }
2004 }
2005
2006 static void
2007 wctdm_check_battery_present(struct wctdm *wc, struct wctdm_module *const mod)
2008 {
2009 struct fxo *const fxo = &mod->mod.fxo;
2010
2011 switch (fxo->battery_state) {
2012 case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
2013 case BATTERY_DEBOUNCING_PRESENT: /* intentional drop through */
2014 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
2015 if (debug) {
2016 dev_info(&wc->vb.pdev->dev,
2017 "BATTERY on %d/%d (%s)!\n",
2018 wc->aspan->span.spanno, mod->card + 1,
2019 (fxo->line_voltage_status < 0) ?
2020 "-" : "+");
2021 }
2022 #ifdef ZERO_BATT_RING
2023 if (wc->onhook) {
2024 wc->onhook = 0;
2025 dahdi_hooksig(wc->aspan->chans[card],
2026 DAHDI_RXSIG_OFFHOOK);
2027 if (debug) {
2028 dev_info(&wc->vb.pdev->dev,
2029 "Signalled Off Hook\n");
2030 }
2031 }
2032 #else
2033 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
2034 #endif
2035 /* set the alarm timer, taking into account that part
2036 * of its time period has already passed while
2037 * debouncing occurred */
2038 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
2039 fxo->battdebounce_timer = wc->framecount +
2040 battalarm - battdebounce;
2041 }
2042 break;
2043 case BATTERY_DEBOUNCING_PRESENT_ALARM:
2044 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
2045 fxo->battery_state = BATTERY_PRESENT;
2046 dahdi_alarm_channel(get_dahdi_chan(wc, mod),
2047 DAHDI_ALARM_NONE);
2048 }
2049 break;
2050 case BATTERY_PRESENT:
2051 break;
2052 case BATTERY_DEBOUNCING_LOST_ALARM:
2053 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM;
2054 fxo->battdebounce_timer = wc->framecount + battdebounce;
2055 break;
2056 case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
2057 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
2058 fxo->battdebounce_timer = wc->framecount +
2059 battalarm - battdebounce;
2060 break;
2061 case BATTERY_DEBOUNCING_LOST:
2062 fxo->battery_state = BATTERY_PRESENT;
2063 break;
2064 case BATTERY_UNKNOWN:
2065 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
2066 fallthrough;
2067 case BATTERY_LOST: /* intentional drop through */
2068 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT;
2069 fxo->battdebounce_timer = wc->framecount + battdebounce;
2070 break;
2071 }
2072 }
2073
2074 static void
2075 wctdm_fxo_stop_debouncing_polarity(struct wctdm *wc,
2076 struct wctdm_module *const mod)
2077 {
2078 struct fxo *const fxo = &mod->mod.fxo;
2079 switch (fxo->polarity_state) {
2080 case UNKNOWN_POLARITY:
2081 break;
2082 case POLARITY_DEBOUNCE_POSITIVE:
2083 fxo->polarity_state = POLARITY_NEGATIVE;
2084 break;
2085 case POLARITY_POSITIVE:
2086 break;
2087 case POLARITY_DEBOUNCE_NEGATIVE:
2088 fxo->polarity_state = POLARITY_POSITIVE;
2089 break;
2090 case POLARITY_NEGATIVE:
2091 break;
2092 };
2093 }
2094
2095 static void
2096 wctdm_fxo_check_polarity(struct wctdm *wc, struct wctdm_module *const mod,
2097 const bool positive_polarity)
2098 {
2099 struct fxo *const fxo = &mod->mod.fxo;
2100
2101 switch (fxo->polarity_state) {
2102 case UNKNOWN_POLARITY:
2103 fxo->polarity_state = (positive_polarity) ? POLARITY_POSITIVE :
2104 POLARITY_NEGATIVE;
2105 break;
2106 case POLARITY_DEBOUNCE_POSITIVE:
2107 if (!positive_polarity) {
2108 fxo->polarity_state = POLARITY_NEGATIVE;
2109 } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
2110 fxo->polarity_state = POLARITY_POSITIVE;
2111 dahdi_qevent_lock(get_dahdi_chan(wc, mod),
2112 DAHDI_EVENT_POLARITY);
2113 if (debug & DEBUG_CARD) {
2114 dev_info(&wc->vb.pdev->dev,
2115 "%s: Polarity NEGATIVE -> POSITIVE\n",
2116 get_dahdi_chan(wc, mod)->name);
2117 }
2118 }
2119 break;
2120 case POLARITY_POSITIVE:
2121 if (!positive_polarity) {
2122 fxo->polarity_state = POLARITY_DEBOUNCE_NEGATIVE;
2123 fxo->poldebounce_timer = wc->framecount +
2124 POLARITY_DEBOUNCE;
2125 }
2126 break;
2127 case POLARITY_DEBOUNCE_NEGATIVE:
2128 if (positive_polarity) {
2129 fxo->polarity_state = POLARITY_POSITIVE;
2130 } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
2131 dahdi_qevent_lock(get_dahdi_chan(wc, mod),
2132 DAHDI_EVENT_POLARITY);
2133 if (debug & DEBUG_CARD) {
2134 dev_info(&wc->vb.pdev->dev,
2135 "%s: Polarity POSITIVE -> NEGATIVE\n",
2136 get_dahdi_chan(wc, mod)->name);
2137 }
2138 fxo->polarity_state = POLARITY_NEGATIVE;
2139 }
2140 break;
2141 case POLARITY_NEGATIVE:
2142 if (positive_polarity) {
2143 fxo->polarity_state = POLARITY_DEBOUNCE_POSITIVE;
2144 fxo->poldebounce_timer = wc->framecount +
2145 POLARITY_DEBOUNCE;
2146 }
2147 break;
2148 };
2149 }
2150
2151 static void
2152 wctdm_voicedaa_check_hook(struct wctdm *wc, struct wctdm_module *const mod)
2153 {
2154 signed char b;
2155 u8 abs_voltage;
2156 struct fxo *const fxo = &mod->mod.fxo;
2157
2158 /* Try to track issues that plague slot one FXO's */
2159 b = fxo->hook_ring_shadow & 0x9b;
2160
2161 if (fxo->offhook) {
2162 if (b != 0x9)
2163 wctdm_setreg_intr(wc, mod, 5, 0x9);
2164 } else {
2165 if (b != 0x8)
2166 wctdm_setreg_intr(wc, mod, 5, 0x8);
2167
2168 wctdm_fxo_ring_detect(wc, mod);
2169 }
2170
2171 abs_voltage = abs(fxo->line_voltage_status);
2172
2173 if (fxovoltage && time_after(wc->framecount, fxo->display_fxovoltage)) {
2174 /* Every 100 ms */
2175 fxo->display_fxovoltage = wc->framecount + 100;
2176 dev_info(&wc->vb.pdev->dev,
2177 "Port %d: Voltage: %d\n",
2178 mod->card + 1, fxo->line_voltage_status);
2179 }
2180
2181 if (unlikely(DAHDI_RXSIG_INITIAL == get_dahdi_chan(wc, mod)->rxhooksig)) {
2182 /*
2183 * dahdi-base will set DAHDI_RXSIG_INITIAL after a
2184 * DAHDI_STARTUP or DAHDI_CHANCONFIG ioctl so that new events
2185 * will be queued on the channel with the current received
2186 * hook state. Channels that use robbed-bit signalling always
2187 * report the current received state via the dahdi_rbsbits
2188 * call. Since we only call dahdi_hooksig when we've detected
2189 * a change to report, let's forget our current state in order
2190 * to force us to report it again via dahdi_hooksig.
2191 *
2192 */
2193 fxo->battery_state = BATTERY_UNKNOWN;
2194 }
2195
2196 if (abs_voltage < battthresh) {
2197 wctdm_fxo_stop_debouncing_polarity(wc, mod);
2198 wctdm_check_battery_lost(wc, mod);
2199 } else {
2200 wctdm_check_battery_present(wc, mod);
2201 wctdm_fxo_check_polarity(wc, mod,
2202 (fxo->line_voltage_status > 0));
2203 }
2204
2205 /* Look for neon mwi pulse */
2206 if (neonmwi_monitor && !fxo->offhook) {
2207 /* Look for 4 consecutive voltage readings
2208 * where the voltage is over the neon limit but
2209 * does not vary greatly from the last reading
2210 */
2211 if (fxo->battery_state == BATTERY_PRESENT &&
2212 abs_voltage > neonmwi_level &&
2213 (0 == fxo->neonmwi_last_voltage ||
2214 (fxo->line_voltage_status >= fxo->neonmwi_last_voltage - neonmwi_envelope &&
2215 fxo->line_voltage_status <= fxo->neonmwi_last_voltage + neonmwi_envelope))) {
2216 fxo->neonmwi_last_voltage = fxo->line_voltage_status;
2217 if (NEONMWI_ON_DEBOUNCE == fxo->neonmwi_debounce) {
2218 fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
2219 if (0 == fxo->neonmwi_state) {
2220 dahdi_qevent_lock(get_dahdi_chan(wc, mod), DAHDI_EVENT_NEONMWI_ACTIVE);
2221 fxo->neonmwi_state = 1;
2222 if (debug)
2223 dev_info(&wc->vb.pdev->dev, "NEON MWI active for card %d\n", mod->card+1);
2224 }
2225 fxo->neonmwi_debounce++; /* terminate the processing */
2226 } else if (NEONMWI_ON_DEBOUNCE > fxo->neonmwi_debounce) {
2227 fxo->neonmwi_debounce++;
2228 } else { /* Insure the count gets reset */
2229 fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
2230 }
2231 } else {
2232 fxo->neonmwi_debounce = 0;
2233 fxo->neonmwi_last_voltage = 0;
2234 }
2235 /* If no neon mwi pulse for given period of time, indicte no neon mwi state */
2236 if (fxo->neonmwi_state && 0 < fxo->neonmwi_offcounter ) {
2237 fxo->neonmwi_offcounter--;
2238 if (0 == fxo->neonmwi_offcounter) {
2239 dahdi_qevent_lock(get_dahdi_chan(wc, mod), DAHDI_EVENT_NEONMWI_INACTIVE);
2240 fxo->neonmwi_state = 0;
2241 if (debug)
2242 dev_info(&wc->vb.pdev->dev, "NEON MWI cleared for card %d\n", mod->card+1);
2243 }
2244 }
2245 }
2246 #undef MS_PER_CHECK_HOOK
2247 }
2248
2249 static void
2250 wctdm_fxs_hooksig(struct wctdm *wc, struct wctdm_module *const mod,
2251 enum dahdi_txsig txsig)
2252 {
2253 int x = 0;
2254 unsigned long flags;
2255 struct fxs *const fxs = &mod->mod.fxs;
2256
2257 spin_lock_irqsave(&wc->reglock, flags);
2258 switch (txsig) {
2259 case DAHDI_TXSIG_ONHOOK:
2260 switch (get_dahdi_chan(wc, mod)->sig) {
2261 case DAHDI_SIG_FXOGS:
2262 x = (POLARITY_XOR(fxs)) ?
2263 SLIC_LF_RING_OPEN :
2264 SLIC_LF_TIP_OPEN;
2265 break;
2266 case DAHDI_SIG_EM:
2267 case DAHDI_SIG_FXOKS:
2268 case DAHDI_SIG_FXOLS:
2269 default:
2270 x = fxs->idletxhookstate;
2271 break;
2272 }
2273 break;
2274 case DAHDI_TXSIG_OFFHOOK:
2275 switch (get_dahdi_chan(wc, mod)->sig) {
2276 case DAHDI_SIG_EM:
2277 x = (POLARITY_XOR(fxs)) ?
2278 SLIC_LF_ACTIVE_FWD :
2279 SLIC_LF_ACTIVE_REV;
2280 break;
2281 default:
2282 x = fxs->idletxhookstate;
2283 break;
2284 }
2285 break;
2286 case DAHDI_TXSIG_START:
2287 x = SLIC_LF_RINGING;
2288 break;
2289 case DAHDI_TXSIG_KEWL:
2290 x = SLIC_LF_OPEN;
2291 break;
2292 default:
2293 spin_unlock_irqrestore(&wc->reglock, flags);
2294 dev_notice(&wc->vb.pdev->dev,
2295 "wctdm24xxp: Can't set tx state to %d\n", txsig);
2296 return;
2297 }
2298
2299 if (x != fxs->lasttxhook) {
2300 fxs->lasttxhook = x | SLIC_LF_OPPENDING;
2301 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
2302 fxs->oppending_timeout = wc->framecount + 100;
2303 spin_unlock_irqrestore(&wc->reglock, flags);
2304
2305 if (debug & DEBUG_CARD) {
2306 dev_info(&wc->vb.pdev->dev, "Setting FXS hook state "
2307 "to %d (%02x) framecount=%ld\n", txsig, x,
2308 wc->framecount);
2309 }
2310 } else {
2311 spin_unlock_irqrestore(&wc->reglock, flags);
2312 }
2313 }
2314
2315 static void wctdm_fxs_off_hook(struct wctdm *wc, struct wctdm_module *const mod)
2316 {
2317 struct fxs *const fxs = &mod->mod.fxs;
2318
2319 if (debug & DEBUG_CARD)
2320 dev_info(&wc->vb.pdev->dev,
2321 "fxs_off_hook: Card %d Going off hook\n", mod->card);
2322 switch (fxs->lasttxhook) {
2323 case SLIC_LF_RINGING: /* Ringing */
2324 case SLIC_LF_OHTRAN_FWD: /* Forward On Hook Transfer */
2325 case SLIC_LF_OHTRAN_REV: /* Reverse On Hook Transfer */
2326 /* just detected OffHook, during Ringing or OnHookTransfer */
2327 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
2328 SLIC_LF_ACTIVE_REV :
2329 SLIC_LF_ACTIVE_FWD;
2330 break;
2331 }
2332 if ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
2333 wctdm_fxs_hooksig(wc, mod, DAHDI_TXSIG_OFFHOOK);
2334 dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_OFFHOOK);
2335
2336 #ifdef DEBUG
2337 if (robust)
2338 wctdm_init_proslic(wc, mod, 1, 0, 1);
2339 #endif
2340 }
2341
2342 /**
2343 * wctdm_fxs_on_hook - Report on hook to DAHDI.
2344 * @wc: Board hosting the module.
2345 * @card: Index of the module / port to place on hook.
2346 *
2347 * If we are intentionally dropping battery to signal a forward
2348 * disconnect we do not want to place the line "On-Hook". In this
2349 * case, the core of DAHDI will place us on hook when one of the RBS
2350 * timers expires.
2351 *
2352 */
2353 static void wctdm_fxs_on_hook(struct wctdm *wc, struct wctdm_module *const mod)
2354 {
2355 if (debug & DEBUG_CARD) {
2356 dev_info(&wc->vb.pdev->dev,
2357 "fxs_on_hook: Card %d Going on hook\n", mod->card);
2358 }
2359
2360 if ((mod->mod.fxs.lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
2361 wctdm_fxs_hooksig(wc, mod, DAHDI_TXSIG_ONHOOK);
2362 dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_ONHOOK);
2363 }
2364
2365 static const char *wctdm_echocan_name(const struct dahdi_chan *chan)
2366 {
2367 struct wctdm *wc = chan->pvt;
2368 if (wc->vpmadt032)
2369 return vpmadt032_name;
2370 else if (wc->vpmoct)
2371 return vpmoct_name;
2372
2373 return NULL;
2374 }
2375
2376 static int wctdm_echocan_create(struct dahdi_chan *chan,
2377 struct dahdi_echocanparams *ecp,
2378 struct dahdi_echocanparam *p,
2379 struct dahdi_echocan_state **ec)
2380 {
2381 struct wctdm *wc = chan->pvt;
2382 struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);
2383 const struct dahdi_echocan_ops *ops;
2384 const struct dahdi_echocan_features *features;
2385 enum adt_companding comp;
2386
2387
2388 #ifdef VPM_SUPPORT
2389 if (!vpmsupport)
2390 return -ENODEV;
2391 #endif
2392 if (wc->vpmadt032) {
2393 ops = &vpm_ec_ops;
2394 features = &vpm_ec_features;
2395
2396 *ec = &wchan->ec;
2397 (*ec)->ops = ops;
2398 (*ec)->features = *features;
2399
2400 comp = (DAHDI_LAW_ALAW == chan->span->deflaw) ?
2401 ADT_COMP_ALAW : ADT_COMP_ULAW;
2402
2403 return vpmadt032_echocan_create(wc->vpmadt032, wchan->timeslot,
2404 comp, ecp, p);
2405 } else if (wc->vpmoct) {
2406 ops = &vpm_ec_ops;
2407 features = &vpm_ec_features;
2408
2409 *ec = &wchan->ec;
2410 (*ec)->ops = ops;
2411 (*ec)->features = *features;
2412 return vpmoct_echocan_create(wc->vpmoct, wchan->timeslot,
2413 chan->span->deflaw);
2414 } else {
2415 return -ENODEV;
2416 }
2417 }
2418
2419 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
2420 {
2421 struct wctdm *wc = chan->pvt;
2422 struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);
2423
2424 if (wc->vpmadt032) {
2425 memset(ec, 0, sizeof(*ec));
2426 vpmadt032_echocan_free(wc->vpmadt032, wchan->timeslot, ec);
2427 } else if (wc->vpmoct) {
2428 memset(ec, 0, sizeof(*ec));
2429 vpmoct_echocan_free(wc->vpmoct, wchan->timeslot);
2430 }
2431 }
2432
2433 /* 1ms interrupt */
2434 static void wctdm_isr_misc_fxs(struct wctdm *wc, struct wctdm_module *const mod)
2435 {
2436 struct fxs *const fxs = &mod->mod.fxs;
2437
2438 if (time_after(wc->framecount, fxs->check_alarm)) {
2439 /* Accept an alarm once per 10 seconds */
2440 fxs->check_alarm = wc->framecount + (1000*10);
2441 if (fxs->palarms)
2442 fxs->palarms--;
2443 }
2444
2445 if (fxs->off_hook && !(fxs->hook_state_shadow & 1)) {
2446 wctdm_fxs_on_hook(wc, mod);
2447 fxs->off_hook = 0;
2448 } else if (!fxs->off_hook && (fxs->hook_state_shadow & 1)) {
2449 wctdm_fxs_off_hook(wc, mod);
2450 fxs->off_hook = 1;
2451 }
2452
2453 wctdm_proslic_check_oppending(wc, mod);
2454
2455 if (time_after(wc->framecount, fxs->check_proslic)) {
2456 fxs->check_proslic = wc->framecount + 250; /* every 250ms */
2457 wctdm_proslic_recheck_sanity(wc, mod);
2458 }
2459
2460 if (SLIC_LF_RINGING == fxs->lasttxhook) {
2461 /* RINGing, prepare for OHT */
2462 fxs->ohttimer = wc->framecount + OHT_TIMER;
2463 /* OHT mode when idle */
2464 fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_OHTRAN_REV :
2465 SLIC_LF_OHTRAN_FWD;
2466 } else if (fxs->oht_active) {
2467 /* check if still OnHook */
2468 if (!fxs->off_hook) {
2469 if (time_before(wc->framecount, fxs->ohttimer))
2470 return;
2471
2472 /* Switch to active */
2473 fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_ACTIVE_REV :
2474 SLIC_LF_ACTIVE_FWD;
2475 spin_lock(&wc->reglock);
2476 if (SLIC_LF_OHTRAN_FWD == fxs->lasttxhook) {
2477 /* Apply the change if appropriate */
2478 fxs->lasttxhook = SLIC_LF_OPPENDING | SLIC_LF_ACTIVE_FWD;
2479 /* Data enqueued here */
2480 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
2481 if (debug & DEBUG_CARD) {
2482 dev_info(&wc->vb.pdev->dev,
2483 "Channel %d OnHookTransfer "
2484 "stop\n", mod->card);
2485 }
2486 } else if (SLIC_LF_OHTRAN_REV == fxs->lasttxhook) {
2487 /* Apply the change if appropriate */
2488 fxs->lasttxhook = SLIC_LF_OPPENDING | SLIC_LF_ACTIVE_REV;
2489 /* Data enqueued here */
2490 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
2491 if (debug & DEBUG_CARD) {
2492 dev_info(&wc->vb.pdev->dev,
2493 "Channel %d OnHookTransfer "
2494 "stop\n", mod->card);
2495 }
2496 }
2497 spin_unlock(&wc->reglock);
2498 } else {
2499 fxs->oht_active = 0;
2500 /* Switch to active */
2501 fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
2502 if (debug & DEBUG_CARD) {
2503 dev_info(&wc->vb.pdev->dev,
2504 "Channel %d OnHookTransfer abort\n",
2505 mod->card);
2506 }
2507 }
2508
2509 }
2510 }
2511
2512 /* 1ms interrupt */
2513 static inline void wctdm_isr_misc(struct wctdm *wc)
2514 {
2515 int x;
2516
2517 if (unlikely(!is_initialized(wc)))
2518 return;
2519
2520 for (x = 0; x < wc->mods_per_board; x++) {
2521 struct wctdm_module *const mod = &wc->mods[x];
2522
2523 spin_lock(&wc->reglock);
2524 cmd_checkisr(wc, mod);
2525 spin_unlock(&wc->reglock);
2526
2527 switch (mod->type) {
2528 case FXS:
2529 wctdm_isr_misc_fxs(wc, mod);
2530 break;
2531 case FXO:
2532 wctdm_voicedaa_check_hook(wc, mod);
2533 break;
2534 case QRV:
2535 wctdm_qrvdri_check_hook(wc, x);
2536 break;
2537 default:
2538 break;
2539 }
2540 }
2541 }
2542
2543 static void handle_receive(struct voicebus *vb, struct list_head *buffers)
2544 {
2545 struct wctdm *wc = container_of(vb, struct wctdm, vb);
2546 struct vbb *vbb;
2547 list_for_each_entry(vbb, buffers, entry)
2548 wctdm_receiveprep(wc, vbb->data);
2549 }
2550
2551 static void handle_transmit(struct voicebus *vb, struct list_head *buffers)
2552 {
2553 struct wctdm *wc = container_of(vb, struct wctdm, vb);
2554 struct vbb *vbb;
2555
2556 list_for_each_entry(vbb, buffers, entry) {
2557 memset(vbb->data, 0, sizeof(vbb->data));
2558 wctdm_transmitprep(wc, vbb->data);
2559 wctdm_isr_misc(wc);
2560 wc->framecount++;
2561 }
2562 }
2563
2564 struct sframe_packet {
2565 struct list_head node;
2566 u8 sframe[SFRAME_SIZE];
2567 };
2568
2569 /**
2570 * handle_hx8_bootmode_receive() - queue up the receive packet for later...
2571 *
2572 * This function is called from interrupt context and isn't optimal, but it's
2573 * not the main code path.
2574 */
2575 static void handle_hx8_bootmode_receive(struct wctdm *wc, const void *vbb)
2576 {
2577 struct sframe_packet *frame;
2578
2579 frame = kzalloc(sizeof(*frame), GFP_ATOMIC);
2580 if (unlikely(!frame)) {
2581 WARN_ON(1);
2582 return;
2583 }
2584
2585 memcpy(frame->sframe, vbb, sizeof(frame->sframe));
2586 spin_lock(&wc->frame_list_lock);
2587 list_add_tail(&frame->node, &wc->frame_list);
2588 spin_unlock(&wc->frame_list_lock);
2589
2590 /* Wake up anyone waiting for a new packet. */
2591 wake_up(&wc->regq);
2592 return;
2593 }
2594
2595 static void handle_hx8_receive(struct voicebus *vb, struct list_head *buffers)
2596 {
2597 struct wctdm *wc = container_of(vb, struct wctdm, vb);
2598 struct vbb *vbb;
2599 list_for_each_entry(vbb, buffers, entry)
2600 handle_hx8_bootmode_receive(wc, vbb->data);
2601 }
2602
2603 static void handle_hx8_transmit(struct voicebus *vb, struct list_head *buffers)
2604 {
2605 struct vbb *vbb, *n;
2606
2607 list_for_each_entry_safe(vbb, n, buffers, entry) {
2608 list_del(&vbb->entry);
2609 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
2610 }
2611 }
2612
2613 static int wctdm_voicedaa_insane(struct wctdm *wc, struct wctdm_module *mod)
2614 {
2615 int blah;
2616 blah = wctdm_getreg(wc, mod, 2);
2617 if (blah != 0x3)
2618 return -2;
2619 blah = wctdm_getreg(wc, mod, 11);
2620 if (debug & DEBUG_CARD) {
2621 dev_info(&wc->vb.pdev->dev,
2622 "VoiceDAA System: %02x\n", blah & 0xf);
2623 }
2624 return 0;
2625 }
2626
2627 static int
2628 wctdm_proslic_insane(struct wctdm *wc, struct wctdm_module *const mod)
2629 {
2630 int blah, reg1, insane_report;
2631 insane_report=0;
2632
2633 blah = wctdm_getreg(wc, mod, 0);
2634 if (blah != 0xff && (debug & DEBUG_CARD)) {
2635 dev_info(&wc->vb.pdev->dev,
2636 "ProSLIC on module %d, product %d, "
2637 "version %d\n", mod->card, (blah & 0x30) >> 4,
2638 (blah & 0xf));
2639 }
2640
2641 #if 0
2642 if ((blah & 0x30) >> 4) {
2643 dev_info(&wc->vb.pdev->dev,
2644 "ProSLIC on module %d is not a 3210.\n", mod->card);
2645 return -1;
2646 }
2647 #endif
2648 if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
2649 /* SLIC not loaded */
2650 return -1;
2651 }
2652
2653 /* let's be really sure this is an FXS before we continue */
2654 reg1 = wctdm_getreg(wc, mod, 1);
2655 if ((0x80 != (blah & 0xf0)) || (0x88 != reg1)) {
2656 if (debug & DEBUG_CARD) {
2657 dev_info(&wc->vb.pdev->dev,
2658 "DEBUG: not FXS b/c reg0=%x or "
2659 "reg1 != 0x88 (%x).\n", blah, reg1);
2660 }
2661 return -1;
2662 }
2663
2664 blah = wctdm_getreg(wc, mod, 8);
2665 if (blah != 0x2) {
2666 dev_notice(&wc->vb.pdev->dev,
2667 "ProSLIC on module %d insane (1) %d should be 2\n",
2668 mod->card, blah);
2669 return -1;
2670 } else if (insane_report) {
2671 dev_notice(&wc->vb.pdev->dev,
2672 "ProSLIC on module %d Reg 8 Reads %d Expected "
2673 "is 0x2\n", mod->card, blah);
2674 }
2675
2676 blah = wctdm_getreg(wc, mod, 64);
2677 if (blah != 0x0) {
2678 dev_notice(&wc->vb.pdev->dev,
2679 "ProSLIC on module %d insane (2)\n",
2680 mod->card);
2681 return -1;
2682 } else if (insane_report) {
2683 dev_notice(&wc->vb.pdev->dev,
2684 "ProSLIC on module %d Reg 64 Reads %d Expected "
2685 "is 0x0\n", mod->card, blah);
2686 }
2687
2688 blah = wctdm_getreg(wc, mod, 11);
2689 if (blah != 0x33) {
2690 dev_notice(&wc->vb.pdev->dev,
2691 "ProSLIC on module %d insane (3)\n", mod->card);
2692 return -1;
2693 } else if (insane_report) {
2694 dev_notice(&wc->vb.pdev->dev,
2695 "ProSLIC on module %d Reg 11 Reads %d "
2696 "Expected is 0x33\n", mod->card, blah);
2697 }
2698
2699 /* Just be sure it's setup right. */
2700 wctdm_setreg(wc, mod, 30, 0);
2701
2702 if (debug & DEBUG_CARD) {
2703 dev_info(&wc->vb.pdev->dev,
2704 "ProSLIC on module %d seems sane.\n", mod->card);
2705 }
2706 return 0;
2707 }
2708
2709 static int
2710 wctdm_proslic_powerleak_test(struct wctdm *wc, struct wctdm_module *const mod)
2711 {
2712 unsigned long start;
2713 unsigned char vbat;
2714
2715 /* Turn off linefeed */
2716 wctdm_setreg(wc, mod, LINE_STATE, 0);
2717
2718 /* Power down */
2719 wctdm_setreg(wc, mod, 14, 0x10);
2720
2721 start = jiffies;
2722
2723 /* TODO: Why is this sleep necessary. Without it, the first read
2724 * comes back with a 0 value. */
2725 msleep(20);
2726
2727 while ((vbat = wctdm_getreg(wc, mod, 82)) > 0x6) {
2728 if (time_after(jiffies, start + HZ/4))
2729 break;
2730 }
2731
2732 if (vbat < 0x06) {
2733 dev_notice(&wc->vb.pdev->dev,
2734 "Excessive leakage detected on module %d: %d "
2735 "volts (%02x) after %d ms\n", mod->card,
2736 376 * vbat / 1000, vbat,
2737 (int)((jiffies - start) * 1000 / HZ));
2738 return -1;
2739 } else if (debug & DEBUG_CARD) {
2740 dev_info(&wc->vb.pdev->dev,
2741 "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
2742 }
2743 return 0;
2744 }
2745
2746 static int wctdm_powerup_proslic(struct wctdm *wc,
2747 struct wctdm_module *mod, int fast)
2748 {
2749 unsigned char vbat;
2750 unsigned long origjiffies;
2751 int lim;
2752
2753 /* Set period of DC-DC converter to 1/64 khz */
2754 wctdm_setreg(wc, mod, 92, 0xc0 /* was 0xff */);
2755
2756 /* Wait for VBat to powerup */
2757 origjiffies = jiffies;
2758
2759 /* Disable powerdown */
2760 wctdm_setreg(wc, mod, 14, 0);
2761
2762 /* If fast, don't bother checking anymore */
2763 if (fast)
2764 return 0;
2765
2766 while ((vbat = wctdm_getreg(wc, mod, 82)) < 0xc0) {
2767 /* Wait no more than 500ms */
2768 if ((jiffies - origjiffies) > HZ/2) {
2769 break;
2770 }
2771 }
2772
2773 if (vbat < 0xc0) {
2774 dev_notice(&wc->vb.pdev->dev, "ProSLIC on module %d failed to powerup within %d ms (%d mV only)\n\n -- DID YOU REMEMBER TO PLUG IN THE HD POWER CABLE TO THE TDM CARD??\n",
2775 mod->card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
2776 vbat * 375);
2777 return -1;
2778 } else if (debug & DEBUG_CARD) {
2779 dev_info(&wc->vb.pdev->dev,
2780 "ProSLIC on module %d powered up to -%d volts (%02x) "
2781 "in %d ms\n", mod->card, vbat * 376 / 1000, vbat,
2782 (int)(((jiffies - origjiffies) * 1000 / HZ)));
2783 }
2784
2785 /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
2786 /* If out of range, just set it to the default value */
2787 lim = (loopcurrent - 20) / 3;
2788 if ( loopcurrent > 41 ) {
2789 lim = 0;
2790 if (debug & DEBUG_CARD)
2791 dev_info(&wc->vb.pdev->dev, "Loop current out of range! Setting to default 20mA!\n");
2792 }
2793 else if (debug & DEBUG_CARD)
2794 dev_info(&wc->vb.pdev->dev, "Loop current set to %dmA!\n",(lim*3)+20);
2795 wctdm_setreg(wc, mod, LOOP_I_LIMIT, lim);
2796
2797 /* Engage DC-DC converter */
2798 wctdm_setreg(wc, mod, 93, 0x19 /* was 0x19 */);
2799 return 0;
2800
2801 }
2802
2803 static int
2804 wctdm_proslic_manual_calibrate(struct wctdm *wc, struct wctdm_module *const mod)
2805 {
2806 unsigned long origjiffies;
2807 unsigned char i;
2808
2809 /* Disable all interupts in DR21-23 */
2810 wctdm_setreg(wc, mod, 21, 0);
2811 wctdm_setreg(wc, mod, 22, 0);
2812 wctdm_setreg(wc, mod, 23, 0);
2813
2814 wctdm_setreg(wc, mod, 64, 0);
2815
2816 /* (0x18) Calibrations without the ADC and DAC offset and without
2817 * common mode calibration. */
2818 wctdm_setreg(wc, mod, 97, 0x18);
2819
2820 /* (0x47) Calibrate common mode and differential DAC mode DAC + ILIM */
2821 wctdm_setreg(wc, mod, 96, 0x47);
2822
2823 origjiffies=jiffies;
2824 while (wctdm_getreg(wc, mod, 96) != 0) {
2825 if ((jiffies-origjiffies) > 80)
2826 return -1;
2827 }
2828 //Initialized DR 98 and 99 to get consistant results.
2829 // 98 and 99 are the results registers and the search should have same intial conditions.
2830
2831 /*******************************The following is the manual gain mismatch calibration****************************/
2832 /*******************************This is also available as a function *******************************************/
2833 msleep(10);
2834 wctdm_proslic_setreg_indirect(wc, mod, 88, 0);
2835 wctdm_proslic_setreg_indirect(wc, mod, 89, 0);
2836 wctdm_proslic_setreg_indirect(wc, mod, 90, 0);
2837 wctdm_proslic_setreg_indirect(wc, mod, 91, 0);
2838 wctdm_proslic_setreg_indirect(wc, mod, 92, 0);
2839 wctdm_proslic_setreg_indirect(wc, mod, 93, 0);
2840
2841 /* This is necessary if the calibration occurs other than at reset */
2842 wctdm_setreg(wc, mod, 98, 0x10);
2843 wctdm_setreg(wc, mod, 99, 0x10);
2844
2845 for ( i=0x1f; i>0; i--)
2846 {
2847 wctdm_setreg(wc, mod, 98, i);
2848 msleep(40);
2849 if ((wctdm_getreg(wc, mod, 88)) == 0)
2850 break;
2851 } // for
2852
2853 for ( i=0x1f; i>0; i--)
2854 {
2855 wctdm_setreg(wc, mod, 99, i);
2856 msleep(40);
2857 if ((wctdm_getreg(wc, mod, 89)) == 0)
2858 break;
2859 }//for
2860
2861 /*******************************The preceding is the manual gain mismatch calibration****************************/
2862 /**********************************The following is the longitudinal Balance Cal***********************************/
2863 wctdm_setreg(wc, mod, 64, 1);
2864 msleep(100);
2865
2866 wctdm_setreg(wc, mod, 64, 0);
2867
2868 /* enable interrupt for the balance Cal */
2869 wctdm_setreg(wc, mod, 23, 0x4);
2870
2871 /* this is a singular calibration bit for longitudinal calibration */
2872 wctdm_setreg(wc, mod, 97, 0x1);
2873 wctdm_setreg(wc, mod, 96, 0x40);
2874
2875 wctdm_getreg(wc, mod, 96); /* Read Reg 96 just cause */
2876
2877 wctdm_setreg(wc, mod, 21, 0xFF);
2878 wctdm_setreg(wc, mod, 22, 0xFF);
2879 wctdm_setreg(wc, mod, 23, 0xFF);
2880
2881 /**The preceding is the longitudinal Balance Cal***/
2882 return(0);
2883
2884 }
2885
2886 static int
2887 wctdm_proslic_calibrate(struct wctdm *wc, struct wctdm_module *mod)
2888 {
2889 unsigned long origjiffies;
2890 int x;
2891
2892 /* Perform all calibrations */
2893 wctdm_setreg(wc, mod, 97, 0x1f);
2894
2895 /* Begin, no speedup */
2896 wctdm_setreg(wc, mod, 96, 0x5f);
2897
2898 /* Wait for it to finish */
2899 origjiffies = jiffies;
2900 while (wctdm_getreg(wc, mod, 96)) {
2901 if (time_after(jiffies, (origjiffies + (2*HZ)))) {
2902 dev_notice(&wc->vb.pdev->dev,
2903 "Timeout waiting for calibration of "
2904 "module %d\n", mod->card);
2905 return -1;
2906 }
2907 }
2908
2909 if (debug & DEBUG_CARD) {
2910 /* Print calibration parameters */
2911 dev_info(&wc->vb.pdev->dev,
2912 "Calibration Vector Regs 98 - 107:\n");
2913 for (x=98;x<108;x++) {
2914 dev_info(&wc->vb.pdev->dev,
2915 "%d: %02x\n", x, wctdm_getreg(wc, mod, x));
2916 }
2917 }
2918 return 0;
2919 }
2920
2921 /*********************************************************************
2922 * Set the hwgain on the analog modules
2923 *
2924 * card = the card position for this module (0-23)
2925 * gain = gain in dB x10 (e.g. -3.5dB would be gain=-35)
2926 * tx = (0 for rx; 1 for tx)
2927 *
2928 *******************************************************************/
2929 static int
2930 wctdm_set_hwgain(struct wctdm *wc, struct wctdm_module *mod,
2931 __s32 gain, __u32 tx)
2932 {
2933 if (mod->type != FXO) {
2934 dev_notice(&wc->vb.pdev->dev,
2935 "Cannot adjust gain. Unsupported module type!\n");
2936 return -1;
2937 }
2938
2939 if (tx) {
2940 if (debug) {
2941 dev_info(&wc->vb.pdev->dev,
2942 "setting FXO tx gain for card=%d to %d\n",
2943 mod->card, gain);
2944 }
2945 if (gain >= -150 && gain <= 0) {
2946 wctdm_setreg(wc, mod, 38, 16 + (gain / -10));
2947 wctdm_setreg(wc, mod, 40, 16 + (-gain % 10));
2948 } else if (gain <= 120 && gain > 0) {
2949 wctdm_setreg(wc, mod, 38, gain/10);
2950 wctdm_setreg(wc, mod, 40, (gain%10));
2951 } else {
2952 dev_notice(&wc->vb.pdev->dev,
2953 "FXO tx gain is out of range (%d)\n", gain);
2954 return -1;
2955 }
2956 } else { /* rx */
2957 if (debug) {
2958 dev_info(&wc->vb.pdev->dev,
2959 "setting FXO rx gain for card=%d to %d\n",
2960 mod->card, gain);
2961 }
2962 if (gain >= -150 && gain <= 0) {
2963 wctdm_setreg(wc, mod, 39, 16 + (gain / -10));
2964 wctdm_setreg(wc, mod, 41, 16 + (-gain % 10));
2965 } else if (gain <= 120 && gain > 0) {
2966 wctdm_setreg(wc, mod, 39, gain/10);
2967 wctdm_setreg(wc, mod, 41, (gain%10));
2968 } else {
2969 dev_notice(&wc->vb.pdev->dev,
2970 "FXO rx gain is out of range (%d)\n", gain);
2971 return -1;
2972 }
2973 }
2974
2975 return 0;
2976 }
2977
2978 static int set_lasttxhook_interruptible(struct wctdm *wc, struct fxs *fxs,
2979 unsigned newval, int *psethook)
2980 {
2981 int res = 0;
2982 unsigned long flags;
2983 int timeout = 0;
2984
2985 do {
2986 spin_lock_irqsave(&wc->reglock, flags);
2987 if (SLIC_LF_OPPENDING & fxs->lasttxhook) {
2988 spin_unlock_irqrestore(&wc->reglock, flags);
2989 if (timeout++ > 100)
2990 return -1;
2991 msleep(1);
2992 } else {
2993 fxs->lasttxhook = (newval & SLIC_LF_SETMASK) | SLIC_LF_OPPENDING;
2994 *psethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
2995 spin_unlock_irqrestore(&wc->reglock, flags);
2996 break;
2997 }
2998 } while (1);
2999
3000 return res;
3001 }
3002
3003 /* Must be called from within an interruptible context */
3004 static int set_vmwi(struct wctdm *wc, struct wctdm_module *const mod)
3005 {
3006 int x;
3007 struct fxs *const fxs = &mod->mod.fxs;
3008
3009 /* Presently only supports line reversal MWI */
3010 if ((fxs->vmwi_active_messages) &&
3011 (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_LREV))
3012 fxs->vmwi_linereverse = 1;
3013 else
3014 fxs->vmwi_linereverse = 0;
3015
3016 /* Set line polarity for new VMWI state */
3017 if (POLARITY_XOR(fxs)) {
3018 fxs->idletxhookstate |= SLIC_LF_REVMASK;
3019 /* Do not set while currently ringing or open */
3020 if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
3021 ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
3022 x = fxs->lasttxhook;
3023 x |= SLIC_LF_REVMASK;
3024 set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
3025 }
3026 } else {
3027 fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
3028 /* Do not set while currently ringing or open */
3029 if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
3030 ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
3031 x = fxs->lasttxhook;
3032 x &= ~SLIC_LF_REVMASK;
3033 set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
3034 }
3035 }
3036 if (debug) {
3037 dev_info(&wc->vb.pdev->dev,
3038 "Setting VMWI on channel %d, messages=%d, lrev=%d\n",
3039 mod->card, fxs->vmwi_active_messages,
3040 fxs->vmwi_linereverse);
3041 }
3042 return 0;
3043 }
3044
3045 static void
3046 wctdm_voicedaa_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
3047 {
3048 wctdm_setreg(wc, mod, 34, (ts * 8) & 0xff);
3049 wctdm_setreg(wc, mod, 35, (ts * 8) >> 8);
3050 wctdm_setreg(wc, mod, 36, (ts * 8) & 0xff);
3051 wctdm_setreg(wc, mod, 37, (ts * 8) >> 8);
3052
3053 if (debug) {
3054 dev_info(&wc->vb.pdev->dev,
3055 "voicedaa: card %d new timeslot: %d\n",
3056 mod->card + 1, ts);
3057 }
3058 }
3059
3060 static int
3061 wctdm_init_voicedaa(struct wctdm *wc, struct wctdm_module *mod,
3062 int fast, int manual, int sane)
3063 {
3064 unsigned char reg16=0, reg26=0, reg30=0, reg31=0;
3065 unsigned long flags;
3066 long newjiffies;
3067
3068 #if 0 /* TODO */
3069 if ((wc->mods[card & 0xfc].type == QRV) ||
3070 (wc->mods[card & 0xfc].type == BRI))
3071 return -2;
3072 #endif
3073
3074 spin_lock_irqsave(&wc->reglock, flags);
3075 mod->type = NONE;
3076 spin_unlock_irqrestore(&wc->reglock, flags);
3077 msleep(20);
3078
3079 spin_lock_irqsave(&wc->reglock, flags);
3080 mod->type = FXO;
3081 spin_unlock_irqrestore(&wc->reglock, flags);
3082 msleep(20);
3083
3084 memset(&mod->mod.fxo, 0, sizeof(mod->mod.fxo));
3085
3086 if (!sane && wctdm_voicedaa_insane(wc, mod))
3087 return -2;
3088
3089 /* Software reset */
3090 wctdm_setreg(wc, mod, 1, 0x80);
3091 msleep(100);
3092
3093 /* Set On-hook speed, Ringer impedence, and ringer threshold */
3094 reg16 |= (fxo_modes[_opermode].ohs << 6);
3095 reg16 |= (fxo_modes[_opermode].rz << 1);
3096 reg16 |= (fxo_modes[_opermode].rt);
3097 wctdm_setreg(wc, mod, 16, reg16);
3098
3099 /* Enable ring detector full-wave rectifier mode */
3100 wctdm_setreg(wc, mod, 18, 2);
3101 wctdm_setreg(wc, mod, 24, 0);
3102
3103 /* Set DC Termination:
3104 Tip/Ring voltage adjust, minimum operational current, current limitation */
3105 reg26 |= (fxo_modes[_opermode].dcv << 6);
3106 reg26 |= (fxo_modes[_opermode].mini << 4);
3107 reg26 |= (fxo_modes[_opermode].ilim << 1);
3108 wctdm_setreg(wc, mod, 26, reg26);
3109
3110 /* Set AC Impedence */
3111 reg30 = (fxo_modes[_opermode].acim);
3112 wctdm_setreg(wc, mod, 30, reg30);
3113
3114 /* Misc. DAA parameters */
3115
3116 /* If fast pickup is set, then the off hook counter will be set to 8
3117 * ms, otherwise 128 ms. */
3118 reg31 = (fastpickup) ? 0xe3 : 0xa3;
3119
3120 reg31 |= (fxo_modes[_opermode].ohs2 << 3);
3121 wctdm_setreg(wc, mod, 31, reg31);
3122
3123 wctdm_voicedaa_set_ts(wc, mod, mod->card);
3124
3125 /* Enable ISO-Cap */
3126 wctdm_setreg(wc, mod, 6, 0x00);
3127
3128 /* Turn off the calibration delay when fastpickup is enabled. */
3129 if (fastpickup)
3130 wctdm_setreg(wc, mod, 17, wctdm_getreg(wc, mod, 17) | 0x20);
3131
3132 /* Wait 1000ms for ISO-cap to come up */
3133 newjiffies = jiffies;
3134 newjiffies += 2 * HZ;
3135
3136 while ((jiffies < newjiffies) && !(wctdm_getreg(wc, mod, 11) & 0xf0))
3137 msleep(100);
3138
3139 if (!(wctdm_getreg(wc, mod, 11) & 0xf0)) {
3140 dev_notice(&wc->vb.pdev->dev, "VoiceDAA did not bring up ISO link properly!\n");
3141 return -1;
3142 }
3143
3144 if (debug & DEBUG_CARD) {
3145 dev_info(&wc->vb.pdev->dev, "ISO-Cap is now up, line side: %02x rev %02x\n",
3146 wctdm_getreg(wc, mod, 11) >> 4,
3147 (wctdm_getreg(wc, mod, 13) >> 2) & 0xf);
3148 }
3149
3150 /* Enable on-hook line monitor */
3151 wctdm_setreg(wc, mod, 5, 0x08);
3152
3153 /* Take values for fxotxgain and fxorxgain and apply them to module */
3154 wctdm_set_hwgain(wc, mod, fxotxgain, 1);
3155 wctdm_set_hwgain(wc, mod, fxorxgain, 0);
3156
3157 #ifdef DEBUG
3158 if (digitalloopback) {
3159 dev_info(&wc->vb.pdev->dev,
3160 "Turning on digital loopback for port %d.\n",
3161 mod->card + 1);
3162 wctdm_setreg(wc, mod, 10, 0x01);
3163 }
3164 #endif
3165
3166 if (debug) {
3167 dev_info(&wc->vb.pdev->dev,
3168 "DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n",
3169 (wctdm_getreg(wc, mod, 38)/16) ?
3170 -(wctdm_getreg(wc, mod, 38) - 16) :
3171 wctdm_getreg(wc, mod, 38),
3172 (wctdm_getreg(wc, mod, 40)/16) ?
3173 -(wctdm_getreg(wc, mod, 40) - 16) :
3174 wctdm_getreg(wc, mod, 40),
3175 (wctdm_getreg(wc, mod, 39)/16) ?
3176 -(wctdm_getreg(wc, mod, 39) - 16) :
3177 wctdm_getreg(wc, mod, 39),
3178 (wctdm_getreg(wc, mod, 41)/16) ?
3179 -(wctdm_getreg(wc, mod, 41) - 16) :
3180 wctdm_getreg(wc, mod, 41));
3181 }
3182
3183 return 0;
3184 }
3185
3186 static void
3187 wctdm_proslic_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
3188 {
3189 wctdm_setreg(wc, mod, 2, (ts * 8) & 0xff); /* Tx Start low byte 0 */
3190 wctdm_setreg(wc, mod, 3, (ts * 8) >> 8); /* Tx Start high byte 0 */
3191 wctdm_setreg(wc, mod, 4, (ts * 8) & 0xff); /* Rx Start low byte 0 */
3192 wctdm_setreg(wc, mod, 5, (ts * 8) >> 8); /* Rx Start high byte 0 */
3193
3194 if (debug) {
3195 dev_info(&wc->vb.pdev->dev,
3196 "proslic: card %d new timeslot: %d\n",
3197 mod->card + 1, ts);
3198 }
3199 }
3200
3201 static int
3202 wctdm_init_proslic(struct wctdm *wc, struct wctdm_module *const mod,
3203 int fast, int manual, int sane)
3204 {
3205
3206 struct fxs *const fxs = &mod->mod.fxs;
3207 unsigned short tmp[5];
3208 unsigned long flags;
3209 unsigned char r19,r9;
3210 int x;
3211 int fxsmode=0;
3212 int addresses[NUM_CAL_REGS];
3213
3214 #if 0 /* TODO */
3215 if (wc->mods[mod->card & 0xfc].type == QRV)
3216 return -2;
3217 #endif
3218
3219 spin_lock_irqsave(&wc->reglock, flags);
3220 mod->type = FXS;
3221 spin_unlock_irqrestore(&wc->reglock, flags);
3222
3223 /* msleep(100); */
3224
3225 /* Sanity check the ProSLIC */
3226 if (!sane && wctdm_proslic_insane(wc, mod))
3227 return -2;
3228
3229 /* Initialize VMWI settings */
3230 memset(fxs, 0, sizeof(*fxs));
3231
3232 /* By default, don't send on hook */
3233 if (!reversepolarity != !fxs->reversepolarity)
3234 fxs->idletxhookstate = SLIC_LF_ACTIVE_REV;
3235 else
3236 fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;
3237
3238 if (sane) {
3239 /* Make sure we turn off the DC->DC converter to prevent anything from blowing up */
3240 wctdm_setreg(wc, mod, 14, 0x10);
3241 }
3242
3243 if (wctdm_proslic_init_indirect_regs(wc, mod)) {
3244 dev_info(&wc->vb.pdev->dev,
3245 "Indirect Registers failed to initialize on "
3246 "module %d.\n", mod->card);
3247 return -1;
3248 }
3249
3250 /* Clear scratch pad area */
3251 wctdm_proslic_setreg_indirect(wc, mod, 97, 0);
3252
3253 /* Clear digital loopback */
3254 wctdm_setreg(wc, mod, 8, 0);
3255
3256 /* Revision C optimization */
3257 wctdm_setreg(wc, mod, 108, 0xeb);
3258
3259 /* Disable automatic VBat switching for safety to prevent
3260 * Q7 from accidently turning on and burning out.
3261 * If pulse dialing has trouble at high REN loads change this to 0x17 */
3262 wctdm_setreg(wc, mod, 67, 0x07);
3263
3264 /* Turn off Q7 */
3265 wctdm_setreg(wc, mod, 66, 1);
3266
3267 /* Flush ProSLIC digital filters by setting to clear, while
3268 saving old values */
3269 for (x=0;x<5;x++) {
3270 tmp[x] = wctdm_proslic_getreg_indirect(wc, mod, x + 35);
3271 wctdm_proslic_setreg_indirect(wc, mod, x + 35, 0x8000);
3272 }
3273
3274 /* Power up the DC-DC converter */
3275 if (wctdm_powerup_proslic(wc, mod, fast)) {
3276 dev_notice(&wc->vb.pdev->dev,
3277 "Unable to do INITIAL ProSLIC powerup on "
3278 "module %d\n", mod->card);
3279 return -1;
3280 }
3281
3282 if (!fast) {
3283 /* Check for power leaks */
3284 if (wctdm_proslic_powerleak_test(wc, mod)) {
3285 dev_notice(&wc->vb.pdev->dev,
3286 "ProSLIC module %d failed leakage test. "
3287 "Check for short circuit\n", mod->card);
3288 }
3289 /* Power up again */
3290 if (wctdm_powerup_proslic(wc, mod, fast)) {
3291 dev_notice(&wc->vb.pdev->dev,
3292 "Unable to do FINAL ProSLIC powerup on "
3293 "module %d\n", mod->card);
3294 return -1;
3295 }
3296 #ifndef NO_CALIBRATION
3297 /* Perform calibration */
3298 if (manual) {
3299 if (wctdm_proslic_manual_calibrate(wc, mod)) {
3300 //dev_notice(&wc->vb.pdev->dev, "Proslic failed on Manual Calibration\n");
3301 if (wctdm_proslic_manual_calibrate(wc, mod)) {
3302 dev_notice(&wc->vb.pdev->dev, "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
3303 return -1;
3304 }
3305 dev_info(&wc->vb.pdev->dev, "Proslic Passed Manual Calibration on Second Attempt\n");
3306 }
3307 }
3308 else {
3309 if (wctdm_proslic_calibrate(wc, mod)) {
3310 //dev_notice(&wc->vb.pdev->dev, "ProSlic died on Auto Calibration.\n");
3311 if (wctdm_proslic_calibrate(wc, mod)) {
3312 dev_notice(&wc->vb.pdev->dev, "Proslic Failed on Second Attempt to Auto Calibrate\n");
3313 return -1;
3314 }
3315 dev_info(&wc->vb.pdev->dev, "Proslic Passed Auto Calibration on Second Attempt\n");
3316 }
3317 }
3318 /* Perform DC-DC calibration */
3319 wctdm_setreg(wc, mod, 93, 0x99);
3320 r19 = wctdm_getreg(wc, mod, 107);
3321 if ((r19 < 0x2) || (r19 > 0xd)) {
3322 dev_notice(&wc->vb.pdev->dev, "DC-DC cal has a surprising direct 107 of 0x%02x!\n", r19);
3323 wctdm_setreg(wc, mod, 107, 0x8);
3324 }
3325
3326 /* Save calibration vectors */
3327 for (x = 0; x < NUM_CAL_REGS; x++)
3328 addresses[x] = 96 + x;
3329 wctdm_getregs(wc, mod, addresses, ARRAY_SIZE(addresses));
3330 for (x = 0; x < NUM_CAL_REGS; x++)
3331 fxs->calregs.vals[x] = addresses[x];
3332 #endif
3333
3334 } else {
3335 /* Restore calibration registers */
3336 for (x = 0; x < NUM_CAL_REGS; x++)
3337 wctdm_setreg(wc, mod, 96 + x, fxs->calregs.vals[x]);
3338 }
3339 /* Calibration complete, restore original values */
3340 for (x=0;x<5;x++) {
3341 wctdm_proslic_setreg_indirect(wc, mod, x + 35, tmp[x]);
3342 }
3343
3344 if (wctdm_proslic_verify_indirect_regs(wc, mod)) {
3345 dev_info(&wc->vb.pdev->dev, "Indirect Registers failed verification.\n");
3346 return -1;
3347 }
3348
3349
3350 #if 0
3351 /* Disable Auto Power Alarm Detect and other "features" */
3352 wctdm_setreg(wc, card, 67, 0x0e);
3353 blah = wctdm_getreg(wc, card, 67);
3354 #endif
3355
3356 #if 0
3357 if (wctdm_proslic_setreg_indirect(wc, card, 97, 0x0)) { // Stanley: for the bad recording fix
3358 dev_info(&wc->vb.pdev->dev, "ProSlic IndirectReg Died.\n");
3359 return -1;
3360 }
3361 #endif
3362
3363 /* U-Law 8-bit interface */
3364 wctdm_proslic_set_ts(wc, mod, mod->card);
3365
3366 wctdm_setreg(wc, mod, 18, 0xff); /* clear all interrupt */
3367 wctdm_setreg(wc, mod, 19, 0xff);
3368 wctdm_setreg(wc, mod, 20, 0xff);
3369 wctdm_setreg(wc, mod, 22, 0xff);
3370 wctdm_setreg(wc, mod, 73, 0x04);
3371
3372 wctdm_setreg(wc, mod, 69, 0x4);
3373
3374 if (fxshonormode) {
3375 static const int ACIM2TISS[16] = { 0x0, 0x1, 0x4, 0x5, 0x7,
3376 0x0, 0x0, 0x6, 0x0, 0x0,
3377 0x0, 0x2, 0x0, 0x3 };
3378 fxsmode = ACIM2TISS[fxo_modes[_opermode].acim];
3379 wctdm_setreg(wc, mod, 10, 0x08 | fxsmode);
3380 if (fxo_modes[_opermode].ring_osc) {
3381 wctdm_proslic_setreg_indirect(wc, mod, 20,
3382 fxo_modes[_opermode].ring_osc);
3383 }
3384 if (fxo_modes[_opermode].ring_x) {
3385 wctdm_proslic_setreg_indirect(wc, mod, 21,
3386 fxo_modes[_opermode].ring_x);
3387 }
3388 }
3389 if (lowpower)
3390 wctdm_setreg(wc, mod, 72, 0x10);
3391
3392 #if 0
3393 wctdm_setreg(wc, card, 21, 0x00); // enable interrupt
3394 wctdm_setreg(wc, card, 22, 0x02); // Loop detection interrupt
3395 wctdm_setreg(wc, card, 23, 0x01); // DTMF detection interrupt
3396 #endif
3397
3398 #if 0
3399 /* Enable loopback */
3400 wctdm_setreg(wc, card, 8, 0x2);
3401 wctdm_setreg(wc, card, 14, 0x0);
3402 wctdm_setreg(wc, card, 64, 0x0);
3403 wctdm_setreg(wc, card, 1, 0x08);
3404 #endif
3405
3406 if (fastringer) {
3407 /* Speed up Ringer */
3408 wctdm_proslic_setreg_indirect(wc, mod, 20, 0x7e6d);
3409 wctdm_proslic_setreg_indirect(wc, mod, 21, 0x01b9);
3410 /* Beef up Ringing voltage to 89V */
3411 if (boostringer) {
3412 wctdm_setreg(wc, mod, 74, 0x3f);
3413 if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x247))
3414 return -1;
3415 dev_info(&wc->vb.pdev->dev,
3416 "Boosting fast ringer on slot %d (89V peak)\n",
3417 mod->card + 1);
3418 } else if (lowpower) {
3419 if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x14b))
3420 return -1;
3421 dev_info(&wc->vb.pdev->dev,
3422 "Reducing fast ring power on slot %d "
3423 "(50V peak)\n", mod->card + 1);
3424 } else
3425 dev_info(&wc->vb.pdev->dev,
3426 "Speeding up ringer on slot %d (25Hz)\n",
3427 mod->card + 1);
3428 } else {
3429 /* Beef up Ringing voltage to 89V */
3430 if (boostringer) {
3431 wctdm_setreg(wc, mod, 74, 0x3f);
3432 if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x1d1))
3433 return -1;
3434 dev_info(&wc->vb.pdev->dev,
3435 "Boosting ringer on slot %d (89V peak)\n",
3436 mod->card + 1);
3437 } else if (lowpower) {
3438 if (wctdm_proslic_setreg_indirect(wc, mod, 21, 0x108))
3439 return -1;
3440 dev_info(&wc->vb.pdev->dev,
3441 "Reducing ring power on slot %d "
3442 "(50V peak)\n", mod->card + 1);
3443 }
3444 }
3445
3446 if (fxstxgain || fxsrxgain) {
3447 r9 = wctdm_getreg(wc, mod, 9);
3448 switch (fxstxgain) {
3449
3450 case 35:
3451 r9+=8;
3452 break;
3453 case -35:
3454 r9+=4;
3455 break;
3456 case 0:
3457 break;
3458 }
3459
3460 switch (fxsrxgain) {
3461
3462 case 35:
3463 r9+=2;
3464 break;
3465 case -35:
3466 r9+=1;
3467 break;
3468 case 0:
3469 break;
3470 }
3471 wctdm_setreg(wc, mod, 9, r9);
3472 }
3473
3474 if (debug) {
3475 dev_info(&wc->vb.pdev->dev,
3476 "DEBUG: fxstxgain:%s fxsrxgain:%s\n",
3477 ((wctdm_getreg(wc, mod, 9) / 8) == 1) ?
3478 "3.5" : (((wctdm_getreg(wc, mod, 9) / 4) == 1) ?
3479 "-3.5" : "0.0"),
3480 ((wctdm_getreg(wc, mod, 9) / 2) == 1) ?
3481 "3.5" : ((wctdm_getreg(wc, mod, 9) % 2) ?
3482 "-3.5" : "0.0"));
3483 }
3484
3485 fxs->lasttxhook = fxs->idletxhookstate;
3486 wctdm_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);
3487
3488 /* Preset the shadow register so that we won't get a power alarm when
3489 * we finish initialization, otherwise the line state register may not
3490 * have been read yet. */
3491 fxs->linefeed_control_shadow = fxs->lasttxhook;
3492 return 0;
3493 }
3494
3495 static void
3496 wctdm_qrvdri_set_ts(struct wctdm *wc, struct wctdm_module *mod, int ts)
3497 {
3498 wctdm_setreg(wc, mod, 0x13, ts + 0x80); /* codec 2 tx, ts0 */
3499 wctdm_setreg(wc, mod, 0x17, ts + 0x80); /* codec 0 rx, ts0 */
3500 wctdm_setreg(wc, mod, 0x14, ts + 0x81); /* codec 1 tx, ts1 */
3501 wctdm_setreg(wc, mod, 0x18, ts + 0x81); /* codec 1 rx, ts1 */
3502
3503 if (debug) {
3504 dev_info(&wc->vb.pdev->dev,
3505 "qrvdri: card %d new timeslot: %d\n",
3506 mod->card + 1, ts);
3507 }
3508 }
3509
3510 static int wctdm_init_qrvdri(struct wctdm *wc, int card)
3511 {
3512 struct wctdm_module *const mod = &wc->mods[card];
3513 unsigned char x,y;
3514
3515 if (BRI == wc->mods[card & 0xfc].type)
3516 return -2;
3517
3518 /* have to set this, at least for now */
3519 mod->type = QRV;
3520 if (!(card & 3)) { /* if at base of card, reset and write it */
3521 struct qrv *const qrv = &mod->mod.qrv;
3522 struct qrv *const qrv1 = &wc->mods[card + 1].mod.qrv;
3523 struct qrv *const qrv2 = &wc->mods[card + 2].mod.qrv;
3524 struct qrv *const qrv3 = &wc->mods[card + 3].mod.qrv;
3525
3526 wctdm_setreg(wc, mod, 0, 0x80);
3527 wctdm_setreg(wc, mod, 0, 0x55);
3528 wctdm_setreg(wc, mod, 1, 0x69);
3529 qrv->hook = qrv1->hook = 0;
3530 qrv2->hook = qrv3->hook = 0xff;
3531 qrv->debouncetime = qrv1->debouncetime = QRV_DEBOUNCETIME;
3532 qrv->debtime = qrv1->debtime = 0;
3533 qrv->radmode = qrv1->radmode = 0;
3534 qrv->txgain = qrv1->txgain = 3599;
3535 qrv->rxgain = qrv1->rxgain = 1199;
3536 } else { /* channel is on same card as base, no need to test */
3537 if (wc->mods[card & 0x7c].type == QRV) {
3538 /* only lower 2 are valid */
3539 if (!(card & 2))
3540 return 0;
3541 }
3542 mod->type = NONE;
3543 return 1;
3544 }
3545 x = wctdm_getreg(wc, mod, 0);
3546 y = wctdm_getreg(wc, mod, 1);
3547 /* if not a QRV card, return as such */
3548 if ((x != 0x55) || (y != 0x69))
3549 {
3550 mod->type = NONE;
3551 return 1;
3552 }
3553 for (x = 0; x < 0x30; x++) {
3554 if ((x >= 0x1c) && (x <= 0x1e))
3555 wctdm_setreg(wc, mod, x, 0xff);
3556 else
3557 wctdm_setreg(wc, mod, x, 0);
3558 }
3559 wctdm_setreg(wc, mod, 0, 0x80);
3560 msleep(100);
3561 wctdm_setreg(wc, mod, 0, 0x10);
3562 wctdm_setreg(wc, mod, 0, 0x10);
3563 msleep(100);
3564 /* set up modes */
3565 wctdm_setreg(wc, mod, 0, 0x1c);
3566 /* set up I/O directions */
3567 wctdm_setreg(wc, mod, 1, 0x33);
3568 wctdm_setreg(wc, mod, 2, 0x0f);
3569 wctdm_setreg(wc, mod, 5, 0x0f);
3570 /* set up I/O to quiescent state */
3571 wctdm_setreg(wc, mod, 3, 0x11); /* D0-7 */
3572 wctdm_setreg(wc, mod, 4, 0xa); /* D8-11 */
3573 wctdm_setreg(wc, mod, 7, 0); /* CS outputs */
3574
3575 wctdm_qrvdri_set_ts(wc, mod, card);
3576
3577 /* set up for max gains */
3578 wctdm_setreg(wc, mod, 0x26, 0x24);
3579 wctdm_setreg(wc, mod, 0x27, 0x24);
3580 wctdm_setreg(wc, mod, 0x0b, 0x01); /* "Transmit" gain codec 0 */
3581 wctdm_setreg(wc, mod, 0x0c, 0x01); /* "Transmit" gain codec 1 */
3582 wctdm_setreg(wc, mod, 0x0f, 0xff); /* "Receive" gain codec 0 */
3583 wctdm_setreg(wc, mod, 0x10, 0xff); /* "Receive" gain codec 1 */
3584 return 0;
3585 }
3586
3587 static void qrv_dosetup(struct dahdi_chan *chan, struct wctdm *wc)
3588 {
3589 struct wctdm_module *qrvmod;
3590 struct wctdm_module *nextqrvmod;
3591 int qrvcard;
3592 unsigned char r;
3593 long l;
3594
3595 /* actually do something with the values */
3596 qrvcard = (chan->chanpos - 1) & 0xfc;
3597 qrvmod = &wc->mods[qrvcard];
3598 nextqrvmod = &wc->mods[qrvcard + 1];
3599 if (debug) {
3600 dev_info(&wc->vb.pdev->dev,
3601 "@@@@@ radmodes: %d,%d rxgains: %d,%d "
3602 "txgains: %d,%d\n", wc->mods[qrvcard].mod.qrv.radmode,
3603 nextqrvmod->mod.qrv.radmode,
3604 wc->mods[qrvcard].mod.qrv.rxgain,
3605 nextqrvmod->mod.qrv.rxgain,
3606 wc->mods[qrvcard].mod.qrv.txgain,
3607 nextqrvmod->mod.qrv.txgain);
3608 }
3609 r = 0;
3610 if (qrvmod->mod.qrv.radmode & RADMODE_DEEMP)
3611 r |= 4;
3612 if (nextqrvmod->mod.qrv.radmode & RADMODE_DEEMP)
3613 r |= 8;
3614 if (qrvmod->mod.qrv.rxgain < 1200)
3615 r |= 1;
3616 if (nextqrvmod->mod.qrv.rxgain < 1200)
3617 r |= 2;
3618 wctdm_setreg(wc, qrvmod, 7, r);
3619 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 7 to %02x hex\n",r);
3620 r = 0;
3621 if (qrvmod->mod.qrv.radmode & RADMODE_PREEMP)
3622 r |= 3;
3623 else if (qrvmod->mod.qrv.txgain >= 3600)
3624 r |= 1;
3625 else if (qrvmod->mod.qrv.txgain >= 1200)
3626 r |= 2;
3627 if (nextqrvmod->mod.qrv.radmode & RADMODE_PREEMP)
3628 r |= 0xc;
3629 else if (nextqrvmod->mod.qrv.txgain >= 3600)
3630 r |= 4;
3631 else if (nextqrvmod->mod.qrv.txgain >= 1200)
3632 r |= 8;
3633 wctdm_setreg(wc, qrvmod, 4, r);
3634 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 4 to %02x hex\n",r);
3635 r = 0;
3636 if (qrvmod->mod.qrv.rxgain >= 2400)
3637 r |= 1;
3638 if (nextqrvmod->mod.qrv.rxgain >= 2400)
3639 r |= 2;
3640 wctdm_setreg(wc, qrvmod, 0x25, r);
3641 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x25 to %02x hex\n",r);
3642 r = 0;
3643 if (qrvmod->mod.qrv.txgain < 2400)
3644 r |= 1;
3645 else
3646 r |= 4;
3647 if (nextqrvmod->mod.qrv.txgain < 2400)
3648 r |= 8;
3649 else
3650 r |= 0x20;
3651 wctdm_setreg(wc, qrvmod, 0x26, r);
3652 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x26 to %02x hex\n",r);
3653 l = ((long)(qrvmod->mod.qrv.rxgain % 1200) * 10000) / 46875;
3654 if (l == 0) l = 1;
3655 if (qrvmod->mod.qrv.rxgain >= 2400)
3656 l += 181;
3657 wctdm_setreg(wc, qrvmod, 0x0b, (unsigned char)l);
3658 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0b to %02x hex\n",(unsigned char)l);
3659 l = ((long)(nextqrvmod->mod.qrv.rxgain % 1200) * 10000) / 46875;
3660 if (l == 0) l = 1;
3661 if (nextqrvmod->mod.qrv.rxgain >= 2400)
3662 l += 181;
3663 wctdm_setreg(wc, qrvmod, 0x0c, (unsigned char)l);
3664 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0c to %02x hex\n",(unsigned char)l);
3665 l = ((long)(qrvmod->mod.qrv.txgain % 1200) * 10000) / 46875;
3666 if (l == 0) l = 1;
3667 wctdm_setreg(wc, qrvmod, 0x0f, (unsigned char)l);
3668 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x0f to %02x hex\n", (unsigned char)l);
3669 l = ((long)(nextqrvmod->mod.qrv.txgain % 1200) * 10000) / 46875;
3670 if (l == 0) l = 1;
3671 wctdm_setreg(wc, qrvmod, 0x10, (unsigned char)l);
3672 if (debug) dev_info(&wc->vb.pdev->dev, "@@@@@ setting reg 0x10 to %02x hex\n",(unsigned char)l);
3673 return;
3674 }
3675
3676 static void wctdm24xxp_get_fxs_regs(struct wctdm *wc, struct wctdm_module *mod,
3677 struct wctdm_regs *regs)
3678 {
3679 int x;
3680
3681 for (x = 0; x < NUM_INDIRECT_REGS; x++)
3682 regs->indirect[x] = wctdm_proslic_getreg_indirect(wc, mod, x);
3683
3684 for (x = 0; x < NUM_REGS; x++)
3685 regs->direct[x] = wctdm_getreg(wc, mod, x);
3686 }
3687
3688 static void wctdm24xxp_get_fxo_regs(struct wctdm *wc, struct wctdm_module *mod,
3689 struct wctdm_regs *regs)
3690 {
3691 int x;
3692 for (x = 0; x < NUM_FXO_REGS; x++)
3693 regs->direct[x] = wctdm_getreg(wc, mod, x);
3694 }
3695
3696 static void wctdm24xxp_get_qrv_regs(struct wctdm *wc, struct wctdm_module *mod,
3697 struct wctdm_regs *regs)
3698 {
3699 int x;
3700 for (x = 0; x < 0x32; x++)
3701 regs->direct[x] = wctdm_getreg(wc, mod, x);
3702 }
3703
3704 static int wctdm_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
3705 {
3706 struct wctdm_stats stats;
3707 struct wctdm_regop regop;
3708 struct wctdm_echo_coefs echoregs;
3709 struct dahdi_hwgain hwgain;
3710 struct wctdm *wc = chan->pvt;
3711 int x;
3712 union {
3713 struct dahdi_radio_stat s;
3714 struct dahdi_radio_param p;
3715 } stack;
3716 struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];
3717 struct fxs *const fxs = &mod->mod.fxs;
3718
3719 switch (cmd) {
3720 case DAHDI_ONHOOKTRANSFER:
3721 if (mod->type != FXS)
3722 return -EINVAL;
3723 if (get_user(x, (__user int *) data))
3724 return -EFAULT;
3725
3726 /* Active mode when idle */
3727 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
3728 SLIC_LF_ACTIVE_REV :
3729 SLIC_LF_ACTIVE_FWD;
3730
3731 if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_ACTIVE_FWD) ||
3732 ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_ACTIVE_REV)) {
3733 int res;
3734
3735 res = set_lasttxhook_interruptible(wc, fxs,
3736 (POLARITY_XOR(fxs) ?
3737 SLIC_LF_OHTRAN_REV : SLIC_LF_OHTRAN_FWD),
3738 &mod->sethook);
3739
3740 if (debug & DEBUG_CARD) {
3741 if (res) {
3742 dev_info(&wc->vb.pdev->dev,
3743 "Channel %d TIMEOUT: "
3744 "OnHookTransfer start\n",
3745 chan->chanpos - 1);
3746 } else {
3747 dev_info(&wc->vb.pdev->dev,
3748 "Channel %d OnHookTransfer "
3749 "start\n", chan->chanpos - 1);
3750 }
3751 }
3752
3753 }
3754
3755 fxs->ohttimer = wc->framecount + x;
3756 fxs->oht_active = 1;
3757
3758 break;
3759 case DAHDI_VMWI_CONFIG:
3760 if (mod->type != FXS)
3761 return -EINVAL;
3762 if (copy_from_user(&(fxs->vmwisetting),
3763 (__user void *)data,
3764 sizeof(fxs->vmwisetting)))
3765 return -EFAULT;
3766 set_vmwi(wc, mod);
3767 break;
3768 case DAHDI_VMWI:
3769 if (mod->type != FXS)
3770 return -EINVAL;
3771 if (get_user(x, (__user int *) data))
3772 return -EFAULT;
3773 if (0 > x)
3774 return -EFAULT;
3775 fxs->vmwi_active_messages = x;
3776 set_vmwi(wc, mod);
3777 break;
3778 case WCTDM_GET_STATS:
3779 if (mod->type == FXS) {
3780 stats.tipvolt = wctdm_getreg(wc, mod, 80) * -376;
3781 stats.ringvolt = wctdm_getreg(wc, mod, 81) * -376;
3782 stats.batvolt = wctdm_getreg(wc, mod, 82) * -376;
3783 } else if (mod->type == FXO) {
3784 stats.tipvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
3785 stats.ringvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
3786 stats.batvolt = (s8)wctdm_getreg(wc, mod, 29) * 1000;
3787 } else
3788 return -EINVAL;
3789 if (copy_to_user((__user void *) data, &stats, sizeof(stats)))
3790 return -EFAULT;
3791 break;
3792 case WCTDM_GET_REGS:
3793 {
3794 struct wctdm_regs *regs = kzalloc(sizeof(*regs), GFP_KERNEL);
3795 if (!regs)
3796 return -ENOMEM;
3797
3798 if (mod->type == FXS)
3799 wctdm24xxp_get_fxs_regs(wc, mod, regs);
3800 else if (mod->type == QRV)
3801 wctdm24xxp_get_qrv_regs(wc, mod, regs);
3802 else
3803 wctdm24xxp_get_fxo_regs(wc, mod, regs);
3804
3805 if (copy_to_user((__user void *)data, regs, sizeof(*regs))) {
3806 kfree(regs);
3807 return -EFAULT;
3808 }
3809
3810 kfree(regs);
3811 break;
3812 }
3813 case WCTDM_SET_REG:
3814 if (copy_from_user(&regop, (__user void *) data, sizeof(regop)))
3815 return -EFAULT;
3816 if (regop.indirect) {
3817 if (mod->type != FXS)
3818 return -EINVAL;
3819 dev_info(&wc->vb.pdev->dev, "Setting indirect %d to 0x%04x on %d\n", regop.reg, regop.val, chan->chanpos);
3820 wctdm_proslic_setreg_indirect(wc, mod, regop.reg,
3821 regop.val);
3822 } else {
3823 regop.val &= 0xff;
3824 if (regop.reg == LINE_STATE) {
3825 /* Set feedback register to indicate the new state that is being set */
3826 fxs->lasttxhook = (regop.val & 0x0f) | SLIC_LF_OPPENDING;
3827 }
3828 dev_info(&wc->vb.pdev->dev, "Setting direct %d to %04x on %d\n", regop.reg, regop.val, chan->chanpos);
3829 wctdm_setreg(wc, mod, regop.reg, regop.val);
3830 }
3831 break;
3832 case WCTDM_SET_ECHOTUNE:
3833 dev_info(&wc->vb.pdev->dev, "-- Setting echo registers: \n");
3834 if (copy_from_user(&echoregs, (__user void *) data, sizeof(echoregs)))
3835 return -EFAULT;
3836
3837 if (mod->type == FXO) {
3838 /* Set the ACIM register */
3839 wctdm_setreg(wc, mod, 30, echoregs.acim);
3840
3841 /* Set the digital echo canceller registers */
3842 wctdm_setreg(wc, mod, 45, echoregs.coef1);
3843 wctdm_setreg(wc, mod, 46, echoregs.coef2);
3844 wctdm_setreg(wc, mod, 47, echoregs.coef3);
3845 wctdm_setreg(wc, mod, 48, echoregs.coef4);
3846 wctdm_setreg(wc, mod, 49, echoregs.coef5);
3847 wctdm_setreg(wc, mod, 50, echoregs.coef6);
3848 wctdm_setreg(wc, mod, 51, echoregs.coef7);
3849 wctdm_setreg(wc, mod, 52, echoregs.coef8);
3850
3851 dev_info(&wc->vb.pdev->dev, "-- Set echo registers successfully\n");
3852
3853 break;
3854 } else {
3855 return -EINVAL;
3856
3857 }
3858 break;
3859 case DAHDI_SET_HWGAIN:
3860 if (copy_from_user(&hwgain, (__user void *) data, sizeof(hwgain)))
3861 return -EFAULT;
3862
3863 wctdm_set_hwgain(wc, mod, hwgain.newgain, hwgain.tx);
3864
3865 if (debug)
3866 dev_info(&wc->vb.pdev->dev, "Setting hwgain on channel %d to %d for %s direction\n",
3867 chan->chanpos-1, hwgain.newgain, hwgain.tx ? "tx" : "rx");
3868 break;
3869 #ifdef VPM_SUPPORT
3870 case DAHDI_TONEDETECT:
3871 /* Hardware DTMF detection is not supported. */
3872 return -ENOSYS;
3873 #endif
3874 case DAHDI_SETPOLARITY:
3875 if (get_user(x, (__user int *) data))
3876 return -EFAULT;
3877 if (mod->type != FXS)
3878 return -EINVAL;
3879 /* Can't change polarity while ringing or when open */
3880 if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_RINGING) ||
3881 ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_OPEN)) {
3882 if (debug & DEBUG_CARD) {
3883 dev_info(&wc->vb.pdev->dev,
3884 "Channel %d Unable to Set Polarity\n", chan->chanpos - 1);
3885 }
3886 return -EINVAL;
3887 }
3888
3889 fxs->reversepolarity = (x) ? 1 : 0;
3890
3891 if (POLARITY_XOR(fxs)) {
3892 fxs->idletxhookstate |= SLIC_LF_REVMASK;
3893 x = fxs->lasttxhook & SLIC_LF_SETMASK;
3894 x |= SLIC_LF_REVMASK;
3895 if (x != fxs->lasttxhook) {
3896 x = set_lasttxhook_interruptible(wc, fxs, x,
3897 &mod->sethook);
3898 if ((debug & DEBUG_CARD) && x) {
3899 dev_info(&wc->vb.pdev->dev,
3900 "Channel %d TIMEOUT: Set Reverse "
3901 "Polarity\n", chan->chanpos - 1);
3902 } else if (debug & DEBUG_CARD) {
3903 dev_info(&wc->vb.pdev->dev,
3904 "Channel %d Set Reverse Polarity\n",
3905 chan->chanpos - 1);
3906 }
3907 }
3908 } else {
3909 fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
3910 x = fxs->lasttxhook & SLIC_LF_SETMASK;
3911 x &= ~SLIC_LF_REVMASK;
3912 if (x != fxs->lasttxhook) {
3913 x = set_lasttxhook_interruptible(wc, fxs, x,
3914 &mod->sethook);
3915 if ((debug & DEBUG_CARD) & x) {
3916 dev_info(&wc->vb.pdev->dev,
3917 "Channel %d TIMEOUT: Set Normal "
3918 "Polarity\n", chan->chanpos - 1);
3919 } else if (debug & DEBUG_CARD) {
3920 dev_info(&wc->vb.pdev->dev,
3921 "Channel %d Set Normal Polarity\n",
3922 chan->chanpos - 1);
3923 }
3924 }
3925 }
3926 break;
3927 case DAHDI_RADIO_GETPARAM:
3928 if (mod->type != QRV)
3929 return -ENOTTY;
3930 if (copy_from_user(&stack.p, (__user void *) data, sizeof(stack.p)))
3931 return -EFAULT;
3932 stack.p.data = 0; /* start with 0 value in output */
3933 switch(stack.p.radpar) {
3934 case DAHDI_RADPAR_INVERTCOR:
3935 if (mod->mod.qrv.radmode & RADMODE_INVERTCOR)
3936 stack.p.data = 1;
3937 break;
3938 case DAHDI_RADPAR_IGNORECOR:
3939 if (mod->mod.qrv.radmode & RADMODE_IGNORECOR)
3940 stack.p.data = 1;
3941 break;
3942 case DAHDI_RADPAR_IGNORECT:
3943 if (mod->mod.qrv.radmode & RADMODE_IGNORECT)
3944 stack.p.data = 1;
3945 break;
3946 case DAHDI_RADPAR_EXTRXTONE:
3947 stack.p.data = 0;
3948 if (mod->mod.qrv.radmode & RADMODE_EXTTONE) {
3949 stack.p.data = 1;
3950 if (mod->mod.qrv.radmode & RADMODE_EXTINVERT)
3951 stack.p.data = 2;
3952 }
3953 break;
3954 case DAHDI_RADPAR_DEBOUNCETIME:
3955 stack.p.data = mod->mod.qrv.debouncetime;
3956 break;
3957 case DAHDI_RADPAR_RXGAIN:
3958 stack.p.data = mod->mod.qrv.rxgain - 1199;
3959 break;
3960 case DAHDI_RADPAR_TXGAIN:
3961 stack.p.data = mod->mod.qrv.txgain - 3599;
3962 break;
3963 case DAHDI_RADPAR_DEEMP:
3964 stack.p.data = 0;
3965 if (mod->mod.qrv.radmode & RADMODE_DEEMP)
3966 stack.p.data = 1;
3967 break;
3968 case DAHDI_RADPAR_PREEMP:
3969 stack.p.data = 0;
3970 if (mod->mod.qrv.radmode & RADMODE_PREEMP)
3971 stack.p.data = 1;
3972 break;
3973 default:
3974 return -EINVAL;
3975 }
3976 if (copy_to_user((__user void *) data, &stack.p, sizeof(stack.p)))
3977 return -EFAULT;
3978 break;
3979 case DAHDI_RADIO_SETPARAM:
3980 if (mod->type != QRV)
3981 return -ENOTTY;
3982 if (copy_from_user(&stack.p, (__user void *) data, sizeof(stack.p)))
3983 return -EFAULT;
3984 switch(stack.p.radpar) {
3985 case DAHDI_RADPAR_INVERTCOR:
3986 if (stack.p.data)
3987 mod->mod.qrv.radmode |= RADMODE_INVERTCOR;
3988 else
3989 mod->mod.qrv.radmode &= ~RADMODE_INVERTCOR;
3990 return 0;
3991 case DAHDI_RADPAR_IGNORECOR:
3992 if (stack.p.data)
3993 mod->mod.qrv.radmode |= RADMODE_IGNORECOR;
3994 else
3995 mod->mod.qrv.radmode &= ~RADMODE_IGNORECOR;
3996 return 0;
3997 case DAHDI_RADPAR_IGNORECT:
3998 if (stack.p.data)
3999 mod->mod.qrv.radmode |= RADMODE_IGNORECT;
4000 else
4001 mod->mod.qrv.radmode &= ~RADMODE_IGNORECT;
4002 return 0;
4003 case DAHDI_RADPAR_EXTRXTONE:
4004 if (stack.p.data)
4005 mod->mod.qrv.radmode |= RADMODE_EXTTONE;
4006 else
4007 mod->mod.qrv.radmode &= ~RADMODE_EXTTONE;
4008 if (stack.p.data > 1)
4009 mod->mod.qrv.radmode |= RADMODE_EXTINVERT;
4010 else
4011 mod->mod.qrv.radmode &= ~RADMODE_EXTINVERT;
4012 return 0;
4013 case DAHDI_RADPAR_DEBOUNCETIME:
4014 mod->mod.qrv.debouncetime = stack.p.data;
4015 return 0;
4016 case DAHDI_RADPAR_RXGAIN:
4017 /* if out of range */
4018 if ((stack.p.data <= -1200) || (stack.p.data > 1552))
4019 {
4020 return -EINVAL;
4021 }
4022 mod->mod.qrv.rxgain = stack.p.data + 1199;
4023 break;
4024 case DAHDI_RADPAR_TXGAIN:
4025 /* if out of range */
4026 if (mod->mod.qrv.radmode & RADMODE_PREEMP) {
4027 if ((stack.p.data <= -2400) ||
4028 (stack.p.data > 0))
4029 return -EINVAL;
4030 } else {
4031 if ((stack.p.data <= -3600) ||
4032 (stack.p.data > 1200))
4033 return -EINVAL;
4034 }
4035 mod->mod.qrv.txgain = stack.p.data + 3599;
4036 break;
4037 case DAHDI_RADPAR_DEEMP:
4038 if (stack.p.data)
4039 mod->mod.qrv.radmode |= RADMODE_DEEMP;
4040 else
4041 mod->mod.qrv.radmode &= ~RADMODE_DEEMP;
4042 mod->mod.qrv.rxgain = 1199;
4043 break;
4044 case DAHDI_RADPAR_PREEMP:
4045 if (stack.p.data)
4046 mod->mod.qrv.radmode |= RADMODE_PREEMP;
4047 else
4048 mod->mod.qrv.radmode &= ~RADMODE_PREEMP;
4049 mod->mod.qrv.txgain = 3599;
4050 break;
4051 default:
4052 return -EINVAL;
4053 }
4054 qrv_dosetup(chan,wc);
4055 return 0;
4056 default:
4057 return -ENOTTY;
4058 }
4059 return 0;
4060 }
4061
4062 static int wctdm_open(struct dahdi_chan *chan)
4063 {
4064 struct wctdm *const wc = chan->pvt;
4065 unsigned long flags;
4066 struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];
4067
4068 #if 0
4069 if (wc->dead)
4070 return -ENODEV;
4071 #endif
4072 if (mod->type == FXO) {
4073 /* Reset the mwi indicators */
4074 spin_lock_irqsave(&wc->reglock, flags);
4075 mod->mod.fxo.neonmwi_debounce = 0;
4076 mod->mod.fxo.neonmwi_offcounter = 0;
4077 mod->mod.fxo.neonmwi_state = 0;
4078 spin_unlock_irqrestore(&wc->reglock, flags);
4079 }
4080
4081 return 0;
4082 }
4083
4084 static inline struct wctdm *span_to_wctdm(struct dahdi_span *span)
4085 {
4086 struct wctdm_span *s = container_of(span, struct wctdm_span, span);
4087 return s->wc;
4088 }
4089
4090 static int wctdm_watchdog(struct dahdi_span *span, int event)
4091 {
4092 struct wctdm *wc = span_to_wctdm(span);
4093 dev_info(&wc->vb.pdev->dev, "TDM: Called watchdog\n");
4094 return 0;
4095 }
4096
4097 static int wctdm_close(struct dahdi_chan *chan)
4098 {
4099 struct wctdm *wc;
4100 int x;
4101 signed char reg;
4102
4103 wc = chan->pvt;
4104 for (x = 0; x < wc->mods_per_board; x++) {
4105 struct wctdm_module *const mod = &wc->mods[x];
4106 if (FXS == mod->type) {
4107 mod->mod.fxs.idletxhookstate =
4108 POLARITY_XOR(&mod->mod.fxs) ? SLIC_LF_ACTIVE_REV :
4109 SLIC_LF_ACTIVE_FWD;
4110 } else if (QRV == mod->type) {
4111 int qrvcard = x & 0xfc;
4112
4113 mod->mod.qrv.hook = 0;
4114 wc->mods[x + 2].mod.qrv.hook = 0xff;
4115 mod->mod.qrv.debouncetime = QRV_DEBOUNCETIME;
4116 mod->mod.qrv.debtime = 0;
4117 mod->mod.qrv.radmode = 0;
4118 mod->mod.qrv.txgain = 3599;
4119 mod->mod.qrv.rxgain = 1199;
4120 reg = 0;
4121 if (!wc->mods[qrvcard].mod.qrv.hook)
4122 reg |= 1;
4123 if (!wc->mods[qrvcard + 1].mod.qrv.hook)
4124 reg |= 0x10;
4125 wc->mods[qrvcard].sethook = CMD_WR(3, reg);
4126 qrv_dosetup(chan,wc);
4127 }
4128 }
4129
4130 return 0;
4131 }
4132
4133 static int wctdm_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
4134 {
4135 struct wctdm *wc = chan->pvt;
4136 int reg = 0;
4137 struct wctdm_module *const mod = &wc->mods[chan->chanpos - 1];
4138
4139 if (mod->type == QRV) {
4140 const int qrvcard = (chan->chanpos - 1) & 0xfc;
4141
4142 switch(txsig) {
4143 case DAHDI_TXSIG_START:
4144 case DAHDI_TXSIG_OFFHOOK:
4145 mod->mod.qrv.hook = 1;
4146 break;
4147 case DAHDI_TXSIG_ONHOOK:
4148 mod->mod.qrv.hook = 0;
4149 break;
4150 default:
4151 dev_notice(&wc->vb.pdev->dev, "wctdm24xxp: Can't set tx state to %d\n", txsig);
4152 }
4153 reg = 0;
4154 if (!wc->mods[qrvcard].mod.qrv.hook)
4155 reg |= 1;
4156 if (!wc->mods[qrvcard + 1].mod.qrv.hook)
4157 reg |= 0x10;
4158 wc->mods[qrvcard].sethook = CMD_WR(3, reg);
4159 /* wctdm_setreg(wc, qrvcard, 3, reg); */
4160 } else if (mod->type == FXO) {
4161 switch(txsig) {
4162 case DAHDI_TXSIG_START:
4163 case DAHDI_TXSIG_OFFHOOK:
4164 mod->mod.fxo.offhook = 1;
4165 mod->sethook = CMD_WR(5, 0x9);
4166 /* wctdm_setreg(wc, chan->chanpos - 1, 5, 0x9); */
4167 break;
4168 case DAHDI_TXSIG_ONHOOK:
4169 mod->mod.fxo.offhook = 0;
4170 mod->sethook = CMD_WR(5, 0x8);
4171 /* wctdm_setreg(wc, chan->chanpos - 1, 5, 0x8); */
4172 break;
4173 default:
4174 dev_notice(&wc->vb.pdev->dev, "wctdm24xxp: Can't set tx state to %d\n", txsig);
4175 }
4176 } else if (mod->type == FXS) {
4177 wctdm_fxs_hooksig(wc, mod, txsig);
4178 }
4179 return 0;
4180 }
4181
4182 static void wctdm_dacs_connect(struct wctdm *wc, int srccard, int dstcard)
4183 {
4184 struct wctdm_module *const srcmod = &wc->mods[srccard];
4185 struct wctdm_module *const dstmod = &wc->mods[dstcard];
4186 unsigned int type;
4187
4188 if (wc->mods[dstcard].dacssrc > -1) {
4189 dev_notice(&wc->vb.pdev->dev, "wctdm_dacs_connect: Can't have double sourcing yet!\n");
4190 return;
4191 }
4192 type = wc->mods[srccard].type;
4193 if ((type == FXS) || (type == FXO)) {
4194 dev_notice(&wc->vb.pdev->dev,
4195 "wctdm_dacs_connect: Unsupported modtype for "
4196 "card %d\n", srccard);
4197 return;
4198 }
4199 type = wc->mods[dstcard].type;
4200 if ((type != FXS) && (type != FXO)) {
4201 dev_notice(&wc->vb.pdev->dev,
4202 "wctdm_dacs_connect: Unsupported modtype "
4203 "for card %d\n", dstcard);
4204 return;
4205 }
4206
4207 if (debug) {
4208 dev_info(&wc->vb.pdev->dev,
4209 "connect %d => %d\n", srccard, dstcard);
4210 }
4211
4212 dstmod->dacssrc = srccard;
4213
4214 /* make srccard transmit to srccard+24 on the TDM bus */
4215 if (srcmod->type == FXS) {
4216 /* proslic */
4217 wctdm_setreg(wc, srcmod, PCM_XMIT_START_COUNT_LSB,
4218 ((srccard+24) * 8) & 0xff);
4219 wctdm_setreg(wc, srcmod, PCM_XMIT_START_COUNT_MSB,
4220 ((srccard+24) * 8) >> 8);
4221 } else if (srcmod->type == FXO) {
4222 /* daa TX */
4223 wctdm_setreg(wc, srcmod, 34, ((srccard+24) * 8) & 0xff);
4224 wctdm_setreg(wc, srcmod, 35, ((srccard+24) * 8) >> 8);
4225 }
4226
4227 /* have dstcard receive from srccard+24 on the TDM bus */
4228 if (dstmod->type == FXS) {
4229 /* proslic */
4230 wctdm_setreg(wc, dstmod, PCM_RCV_START_COUNT_LSB,
4231 ((srccard+24) * 8) & 0xff);
4232 wctdm_setreg(wc, dstmod, PCM_RCV_START_COUNT_MSB,
4233 ((srccard+24) * 8) >> 8);
4234 } else if (dstmod->type == FXO) {
4235 /* daa RX */
4236 wctdm_setreg(wc, dstmod, 36, ((srccard+24) * 8) & 0xff);
4237 wctdm_setreg(wc, dstmod, 37, ((srccard+24) * 8) >> 8);
4238 }
4239 }
4240
4241 static void wctdm_dacs_disconnect(struct wctdm *wc, int card)
4242 {
4243 struct wctdm_module *const mod = &wc->mods[card];
4244 struct wctdm_module *dacssrc;
4245
4246 if (mod->dacssrc <= -1)
4247 return;
4248
4249 dacssrc = &wc->mods[mod->dacssrc];
4250
4251 if (debug) {
4252 dev_info(&wc->vb.pdev->dev, "wctdm_dacs_disconnect: "
4253 "restoring TX for %d and RX for %d\n",
4254 mod->dacssrc, card);
4255 }
4256
4257 /* restore TX (source card) */
4258 if (dacssrc->type == FXS) {
4259 wctdm_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_LSB,
4260 (mod->dacssrc * 8) & 0xff);
4261 wctdm_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_MSB,
4262 (mod->dacssrc * 8) >> 8);
4263 } else if (dacssrc->type == FXO) {
4264 wctdm_setreg(wc, mod, 34, (card * 8) & 0xff);
4265 wctdm_setreg(wc, mod, 35, (card * 8) >> 8);
4266 } else {
4267 dev_warn(&wc->vb.pdev->dev,
4268 "WARNING: wctdm_dacs_disconnect() called "
4269 "on unsupported modtype\n");
4270 }
4271
4272 /* restore RX (this card) */
4273 if (FXS == mod->type) {
4274 wctdm_setreg(wc, mod, PCM_RCV_START_COUNT_LSB,
4275 (card * 8) & 0xff);
4276 wctdm_setreg(wc, mod, PCM_RCV_START_COUNT_MSB,
4277 (card * 8) >> 8);
4278 } else if (FXO == mod->type) {
4279 wctdm_setreg(wc, mod, 36, (card * 8) & 0xff);
4280 wctdm_setreg(wc, mod, 37, (card * 8) >> 8);
4281 } else {
4282 dev_warn(&wc->vb.pdev->dev,
4283 "WARNING: wctdm_dacs_disconnect() called "
4284 "on unsupported modtype\n");
4285 }
4286
4287 mod->dacssrc = -1;
4288 }
4289
4290 static int wctdm_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
4291 {
4292 struct wctdm *wc;
4293
4294 if (!nativebridge)
4295 return 0; /* should this return -1 since unsuccessful? */
4296
4297 wc = dst->pvt;
4298
4299 if (src) {
4300 wctdm_dacs_connect(wc, src->chanpos - 1, dst->chanpos - 1);
4301 if (debug)
4302 dev_info(&wc->vb.pdev->dev, "dacs connecct: %d -> %d!\n\n", src->chanpos, dst->chanpos);
4303 } else {
4304 wctdm_dacs_disconnect(wc, dst->chanpos - 1);
4305 if (debug)
4306 dev_info(&wc->vb.pdev->dev, "dacs disconnect: %d!\n", dst->chanpos);
4307 }
4308 return 0;
4309 }
4310
4311 /**
4312 * wctdm_wait_for_ready
4313 *
4314 * Check if the board has finished any setup and is ready to start processing
4315 * calls.
4316 */
4317 int wctdm_wait_for_ready(struct wctdm *wc)
4318 {
4319 while (!is_initialized(wc)) {
4320 if (fatal_signal_pending(current))
4321 return -EIO;
4322 msleep_interruptible(250);
4323 }
4324 return 0;
4325 }
4326
4327 static int wctdm_enable_hw_preechocan(struct dahdi_chan *chan)
4328 {
4329 struct wctdm *wc = chan->pvt;
4330 struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);
4331
4332 if (!wc->vpmoct)
4333 return 0;
4334
4335 return vpmoct_preecho_enable(wc->vpmoct, wchan->timeslot);
4336 }
4337
4338 static void wctdm_disable_hw_preechocan(struct dahdi_chan *chan)
4339 {
4340 struct wctdm *wc = chan->pvt;
4341 struct wctdm_chan *wchan = container_of(chan, struct wctdm_chan, chan);
4342
4343 if (!wc->vpmoct)
4344 return;
4345
4346 vpmoct_preecho_disable(wc->vpmoct, wchan->timeslot);
4347 }
4348
4349 /**
4350 * wctdm_chanconfig - Called when the channels are being configured.
4351 *
4352 * Ensure that the card is completely ready to go before we allow the channels
4353 * to be completely configured. This is to allow lengthy initialization
4354 * actions to take place in background on driver load and ensure we're synced
4355 * up by the time dahdi_cfg is run.
4356 *
4357 */
4358 static int
4359 wctdm_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
4360 {
4361 struct wctdm *wc = chan->pvt;
4362
4363 if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
4364 return -EAGAIN;
4365
4366 return wctdm_wait_for_ready(wc);
4367 }
4368
4369 static const struct dahdi_span_ops wctdm24xxp_analog_span_ops = {
4370 .owner = THIS_MODULE,
4371 .hooksig = wctdm_hooksig,
4372 .open = wctdm_open,
4373 .close = wctdm_close,
4374 .ioctl = wctdm_ioctl,
4375 .watchdog = wctdm_watchdog,
4376 .chanconfig = wctdm_chanconfig,
4377 .dacs = wctdm_dacs,
4378 #ifdef VPM_SUPPORT
4379 .enable_hw_preechocan = wctdm_enable_hw_preechocan,
4380 .disable_hw_preechocan = wctdm_disable_hw_preechocan,
4381 .echocan_create = wctdm_echocan_create,
4382 .echocan_name = wctdm_echocan_name,
4383 #endif
4384 };
4385
4386 static const struct dahdi_span_ops wctdm24xxp_digital_span_ops = {
4387 .owner = THIS_MODULE,
4388 .open = wctdm_open,
4389 .close = wctdm_close,
4390 .ioctl = wctdm_ioctl,
4391 .watchdog = wctdm_watchdog,
4392 .hdlc_hard_xmit = wctdm_hdlc_hard_xmit,
4393 .spanconfig = b400m_spanconfig,
4394 .chanconfig = b400m_chanconfig,
4395 .dacs = wctdm_dacs,
4396 #ifdef VPM_SUPPORT
4397 .enable_hw_preechocan = wctdm_enable_hw_preechocan,
4398 .disable_hw_preechocan = wctdm_disable_hw_preechocan,
4399 .echocan_create = wctdm_echocan_create,
4400 .echocan_name = wctdm_echocan_name,
4401 #endif
4402 };
4403
4404 static struct wctdm_chan *
4405 wctdm_init_chan(struct wctdm *wc, struct wctdm_span *s, int chanoffset,
4406 int channo, unsigned int card_position)
4407 {
4408 struct wctdm_chan *c;
4409
4410 c = kzalloc(sizeof(*c), GFP_KERNEL);
4411 if (!c)
4412 return NULL;
4413
4414 /* Do not change the procfs representation for non-hx8 cards. */
4415 if (dahdi_is_digital_span(&s->span)) {
4416 sprintf(c->chan.name, "WCBRI/%d/%d/%d", card_position,
4417 s->spanno, channo);
4418 } else {
4419 sprintf(c->chan.name, "WCTDM/%d/%d", card_position, channo);
4420 }
4421
4422 c->chan.chanpos = channo+1;
4423 c->chan.span = &s->span;
4424 c->chan.pvt = wc;
4425 c->timeslot = chanoffset + channo;
4426 return c;
4427 }
4428
4429 #if 0
4430 /**
4431 * wctdm_span_count() - Return the number of spans exported by this board.
4432 *
4433 * This is only called during initialization so let's just count the spans each
4434 * time we need this information as opposed to storing another variable in the
4435 * wctdm structure.
4436 */
4437 static int wctdm_span_count(const struct wctdm *wc)
4438 {
4439 int i;
4440 int count = 0;
4441 for (i = 0; i < MAX_SPANS; ++i) {
4442 if (wc->spans[i])
4443 ++count;
4444 }
4445 return count;
4446 }
4447 #endif
4448
4449 static struct wctdm_span *
4450 wctdm_init_span(struct wctdm *wc, int spanno, int chanoffset, int chancount,
4451 int digital_span, unsigned int card_position)
4452 {
4453 int x;
4454 struct wctdm_chan *c;
4455 struct wctdm_span *s;
4456 static int spancount;
4457
4458 s = kzalloc(sizeof(*s), GFP_KERNEL);
4459 if (!s)
4460 return NULL;
4461
4462 /* DAHDI stuff */
4463 s->span.offset = spanno;
4464
4465 s->spanno = spancount++;
4466 s->wc = wc;
4467
4468 /* Do not change the procfs representation for non-hx8 cards. */
4469 if (digital_span)
4470 sprintf(s->span.name, "WCBRI/%d/%d", card_position, s->spanno);
4471 else
4472 sprintf(s->span.name, "WCTDM/%d", card_position);
4473
4474 snprintf(s->span.desc, sizeof(s->span.desc) - 1, "%s", wc->desc->name);
4475
4476 if (wc->companding == DAHDI_LAW_DEFAULT) {
4477 if (wc->digi_mods || digital_span)
4478 /* If we have a BRI module, Auto set to alaw */
4479 s->span.deflaw = DAHDI_LAW_ALAW;
4480 else
4481 /* Auto set to ulaw */
4482 s->span.deflaw = DAHDI_LAW_MULAW;
4483 } else if (wc->companding == DAHDI_LAW_ALAW) {
4484 /* Force everything to alaw */
4485 s->span.deflaw = DAHDI_LAW_ALAW;
4486 } else {
4487 /* Auto set to ulaw */
4488 s->span.deflaw = DAHDI_LAW_MULAW;
4489 }
4490
4491 if (digital_span) {
4492 s->span.ops = &wctdm24xxp_digital_span_ops;
4493 s->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4;
4494 s->span.linecompat |= DAHDI_CONFIG_ESF | DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
4495 s->span.linecompat |= DAHDI_CONFIG_NTTE | DAHDI_CONFIG_TERM;
4496 s->span.spantype = SPANTYPE_DIGITAL_BRI_TE;
4497 } else {
4498 s->span.ops = &wctdm24xxp_analog_span_ops;
4499 s->span.flags = DAHDI_FLAG_RBS;
4500 s->span.spantype = SPANTYPE_ANALOG_MIXED;
4501 /* analog sigcap handled in fixup_analog_span() */
4502 }
4503
4504 s->span.chans = kmalloc(sizeof(struct dahdi_chan *) * chancount, GFP_KERNEL);
4505 if (!s->span.chans)
4506 return NULL;
4507
4508 /* allocate channels for the span */
4509 for (x = 0; x < chancount; x++) {
4510 c = wctdm_init_chan(wc, s, chanoffset, x, card_position);
4511 if (!c)
4512 return NULL;
4513 wc->chans[chanoffset + x] = c;
4514 s->span.chans[x] = &c->chan;
4515 }
4516
4517 s->span.channels = chancount;
4518
4519 if (digital_span) {
4520 wc->chans[chanoffset + 0]->chan.sigcap = DAHDI_SIG_CLEAR;
4521 wc->chans[chanoffset + 1]->chan.sigcap = DAHDI_SIG_CLEAR;
4522 wc->chans[chanoffset + 2]->chan.sigcap = DAHDI_SIG_HARDHDLC;
4523 }
4524
4525 wc->spans[spanno] = s;
4526 return s;
4527 }
4528
4529 /**
4530 * should_set_alaw() - Should be called after all the spans are initialized.
4531 *
4532 * Returns true if the module companding should be set to alaw, otherwise
4533 * false.
4534 */
4535 static bool should_set_alaw(const struct wctdm *wc)
4536 {
4537 if (DAHDI_LAW_DEFAULT == wc->companding)
4538 return (wc->digi_mods > 0);
4539 else if (DAHDI_LAW_ALAW == wc->companding)
4540 return true;
4541 else
4542 return false;
4543 }
4544
4545 static void wctdm_fixup_analog_span(struct wctdm *wc, int spanno)
4546 {
4547 struct dahdi_span *s;
4548 int x, y;
4549
4550 /* Finalize signalling */
4551 y = 0;
4552 s = &wc->spans[spanno]->span;
4553
4554 for (x = 0; x < wc->desc->ports; x++) {
4555 struct wctdm_module *const mod = &wc->mods[x];
4556 if (debug) {
4557 dev_info(&wc->vb.pdev->dev,
4558 "fixup_analog: x=%d, y=%d modtype=%d, "
4559 "s->chans[%d]=%p\n", x, y, mod->type,
4560 y, s->chans[y]);
4561 }
4562 if (mod->type == FXO) {
4563 int val;
4564 s->chans[y++]->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF | DAHDI_SIG_CLEAR;
4565 val = should_set_alaw(wc) ? 0x20 : 0x28;
4566 #ifdef DEBUG
4567 val = (digitalloopback) ? 0x30 : val;
4568 #endif
4569 wctdm_setreg(wc, mod, 33, val);
4570 } else if (mod->type == FXS) {
4571 s->chans[y++]->sigcap = DAHDI_SIG_FXOKS | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
4572 wctdm_setreg(wc, mod, 1,
4573 (should_set_alaw(wc) ? 0x20 : 0x28));
4574 } else if (mod->type == QRV) {
4575 s->chans[y++]->sigcap = DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
4576 } else {
4577 s->chans[y++]->sigcap = 0;
4578 }
4579 }
4580 }
4581
4582 static int wctdm_initialize_vpmadt032(struct wctdm *wc)
4583 {
4584 int res;
4585 struct vpmadt032_options options;
4586 struct vpmadt032 *vpm;
4587 unsigned long flags;
4588
4589 options.debug = debug;
4590 options.vpmnlptype = vpmnlptype;
4591 options.vpmnlpthresh = vpmnlpthresh;
4592 options.vpmnlpmaxsupp = vpmnlpmaxsupp;
4593 if (is_hx8(wc)) {
4594 /* Hybrid cards potentially have 3 channels of EC on their
4595 * ports since they may be BRI spans. */
4596 options.channels = 3 * wc->desc->ports;
4597 } else {
4598 options.channels = wc->desc->ports;
4599 }
4600
4601 BUG_ON(options.channels > 24);
4602
4603 wc->vpmadt032 = vpmadt032_alloc(&options);
4604 if (!wc->vpmadt032)
4605 return -ENOMEM;
4606
4607 wc->vpmadt032->setchanconfig_from_state = setchanconfig_from_state;
4608
4609 /* Pull the configuration information from the span holding
4610 * the analog channels. */
4611 res = vpmadt032_test(wc->vpmadt032, &wc->vb);
4612 if (!res)
4613 res = vpmadt032_init(wc->vpmadt032);
4614 if (res) {
4615 vpm = wc->vpmadt032;
4616 spin_lock_irqsave(&wc->reglock, flags);
4617 wc->vpmadt032 = NULL;
4618 spin_unlock_irqrestore(&wc->reglock, flags);
4619 vpmadt032_free(vpm);
4620 return res;
4621 }
4622
4623 /* Now we need to configure the VPMADT032 module for this
4624 * particular board. */
4625 res = config_vpmadt032(wc->vpmadt032, wc);
4626 if (res) {
4627 vpm = wc->vpmadt032;
4628 spin_lock_irqsave(&wc->reglock, flags);
4629 wc->vpmadt032 = NULL;
4630 spin_unlock_irqrestore(&wc->reglock, flags);
4631 vpmadt032_free(vpm);
4632 return res;
4633 }
4634
4635 return 0;
4636 }
4637
4638 static void wctdm_vpm_load_complete(struct device *dev, bool operational)
4639 {
4640 unsigned long flags;
4641 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
4642 struct wctdm *wc = pci_get_drvdata(pdev);
4643 struct vpmoct *vpm = NULL;
4644
4645 WARN_ON(!wc || !wc->not_ready);
4646 if (!wc || !wc->not_ready)
4647 return;
4648
4649 spin_lock_irqsave(&wc->reglock, flags);
4650 wc->not_ready--;
4651 if (operational) {
4652 wc->ctlreg |= 0x10;
4653 } else {
4654 vpm = wc->vpmoct;
4655 wc->vpmoct = NULL;
4656 }
4657 spin_unlock_irqrestore(&wc->reglock, flags);
4658
4659 if (vpm)
4660 vpmoct_free(vpm);
4661 }
4662
4663 static int wctdm_initialize_vpm(struct wctdm *wc, unsigned long unused)
4664 {
4665 int res = 0;
4666
4667 if (!vpmsupport) {
4668 dev_notice(&wc->vb.pdev->dev, "VPM: Support Disabled\n");
4669 return 0;
4670 }
4671
4672 res = wctdm_initialize_vpmadt032(wc);
4673 if (!res) {
4674 wc->ctlreg |= 0x10;
4675 return 0;
4676 } else {
4677 struct vpmoct *vpm;
4678 unsigned long flags;
4679
4680 vpm = vpmoct_alloc();
4681 if (!vpm) {
4682 dev_info(&wc->vb.pdev->dev,
4683 "Unable to allocate memory for struct vpmoct\n");
4684 return -ENOMEM;
4685 }
4686
4687 vpm->dev = &wc->vb.pdev->dev;
4688
4689 spin_lock_irqsave(&wc->reglock, flags);
4690 wc->vpmoct = vpm;
4691 wc->not_ready++;
4692 spin_unlock_irqrestore(&wc->reglock, flags);
4693
4694 res = vpmoct_init(vpm, wctdm_vpm_load_complete);
4695 if (-EINVAL == res) {
4696 spin_lock_irqsave(&wc->reglock, flags);
4697 wc->vpmoct = NULL;
4698 wc->not_ready--;
4699 spin_unlock_irqrestore(&wc->reglock, flags);
4700 vpmoct_free(vpm);
4701 }
4702 }
4703 return 0;
4704 }
4705
4706 static int __wctdm_identify_module_group(struct wctdm *wc, unsigned long base)
4707 {
4708 int x;
4709 unsigned long flags;
4710
4711 for (x = base; x < base + 4; ++x) {
4712 struct wctdm_module *const mod = &wc->mods[x];
4713 enum {SANE = 1, UNKNOWN = 0};
4714 int ret = 0, readi = 0;
4715 bool altcs = false;
4716
4717 if (fatal_signal_pending(current))
4718 break;
4719 retry:
4720 ret = wctdm_init_proslic(wc, mod, 0, 0, UNKNOWN);
4721 if (!ret) {
4722 if (debug & DEBUG_CARD) {
4723 readi = wctdm_getreg(wc, mod, LOOP_I_LIMIT);
4724 dev_info(&wc->vb.pdev->dev,
4725 "Proslic module %d loop current "
4726 "is %dmA\n", x, ((readi*3) + 20));
4727 }
4728 continue;
4729 }
4730
4731 if (ret != -2) {
4732 /* Init with Manual Calibration */
4733 if (!wctdm_init_proslic(wc, mod, 0, 1, SANE)) {
4734
4735 if (debug & DEBUG_CARD) {
4736 readi = wctdm_getreg(wc, mod,
4737 LOOP_I_LIMIT);
4738 dev_info(&wc->vb.pdev->dev,
4739 "Proslic module %d loop "
4740 "current is %dmA\n", x,
4741 ((readi*3)+20));
4742 }
4743 } else {
4744 dev_notice(&wc->vb.pdev->dev,
4745 "Port %d: FAILED FXS (%s)\n",
4746 x + 1, fxshonormode ?
4747 fxo_modes[_opermode].name : "FCC");
4748 }
4749 continue;
4750 }
4751
4752 ret = wctdm_init_voicedaa(wc, mod, 0, 0, UNKNOWN);
4753 if (!ret)
4754 continue;
4755
4756 if (!wctdm_init_qrvdri(wc, x))
4757 continue;
4758
4759 if (is_hx8(wc) && !wctdm_init_b400m(wc, x))
4760 continue;
4761
4762 if ((wc->desc->ports != 24) && ((x&0x3) == 1) && !altcs) {
4763
4764 spin_lock_irqsave(&wc->reglock, flags);
4765 set_offsets(mod, 2);
4766 altcs = true;
4767
4768 if (wc->desc->ports == 4) {
4769 set_offsets(&wc->mods[x+1], 3);
4770 set_offsets(&wc->mods[x+2], 3);
4771 }
4772
4773 mod->type = FXSINIT;
4774 spin_unlock_irqrestore(&wc->reglock, flags);
4775
4776 udelay(1000);
4777 udelay(1000);
4778
4779 spin_lock_irqsave(&wc->reglock, flags);
4780 mod->type = FXS;
4781 spin_unlock_irqrestore(&wc->reglock, flags);
4782
4783 if (debug & DEBUG_CARD) {
4784 dev_info(&wc->vb.pdev->dev,
4785 "Trying port %d with alternate chip "
4786 "select\n", x + 1);
4787 }
4788 goto retry;
4789 }
4790
4791 mod->type = NONE;
4792 }
4793 return 0;
4794 }
4795
4796 /**
4797 * wctdm_print_moule_configuration - Print the configuration to the kernel log
4798 * @wc: The card we're interested in.
4799 *
4800 * This is to ensure that the module configuration from each card shows up
4801 * sequentially in the kernel log, as opposed to interleaved with one another.
4802 *
4803 */
4804 static void wctdm_print_module_configuration(const struct wctdm *const wc)
4805 {
4806 int i;
4807 static DEFINE_MUTEX(print);
4808
4809 mutex_lock(&print);
4810 for (i = 0; i < wc->mods_per_board; ++i) {
4811 const struct wctdm_module *const mod = &wc->mods[i];
4812
4813 switch (mod->type) {
4814 case FXO:
4815 dev_info(&wc->vb.pdev->dev, "Port %d: Installed -- "
4816 "AUTO FXO (%s mode)\n", i + 1,
4817 fxo_modes[_opermode].name);
4818 break;
4819 case FXS:
4820 dev_info(&wc->vb.pdev->dev,
4821 "Port %d: Installed -- AUTO FXS/DPO\n", i + 1);
4822 break;
4823 case BRI:
4824 dev_info(&wc->vb.pdev->dev, "Port %d: Installed -- BRI "
4825 "quad-span module\n", i + 1);
4826 break;
4827 case QRV:
4828 dev_info(&wc->vb.pdev->dev,
4829 "Port %d: Installed -- QRV DRI card\n", i + 1);
4830 break;
4831 case NONE:
4832 dev_info(&wc->vb.pdev->dev,
4833 "Port %d: Not installed\n", i + 1);
4834 break;
4835 case FXSINIT:
4836 break;
4837 }
4838 }
4839 mutex_unlock(&print);
4840 }
4841
4842 static void wctdm_identify_modules(struct wctdm *wc)
4843 {
4844 int x;
4845 unsigned long flags;
4846 struct bg *bg_work[ARRAY_SIZE(wc->mods)/4 + 1] = {NULL, };
4847
4848 wc->ctlreg = 0x00;
4849
4850 /*
4851 * This looks a little weird.
4852 *
4853 * There are only 8 physical ports on the TDM/AEX800, but the code
4854 * immediately below sets 24 modules up. This has to do with the
4855 * altcs magic that allows us to have single-port and quad-port
4856 * modules on these products. The variable "mods_per_board" is set to
4857 * the appropriate value just below the next code block.
4858 *
4859 * Now why this is important: The FXS modules come out of reset in a
4860 * two-byte, non-chainable SPI mode. This is currently incompatible
4861 * with how we do things, so we need to set them to a chained, 3-byte
4862 * command mode. This is done by setting the module type to FXSINIT
4863 * for a little while so that cmd_dequeue will initialize the SLIC
4864 * into the appropriate mode.
4865 *
4866 * This "go to 3-byte chained mode" command, however, wreaks havoc
4867 * with HybridBRI.
4868 *
4869 * The solution: Since HybridBRI is only designed to work in an 8-port
4870 * card, and since the single-port modules "show up" in SPI slots >= 8
4871 * in these cards, we only set SPI slots 8-23 to FXSINIT. The
4872 * HybridBRI will never see the command that causes it to freak out
4873 * and the single-port FXS cards get what they need so that when we
4874 * probe with altcs we see them.
4875 */
4876
4877 /* Make sure all units go into daisy chain mode */
4878 spin_lock_irqsave(&wc->reglock, flags);
4879 for (x = 0; x < ARRAY_SIZE(wc->mods); x++)
4880 wc->mods[x].type = FXSINIT;
4881 spin_unlock_irqrestore(&wc->reglock, flags);
4882
4883 /* Wait just a bit; this makes sure that cmd_dequeue is emitting SPI
4884 * commands in the appropriate mode(s). */
4885 msleep(20);
4886
4887 /* Now that all the cards have been reset, we can stop checking them
4888 * all if there aren't as many */
4889 spin_lock_irqsave(&wc->reglock, flags);
4890 wc->mods_per_board = wc->desc->ports;
4891 spin_unlock_irqrestore(&wc->reglock, flags);
4892
4893 BUG_ON(wc->desc->ports % 4);
4894
4895 /* Detecting and configuring the modules over voicebus takes a
4896 * significant amount of time. We can speed things up by performing
4897 * this in parallel for each group of four modules. */
4898 for (x = 0; x < wc->desc->ports/4; x++)
4899 bg_work[x] = bg_create(wc, __wctdm_identify_module_group, x*4);
4900
4901 for (x = 0; bg_work[x]; ++x)
4902 bg_join(bg_work[x]);
4903
4904 wctdm_print_module_configuration(wc);
4905 }
4906
4907 static struct pci_driver wctdm_driver;
4908
4909 static void wctdm_back_out_gracefully(struct wctdm *wc)
4910 {
4911 int i;
4912 unsigned long flags;
4913 struct vpmadt032 *vpm;
4914 LIST_HEAD(local_list);
4915
4916 spin_lock_irqsave(&wc->reglock, flags);
4917 if (wc->not_ready) {
4918 wc->not_ready--;
4919 spin_unlock_irqrestore(&wc->reglock, flags);
4920 while (wctdm_wait_for_ready(wc))
4921 schedule();
4922 spin_lock_irqsave(&wc->reglock, flags);
4923 }
4924 spin_unlock_irqrestore(&wc->reglock, flags);
4925
4926 if (wc->vpmadt032) {
4927 flush_workqueue(wc->vpmadt032->wq);
4928 clear_bit(VPM150M_ACTIVE, &wc->vpmadt032->control);
4929 flush_workqueue(wc->vpmadt032->wq);
4930 spin_lock_irqsave(&wc->reglock, flags);
4931 vpm = wc->vpmadt032;
4932 wc->vpmadt032 = NULL;
4933 spin_unlock_irqrestore(&wc->reglock, flags);
4934 vpmadt032_free(vpm);
4935 }
4936
4937 voicebus_release(&wc->vb);
4938 #ifdef CONFIG_VOICEBUS_ECREFERENCE
4939 for (i = 0; i < ARRAY_SIZE(wc->ec_reference); ++i) {
4940 if (wc->ec_reference[i])
4941 dahdi_fifo_free(wc->ec_reference[i]);
4942 }
4943 #endif
4944
4945 for (i = 0; i < ARRAY_SIZE(wc->spans); ++i) {
4946 if (wc->spans[i] && wc->spans[i]->span.chans)
4947 kfree(wc->spans[i]->span.chans);
4948
4949 kfree(wc->spans[i]);
4950 wc->spans[i] = NULL;
4951 }
4952
4953 spin_lock_irqsave(&wc->reglock, flags);
4954 for (i = 0; i < ARRAY_SIZE(wc->mods); ++i) {
4955 struct wctdm_module *const mod = &wc->mods[i];
4956 kfree(wc->chans[i]);
4957 wc->chans[i] = NULL;
4958 list_splice_init(&mod->pending_cmds, &local_list);
4959 list_splice_init(&mod->active_cmds, &local_list);
4960 }
4961 list_splice_init(&wc->free_isr_commands, &local_list);
4962 spin_unlock_irqrestore(&wc->reglock, flags);
4963
4964 while (!list_empty(&local_list)) {
4965 struct wctdm_cmd *cmd;
4966 cmd = list_entry(local_list.next,
4967 struct wctdm_cmd, node);
4968 list_del(&cmd->node);
4969 kfree(cmd->complete);
4970 kfree(cmd);
4971 }
4972
4973 spin_lock_irqsave(&wc->frame_list_lock, flags);
4974 list_splice(&wc->frame_list, &local_list);
4975 spin_unlock_irqrestore(&wc->frame_list_lock, flags);
4976
4977 while (!list_empty(&local_list)) {
4978 struct sframe_packet *frame;
4979 frame = list_entry(local_list.next,
4980 struct sframe_packet, node);
4981 list_del(&frame->node);
4982 kfree(frame);
4983 }
4984
4985 kfree(wc->board_name);
4986 kfree(wc->ddev->devicetype);
4987 kfree(wc->ddev->location);
4988 dahdi_free_device(wc->ddev);
4989 kfree(wc);
4990 }
4991
4992 static const struct voicebus_operations voicebus_operations = {
4993 .handle_receive = handle_receive,
4994 .handle_transmit = handle_transmit,
4995 };
4996
4997 static const struct voicebus_operations hx8_voicebus_operations = {
4998 .handle_receive = handle_hx8_receive,
4999 .handle_transmit = handle_hx8_transmit,
5000 };
5001
5002 struct cmd_results {
5003 u8 results[8];
5004 };
5005
5006 static int hx8_send_command(struct wctdm *wc, const u8 *command,
5007 size_t count, int checksum,
5008 int application, int bootloader,
5009 struct cmd_results *results)
5010 {
5011 int ret = 0;
5012 struct vbb *vbb;
5013 struct sframe_packet *frame;
5014 const int MAX_COMMAND_LENGTH = 264 + 4;
5015 unsigned long flags;
5016 dma_addr_t dma_addr;
5017
5018 might_sleep();
5019
5020 /* can't boot both into the application and the bootloader at once. */
5021 WARN_ON((application > 0) && (bootloader > 0));
5022 if ((application > 0) && (bootloader > 0))
5023 return -EINVAL;
5024
5025 WARN_ON(count > MAX_COMMAND_LENGTH);
5026 if (count > MAX_COMMAND_LENGTH)
5027 return -EINVAL;
5028
5029 vbb = dma_pool_alloc(wc->vb.pool, GFP_KERNEL, &dma_addr);
5030 WARN_ON(!vbb);
5031 if (!vbb)
5032 return -ENOMEM;
5033
5034 vbb->dma_addr = dma_addr;
5035 memset(vbb->data, 0, SFRAME_SIZE);
5036 memcpy(&vbb->data[EFRAME_SIZE + EFRAME_GAP], command, count);
5037
5038 vbb->data[EFRAME_SIZE] = 0x80 | ((application) ? 0 : 0x40) |
5039 ((checksum) ? 0x20 : 0) | ((count & 0x100) >> 4);
5040 vbb->data[EFRAME_SIZE + 1] = count & 0xff;
5041
5042 if (bootloader)
5043 vbb->data[EFRAME_SIZE + 3] = 0xAA;
5044
5045 spin_lock_irqsave(&wc->vb.lock, flags);
5046 voicebus_transmit(&wc->vb, vbb);
5047 spin_unlock_irqrestore(&wc->vb.lock, flags);
5048
5049 /* Do not wait for the response if the caller doesn't care about the
5050 * results. */
5051 if (NULL == results)
5052 return 0;
5053
5054 if (!wait_event_timeout(wc->regq, !list_empty(&wc->frame_list), 2*HZ)) {
5055 dev_err(&wc->vb.pdev->dev, "Timeout waiting "
5056 "for receive frame.\n");
5057 ret = -EIO;
5058 }
5059
5060 /* We only want the last packet received. Throw away anything else on
5061 * the list */
5062 frame = NULL;
5063 spin_lock_irqsave(&wc->frame_list_lock, flags);
5064 while (!list_empty(&wc->frame_list)) {
5065 frame = list_entry(wc->frame_list.next,
5066 struct sframe_packet, node);
5067 list_del(&frame->node);
5068 if (!list_empty(&wc->frame_list)) {
5069 kfree(frame);
5070 frame = NULL;
5071 }
5072 }
5073 spin_unlock_irqrestore(&wc->frame_list_lock, flags);
5074
5075 if (frame) {
5076 memcpy(results->results, &frame->sframe[EFRAME_SIZE],
5077 sizeof(results->results));
5078 } else {
5079 ret = -EIO;
5080 }
5081
5082 return ret;
5083
5084 }
5085
5086 static int hx8_get_fpga_version(struct wctdm *wc, u8 *major, u8 *minor)
5087 {
5088 int ret;
5089 struct cmd_results results;
5090 u8 command[] = {0xD7, 0x00};
5091
5092 ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
5093 0, 0, 0, &results);
5094 if (ret)
5095 return ret;
5096
5097 *major = results.results[0];
5098 *minor = results.results[2];
5099 return 0;
5100 }
5101
5102 static void hx8_cleanup_frame_list(struct wctdm *wc)
5103 {
5104 unsigned long flags;
5105 LIST_HEAD(local_list);
5106 struct sframe_packet *frame;
5107
5108 spin_lock_irqsave(&wc->frame_list_lock, flags);
5109 list_splice_init(&wc->frame_list, &local_list);
5110 spin_unlock_irqrestore(&wc->frame_list_lock, flags);
5111
5112 while (!list_empty(&local_list)) {
5113 frame = list_entry(local_list.next, struct sframe_packet, node);
5114 list_del(&frame->node);
5115 kfree(frame);
5116 }
5117 }
5118
5119 static int hx8_switch_to_application(struct wctdm *wc)
5120 {
5121 int ret;
5122 u8 command[] = {0xD7, 0x00};
5123
5124 ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
5125 0, 1, 0, NULL);
5126 if (ret)
5127 return ret;
5128
5129 msleep(1000);
5130 hx8_cleanup_frame_list(wc);
5131
5132 return 0;
5133 }
5134
5135 /**
5136 * hx8_switch_to_bootloader() - Send packet to switch hx8 into bootloader
5137 *
5138 */
5139 static int hx8_switch_to_bootloader(struct wctdm *wc)
5140 {
5141 int ret;
5142 u8 command[] = {0xD7, 0x00};
5143
5144 ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
5145 0, 0, 1, NULL);
5146 if (ret)
5147 return ret;
5148
5149 /* It takes some time for the FPGA to reload and switch it's
5150 * configuration. */
5151 msleep(300);
5152 hx8_cleanup_frame_list(wc);
5153
5154 return 0;
5155 }
5156
5157 struct ha80000_firmware {
5158 u8 header[6];
5159 u8 major_ver;
5160 u8 minor_ver;
5161 u8 data[54648];
5162 __le32 chksum;
5163 } __attribute__((packed));
5164
5165 static void hx8_send_dummy(struct wctdm *wc)
5166 {
5167 u8 command[] = {0xD7, 0x00};
5168
5169 hx8_send_command(wc, command, ARRAY_SIZE(command),
5170 0, 0, 0, NULL);
5171 }
5172
5173 static int hx8_read_status_register(struct wctdm *wc, u8 *status)
5174 {
5175 int ret;
5176 struct cmd_results results;
5177 u8 command[] = {0xD7, 0x00};
5178
5179 ret = hx8_send_command(wc, command, ARRAY_SIZE(command),
5180 0, 0, 0, &results);
5181 if (ret)
5182 return ret;
5183
5184 *status = results.results[3];
5185 return 0;
5186 }
5187
5188 static const unsigned int HYBRID_PAGE_SIZE = 264;
5189
5190 static int hx8_write_buffer(struct wctdm *wc, const u8 *buffer, size_t size)
5191 {
5192 int ret = 0;
5193 struct cmd_results results;
5194 int padding_bytes = 0;
5195 u8 *local_data;
5196 u8 command[] = {0x84, 0, 0, 0};
5197
5198 if (size > HYBRID_PAGE_SIZE)
5199 return -EINVAL;
5200
5201 if (size < HYBRID_PAGE_SIZE)
5202 padding_bytes = HYBRID_PAGE_SIZE - size;
5203
5204 local_data = kmalloc(sizeof(command) + size + padding_bytes, GFP_KERNEL);
5205 if (!local_data)
5206 return -ENOMEM;
5207
5208 memcpy(local_data, command, sizeof(command));
5209 memcpy(&local_data[sizeof(command)], buffer, size);
5210 memset(&local_data[sizeof(command) + size], 0xff, padding_bytes);
5211
5212 ret = hx8_send_command(wc, local_data,
5213 sizeof(command) + size + padding_bytes,
5214 1, 0, 0, &results);
5215 if (ret)
5216 goto cleanup;
5217
5218 cleanup:
5219 kfree(local_data);
5220 return ret;
5221 }
5222
5223 static int hx8_buffer_to_page(struct wctdm *wc, const unsigned int page)
5224 {
5225 int ret;
5226 struct cmd_results results;
5227 u8 command[] = {0x83, (page & 0x180) >> 7, (page & 0x7f) << 1, 0x00};
5228
5229 ret = hx8_send_command(wc, command, sizeof(command),
5230 1, 0, 0, &results);
5231 if (ret)
5232 return ret;
5233
5234 return 0;
5235 }
5236
5237 static int hx8_wait_for_ready(struct wctdm *wc, const int timeout)
5238 {
5239 int ret;
5240 u8 status;
5241 unsigned long local_timeout = jiffies + timeout;
5242
5243 do {
5244 ret = hx8_read_status_register(wc, &status);
5245 if (ret)
5246 return ret;
5247 if ((status & 0x80) > 0)
5248 break;
5249 } while (time_after(local_timeout, jiffies));
5250
5251 if (time_after(jiffies, local_timeout))
5252 return -EIO;
5253
5254 return 0;
5255 }
5256
5257 /**
5258 * hx8_reload_application - reload the application firmware
5259 *
5260 * NOTE: The caller should ensure that the board is in bootloader mode before
5261 * calling this function.
5262 */
5263 static int hx8_reload_application(struct wctdm *wc, const struct ha80000_firmware *ha8_fw)
5264 {
5265 unsigned int cur_page;
5266 const u8 *data;
5267 u8 status;
5268 int ret = 0;
5269 const int HYBRID_PAGE_COUNT = (sizeof(ha8_fw->data)) / HYBRID_PAGE_SIZE;
5270
5271 dev_info(&wc->vb.pdev->dev, "Reloading firmware. Do not power down "
5272 "the system until the process is complete.\n");
5273
5274 BUG_ON(!ha8_fw);
5275 might_sleep();
5276
5277 data = &ha8_fw->data[0];
5278 ret = hx8_read_status_register(wc, &status);
5279 if (ret)
5280 return ret;
5281
5282 for (cur_page = 0; cur_page < HYBRID_PAGE_COUNT; ++cur_page) {
5283 ret = hx8_write_buffer(wc, data, HYBRID_PAGE_SIZE);
5284 if (ret)
5285 return ret;
5286 /* The application starts out at page 0x100 */
5287 ret = hx8_buffer_to_page(wc, 0x100 + cur_page);
5288 if (ret)
5289 return ret;
5290
5291 /* wait no more than a second for the write to the page to
5292 * finish */
5293 ret = hx8_wait_for_ready(wc, HZ);
5294 if (ret)
5295 return ret;
5296
5297 data += HYBRID_PAGE_SIZE;
5298 }
5299
5300 return ret;
5301 }
5302
5303 static void print_hx8_recovery_message(struct device *dev)
5304 {
5305 dev_warn(dev, "The firmware may be corrupted. Please completely "
5306 "power off your system, power on, and then reload the driver "
5307 "with the 'forceload' module parameter set to 1 to attempt "
5308 "recovery.\n");
5309 }
5310
5311 /**
5312 * hx8_check_firmware - Check the firmware version and load a new one possibly.
5313 *
5314 */
5315 static int hx8_check_firmware(struct wctdm *wc)
5316 {
5317 int ret;
5318 u8 major;
5319 u8 minor;
5320 const struct firmware *fw;
5321 const struct ha80000_firmware *ha8_fw;
5322 struct device *dev = &wc->vb.pdev->dev;
5323 int retries = 10;
5324
5325 BUG_ON(!is_hx8(wc));
5326
5327 might_sleep();
5328
5329 do {
5330 hx8_send_dummy(wc);
5331 ret = hx8_get_fpga_version(wc, &major, &minor);
5332 if (!ret)
5333 break;
5334 if (fatal_signal_pending(current))
5335 return -EINTR;
5336 } while (--retries);
5337
5338 if (ret) {
5339 print_hx8_recovery_message(dev);
5340 return ret;
5341 }
5342
5343 /* If we're in the bootloader, try to jump into the application. */
5344 if ((1 == major) && (0x80 == minor) && !forceload) {
5345 dev_dbg(dev, "Switching to application.\n");
5346 hx8_switch_to_application(wc);
5347 ret = hx8_get_fpga_version(wc, &major, &minor);
5348 if (ret) {
5349 print_hx8_recovery_message(dev);
5350 return ret;
5351 }
5352 }
5353
5354 dev_dbg(dev, "FPGA VERSION: %02x.%02x\n", major, minor);
5355
5356 ret = request_firmware(&fw, "dahdi-fw-hx8.bin", dev);
5357 if (ret) {
5358 dev_warn(dev, "Failed to load firmware from userspace, skipping "
5359 "check. (%d)\n", ret);
5360 return 0;
5361 }
5362 ha8_fw = (const struct ha80000_firmware *)fw->data;
5363
5364 if ((fw->size != sizeof(*ha8_fw)) ||
5365 (0 != memcmp("DIGIUM", ha8_fw->header, sizeof(ha8_fw->header))) ||
5366 ((crc32(~0, (void *)ha8_fw, sizeof(*ha8_fw) - sizeof(u32)) ^ ~0) !=
5367 le32_to_cpu(ha8_fw->chksum))) {
5368 dev_warn(dev, "Firmware file is invalid. Skipping load.\n");
5369 ret = 0;
5370 goto cleanup;
5371 }
5372
5373 dev_dbg(dev, "FIRMWARE: %02x.%02x\n", ha8_fw->major_ver, ha8_fw->minor_ver);
5374
5375 if (ha8_fw->major_ver == major &&
5376 ha8_fw->minor_ver == minor) {
5377 dev_dbg(dev, "Firmware versions match, skipping load.\n");
5378 ret = 0;
5379 goto cleanup;
5380 }
5381
5382 if (2 == major) {
5383 hx8_switch_to_bootloader(wc);
5384 ret = hx8_get_fpga_version(wc, &major, &minor);
5385 if (ret)
5386 goto cleanup;
5387 }
5388
5389 /* so now we're in boot loader mode, ready to load the new firmware. */
5390 ret = hx8_reload_application(wc, ha8_fw);
5391 if (ret)
5392 goto cleanup;
5393
5394 dev_dbg(dev, "Firmware reloaded. Booting into application.\n");
5395
5396 hx8_switch_to_application(wc);
5397 ret = hx8_get_fpga_version(wc, &major, &minor);
5398 if (ret)
5399 goto cleanup;
5400
5401 dev_dbg(dev, "FPGA VERSION AFTER LOAD: %02x.%02x\n", major, minor);
5402
5403 if (forceload) {
5404 dev_warn(dev, "Please unset forceload if your card is able to "
5405 "detect the installed modules.\n");
5406 }
5407
5408 cleanup:
5409 release_firmware(fw);
5410 dev_info(dev, "Hx8 firmware version: %d.%02d\n", major, minor);
5411 return ret;
5412 }
5413
5414 #ifdef CONFIG_VOICEBUS_SYSFS
5415 static ssize_t
5416 voicebus_current_latency_show(struct device *dev,
5417 struct device_attribute *attr, char *buf)
5418 {
5419 unsigned long flags;
5420 struct wctdm *wc = dev_get_drvdata(dev);
5421 unsigned int current_latency;
5422 spin_lock_irqsave(&wc->vb.lock, flags);
5423 current_latency = wc->vb.min_tx_buffer_count;
5424 spin_unlock_irqrestore(&wc->vb.lock, flags);
5425 return sprintf(buf, "%d\n", current_latency);
5426 }
5427
5428 static DEVICE_ATTR(voicebus_current_latency, 0400,
5429 voicebus_current_latency_show, NULL);
5430
5431 static ssize_t vpm_firmware_version_show(struct device *dev,
5432 struct device_attribute *attr,
5433 char *buf)
5434 {
5435 int res;
5436 u16 version = 0;
5437 struct wctdm *wc = dev_get_drvdata(dev);
5438
5439 if (wc->vpmadt032) {
5440 res = gpakPingDsp(wc->vpmadt032->dspid, &version);
5441 if (res) {
5442 dev_info(&wc->vb.pdev->dev, "Failed gpakPingDsp %d\n", res);
5443 version = -1;
5444 }
5445 }
5446
5447 return sprintf(buf, "%x.%02x\n", (version & 0xff00) >> 8, (version & 0xff));
5448 }
5449
5450 static DEVICE_ATTR(vpm_firmware_version, 0400,
5451 vpm_firmware_version_show, NULL);
5452 static ssize_t
5453 enable_vpm_show(struct device *dev,
5454 struct device_attribute *attr, char *buf)
5455 {
5456 unsigned long flags;
5457 struct wctdm *wc = dev_get_drvdata(dev);
5458 unsigned int enable_vpm;
5459 spin_lock_irqsave(&wc->reglock, flags);
5460 enable_vpm = (wc->ctlreg & 0x10) != 0;
5461 spin_unlock_irqrestore(&wc->reglock, flags);
5462 return sprintf(buf, "%d\n", enable_vpm);
5463 }
5464
5465 static ssize_t
5466 enable_vpm_store(struct device *dev, struct device_attribute *attr,
5467 const char *buf, size_t count)
5468 {
5469 unsigned long flags;
5470 struct wctdm *wc = dev_get_drvdata(dev);
5471 unsigned int enable_vpm;
5472 if (count < 2)
5473 return -EINVAL;
5474
5475 if (('0' == buf[0]) || (0 == buf[0]))
5476 enable_vpm = 0;
5477 else
5478 enable_vpm = 1;
5479
5480 spin_lock_irqsave(&wc->reglock, flags);
5481 if (enable_vpm)
5482 wc->ctlreg |= 0x10;
5483 else
5484 wc->ctlreg &= ~0x10;
5485 spin_unlock_irqrestore(&wc->reglock, flags);
5486 return count;
5487 }
5488
5489 static DEVICE_ATTR(enable_vpm, 0644,
5490 enable_vpm_show, enable_vpm_store);
5491
5492 static void create_sysfs_files(struct wctdm *wc)
5493 {
5494 int ret;
5495 ret = device_create_file(&wc->vb.pdev->dev,
5496 &dev_attr_voicebus_current_latency);
5497 if (ret) {
5498 dev_info(&wc->vb.pdev->dev,
5499 "Failed to create device attributes.\n");
5500 }
5501
5502 ret = device_create_file(&wc->vb.pdev->dev,
5503 &dev_attr_vpm_firmware_version);
5504 if (ret) {
5505 dev_info(&wc->vb.pdev->dev,
5506 "Failed to create device attributes.\n");
5507 }
5508
5509 ret = device_create_file(&wc->vb.pdev->dev,
5510 &dev_attr_enable_vpm);
5511 if (ret) {
5512 dev_info(&wc->vb.pdev->dev,
5513 "Failed to create device attributes.\n");
5514 }
5515 }
5516
5517 static void remove_sysfs_files(struct wctdm *wc)
5518 {
5519 device_remove_file(&wc->vb.pdev->dev,
5520 &dev_attr_enable_vpm);
5521
5522 device_remove_file(&wc->vb.pdev->dev,
5523 &dev_attr_vpm_firmware_version);
5524
5525 device_remove_file(&wc->vb.pdev->dev,
5526 &dev_attr_voicebus_current_latency);
5527 }
5528
5529 #else
5530
5531 static inline void create_sysfs_files(struct wctdm *wc) { return; }
5532 static inline void remove_sysfs_files(struct wctdm *wc) { return; }
5533
5534 #endif /* CONFIG_VOICEBUS_SYSFS */
5535
5536 static void wctdm_set_tdm410_leds(struct wctdm *wc)
5537 {
5538 int i;
5539
5540 if (4 != wc->desc->ports)
5541 return;
5542
5543 wc->tdm410leds = 0; /* all on by default */
5544 for (i = 0; i < wc->desc->ports; ++i) {
5545 /* Turn off the LED for any module that isn't installed. */
5546 if (NONE == wc->mods[i].type)
5547 wc->tdm410leds |= (1 << i);
5548 }
5549 }
5550
5551 /**
5552 * wctdm_allocate_irq_commands - Preallocate some commands for use in interrupt context.
5553 * @wc: The board which we're allocating for.
5554 * @count: The number of IRQ commands to allocate.
5555 *
5556 * We need a minimum of 4 * the current latency worth of commands for each
5557 * analog module. When the latency grows, new commands will be allocated, but
5558 * this just represents are best guess as to the number of commands we'll need
5559 * after probing for modules, and reduces the chance that we'll allocate
5560 * memory in interrupt context when the driver first loads.
5561 *
5562 */
5563 static void wctdm_allocate_irq_commands(struct wctdm *wc, unsigned int count)
5564 {
5565 unsigned long flags;
5566 LIST_HEAD(local_list);
5567
5568 if (!count)
5569 return;
5570
5571 while (count--) {
5572 struct wctdm_cmd *cmd;
5573 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
5574 if (!cmd)
5575 break;
5576 list_add(&cmd->node, &local_list);
5577 }
5578
5579 spin_lock_irqsave(&wc->reglock, flags);
5580 list_splice(&local_list, &wc->free_isr_commands);
5581 spin_unlock_irqrestore(&wc->reglock, flags);
5582 }
5583
5584 #ifdef USE_ASYNC_INIT
5585 struct async_data {
5586 struct pci_dev *pdev;
5587 const struct pci_device_id *ent;
5588 };
5589 static int __devinit
5590 __wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent,
5591 async_cookie_t cookie)
5592 #else
5593 static int __devinit
5594 __wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5595 #endif
5596 {
5597 struct wctdm *wc;
5598 unsigned int pos;
5599 int i, ret;
5600 struct bg *vpm_work;
5601 int anamods, digimods, curchan, curspan;
5602
5603 neonmwi_offlimit_cycles = neonmwi_offlimit / MS_PER_HOOKCHECK;
5604
5605 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
5606 if (!wc)
5607 return -ENOMEM;
5608
5609 wc->not_ready = 1;
5610
5611 down(&ifacelock);
5612 /* \todo this is a candidate for removal... */
5613 for (pos = 0; pos < WC_MAX_IFACES; ++pos) {
5614 if (!ifaces[pos]) {
5615 ifaces[pos] = wc;
5616 break;
5617 }
5618 }
5619 up(&ifacelock);
5620
5621 wc->desc = (struct wctdm_desc *)ent->driver_data;
5622
5623 /* This is to insure that the analog span is given lowest priority */
5624 sema_init(&wc->syncsem, 1);
5625 INIT_LIST_HEAD(&wc->frame_list);
5626 spin_lock_init(&wc->frame_list_lock);
5627 init_waitqueue_head(&wc->regq);
5628 spin_lock_init(&wc->reglock);
5629 INIT_LIST_HEAD(&wc->free_isr_commands);
5630 wc->oldsync = -1;
5631
5632 wc->board_name = kasprintf(GFP_KERNEL, "%s%d", wctdm_driver.name, pos);
5633 if (!wc->board_name) {
5634 wctdm_back_out_gracefully(wc);
5635 return -ENOMEM;
5636 }
5637
5638 #ifdef CONFIG_VOICEBUS_ECREFERENCE
5639 for (i = 0; i < ARRAY_SIZE(wc->ec_reference); ++i) {
5640 /* 256 is the smallest power of 2 that will contains the
5641 * maximum possible amount of latency. */
5642 wc->ec_reference[i] = dahdi_fifo_alloc(256, GFP_KERNEL);
5643
5644 if (IS_ERR(wc->ec_reference[i])) {
5645 ret = PTR_ERR(wc->ec_reference[i]);
5646 wc->ec_reference[i] = NULL;
5647 wctdm_back_out_gracefully(wc);
5648 return ret;
5649 }
5650 }
5651 #endif
5652
5653 pci_set_drvdata(pdev, wc);
5654 wc->vb.ops = &voicebus_operations;
5655 wc->vb.pdev = pdev;
5656 wc->vb.debug = &debug;
5657
5658 #ifdef CONFIG_VOICEBUS_DISABLE_ASPM
5659 if (is_pcie(wc)) {
5660 pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
5661 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
5662 };
5663 #endif
5664
5665 if (is_hx8(wc)) {
5666 wc->vb.ops = &hx8_voicebus_operations;
5667 ret = voicebus_boot_init(&wc->vb, wc->board_name);
5668 } else {
5669 wc->vb.ops = &voicebus_operations;
5670 ret = voicebus_init(&wc->vb, wc->board_name);
5671 voicebus_set_minlatency(&wc->vb, latency);
5672 voicebus_set_maxlatency(&wc->vb, max_latency);
5673 }
5674
5675 if (ret) {
5676 wctdm_back_out_gracefully(wc);
5677 return ret;
5678 }
5679
5680 create_sysfs_files(wc);
5681
5682 voicebus_lock_latency(&wc->vb);
5683
5684 wc->mods_per_board = NUM_MODULES;
5685 wc->txident = 1;
5686
5687 if (alawoverride) {
5688 companding = "alaw";
5689 dev_info(&wc->vb.pdev->dev, "The module parameter alawoverride"\
5690 " has been deprecated. Please use the "\
5691 "parameter companding=alaw instead");
5692 }
5693
5694 if (!strcasecmp(companding, "alaw"))
5695 /* Force this card's companding to alaw */
5696 wc->companding = DAHDI_LAW_ALAW;
5697 else if (!strcasecmp(companding, "ulaw"))
5698 /* Force this card's companding to ulaw */
5699 wc->companding = DAHDI_LAW_MULAW;
5700 else
5701 /* Auto detect this card's companding */
5702 wc->companding = DAHDI_LAW_DEFAULT;
5703
5704 for (i = 0; i < ARRAY_SIZE(wc->mods); i++) {
5705 struct wctdm_module *const mod = &wc->mods[i];
5706 INIT_LIST_HEAD(&mod->pending_cmds);
5707 INIT_LIST_HEAD(&mod->active_cmds);
5708 mod->dacssrc = -1;
5709 mod->card = i;
5710 set_offsets(mod, 0);
5711 }
5712
5713 /* Start the hardware processing. */
5714 if (voicebus_start(&wc->vb)) {
5715 BUG_ON(1);
5716 }
5717
5718 if (is_hx8(wc)) {
5719 ret = hx8_check_firmware(wc);
5720 if (ret) {
5721 wctdm_back_out_gracefully(wc);
5722 return -EIO;
5723 }
5724
5725 /* Switch to the normal operating mode for this card. */
5726 voicebus_stop(&wc->vb);
5727 wc->vb.ops = &voicebus_operations;
5728 voicebus_set_minlatency(&wc->vb, latency);
5729 voicebus_set_maxlatency(&wc->vb, max_latency);
5730 voicebus_set_hx8_mode(&wc->vb);
5731 if (voicebus_start(&wc->vb))
5732 BUG_ON(1);
5733 }
5734
5735 /* first we have to make sure that we process all module data, we'll fine-tune it later in this routine. */
5736 wc->avchannels = NUM_MODULES;
5737
5738 vpm_work = bg_create(wc, wctdm_initialize_vpm, 0);
5739 if (!vpm_work) {
5740 wctdm_back_out_gracefully(wc);
5741 return -ENOMEM;
5742 }
5743
5744 /* Now track down what modules are installed */
5745 wctdm_identify_modules(wc);
5746
5747 wctdm_set_tdm410_leds(wc);
5748
5749 if (fatal_signal_pending(current)) {
5750 wctdm_back_out_gracefully(wc);
5751 bg_join(vpm_work);
5752 return -EINTR;
5753 }
5754
5755 /* We need to wait for the vpm thread to finish before we setup the
5756 * spans in order to ensure they are named properly. */
5757 bg_join(vpm_work);
5758
5759 /*
5760 * Walk the module list and create a 3-channel span for every BRI module found.
5761 * Empty and analog modules get a common span which is allocated outside of this loop.
5762 */
5763 anamods = digimods = 0;
5764 curchan = curspan = 0;
5765
5766 for (i = 0; i < wc->mods_per_board; i++) {
5767 struct wctdm_module *const mod = &wc->mods[i];
5768 struct b400m *b4;
5769
5770 if (mod->type == NONE) {
5771 ++curspan;
5772 continue;
5773 } else if (mod->type == BRI) {
5774 if (!is_hx8(wc)) {
5775 dev_info(&wc->vb.pdev->dev, "Digital modules "
5776 "detected on a non-hybrid card. "
5777 "This is unsupported.\n");
5778 wctdm_back_out_gracefully(wc);
5779 bg_join(vpm_work);
5780 return -EIO;
5781 }
5782 wc->spans[curspan] = wctdm_init_span(wc, curspan,
5783 curchan, 3, 1,
5784 pos);
5785 if (!wc->spans[curspan]) {
5786 wctdm_back_out_gracefully(wc);
5787 bg_join(vpm_work);
5788 return -EIO;
5789 }
5790 b4 = mod->mod.bri;
5791 b400m_set_dahdi_span(b4, i & 0x03, wc->spans[curspan]);
5792
5793 ++curspan;
5794 curchan += 3;
5795 if (!(i & 0x03)) {
5796 b400m_post_init(b4);
5797 ++digimods;
5798 }
5799 } else {
5800 /*
5801 * FIXME: ABK:
5802 * create a wctdm_chan for every analog module and link them into a span of their own down below.
5803 * then evaluate all of the callbacks and hard-code whether they are receiving a dahdi_chan or wctdm_chan *.
5804 * Finally, move the union from the wctdm structure to the dahdi_chan structure, and we should have something
5805 * resembling a clean dynamic # of channels/dynamic # of spans driver.
5806 */
5807 ++curspan;
5808 ++anamods;
5809 }
5810
5811 if (digimods > 2) {
5812 dev_info(&wc->vb.pdev->dev, "More than two digital modules detected. This is unsupported.\n");
5813 wctdm_back_out_gracefully(wc);
5814 return -EIO;
5815 }
5816 }
5817
5818 wc->digi_mods = digimods;
5819
5820 /* create an analog span if there are analog modules, or if there are no digital ones. */
5821 if (anamods || !digimods) {
5822 if (!digimods) {
5823 curspan = 0;
5824 }
5825 wctdm_init_span(wc, curspan, curchan, wc->desc->ports, 0, pos);
5826 wctdm_fixup_analog_span(wc, curspan);
5827 wc->aspan = wc->spans[curspan];
5828 curchan += wc->desc->ports;
5829 ++curspan;
5830 }
5831
5832 /* Now fix up the timeslots for the analog modules, since the digital
5833 * modules are always first */
5834 for (i = 0; i < wc->mods_per_board; i++) {
5835 struct wctdm_module *const mod = &wc->mods[i];
5836 switch (mod->type) {
5837 case FXS:
5838 wctdm_proslic_set_ts(wc, mod, (digimods * 12) + i);
5839 break;
5840 case FXO:
5841 wctdm_voicedaa_set_ts(wc, mod, (digimods * 12) + i);
5842 break;
5843 case QRV:
5844 wctdm_qrvdri_set_ts(wc, mod, (digimods * 12) + i);
5845 break;
5846 default:
5847 break;
5848 }
5849 }
5850
5851 /* This shouldn't ever occur, but if we don't try to trap it, the driver
5852 * will be scribbling into memory it doesn't own. */
5853 BUG_ON(curchan > 24);
5854
5855 wc->avchannels = curchan;
5856
5857 #ifdef USE_ASYNC_INIT
5858 async_synchronize_cookie(cookie);
5859 #endif
5860 wc->ddev = dahdi_create_device();
5861 wc->ddev->manufacturer = "Digium";
5862 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI%s Bus %02d Slot %02d",
5863 (wc->desc->flags & FLAG_EXPRESS) ?
5864 " Express" : "",
5865 pdev->bus->number,
5866 PCI_SLOT(pdev->devfn) + 1);
5867 if (!wc->ddev->location) {
5868 wctdm_back_out_gracefully(wc);
5869 return -ENOMEM;
5870 }
5871
5872 if (wc->vpmadt032) {
5873 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (VPMADT032)",
5874 wc->desc->name);
5875 } else if (wc->vpmoct) {
5876 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (VPMOCT032)",
5877 wc->desc->name);
5878 } else {
5879 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s",
5880 wc->desc->name);
5881 }
5882
5883 if (!wc->ddev->devicetype) {
5884 wctdm_back_out_gracefully(wc);
5885 return -ENOMEM;
5886 }
5887
5888 /* We should be ready for DAHDI to come in now. */
5889 for (i = 0; i < MAX_SPANS; ++i) {
5890 struct dahdi_span *span;
5891
5892 if (!wc->spans[i])
5893 continue;
5894
5895 span = &wc->spans[i]->span;
5896
5897 list_add_tail(&span->device_node, &wc->ddev->spans);
5898 }
5899
5900 wctdm_allocate_irq_commands(wc, anamods * latency * 4);
5901
5902 if (dahdi_register_device(wc->ddev, &wc->vb.pdev->dev)) {
5903 dev_notice(&wc->vb.pdev->dev, "Unable to register device with DAHDI\n");
5904 wctdm_back_out_gracefully(wc);
5905 return -1;
5906 }
5907
5908 WARN_ON(wc->not_ready <= 0);
5909 --wc->not_ready;
5910
5911 dev_info(&wc->vb.pdev->dev,
5912 "Found a %s: %s (%d BRI spans, %d analog %s)\n",
5913 (is_hx8(wc)) ? "Hybrid card" : "Wildcard TDM",
5914 wc->desc->name, digimods*4, anamods,
5915 (anamods == 1) ? "channel" : "channels");
5916 ret = 0;
5917
5918 voicebus_unlock_latency(&wc->vb);
5919 return 0;
5920 }
5921
5922 #ifdef USE_ASYNC_INIT
5923 static __devinit void
5924 wctdm_init_one_async(void *data, async_cookie_t cookie)
5925 {
5926 struct async_data *dat = data;
5927 __wctdm_init_one(dat->pdev, dat->ent, cookie);
5928 kfree(dat);
5929 }
5930
5931 static int __devinit
5932 wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5933 {
5934 struct async_data *dat;
5935
5936 dat = kmalloc(sizeof(*dat), GFP_KERNEL);
5937 /* If we can't allocate the memory for the async_data, odds are we won't
5938 * be able to initialize the device either, but let's try synchronously
5939 * anyway... */
5940 if (!dat)
5941 return __wctdm_init_one(pdev, ent, 0);
5942
5943 dat->pdev = pdev;
5944 dat->ent = ent;
5945 async_schedule(wctdm_init_one_async, dat);
5946 return 0;
5947 }
5948 #else
5949 static int __devinit
5950 wctdm_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
5951 {
5952 return __wctdm_init_one(pdev, ent);
5953 }
5954 #endif
5955
5956
5957 static void wctdm_release(struct wctdm *wc)
5958 {
5959 int i;
5960
5961 if (is_initialized(wc))
5962 dahdi_unregister_device(wc->ddev);
5963
5964 down(&ifacelock);
5965 for (i = 0; i < WC_MAX_IFACES; i++)
5966 if (ifaces[i] == wc)
5967 break;
5968 ifaces[i] = NULL;
5969 up(&ifacelock);
5970
5971 wctdm_back_out_gracefully(wc);
5972 }
5973
5974 static void __devexit wctdm_remove_one(struct pci_dev *pdev)
5975 {
5976 int i;
5977 unsigned long flags;
5978 struct wctdm *wc = pci_get_drvdata(pdev);
5979 struct vpmadt032 *vpmadt032;
5980 struct vpmoct *vpmoct;
5981
5982 if (!wc)
5983 return;
5984
5985 vpmadt032 = wc->vpmadt032;
5986 vpmoct = wc->vpmoct;
5987
5988 remove_sysfs_files(wc);
5989
5990 if (vpmadt032) {
5991 flush_workqueue(vpmadt032->wq);
5992 clear_bit(VPM150M_ACTIVE, &vpmadt032->control);
5993 flush_workqueue(vpmadt032->wq);
5994 } else if (vpmoct) {
5995 while (wctdm_wait_for_ready(wc))
5996 schedule();
5997 }
5998
5999 flush_scheduled_work();
6000
6001 /* shut down any BRI modules */
6002 for (i = 0; i < wc->mods_per_board; i += 4) {
6003 if (wc->mods[i].type == BRI)
6004 wctdm_unload_b400m(wc, i);
6005 }
6006
6007 voicebus_stop(&wc->vb);
6008
6009 if (vpmadt032) {
6010 spin_lock_irqsave(&wc->reglock, flags);
6011 wc->vpmadt032 = NULL;
6012 spin_unlock_irqrestore(&wc->reglock, flags);
6013 vpmadt032_free(vpmadt032);
6014 } else if (vpmoct) {
6015 spin_lock_irqsave(&wc->reglock, flags);
6016 wc->vpmoct = NULL;
6017 spin_unlock_irqrestore(&wc->reglock, flags);
6018 vpmoct_free(vpmoct);
6019 }
6020
6021 dev_info(&wc->vb.pdev->dev, "Freed a %s\n",
6022 (is_hx8(wc)) ? "Hybrid card" : "Wildcard");
6023
6024 /* Release span */
6025 wctdm_release(wc);
6026 }
6027
6028 static DEFINE_PCI_DEVICE_TABLE(wctdm_pci_tbl) = {
6029 { 0xd161, 0x2400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm2400 },
6030 { 0xd161, 0x0800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm800 },
6031 { 0xd161, 0x8002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex800 },
6032 { 0xd161, 0x8003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex2400 },
6033 { 0xd161, 0x8005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wctdm410 },
6034 { 0xd161, 0x8006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcaex410 },
6035 { 0xd161, 0x8007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcha80000 },
6036 { 0xd161, 0x8008, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wchb80000 },
6037 { 0 }
6038 };
6039
6040 static void wctdm_shutdown(struct pci_dev *pdev)
6041 {
6042 struct wctdm *wc = pci_get_drvdata(pdev);
6043 voicebus_quiesce(&wc->vb);
6044 }
6045
6046 MODULE_DEVICE_TABLE(pci, wctdm_pci_tbl);
6047
6048 static int wctdm_suspend(struct pci_dev *pdev, pm_message_t state)
6049 {
6050 return -ENOSYS;
6051 }
6052
6053 static struct pci_driver wctdm_driver = {
6054 .name = "wctdm24xxp",
6055 .probe = wctdm_init_one,
6056 .remove = __devexit_p(wctdm_remove_one),
6057 .shutdown = wctdm_shutdown,
6058 .suspend = wctdm_suspend,
6059 .id_table = wctdm_pci_tbl,
6060 };
6061
6062 static int __init wctdm_init(void)
6063 {
6064 int res;
6065 int x;
6066
6067 for (x = 0; x < ARRAY_SIZE(fxo_modes); x++) {
6068 if (!strcmp(fxo_modes[x].name, opermode))
6069 break;
6070 }
6071 if (x < ARRAY_SIZE(fxo_modes)) {
6072 _opermode = x;
6073 } else {
6074 printk(KERN_NOTICE "Invalid/unknown operating mode '%s' "
6075 "specified. Please choose one of:\n", opermode);
6076 for (x = 0; x < ARRAY_SIZE(fxo_modes); x++)
6077 printk(KERN_CONT " %s\n", fxo_modes[x].name);
6078 printk(KERN_NOTICE "Note this option is CASE SENSITIVE!\n");
6079 return -ENODEV;
6080 }
6081
6082 if (!strcmp(opermode, "AUSTRALIA")) {
6083 boostringer = 1;
6084 fxshonormode = 1;
6085 }
6086
6087 if (-1 == fastpickup) {
6088 if (!strcmp(opermode, "JAPAN"))
6089 fastpickup = 1;
6090 else
6091 fastpickup = 0;
6092 }
6093
6094 /* for the voicedaa_check_hook defaults, if the user has not overridden
6095 them by specifying them as module parameters, then get the values
6096 from the selected operating mode
6097 */
6098 if (battdebounce == 0) {
6099 battdebounce = fxo_modes[_opermode].battdebounce;
6100 }
6101 if (battalarm == 0) {
6102 battalarm = fxo_modes[_opermode].battalarm;
6103 }
6104 if (battthresh == 0) {
6105 battthresh = fxo_modes[_opermode].battthresh;
6106 }
6107
6108 b400m_module_init();
6109
6110 res = pci_register_driver(&wctdm_driver);
6111 if (res)
6112 return -ENODEV;
6113
6114 #ifdef USE_ASYNC_INIT
6115 async_synchronize_full();
6116 #endif
6117 return 0;
6118 }
6119
6120 static void __exit wctdm_cleanup(void)
6121 {
6122 pci_unregister_driver(&wctdm_driver);
6123 }
6124
6125
6126 module_param(debug, int, 0600);
6127 module_param(fastpickup, int, 0400);
6128 MODULE_PARM_DESC(fastpickup,
6129 "Set to 1 to shorten the calibration delay when taking " \
6130 "an FXO port off hook. This can be required for Type-II " \
6131 "CID. If -1 the calibration delay will depend on the " \
6132 "current opermode.\n");
6133 module_param(fxovoltage, int, 0600);
6134 module_param(loopcurrent, int, 0600);
6135 module_param(reversepolarity, int, 0600);
6136 #ifdef DEBUG
6137 module_param(robust, int, 0600);
6138 module_param(digitalloopback, int, 0400);
6139 MODULE_PARM_DESC(digitalloopback, "Set to 1 to place FXO modules into " \
6140 "loopback mode for troubleshooting.");
6141 #endif
6142 module_param(opermode, charp, 0600);
6143 module_param(lowpower, int, 0600);
6144 module_param(boostringer, int, 0600);
6145 module_param(fastringer, int, 0600);
6146 module_param(fxshonormode, int, 0600);
6147 module_param(battdebounce, uint, 0600);
6148 module_param(battalarm, uint, 0600);
6149 module_param(battthresh, uint, 0600);
6150 module_param(nativebridge, int, 0600);
6151 module_param(fxotxgain, int, 0600);
6152 module_param(fxorxgain, int, 0600);
6153 module_param(fxstxgain, int, 0600);
6154 module_param(fxsrxgain, int, 0600);
6155 module_param(ringdebounce, int, 0600);
6156 module_param(latency, int, 0400);
6157 module_param(max_latency, int, 0400);
6158 module_param(neonmwi_monitor, int, 0600);
6159 module_param(neonmwi_level, int, 0600);
6160 module_param(neonmwi_envelope, int, 0600);
6161 module_param(neonmwi_offlimit, int, 0600);
6162 #ifdef VPM_SUPPORT
6163 module_param(vpmsupport, int, 0400);
6164 module_param(vpmnlptype, int, 0400);
6165 module_param(vpmnlpthresh, int, 0400);
6166 module_param(vpmnlpmaxsupp, int, 0400);
6167 #endif
6168
6169 /* Module parameters backed by code in xhfc.c */
6170 module_param(bri_debug, int, 0600);
6171 MODULE_PARM_DESC(bri_debug, "bitmap: 1=general 2=dtmf 4=regops 8=fops 16=ec 32=st state 64=hdlc 128=alarm");
6172
6173 module_param(bri_spanfilter, int, 0600);
6174 MODULE_PARM_DESC(bri_spanfilter, "debug filter for spans. bitmap: 1=port 1, 2=port 2, 4=port 3, 8=port 4");
6175
6176 module_param(bri_alarmdebounce, int, 0600);
6177 MODULE_PARM_DESC(bri_alarmdebounce, "msec to wait before set/clear alarm condition");
6178
6179 module_param(bri_teignorered, int, 0600);
6180 MODULE_PARM_DESC(bri_teignorered, "1=ignore (do not inform DAHDI) if a red alarm exists in TE mode");
6181
6182 module_param(bri_persistentlayer1, int, 0600);
6183 MODULE_PARM_DESC(bri_persistentlayer1, "Set to 0 for disabling automatic layer 1 reactivation (when other end deactivates it)");
6184
6185 module_param(timingcable, int, 0600);
6186 MODULE_PARM_DESC(timingcable, "Set to 1 for enabling timing cable. This means that *all* cards in the system are linked together with a single timing cable");
6187
6188 module_param(forceload, int, 0600);
6189 MODULE_PARM_DESC(forceload, "Set to 1 in order to force an FPGA reload after power on (currently only for HA8/HB8 cards).");
6190
6191 module_param(alawoverride, int, 0400);
6192 MODULE_PARM_DESC(alawoverride, "This option has been deprecated. Please use "\
6193 "the parameter \"companding\" instead");
6194
6195 module_param(companding, charp, 0400);
6196 MODULE_PARM_DESC(companding, "Change the companding to \"auto\" or \"alaw\" " \
6197 "or \"ulaw\". Auto (default) will set everything to ulaw " \
6198 "unless a BRI module is installed. It will use alaw in that "
6199 "case.");
6200
6201 MODULE_DESCRIPTION("VoiceBus Driver for Wildcard Analog and Hybrid Cards");
6202 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
6203 MODULE_ALIAS("wctdm8xxp");
6204 MODULE_ALIAS("wctdm4xxp");
6205 MODULE_ALIAS("wcaex24xx");
6206 MODULE_ALIAS("wcaex8xx");
6207 MODULE_ALIAS("wcaex4xx");
6208 MODULE_LICENSE("GPL v2");
6209
6210 module_init(wctdm_init);
6211 module_exit(wctdm_cleanup);