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