Debian Codegraph @

drivers/dahdi/wcaxx-base.c

raw
1 /*
2 * A4A,A4B,A8A,A8B TDM FXS/FXO Interface Driver for DAHDI Telephony interface
3 *
4 * Copyright (C) 2013 Digium, Inc.
5 * All rights reserved.
6 *
7 */
8
9 /*
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2 as published by the
18 * Free Software Foundation. See the LICENSE file included with
19 * this program for more details.
20 */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/version.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/sched.h>
31 #include <linux/workqueue.h>
32 #include <linux/delay.h>
33 #include <linux/moduleparam.h>
34 #include <linux/firmware.h>
35 #include <linux/crc32.h>
36
37 #include <stdbool.h>
38
39
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
41 /* Define this if you would like to load the modules in parallel. While this
42 * can speed up loads when multiple cards handled by this driver are installed,
43 * it also makes it impossible to abort module loads with ctrl-c */
44 #undef USE_ASYNC_INIT
45 #include <linux/async.h>
46 #else
47 #undef USE_ASYNC_INIT
48 #endif
49
50 #include <dahdi/kernel.h>
51 #include <dahdi/wctdm_user.h>
52
53 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
54 #include <linux/sched/signal.h>
55 #endif /* 4.11.0 */
56
57 #include "proslic.h"
58
59 #include <dahdi/kernel.h>
60
61 #include <linux/version.h>
62 #include <linux/mutex.h>
63
64 #include <oct612x.h>
65
66 #include "wcxb.h"
67 #include "wcxb_spi.h"
68 #include "wcxb_flash.h"
69
70 /*!
71 * \brief Default ringer debounce (in ms)
72 */
73 #define DEFAULT_RING_DEBOUNCE 1024
74 #define POLARITY_DEBOUNCE 64 /* Polarity debounce (in ms) */
75
76 #define OHT_TIMER 6000 /* How long after RING to retain OHT */
77
78 #define FLAG_EXPRESS (1 << 0)
79
80 #define NUM_MODULES 8
81
82 #define CMD_WR(addr, val) (((addr<<8)&0xff00) | (val&0xff))
83
84 enum battery_state {
85 BATTERY_UNKNOWN = 0,
86 BATTERY_DEBOUNCING_PRESENT,
87 BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM,
88 BATTERY_DEBOUNCING_PRESENT_ALARM,
89 BATTERY_PRESENT,
90 BATTERY_DEBOUNCING_LOST,
91 BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM,
92 BATTERY_DEBOUNCING_LOST_ALARM,
93 BATTERY_LOST,
94 };
95
96 enum ring_detector_state {
97 RINGOFF = 0,
98 DEBOUNCING_RINGING_POSITIVE,
99 DEBOUNCING_RINGING_NEGATIVE,
100 RINGING,
101 DEBOUNCING_RINGOFF,
102 };
103
104 enum polarity_state {
105 UNKNOWN_POLARITY = 0,
106 POLARITY_DEBOUNCE_POSITIVE,
107 POLARITY_POSITIVE,
108 POLARITY_DEBOUNCE_NEGATIVE,
109 POLARITY_NEGATIVE,
110 };
111
112 struct wcaxx_chan {
113 struct dahdi_chan chan;
114 struct dahdi_echocan_state ec;
115 int timeslot;
116 unsigned int hwpreec_enabled:1;
117 };
118
119 struct fxo {
120 enum ring_detector_state ring_state:4;
121 enum battery_state battery_state:4;
122 enum polarity_state polarity_state:4;
123 u8 ring_polarity_change_count:4;
124 u8 hook_ring_shadow;
125 s8 line_voltage_status;
126 int offhook;
127 int neonmwi_state;
128 int neonmwi_last_voltage;
129 unsigned int neonmwi_debounce;
130 unsigned int neonmwi_offcounter;
131 unsigned long display_fxovoltage;
132 unsigned long ringdebounce_timer;
133 unsigned long battdebounce_timer;
134 unsigned long poldebounce_timer;
135 };
136
137 struct fxs {
138 int idletxhookstate; /* IDLE changing hook state */
139 /* lasttxhook reflects the last value written to the proslic's reg 64
140 * (LINEFEED_CONTROL) in bits 0-2. Bit 4 indicates if the last write is
141 * pending i.e. it is in process of being written to the register NOTE:
142 * in order for this value to actually be written to the proslic, the
143 * appropriate matching value must be written into the sethook variable
144 * so that it gets queued and handled by the voicebus ISR.
145 */
146 u8 lasttxhook;
147 u8 linefeed_control_shadow;
148 u8 hook_state_shadow;
149 u8 oht_active:1;
150 u8 off_hook:1;
151 int palarms;
152 struct dahdi_vmwi_info vmwisetting;
153 int vmwi_active_messages;
154 int vmwi_linereverse;
155 int reversepolarity; /* polarity reversal */
156 struct {
157 u8 vals[12];
158 } calregs;
159 unsigned long check_alarm;
160 unsigned long check_proslic;
161 unsigned long oppending_timeout;
162 unsigned long ohttimer;
163 };
164
165 #define fxs_lf(fxs, value) _fxs_lf((fxs), SLIC_LF_##value)
166 static inline bool _fxs_lf(const struct fxs *fxs, const unsigned value)
167 {
168 return (fxs->lasttxhook & SLIC_LF_SETMASK) == value;
169 }
170
171 enum module_type {
172 NONE = 0,
173 FXS,
174 FXO,
175 };
176
177 #define MODULE_POLL_TIME_MS 10
178
179 struct wcaxx_mod_poll {
180 struct wcxb_spi_message m;
181 struct wcxb_spi_transfer t;
182 struct wcaxx_module *mod;
183 struct wcaxx *wc;
184 u8 buffer[6];
185 u8 master_buffer[6];
186 };
187
188 struct wcaxx_module {
189 union modtypes {
190 struct fxo fxo;
191 struct fxs fxs;
192 } mod;
193 u8 card;
194 u8 subaddr;
195 enum module_type type;
196 int sethook; /* pending hook state command */
197 int dacssrc;
198 struct wcxb_spi_device *spi;
199 struct wcaxx_mod_poll *mod_poll;
200 };
201
202 struct _device_desc {
203 const char *name;
204 unsigned int ports;
205 };
206
207 static const struct _device_desc device_a8a = { "Wildcard A8A", 8};
208 static const struct _device_desc device_a8b = { "Wildcard A8B", 8};
209 static const struct _device_desc device_a4a = { "Wildcard A4A", 4};
210 static const struct _device_desc device_a4b = { "Wildcard A4B", 4};
211
212 struct wcaxx {
213 const struct _device_desc *desc;
214 const char *board_name;
215
216 unsigned long framecount;
217 unsigned long module_poll_time;
218 int mods_per_board;
219
220 spinlock_t reglock;
221 struct wcaxx_module mods[NUM_MODULES];
222 struct wcxb xb;
223 struct dahdi_span span;
224 struct wcaxx_chan *chans[NUM_MODULES];
225 struct dahdi_echocan_state *ec[NUM_MODULES];
226 int companding;
227 struct dahdi_device *ddev;
228 struct wcxb_spi_master *master;
229 #define INITIALIZED 0
230 unsigned long bit_flags;
231 /* 4 SPI devices that are matched to the chip selects. The 4 port
232 * modules will share a single SPI device since they use the same chip
233 * select. */
234 struct wcxb_spi_device *spi_devices[4];
235 struct vpm450m *vpm;
236 struct list_head card_node;
237 u16 num;
238 };
239
240 static inline bool is_pcie(const struct wcaxx *wc)
241 {
242 return (wc->desc == &device_a8b) || (wc->desc == &device_a4b);
243 }
244
245 static inline bool is_four_port(const struct wcaxx *wc)
246 {
247 return (4 == wc->desc->ports);
248 }
249
250 #include <linux/slab.h>
251 #include <linux/vmalloc.h>
252 #include <linux/string.h>
253 #include <linux/time.h>
254 #include <linux/version.h>
255 #include <linux/firmware.h>
256 #include "oct6100api/oct6100_api.h"
257
258 #define ECHOCAN_NUM_CHANS 8
259
260 #define FLAG_DTMF (1 << 0)
261 #define FLAG_MUTE (1 << 1)
262 #define FLAG_ECHO (1 << 2)
263
264 #define OCT_CHIP_ID 0
265 #define OCT_MAX_TDM_STREAMS 4
266 #define OCT_TONEEVENT_BUFFER_SIZE 128
267 #define SOUT_STREAM 1
268 #define RIN_STREAM 0
269 #define SIN_STREAM 2
270
271 static int vpmsupport = 1;
272 static int wcaxx_vpm_init(struct wcaxx *wc);
273 static void echocan_free(struct dahdi_chan *chan,
274 struct dahdi_echocan_state *ec);
275 static const struct dahdi_echocan_features vpm_ec_features = {
276 .NLP_automatic = 1,
277 .CED_tx_detect = 1,
278 .CED_rx_detect = 1,
279 };
280
281 static const struct dahdi_echocan_ops vpm_ec_ops = {
282 .echocan_free = echocan_free,
283 };
284
285 struct vpm450m {
286 tPOCT6100_INSTANCE_API pApiInstance;
287 struct oct612x_context context;
288 UINT32 aulEchoChanHndl[32];
289 int chanflags[32];
290 int ecmode[32];
291 int numchans;
292 };
293
294 static int wcaxx_oct612x_write(struct oct612x_context *context,
295 u32 address, u16 value)
296 {
297 struct wcaxx *wc = dev_get_drvdata(context->dev);
298 wcxb_set_echocan_reg(&wc->xb, address, value);
299 return 0;
300 }
301
302 static int wcaxx_oct612x_read(struct oct612x_context *context, u32 address,
303 u16 *value)
304 {
305 struct wcaxx *wc = dev_get_drvdata(context->dev);
306 *value = wcxb_get_echocan_reg(&wc->xb, address);
307 return 0;
308 }
309
310 static int wcaxx_oct612x_write_smear(struct oct612x_context *context,
311 u32 address, u16 value, size_t count)
312 {
313 unsigned int i;
314 struct wcaxx *wc = dev_get_drvdata(context->dev);
315 for (i = 0; i < count; ++i)
316 wcxb_set_echocan_reg(&wc->xb, address + (i << 1), value);
317 return 0;
318 }
319
320 static int wcaxx_oct612x_write_burst(struct oct612x_context *context,
321 u32 address, const u16 *buffer,
322 size_t count)
323 {
324 unsigned int i;
325 struct wcaxx *wc = dev_get_drvdata(context->dev);
326 for (i = 0; i < count; ++i)
327 wcxb_set_echocan_reg(&wc->xb, address + (i << 1), buffer[i]);
328 return 0;
329 }
330
331 static int wcaxx_oct612x_read_burst(struct oct612x_context *context,
332 u32 address, u16 *buffer, size_t count)
333 {
334 unsigned int i;
335 struct wcaxx *wc = dev_get_drvdata(context->dev);
336 for (i = 0; i < count; ++i)
337 buffer[i] = wcxb_get_echocan_reg(&wc->xb, address + (i << 1));
338 return 0;
339 }
340
341 static const struct oct612x_ops wcaxx_oct612x_ops = {
342 .write = wcaxx_oct612x_write,
343 .read = wcaxx_oct612x_read,
344 .write_smear = wcaxx_oct612x_write_smear,
345 .write_burst = wcaxx_oct612x_write_burst,
346 .read_burst = wcaxx_oct612x_read_burst,
347 };
348
349
350 static void vpm450m_setecmode(struct vpm450m *vpm450m, int channel, int mode)
351 {
352 tOCT6100_CHANNEL_MODIFY *modify;
353 UINT32 ulResult;
354
355 if (vpm450m->ecmode[channel] == mode)
356 return;
357 modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
358 if (!modify) {
359 pr_notice("Unable to allocate memory for setec!\n");
360 return;
361 }
362 Oct6100ChannelModifyDef(modify);
363 modify->ulEchoOperationMode = mode;
364 modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
365 ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
366 if (ulResult != GENERIC_OK) {
367 pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
368 vpm450m->aulEchoChanHndl[channel], channel, ulResult);
369 } else {
370 #ifdef OCTASIC_DEBUG
371 pr_debug("Echo can on channel %d set to %d\n", channel, mode);
372 #endif
373 vpm450m->ecmode[channel] = mode;
374 }
375 kfree(modify);
376 }
377
378 static void vpm450m_setec(struct vpm450m *vpm450m, int channel, int eclen)
379 {
380 if (eclen) {
381 vpm450m->chanflags[channel] |= FLAG_ECHO;
382 vpm450m_setecmode(vpm450m, channel,
383 cOCT6100_ECHO_OP_MODE_NORMAL);
384 } else {
385 vpm450m->chanflags[channel] &= ~FLAG_ECHO;
386 if (vpm450m->chanflags[channel] & (FLAG_DTMF | FLAG_MUTE)) {
387 vpm450m_setecmode(vpm450m, channel,
388 cOCT6100_ECHO_OP_MODE_HT_RESET);
389 } else {
390 vpm450m_setecmode(vpm450m, channel,
391 cOCT6100_ECHO_OP_MODE_POWER_DOWN);
392 }
393 }
394 }
395
396 static UINT32 tdmmode_chan_to_slot_map(int mode, int channel)
397 {
398 /* Four phases on the tdm bus, skip three of them per channel */
399 /* Due to a bug in the octasic, we had to move the data onto phase 2 */
400 return 1+(channel*4);
401 }
402
403 static int echocan_initialize_channel(struct vpm450m *vpm,
404 int channel, int mode)
405 {
406 tOCT6100_CHANNEL_OPEN ChannelOpen;
407 UINT32 law_to_use = cOCT6100_PCM_U_LAW;
408 UINT32 tdmslot_setting;
409 UINT32 ulResult;
410
411 if (0 > channel || ECHOCAN_NUM_CHANS <= channel)
412 return -1;
413
414 tdmslot_setting = tdmmode_chan_to_slot_map(mode, channel);
415
416 /* Fill Open channel structure with defaults */
417 Oct6100ChannelOpenDef(&ChannelOpen);
418
419 /* Assign the handle memory.*/
420 ChannelOpen.pulChannelHndl = &vpm->aulEchoChanHndl[channel];
421 ChannelOpen.ulUserChanId = channel;
422 /* Enable Tone disabling for Fax and Modems */
423 ChannelOpen.fEnableToneDisabler = TRUE;
424
425 /* Passthrough TDM data by default, no echocan */
426 ChannelOpen.ulEchoOperationMode = cOCT6100_ECHO_OP_MODE_POWER_DOWN;
427
428 /* Configure the TDM settings.*/
429 /* Input from the framer */
430 ChannelOpen.TdmConfig.ulSinStream = SIN_STREAM;
431 ChannelOpen.TdmConfig.ulSinTimeslot = tdmslot_setting;
432 ChannelOpen.TdmConfig.ulSinPcmLaw = law_to_use;
433
434 /* Input from the Host (pre-framer) */
435 ChannelOpen.TdmConfig.ulRinStream = RIN_STREAM;
436 ChannelOpen.TdmConfig.ulRinTimeslot = tdmslot_setting;
437 ChannelOpen.TdmConfig.ulRinPcmLaw = law_to_use;
438
439 /* Output to the Host */
440 ChannelOpen.TdmConfig.ulSoutStream = SOUT_STREAM;
441 ChannelOpen.TdmConfig.ulSoutTimeslot = tdmslot_setting;
442 ChannelOpen.TdmConfig.ulSoutPcmLaw = law_to_use;
443
444 /* From asterisk after echo-cancellation - goes nowhere */
445 ChannelOpen.TdmConfig.ulRoutStream = cOCT6100_UNASSIGNED;
446 ChannelOpen.TdmConfig.ulRoutTimeslot = cOCT6100_UNASSIGNED;
447 ChannelOpen.TdmConfig.ulRoutPcmLaw = law_to_use;
448
449 /* Set the desired VQE features.*/
450 ChannelOpen.VqeConfig.fEnableNlp = TRUE;
451 ChannelOpen.VqeConfig.fRinDcOffsetRemoval = TRUE;
452 ChannelOpen.VqeConfig.fSinDcOffsetRemoval = TRUE;
453 ChannelOpen.VqeConfig.ulComfortNoiseMode =
454 cOCT6100_COMFORT_NOISE_NORMAL;
455
456 /* Open the channel.*/
457 ulResult = Oct6100ChannelOpen(vpm->pApiInstance, &ChannelOpen);
458
459 return ulResult;
460 }
461
462 static struct vpm450m *init_vpm450m(struct wcaxx *wc, int isalaw,
463 const struct firmware *firmware)
464 {
465 tOCT6100_CHIP_OPEN *ChipOpen;
466 tOCT6100_GET_INSTANCE_SIZE InstanceSize;
467 tOCT6100_CHANNEL_OPEN *ChannelOpen;
468 UINT32 ulResult;
469 struct vpm450m *vpm450m;
470 int x, i;
471
472 vpm450m = kzalloc(sizeof(struct vpm450m), GFP_KERNEL);
473 if (!vpm450m) {
474 dev_info(&wc->xb.pdev->dev, "Unable to allocate vpm450m struct\n");
475 return NULL;
476 }
477
478 vpm450m->context.dev = &wc->xb.pdev->dev;
479 vpm450m->context.ops = &wcaxx_oct612x_ops;
480
481 ChipOpen = kzalloc(sizeof(tOCT6100_CHIP_OPEN), GFP_KERNEL);
482 if (!ChipOpen) {
483 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChipOpen\n");
484 kfree(vpm450m);
485 return NULL;
486 }
487
488 ChannelOpen = kzalloc(sizeof(tOCT6100_CHANNEL_OPEN), GFP_KERNEL);
489 if (!ChannelOpen) {
490 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChannelOpen\n");
491 kfree(vpm450m);
492 kfree(ChipOpen);
493 return NULL;
494 }
495
496 for (x = 0; x < ARRAY_SIZE(vpm450m->ecmode); x++)
497 vpm450m->ecmode[x] = -1;
498
499 vpm450m->numchans = ECHOCAN_NUM_CHANS;
500 dev_info(&wc->xb.pdev->dev, "Echo cancellation for %d channels\n",
501 wc->desc->ports);
502
503 Oct6100ChipOpenDef(ChipOpen);
504 ChipOpen->pProcessContext = &vpm450m->context;
505
506 /* Change default parameters as needed */
507 /* upclk oscillator is at 33.33 Mhz */
508 ChipOpen->ulUpclkFreq = cOCT6100_UPCLK_FREQ_33_33_MHZ;
509
510 /* mclk will be generated by internal PLL at 133 Mhz */
511 ChipOpen->fEnableMemClkOut = TRUE;
512 ChipOpen->ulMemClkFreq = cOCT6100_MCLK_FREQ_133_MHZ;
513
514 /* User defined Chip ID.*/
515 ChipOpen->ulUserChipId = OCT_CHIP_ID;
516
517 /* Set the maximums that the chip needs to support */
518 ChipOpen->ulMaxChannels = vpm450m->numchans;
519 ChipOpen->ulMaxTdmStreams = OCT_MAX_TDM_STREAMS;
520
521 /* External Memory Settings */
522 /* Use DDR memory.*/
523 ChipOpen->ulMemoryType = cOCT6100_MEM_TYPE_DDR;
524 ChipOpen->ulNumMemoryChips = 1;
525 ChipOpen->ulMemoryChipSize = cOCT6100_MEMORY_CHIP_SIZE_32MB;
526
527 ChipOpen->pbyImageFile = (PUINT8) firmware->data;
528 ChipOpen->ulImageSize = firmware->size;
529
530 /* Set TDM data stream frequency */
531 for (i = 0; i < ChipOpen->ulMaxTdmStreams; i++)
532 ChipOpen->aulTdmStreamFreqs[i] = cOCT6100_TDM_STREAM_FREQ_8MHZ;
533
534 /* Configure TDM sampling */
535 ChipOpen->ulTdmSampling = cOCT6100_TDM_SAMPLE_AT_FALLING_EDGE;
536 /* Disable to save RAM footprint space */
537 ChipOpen->fEnableChannelRecording = FALSE;
538
539 /* In this example we will maintain the API using polling so interrupts
540 * must be disabled */
541 ChipOpen->InterruptConfig.ulErrorH100Config =
542 cOCT6100_INTERRUPT_DISABLE;
543 ChipOpen->InterruptConfig.ulErrorMemoryConfig =
544 cOCT6100_INTERRUPT_DISABLE;
545 ChipOpen->InterruptConfig.ulFatalGeneralConfig =
546 cOCT6100_INTERRUPT_DISABLE;
547 ChipOpen->InterruptConfig.ulFatalMemoryConfig =
548 cOCT6100_INTERRUPT_DISABLE;
549
550 ChipOpen->ulSoftToneEventsBufSize = OCT_TONEEVENT_BUFFER_SIZE;
551
552 /* Inserting default values into tOCT6100_GET_INSTANCE_SIZE structure
553 * parameters. */
554 Oct6100GetInstanceSizeDef(&InstanceSize);
555
556 /* Get the size of the OCT6100 instance structure. */
557 ulResult = Oct6100GetInstanceSize(ChipOpen, &InstanceSize);
558 if (ulResult != cOCT6100_ERR_OK) {
559 dev_info(&wc->xb.pdev->dev, "Unable to get instance size: %x\n",
560 ulResult);
561 return NULL;
562 }
563
564 vpm450m->pApiInstance = vmalloc(InstanceSize.ulApiInstanceSize);
565 if (!vpm450m->pApiInstance) {
566 dev_info(&wc->xb.pdev->dev,
567 "Out of memory (can't allocate %d bytes)!\n",
568 InstanceSize.ulApiInstanceSize);
569 return NULL;
570 }
571
572 /* Perform the actual configuration of the chip. */
573 wcxb_enable_echocan_dram(&wc->xb);
574 ulResult = Oct6100ChipOpen(vpm450m->pApiInstance, ChipOpen);
575 if (ulResult != cOCT6100_ERR_OK) {
576 dev_info(&wc->xb.pdev->dev, "Unable to Oct6100ChipOpen: %x\n",
577 ulResult);
578 return NULL;
579 }
580
581 /* OCT6100 is now booted and channels can be opened */
582 /* Open all channels */
583 for (i = 0; i < ECHOCAN_NUM_CHANS; i++) {
584 ulResult = echocan_initialize_channel(vpm450m, i, isalaw);
585 if (0 != ulResult) {
586 dev_info(&wc->xb.pdev->dev,
587 "Unable to echocan_initialize_channel: %x\n",
588 ulResult);
589 return NULL;
590 }
591 }
592
593 if (vpmsupport)
594 wcxb_enable_echocan(&wc->xb);
595 else
596 wcxb_disable_echocan(&wc->xb);
597
598 kfree(ChipOpen);
599 kfree(ChannelOpen);
600 return vpm450m;
601 }
602
603 static void release_vpm450m(struct vpm450m *vpm450m)
604 {
605 UINT32 ulResult;
606 tOCT6100_CHIP_CLOSE ChipClose;
607
608 Oct6100ChipCloseDef(&ChipClose);
609 ulResult = Oct6100ChipClose(vpm450m->pApiInstance, &ChipClose);
610 if (ulResult != cOCT6100_ERR_OK)
611 pr_notice("Failed to close chip, code %08x!\n", ulResult);
612 vfree(vpm450m->pApiInstance);
613 kfree(vpm450m);
614 }
615
616 static const char *wcaxx_echocan_name(const struct dahdi_chan *chan)
617 {
618 struct wcaxx *wc = chan->pvt;
619 if (wc->vpm)
620 return "VPMOCT032";
621 else
622 return NULL;
623 }
624
625 static int wcaxx_echocan_create(struct dahdi_chan *chan,
626 struct dahdi_echocanparams *ecp,
627 struct dahdi_echocanparam *p,
628 struct dahdi_echocan_state **ec)
629 {
630 struct wcaxx *wc = chan->pvt;
631 int channel;
632 const struct dahdi_echocan_ops *ops;
633 const struct dahdi_echocan_features *features;
634
635 if (!vpmsupport || !wc->vpm)
636 return -ENODEV;
637
638 ops = &vpm_ec_ops;
639 features = &vpm_ec_features;
640
641 if (ecp->param_count > 0) {
642 dev_warn(&wc->xb.pdev->dev,
643 "%s echo canceller does not support parameters; failing request\n",
644 chan->ec_factory->get_name(chan));
645 return -EINVAL;
646 }
647
648 *ec = wc->ec[chan->chanpos - 1];
649 (*ec)->ops = ops;
650 (*ec)->features = *features;
651
652 channel = chan->chanpos-1;
653
654 if (wc->vpm)
655 vpm450m_setec(wc->vpm, channel, ecp->tap_length);
656 return 0;
657 }
658
659 static void echocan_free(struct dahdi_chan *chan,
660 struct dahdi_echocan_state *ec)
661 {
662 struct wcaxx *wc = chan->pvt;
663 int channel;
664
665 memset(ec, 0, sizeof(*ec));
666
667 channel = chan->chanpos - 1;
668
669 if (wc->vpm)
670 vpm450m_setec(wc->vpm, channel, 0);
671 }
672
673 static int wcaxx_vpm_init(struct wcaxx *wc)
674 {
675 int companding = 0;
676 struct firmware embedded_firmware;
677 const struct firmware *firmware = &embedded_firmware;
678 #if !defined(HOTPLUG_FIRMWARE)
679 extern void _binary_dahdi_fw_oct6114_032_bin_size;
680 extern u8 _binary_dahdi_fw_oct6114_032_bin_start[];
681 #else
682 static const char oct032_firmware[] = "dahdi-fw-oct6114-032.bin";
683 #endif
684 int res;
685
686 if (!vpmsupport) {
687 dev_info(&wc->xb.pdev->dev, "VPM: Support Disabled\n");
688 return -1;
689 }
690
691 wcxb_reset_echocan(&wc->xb);
692 if (!wcxb_is_echocan_present(&wc->xb)) {
693 dev_info(&wc->xb.pdev->dev, "VPM not present.\n");
694 return -1;
695 }
696
697 #if defined(HOTPLUG_FIRMWARE)
698 res = request_firmware(&firmware, oct032_firmware, &wc->xb.pdev->dev);
699 if ((0 != res) || !firmware) {
700 dev_notice(&wc->xb.pdev->dev,
701 "VPM450: firmware %s not available from userspace\n",
702 oct032_firmware);
703 return -1;
704 }
705 #else
706 embedded_firmware.data = _binary_dahdi_fw_oct6114_032_bin_start;
707 /* Yes... this is weird. objcopy gives us a symbol containing
708 the size of the firmware, not a pointer a variable containing
709 the size. The only way we can get the value of the symbol
710 is to take its address, so we define it as a pointer and
711 then cast that value to the proper type.
712 */
713 embedded_firmware.size = (size_t)&_binary_dahdi_fw_oct6114_032_bin_size;
714 #endif
715
716 wc->vpm = init_vpm450m(wc, companding, firmware);
717 if (!wc->vpm) {
718 dev_notice(&wc->xb.pdev->dev, "VPM450: Failed to initialize\n");
719 if (firmware != &embedded_firmware)
720 release_firmware(firmware);
721 return -EIO;
722 }
723
724 if (firmware != &embedded_firmware)
725 release_firmware(firmware);
726
727 dev_info(&wc->xb.pdev->dev,
728 "VPM450: Present and operational servicing %d span\n", 1);
729
730 return 0;
731 }
732
733 static inline bool is_initialized(struct wcaxx *wc)
734 {
735 return (test_bit(INITIALIZED, &wc->bit_flags) > 0);
736 }
737
738 static inline struct wcxb_spi_device *
739 _get_spi_device_for_8_port(struct wcaxx *wc, unsigned int port, bool altcs)
740 {
741 switch (port) {
742 case 0:
743 return wc->spi_devices[0];
744 case 1:
745 return (altcs) ? wc->spi_devices[0] : wc->spi_devices[1];
746 case 2:
747 WARN_ON(!altcs);
748 return wc->spi_devices[0];
749 case 3:
750 WARN_ON(!altcs);
751 return wc->spi_devices[0];
752 case 4:
753 return wc->spi_devices[2];
754 case 5:
755 return (altcs) ? wc->spi_devices[2] : wc->spi_devices[3];
756 case 6:
757 WARN_ON(!altcs);
758 return wc->spi_devices[2];
759 case 7:
760 WARN_ON(!altcs);
761 return wc->spi_devices[2];
762 default:
763 WARN_ON(1);
764 return wc->spi_devices[0];
765 }
766 }
767
768 static inline struct wcxb_spi_device *
769 _get_spi_device_for_4_port(struct wcaxx *wc, unsigned int port)
770 {
771 if (port > 3) {
772 WARN_ON(1);
773 return wc->spi_devices[0];
774 } else {
775 return wc->spi_devices[port];
776 }
777 }
778
779 static inline struct wcxb_spi_device *
780 get_spi_device_for_port(struct wcaxx *wc, unsigned int port, bool altcs)
781 {
782 if (is_four_port(wc))
783 return _get_spi_device_for_4_port(wc, port);
784 else
785 return _get_spi_device_for_8_port(wc, port, altcs);
786 }
787
788 static u8 wcaxx_getreg(struct wcaxx *wc,
789 struct wcaxx_module *const mod, int addr);
790 static void wcaxx_setreg(struct wcaxx *wc, struct wcaxx_module *const mod,
791 int addr, int val);
792
793 static DEFINE_MUTEX(card_list_lock);
794 static LIST_HEAD(card_list);
795
796 #include "adt_lec.h"
797
798 /*
799 Experimental max loop current limit for the proslic
800 Loop current limit is from 20 mA to 41 mA in steps of 3
801 (according to datasheet)
802 So set the value below to:
803 0x00 : 20mA (default)
804 0x01 : 23mA
805 0x02 : 26mA
806 0x03 : 29mA
807 0x04 : 32mA
808 0x05 : 35mA
809 0x06 : 37mA
810 0x07 : 41mA
811 */
812 static int loopcurrent = 20;
813
814 /* Following define is a logical exclusive OR to determine if the polarity of
815 * an fxs line is to be reversed. The items taken into account are:
816 * overall polarity reversal for the module,
817 * polarity reversal for the port,
818 * and the state of the line reversal MWI indicator
819 */
820 #define POLARITY_XOR(fxs) \
821 ((reversepolarity != 0) ^ ((fxs)->reversepolarity != 0) ^ \
822 ((fxs)->vmwi_linereverse != 0))
823
824 static int reversepolarity;
825
826 static alpha indirect_regs[] = {
827 {0, 255, "DTMF_ROW_0_PEAK", 0x55C2},
828 {1, 255, "DTMF_ROW_1_PEAK", 0x51E6},
829 {2, 255, "DTMF_ROW2_PEAK", 0x4B85},
830 {3, 255, "DTMF_ROW3_PEAK", 0x4937},
831 {4, 255, "DTMF_COL1_PEAK", 0x3333},
832 {5, 255, "DTMF_FWD_TWIST", 0x0202},
833 {6, 255, "DTMF_RVS_TWIST", 0x0202},
834 {7, 255, "DTMF_ROW_RATIO_TRES", 0x0198},
835 {8, 255, "DTMF_COL_RATIO_TRES", 0x0198},
836 {9, 255, "DTMF_ROW_2ND_ARM", 0x0611},
837 {10, 255, "DTMF_COL_2ND_ARM", 0x0202},
838 {11, 255, "DTMF_PWR_MIN_TRES", 0x00E5},
839 {12, 255, "DTMF_OT_LIM_TRES", 0x0A1C},
840 {13, 0, "OSC1_COEF", 0x7B30},
841 {14, 1, "OSC1X", 0x0063},
842 {15, 2, "OSC1Y", 0x0000},
843 {16, 3, "OSC2_COEF", 0x7870},
844 {17, 4, "OSC2X", 0x007D},
845 {18, 5, "OSC2Y", 0x0000},
846 {19, 6, "RING_V_OFF", 0x0000},
847 {20, 7, "RING_OSC", 0x7EF0},
848 {21, 8, "RING_X", 0x0160},
849 {22, 9, "RING_Y", 0x0000},
850 {23, 255, "PULSE_ENVEL", 0x2000},
851 {24, 255, "PULSE_X", 0x2000},
852 {25, 255, "PULSE_Y", 0x0000},
853 {26, 13, "RECV_DIGITAL_GAIN", 0x2000}, /* playback volume set lower */
854 {27, 14, "XMIT_DIGITAL_GAIN", 0x4000},
855 {28, 15, "LOOP_CLOSE_TRES", 0x1000},
856 {29, 16, "RING_TRIP_TRES", 0x3600},
857 {30, 17, "COMMON_MIN_TRES", 0x1000},
858 {31, 18, "COMMON_MAX_TRES", 0x0200},
859 {32, 19, "PWR_ALARM_Q1Q2", 0x07C0},
860 {33, 20, "PWR_ALARM_Q3Q4", 0x4C00 /* 0x2600 */},
861 {34, 21, "PWR_ALARM_Q5Q6", 0x1B80},
862 {35, 22, "LOOP_CLOSURE_FILTER", 0x8000},
863 {36, 23, "RING_TRIP_FILTER", 0x0320},
864 {37, 24, "TERM_LP_POLE_Q1Q2", 0x008C},
865 {38, 25, "TERM_LP_POLE_Q3Q4", 0x0100},
866 {39, 26, "TERM_LP_POLE_Q5Q6", 0x0010},
867 {40, 27, "CM_BIAS_RINGING", 0x0C00},
868 {41, 64, "DCDC_MIN_V", 0x0C00},
869 {42, 255, "DCDC_XTRA", 0x1000},
870 {43, 66, "LOOP_CLOSE_TRES_LOW", 0x1000},
871 };
872
873 /* Undefine to enable Power alarm / Transistor debug -- note: do not
874 enable for normal operation! */
875 /* #define PAQ_DEBUG */
876
877 #define DEBUG_CARD (1 << 0)
878 #define DEBUG_ECHOCAN (1 << 1)
879
880 #include "fxo_modes.h"
881
882 static inline struct dahdi_chan *
883 get_dahdi_chan(const struct wcaxx *wc, struct wcaxx_module *const mod)
884 {
885 return wc->span.chans[mod->card];
886 }
887
888 static inline void mod_hooksig(struct wcaxx *wc,
889 struct wcaxx_module *mod,
890 enum dahdi_rxsig rxsig)
891 {
892 dahdi_hooksig(get_dahdi_chan(wc, mod), rxsig);
893 }
894
895 static void wcaxx_release(struct wcaxx *wc);
896
897 static int fxovoltage;
898 static unsigned int battdebounce;
899 static unsigned int battalarm;
900 static unsigned int battthresh;
901 static int debug;
902 static int int_mode;
903 #ifdef DEBUG
904 static int robust;
905 static int digitalloopback;
906 #endif
907 static int lowpower;
908 static int boostringer;
909 static int fastringer;
910 static int _opermode;
911 static char *opermode = "FCC";
912 static int fxshonormode;
913 static int alawoverride;
914 static char *companding = "auto";
915 static int fastpickup = -1; /* -1 auto, 0 no, 1 yes */
916 static int fxotxgain;
917 static int fxorxgain;
918 static int fxstxgain;
919 static int fxsrxgain;
920 static int nativebridge;
921 static int ringdebounce = DEFAULT_RING_DEBOUNCE;
922 static int latency = WCXB_DEFAULT_LATENCY;
923 static unsigned int max_latency = WCXB_DEFAULT_MAXLATENCY;
924 static int forceload;
925
926 #define MS_PER_HOOKCHECK (1)
927 #define NEONMWI_ON_DEBOUNCE (100/MS_PER_HOOKCHECK)
928 static int neonmwi_monitor;
929 static int neonmwi_level = 75; /* neon mwi trip voltage */
930 static int neonmwi_envelope = 10;
931 /* Time in milliseconds the monitor is checked before saying no message is
932 * waiting */
933 static int neonmwi_offlimit = 16000;
934 static int neonmwi_offlimit_cycles;
935
936 static int wcaxx_init_proslic(struct wcaxx *wc,
937 struct wcaxx_module *const mod, int fast,
938 int manual, int sane);
939
940
941 struct wcaxx_setreg_memory {
942 struct wcxb_spi_message m;
943 struct wcxb_spi_transfer t;
944 u8 buffer[3];
945 };
946
947 /**
948 * wcxb_spi_complete_setreg - Cleanup after a SPI write.
949 *
950 * We don't care about the results of setreg. Just go ahead and free up the
951 * messages.
952 *
953 */
954 static void wcaxx_complete_setreg(void *arg)
955 {
956 struct wcaxx_setreg_memory *setreg = arg;
957 kfree(setreg);
958 }
959
960 static void wcaxx_setreg(struct wcaxx *wc, struct wcaxx_module *mod,
961 int addr, int val)
962 {
963 struct wcaxx_setreg_memory *setreg = kzalloc(sizeof(*setreg),
964 GFP_ATOMIC);
965 struct wcxb_spi_message *const m = &setreg->m;
966 struct wcxb_spi_transfer *const t = &setreg->t;
967 if (!setreg) {
968 WARN_ON_ONCE(!setreg);
969 return;
970 }
971 wcxb_spi_message_init(m);
972 t->tx_buf = setreg->buffer;
973 wcxb_spi_message_add_tail(t, m);
974 if (FXO == mod->type) {
975 static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
976 setreg->buffer[0] = 0x20 | ADDRS[mod->subaddr];
977 } else {
978 setreg->buffer[0] = 1 << mod->subaddr;
979 }
980 setreg->buffer[1] = (addr) & 0x7f;
981 setreg->buffer[2] = val;
982 t->len = 3;
983 m->complete = &wcaxx_complete_setreg;
984 m->arg = setreg;
985 wcxb_spi_async(mod->spi, m);
986 }
987
988 /**
989 * wcaxx_fsxinit - Initilize all SPI devices to 3 byte mode.
990 *
991 * All the modules on the card need to be initialized to 3 byte mode in order to
992 * talk to the daisy-chained SLIC / DAA on the quad modules.
993 *
994 */
995 static void wcaxx_fxsinit(struct wcxb_spi_device *const spi)
996 {
997 int res;
998 u8 data_byte[2] = {0, 0x80};
999 struct wcxb_spi_transfer t;
1000 struct wcxb_spi_message m;
1001 memset(&t, 0, sizeof(t));
1002 wcxb_spi_message_init(&m);
1003 t.tx_buf = data_byte;
1004 t.len = sizeof(data_byte);
1005 wcxb_spi_message_add_tail(&t, &m);
1006 res = wcxb_spi_sync(spi, &m);
1007 WARN_ON_ONCE(0 != res);
1008 return;
1009 }
1010
1011 static u8
1012 wcaxx_getreg(struct wcaxx *wc, struct wcaxx_module *const mod, int addr)
1013 {
1014 int res;
1015 u8 buffer[3];
1016 struct wcxb_spi_message m;
1017 struct wcxb_spi_transfer t;
1018 memset(&t, 0, sizeof(t));
1019 wcxb_spi_message_init(&m);
1020
1021 t.tx_buf = t.rx_buf = buffer;
1022 t.len = sizeof(buffer);
1023 wcxb_spi_message_add_tail(&t, &m);
1024
1025 if (FXO == mod->type) {
1026 static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
1027 buffer[0] = 0x60 | ADDRS[mod->subaddr];
1028 buffer[1] = addr & 0x7f;
1029 buffer[2] = 0;
1030 } else {
1031 buffer[0] = 1 << mod->subaddr;
1032 buffer[1] = (addr | 0x80) & 0xff;
1033 buffer[2] = 0;
1034 }
1035 res = wcxb_spi_sync(mod->spi, &m);
1036 WARN_ON_ONCE(0 != res);
1037 return buffer[2];
1038 }
1039
1040 static int wcaxx_getregs(struct wcaxx *wc, struct wcaxx_module *const mod,
1041 int *const addresses, const size_t count)
1042 {
1043 int x;
1044 for (x = 0; x < count; ++x)
1045 addresses[x] = wcaxx_getreg(wc, mod, addresses[x]);
1046 return 0;
1047 }
1048
1049 static int wait_access(struct wcaxx *wc, struct wcaxx_module *const mod)
1050 {
1051 unsigned char data = 0;
1052 int count = 0;
1053
1054 #define MAX 10 /* attempts */
1055
1056 /* Wait for indirect access */
1057 while (count++ < MAX) {
1058 data = wcaxx_getreg(wc, mod, I_STATUS);
1059 if (!data)
1060 return 0;
1061 }
1062
1063 if (count > (MAX-1)) {
1064 dev_notice(&wc->xb.pdev->dev,
1065 " ##### Loop error (%02x) #####\n", data);
1066 }
1067
1068 return 0;
1069 }
1070
1071 static unsigned char translate_3215(unsigned char address)
1072 {
1073 int x;
1074 for (x = 0; x < ARRAY_SIZE(indirect_regs); x++) {
1075 if (indirect_regs[x].address == address) {
1076 address = indirect_regs[x].altaddr;
1077 break;
1078 }
1079 }
1080 return address;
1081 }
1082
1083 static int wcaxx_proslic_setreg_indirect(struct wcaxx *wc,
1084 struct wcaxx_module *const mod,
1085 unsigned char address,
1086 unsigned short data)
1087 {
1088 int res = -1;
1089
1090 address = translate_3215(address);
1091 if (address == 255)
1092 return 0;
1093
1094 if (!wait_access(wc, mod)) {
1095 wcaxx_setreg(wc, mod, IDA_LO, (u8)(data & 0xFF));
1096 wcaxx_setreg(wc, mod, IDA_HI, (u8)((data & 0xFF00)>>8));
1097 wcaxx_setreg(wc, mod, IAA, address);
1098 res = 0;
1099 };
1100 return res;
1101 }
1102
1103 static int wcaxx_proslic_getreg_indirect(struct wcaxx *wc,
1104 struct wcaxx_module *const mod,
1105 unsigned char address)
1106 {
1107 int res = -1;
1108 char *p = NULL;
1109
1110 address = translate_3215(address);
1111 if (address == 255)
1112 return 0;
1113
1114 if (!wait_access(wc, mod)) {
1115 wcaxx_setreg(wc, mod, IAA, address);
1116 if (!wait_access(wc, mod)) {
1117 int addresses[2] = {IDA_LO, IDA_HI};
1118 wcaxx_getregs(wc, mod, addresses,
1119 ARRAY_SIZE(addresses));
1120 res = addresses[0] | (addresses[1] << 8);
1121 } else
1122 p = "Failed to wait inside\n";
1123 } else
1124 p = "failed to wait\n";
1125 if (p)
1126 dev_notice(&wc->xb.pdev->dev, "%s", p);
1127 return res;
1128 }
1129
1130 static int
1131 wcaxx_proslic_init_indirect_regs(struct wcaxx *wc, struct wcaxx_module *mod)
1132 {
1133 unsigned char i;
1134
1135 for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
1136 if (wcaxx_proslic_setreg_indirect(wc, mod,
1137 indirect_regs[i].address,
1138 indirect_regs[i].initial))
1139 return -1;
1140 }
1141
1142 return 0;
1143 }
1144
1145 static int wcaxx_proslic_verify_indirect_regs(struct wcaxx *wc,
1146 struct wcaxx_module *mod)
1147 {
1148 int passed = 1;
1149 unsigned short i, initial;
1150 int j;
1151
1152 for (i = 0; i < ARRAY_SIZE(indirect_regs); i++) {
1153 j = wcaxx_proslic_getreg_indirect(wc, mod,
1154 (u8)indirect_regs[i].address);
1155 if (j < 0) {
1156 dev_notice(&wc->xb.pdev->dev,
1157 "Failed to read indirect register %d\n", i);
1158 return -1;
1159 }
1160 initial = indirect_regs[i].initial;
1161
1162 if ((j != initial) && (indirect_regs[i].altaddr != 255)) {
1163 dev_notice(&wc->xb.pdev->dev,
1164 "!!!!!!! %s iREG %X = %X should be %X\n",
1165 indirect_regs[i].name,
1166 indirect_regs[i].address, j, initial);
1167 passed = 0;
1168 }
1169 }
1170
1171 if (passed) {
1172 if (debug & DEBUG_CARD) {
1173 dev_info(&wc->xb.pdev->dev,
1174 "Init Indirect Registers completed successfully.\n");
1175 }
1176 } else {
1177 dev_notice(&wc->xb.pdev->dev,
1178 " !!!!! Init Indirect Registers UNSUCCESSFULLY.\n");
1179 return -1;
1180 }
1181 return 0;
1182 }
1183
1184 /**
1185 * wcaxx_proslic_check_oppending -
1186 *
1187 * Ensures that a write to the line feed register on the SLIC has been
1188 * processed. If it hasn't after the timeout value, then it will resend the
1189 * command and wait for another timeout period.
1190 *
1191 */
1192 static void wcaxx_proslic_check_oppending(struct wcaxx *wc,
1193 struct wcaxx_module *const mod)
1194 {
1195 struct fxs *const fxs = &mod->mod.fxs;
1196 unsigned long flags;
1197
1198 if (!(fxs->lasttxhook & SLIC_LF_OPPENDING))
1199 return;
1200
1201 /* Monitor the Pending LF state change, for the next 100ms */
1202 spin_lock_irqsave(&wc->reglock, flags);
1203
1204 if (!(fxs->lasttxhook & SLIC_LF_OPPENDING)) {
1205 spin_unlock_irqrestore(&wc->reglock, flags);
1206 return;
1207 }
1208
1209 if ((fxs->linefeed_control_shadow & SLIC_LF_SETMASK) ==
1210 (fxs->lasttxhook & SLIC_LF_SETMASK)) {
1211 fxs->lasttxhook &= SLIC_LF_SETMASK;
1212 if (debug & DEBUG_CARD) {
1213 dev_info(&wc->xb.pdev->dev,
1214 "SLIC_LF OK: card=%d shadow=%02x "
1215 "lasttxhook=%02x framecount=%ld\n", mod->card,
1216 fxs->linefeed_control_shadow,
1217 fxs->lasttxhook, wc->framecount);
1218 }
1219 } else if (time_after(wc->framecount, fxs->oppending_timeout)) {
1220 /* Check again in 100 ms */
1221 fxs->oppending_timeout = wc->framecount + 100;
1222
1223 wcaxx_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);
1224 if (debug & DEBUG_CARD) {
1225 dev_info(&wc->xb.pdev->dev,
1226 "SLIC_LF RETRY: card=%d shadow=%02x "
1227 "lasttxhook=%02x framecount=%ld\n", mod->card,
1228 fxs->linefeed_control_shadow,
1229 fxs->lasttxhook, wc->framecount);
1230 }
1231 }
1232
1233 spin_unlock_irqrestore(&wc->reglock, flags);
1234 }
1235
1236 /* 256ms interrupt */
1237 static void wcaxx_proslic_recheck_sanity(struct wcaxx *wc,
1238 struct wcaxx_module *const mod)
1239 {
1240 struct fxs *const fxs = &mod->mod.fxs;
1241 int res;
1242 unsigned long flags;
1243 const unsigned int MAX_ALARMS = 10;
1244
1245 #ifdef PAQ_DEBUG
1246 res = mod->isrshadow[1];
1247 res &= ~0x3;
1248 if (res) {
1249 mod->isrshadow[1] = 0;
1250 fxs->palarms++;
1251 if (fxs->palarms < MAX_ALARMS) {
1252 dev_notice(&wc->xb.pdev->dev,
1253 "Power alarm (%02x) on module %d, resetting!\n",
1254 res, card + 1);
1255 mod->sethook = CMD_WR(19, res);
1256 /* Update shadow register to avoid extra power alarms
1257 * until next read */
1258 mod->isrshadow[1] = 0;
1259 } else {
1260 if (fxs->palarms == MAX_ALARMS) {
1261 dev_notice(&wc->xb.pdev->dev,
1262 "Too many power alarms on card %d, NOT resetting!\n",
1263 card + 1);
1264 }
1265 }
1266 }
1267 #else
1268 spin_lock_irqsave(&wc->reglock, flags);
1269
1270 /* reg 64 has to be zero at last isr read */
1271 res = !fxs->linefeed_control_shadow &&
1272 !(fxs->lasttxhook & SLIC_LF_OPPENDING) && /* not a transition */
1273 fxs->lasttxhook; /* not an intended zero */
1274
1275 if (res) {
1276 fxs->palarms++;
1277 if (fxs->palarms < MAX_ALARMS) {
1278 dev_notice(&wc->xb.pdev->dev,
1279 "Power alarm on module %d, resetting!\n",
1280 mod->card + 1);
1281 if (fxs->lasttxhook == SLIC_LF_RINGING) {
1282 fxs->lasttxhook = POLARITY_XOR(fxs) ?
1283 SLIC_LF_ACTIVE_REV :
1284 SLIC_LF_ACTIVE_FWD;
1285 }
1286 fxs->lasttxhook |= SLIC_LF_OPPENDING;
1287 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
1288 fxs->oppending_timeout = wc->framecount + 100;
1289
1290 /* Update shadow register to avoid extra power alarms
1291 * until next read */
1292 fxs->linefeed_control_shadow = fxs->lasttxhook;
1293 } else {
1294 if (fxs->palarms == MAX_ALARMS) {
1295 dev_notice(&wc->xb.pdev->dev,
1296 "Too many power alarms on card %d, "
1297 "NOT resetting!\n", mod->card + 1);
1298 }
1299 }
1300 }
1301 spin_unlock_irqrestore(&wc->reglock, flags);
1302 #endif
1303 }
1304
1305 static inline bool is_fxo_ringing(const struct fxo *const fxo)
1306 {
1307 return ((fxo->hook_ring_shadow & 0x60) &&
1308 ((fxo->battery_state == BATTERY_PRESENT) ||
1309 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1310 }
1311
1312 static inline bool is_fxo_ringing_positive(const struct fxo *const fxo)
1313 {
1314 return (((fxo->hook_ring_shadow & 0x60) == 0x20) &&
1315 ((fxo->battery_state == BATTERY_PRESENT) ||
1316 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1317 }
1318
1319 static inline bool is_fxo_ringing_negative(const struct fxo *const fxo)
1320 {
1321 return (((fxo->hook_ring_shadow & 0x60) == 0x40) &&
1322 ((fxo->battery_state == BATTERY_PRESENT) ||
1323 (fxo->battery_state == BATTERY_DEBOUNCING_LOST)));
1324 }
1325
1326 static inline void set_ring(struct fxo *fxo, enum ring_detector_state new)
1327 {
1328 fxo->ring_state = new;
1329 }
1330
1331 static void wcaxx_fxo_ring_detect(struct wcaxx *wc, struct wcaxx_module *mod)
1332 {
1333 struct fxo *const fxo = &mod->mod.fxo;
1334 static const unsigned int POLARITY_CHANGES_NEEDED = 2;
1335
1336 /* Look for ring status bits (Ring Detect Signal Negative and Ring
1337 * Detect Signal Positive) to transition back and forth
1338 * POLARITY_CHANGES_NEEDED times to indicate that a ring is occurring.
1339 * Provide some number of samples to allow for the transitions to occur
1340 * before giving up. NOTE: neon mwi voltages will trigger one of these
1341 * bits to go active but not to have transitions between the two bits
1342 * (i.e. no negative to positive or positive to negative traversals) */
1343
1344 switch (fxo->ring_state) {
1345 case DEBOUNCING_RINGING_POSITIVE:
1346 if (is_fxo_ringing_negative(fxo)) {
1347 if (++fxo->ring_polarity_change_count >
1348 POLARITY_CHANGES_NEEDED) {
1349 mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
1350 set_ring(fxo, RINGING);
1351 if (debug) {
1352 dev_info(&wc->xb.pdev->dev,
1353 "RING on %s!\n",
1354 get_dahdi_chan(wc, mod)->name);
1355 }
1356 } else {
1357 set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
1358 }
1359 } else if (time_after(wc->framecount,
1360 fxo->ringdebounce_timer)) {
1361 set_ring(fxo, RINGOFF);
1362 }
1363 break;
1364 case DEBOUNCING_RINGING_NEGATIVE:
1365 if (is_fxo_ringing_positive(fxo)) {
1366 if (++fxo->ring_polarity_change_count >
1367 POLARITY_CHANGES_NEEDED) {
1368 mod_hooksig(wc, mod, DAHDI_RXSIG_RING);
1369 set_ring(fxo, RINGING);
1370 if (debug) {
1371 dev_info(&wc->xb.pdev->dev,
1372 "RING on %s!\n",
1373 get_dahdi_chan(wc, mod)->name);
1374 }
1375 } else {
1376 set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
1377 }
1378 } else if (time_after(wc->framecount,
1379 fxo->ringdebounce_timer)) {
1380 set_ring(fxo, RINGOFF);
1381 }
1382 break;
1383 case RINGING:
1384 if (!is_fxo_ringing(fxo)) {
1385 set_ring(fxo, DEBOUNCING_RINGOFF);
1386 fxo->ringdebounce_timer =
1387 wc->framecount + ringdebounce / 8;
1388 }
1389 break;
1390 case DEBOUNCING_RINGOFF:
1391 if (!is_fxo_ringing(fxo)) {
1392 if (time_after(wc->framecount,
1393 fxo->ringdebounce_timer)) {
1394 if (debug) {
1395 dev_info(&wc->xb.pdev->dev,
1396 "NO RING on %s!\n",
1397 get_dahdi_chan(wc, mod)->name);
1398 }
1399 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
1400 set_ring(fxo, RINGOFF);
1401 }
1402 } else {
1403 set_ring(fxo, RINGING);
1404 }
1405 break;
1406 case RINGOFF:
1407 if (is_fxo_ringing(fxo)) {
1408 /* Look for positive/negative crossings in ring status
1409 * reg */
1410 if (is_fxo_ringing_positive(fxo))
1411 set_ring(fxo, DEBOUNCING_RINGING_POSITIVE);
1412 else
1413 set_ring(fxo, DEBOUNCING_RINGING_NEGATIVE);
1414 fxo->ringdebounce_timer =
1415 wc->framecount + ringdebounce / 8;
1416 fxo->ring_polarity_change_count = 0;
1417 }
1418 break;
1419 }
1420 }
1421
1422 #define MS_PER_CHECK_HOOK 1
1423 static void
1424 wcaxx_check_battery_lost(struct wcaxx *wc, struct wcaxx_module *const mod)
1425 {
1426 struct fxo *const fxo = &mod->mod.fxo;
1427
1428 /* possible existing states:
1429 battery lost, no debounce timer
1430 battery lost, debounce timer (going to battery present)
1431 battery present or unknown, no debounce timer
1432 battery present or unknown, debounce timer (going to battery lost)
1433 */
1434 switch (fxo->battery_state) {
1435 case BATTERY_DEBOUNCING_PRESENT_ALARM:
1436 fxo->battery_state = BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM;
1437 fxo->battdebounce_timer = wc->framecount + battdebounce;
1438 break;
1439 case BATTERY_DEBOUNCING_PRESENT:
1440 fxo->battery_state = BATTERY_LOST;
1441 break;
1442 case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
1443 fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
1444 fxo->battdebounce_timer = wc->framecount +
1445 battalarm - battdebounce;
1446 break;
1447 case BATTERY_UNKNOWN:
1448 mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
1449 case BATTERY_PRESENT:
1450 fxo->battery_state = BATTERY_DEBOUNCING_LOST;
1451 fxo->battdebounce_timer = wc->framecount + battdebounce;
1452 break;
1453 case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
1454 case BATTERY_DEBOUNCING_LOST: /* Intentional drop through */
1455 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1456 if (debug) {
1457 dev_info(&wc->xb.pdev->dev,
1458 "NO BATTERY on %d/%d!\n",
1459 wc->span.spanno,
1460 mod->card + 1);
1461 }
1462 #ifdef JAPAN
1463 if (!wc->ohdebounce && wc->offhook) {
1464 dahdi_hooksig(wc->aspan->chans[card],
1465 DAHDI_RXSIG_ONHOOK);
1466 if (debug) {
1467 dev_info(&wc->vb.pdev->dev,
1468 "Signalled On Hook\n");
1469 }
1470 #ifdef ZERO_BATT_RING
1471 wc->onhook++;
1472 #endif
1473 }
1474 #else
1475 mod_hooksig(wc, mod, DAHDI_RXSIG_ONHOOK);
1476 #endif
1477 /* set the alarm timer, taking into account that part
1478 * of its time period has already passed while
1479 * debouncing occurred */
1480 fxo->battery_state = BATTERY_DEBOUNCING_LOST_ALARM;
1481 fxo->battdebounce_timer = wc->framecount +
1482 battalarm - battdebounce;
1483 }
1484 break;
1485 case BATTERY_DEBOUNCING_LOST_ALARM:
1486 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1487 fxo->battery_state = BATTERY_LOST;
1488 dahdi_alarm_channel(get_dahdi_chan(wc, mod),
1489 DAHDI_ALARM_RED);
1490 }
1491 break;
1492 case BATTERY_LOST:
1493 break;
1494 }
1495 }
1496
1497 static void
1498 wcaxx_check_battery_present(struct wcaxx *wc, struct wcaxx_module *const mod)
1499 {
1500 struct fxo *const fxo = &mod->mod.fxo;
1501
1502 switch (fxo->battery_state) {
1503 case BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM:
1504 case BATTERY_DEBOUNCING_PRESENT: /* intentional drop through */
1505 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1506 if (debug) {
1507 dev_info(&wc->xb.pdev->dev,
1508 "BATTERY on %d/%d (%s)!\n",
1509 wc->span.spanno, mod->card + 1,
1510 (fxo->line_voltage_status < 0) ?
1511 "-" : "+");
1512 }
1513 #ifdef ZERO_BATT_RING
1514 if (wc->onhook) {
1515 wc->onhook = 0;
1516 dahdi_hooksig(wc->aspan->chans[card],
1517 DAHDI_RXSIG_OFFHOOK);
1518 if (debug) {
1519 dev_info(&wc->vb.pdev->dev,
1520 "Signalled Off Hook\n");
1521 }
1522 }
1523 #else
1524 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
1525 #endif
1526 /* set the alarm timer, taking into account that part
1527 * of its time period has already passed while
1528 * debouncing occurred */
1529 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
1530 fxo->battdebounce_timer = wc->framecount +
1531 battalarm - battdebounce;
1532 }
1533 break;
1534 case BATTERY_DEBOUNCING_PRESENT_ALARM:
1535 if (time_after(wc->framecount, fxo->battdebounce_timer)) {
1536 fxo->battery_state = BATTERY_PRESENT;
1537 dahdi_alarm_channel(get_dahdi_chan(wc, mod),
1538 DAHDI_ALARM_NONE);
1539 }
1540 break;
1541 case BATTERY_PRESENT:
1542 break;
1543 case BATTERY_DEBOUNCING_LOST_ALARM:
1544 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_FROM_LOST_ALARM;
1545 fxo->battdebounce_timer = wc->framecount + battdebounce;
1546 break;
1547 case BATTERY_DEBOUNCING_LOST_FROM_PRESENT_ALARM:
1548 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT_ALARM;
1549 fxo->battdebounce_timer = wc->framecount +
1550 battalarm - battdebounce;
1551 break;
1552 case BATTERY_DEBOUNCING_LOST:
1553 fxo->battery_state = BATTERY_PRESENT;
1554 break;
1555 case BATTERY_UNKNOWN:
1556 mod_hooksig(wc, mod, DAHDI_RXSIG_OFFHOOK);
1557 case BATTERY_LOST: /* intentional drop through */
1558 fxo->battery_state = BATTERY_DEBOUNCING_PRESENT;
1559 fxo->battdebounce_timer = wc->framecount + battdebounce;
1560 break;
1561 }
1562 }
1563
1564 static void
1565 wcaxx_fxo_stop_debouncing_polarity(struct wcaxx *wc,
1566 struct wcaxx_module *const mod)
1567 {
1568 struct fxo *const fxo = &mod->mod.fxo;
1569 switch (fxo->polarity_state) {
1570 case UNKNOWN_POLARITY:
1571 break;
1572 case POLARITY_DEBOUNCE_POSITIVE:
1573 fxo->polarity_state = POLARITY_NEGATIVE;
1574 break;
1575 case POLARITY_POSITIVE:
1576 break;
1577 case POLARITY_DEBOUNCE_NEGATIVE:
1578 fxo->polarity_state = POLARITY_POSITIVE;
1579 break;
1580 case POLARITY_NEGATIVE:
1581 break;
1582 };
1583 }
1584
1585 static void
1586 wcaxx_fxo_check_polarity(struct wcaxx *wc, struct wcaxx_module *const mod,
1587 const bool positive_polarity)
1588 {
1589 struct fxo *const fxo = &mod->mod.fxo;
1590
1591 switch (fxo->polarity_state) {
1592 case UNKNOWN_POLARITY:
1593 fxo->polarity_state = (positive_polarity) ? POLARITY_POSITIVE :
1594 POLARITY_NEGATIVE;
1595 break;
1596 case POLARITY_DEBOUNCE_POSITIVE:
1597 if (!positive_polarity) {
1598 fxo->polarity_state = POLARITY_NEGATIVE;
1599 } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
1600 fxo->polarity_state = POLARITY_POSITIVE;
1601 dahdi_qevent_lock(get_dahdi_chan(wc, mod),
1602 DAHDI_EVENT_POLARITY);
1603 if (debug & DEBUG_CARD) {
1604 dev_info(&wc->xb.pdev->dev,
1605 "%s: Polarity NEGATIVE -> POSITIVE\n",
1606 get_dahdi_chan(wc, mod)->name);
1607 }
1608 }
1609 break;
1610 case POLARITY_POSITIVE:
1611 if (!positive_polarity) {
1612 fxo->polarity_state = POLARITY_DEBOUNCE_NEGATIVE;
1613 fxo->poldebounce_timer = wc->framecount +
1614 POLARITY_DEBOUNCE;
1615 }
1616 break;
1617 case POLARITY_DEBOUNCE_NEGATIVE:
1618 if (positive_polarity) {
1619 fxo->polarity_state = POLARITY_POSITIVE;
1620 } else if (time_after(wc->framecount, fxo->poldebounce_timer)) {
1621 dahdi_qevent_lock(get_dahdi_chan(wc, mod),
1622 DAHDI_EVENT_POLARITY);
1623 if (debug & DEBUG_CARD) {
1624 dev_info(&wc->xb.pdev->dev,
1625 "%s: Polarity POSITIVE -> NEGATIVE\n",
1626 get_dahdi_chan(wc, mod)->name);
1627 }
1628 fxo->polarity_state = POLARITY_NEGATIVE;
1629 }
1630 break;
1631 case POLARITY_NEGATIVE:
1632 if (positive_polarity) {
1633 fxo->polarity_state = POLARITY_DEBOUNCE_POSITIVE;
1634 fxo->poldebounce_timer = wc->framecount +
1635 POLARITY_DEBOUNCE;
1636 }
1637 break;
1638 };
1639 }
1640
1641 static bool is_neon_voltage_present(const struct fxo *fxo, u8 abs_voltage)
1642 {
1643 return (fxo->battery_state == BATTERY_PRESENT &&
1644 abs_voltage > neonmwi_level &&
1645 (0 == fxo->neonmwi_last_voltage ||
1646 ((fxo->line_voltage_status >= fxo->neonmwi_last_voltage -
1647 neonmwi_envelope) &&
1648 (fxo->line_voltage_status <= fxo->neonmwi_last_voltage +
1649 neonmwi_envelope)
1650 )
1651 )
1652 );
1653 }
1654
1655 static void do_neon_monitor(struct wcaxx *wc,
1656 struct wcaxx_module *mod, u8 abs_voltage)
1657 {
1658 struct fxo *const fxo = &mod->mod.fxo;
1659 struct dahdi_chan *const chan = get_dahdi_chan(wc, mod);
1660
1661 /* Look for 4 consecutive voltage readings where the voltage is over the
1662 * neon limit but does not vary greatly from the last reading */
1663 if (is_neon_voltage_present(fxo, abs_voltage)) {
1664 fxo->neonmwi_last_voltage = fxo->line_voltage_status;
1665 if (NEONMWI_ON_DEBOUNCE == fxo->neonmwi_debounce) {
1666 fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
1667 if (0 == fxo->neonmwi_state) {
1668 dahdi_qevent_lock(chan,
1669 DAHDI_EVENT_NEONMWI_ACTIVE);
1670 fxo->neonmwi_state = 1;
1671 if (debug) {
1672 dev_info(&wc->xb.pdev->dev,
1673 "NEON MWI active for card %d\n",
1674 mod->card+1);
1675 }
1676 }
1677 fxo->neonmwi_debounce++;
1678 } else if (NEONMWI_ON_DEBOUNCE > fxo->neonmwi_debounce) {
1679 fxo->neonmwi_debounce++;
1680 } else {
1681 fxo->neonmwi_offcounter = neonmwi_offlimit_cycles;
1682 }
1683 } else {
1684 fxo->neonmwi_debounce = 0;
1685 fxo->neonmwi_last_voltage = 0;
1686 }
1687
1688 /* If no neon mwi pulse for given period of time, indicte no neon mwi
1689 * state */
1690 if (fxo->neonmwi_state && 0 < fxo->neonmwi_offcounter) {
1691 fxo->neonmwi_offcounter--;
1692 if (0 == fxo->neonmwi_offcounter) {
1693 dahdi_qevent_lock(get_dahdi_chan(wc, mod),
1694 DAHDI_EVENT_NEONMWI_INACTIVE);
1695 fxo->neonmwi_state = 0;
1696 if (debug) {
1697 dev_info(&wc->xb.pdev->dev,
1698 "NEON MWI cleared for card %d\n",
1699 mod->card+1);
1700 }
1701 }
1702 }
1703 }
1704
1705 static void
1706 wcaxx_voicedaa_check_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
1707 {
1708 signed char b;
1709 u8 abs_voltage;
1710 struct fxo *const fxo = &mod->mod.fxo;
1711
1712 /* Try to track issues that plague slot one FXO's */
1713 b = fxo->hook_ring_shadow & 0x9b;
1714
1715 if (fxo->offhook) {
1716 if (b != 0x9)
1717 wcaxx_setreg(wc, mod, 5, 0x9);
1718 } else {
1719 if (b != 0x8)
1720 wcaxx_setreg(wc, mod, 5, 0x8);
1721
1722 wcaxx_fxo_ring_detect(wc, mod);
1723 }
1724
1725 abs_voltage = abs(fxo->line_voltage_status);
1726
1727 if (fxovoltage && time_after(wc->framecount, fxo->display_fxovoltage)) {
1728 /* Every 100 ms */
1729 fxo->display_fxovoltage = wc->framecount + 100;
1730 dev_info(&wc->xb.pdev->dev,
1731 "Port %d: Voltage: %d\n",
1732 mod->card + 1, fxo->line_voltage_status);
1733 }
1734
1735 if (unlikely(DAHDI_RXSIG_INITIAL ==
1736 get_dahdi_chan(wc, mod)->rxhooksig)) {
1737 /*
1738 * dahdi-base will set DAHDI_RXSIG_INITIAL after a
1739 * DAHDI_STARTUP or DAHDI_CHANCONFIG ioctl so that new events
1740 * will be queued on the channel with the current received
1741 * hook state. Channels that use robbed-bit signalling always
1742 * report the current received state via the dahdi_rbsbits
1743 * call. Since we only call dahdi_hooksig when we've detected
1744 * a change to report, let's forget our current state in order
1745 * to force us to report it again via dahdi_hooksig.
1746 *
1747 */
1748 fxo->battery_state = BATTERY_UNKNOWN;
1749 }
1750
1751 if (abs_voltage < battthresh) {
1752 wcaxx_fxo_stop_debouncing_polarity(wc, mod);
1753 wcaxx_check_battery_lost(wc, mod);
1754 } else {
1755 wcaxx_check_battery_present(wc, mod);
1756 wcaxx_fxo_check_polarity(wc, mod,
1757 (fxo->line_voltage_status > 0));
1758 }
1759
1760 /* Look for neon mwi pulse */
1761 if (neonmwi_monitor && !fxo->offhook)
1762 do_neon_monitor(wc, mod, abs_voltage);
1763 #undef MS_PER_CHECK_HOOK
1764 }
1765
1766 static void
1767 wcaxx_fxs_hooksig(struct wcaxx *wc, struct wcaxx_module *const mod,
1768 enum dahdi_txsig txsig)
1769 {
1770 int x = 0;
1771 unsigned long flags;
1772 struct fxs *const fxs = &mod->mod.fxs;
1773
1774 spin_lock_irqsave(&wc->reglock, flags);
1775 switch (txsig) {
1776 case DAHDI_TXSIG_ONHOOK:
1777 switch (get_dahdi_chan(wc, mod)->sig) {
1778 case DAHDI_SIG_FXOGS:
1779 x = (POLARITY_XOR(fxs)) ?
1780 SLIC_LF_RING_OPEN :
1781 SLIC_LF_TIP_OPEN;
1782 break;
1783 case DAHDI_SIG_EM:
1784 case DAHDI_SIG_FXOKS:
1785 case DAHDI_SIG_FXOLS:
1786 default:
1787 x = fxs->idletxhookstate;
1788 break;
1789 }
1790 break;
1791 case DAHDI_TXSIG_OFFHOOK:
1792 switch (get_dahdi_chan(wc, mod)->sig) {
1793 case DAHDI_SIG_EM:
1794 x = (POLARITY_XOR(fxs)) ?
1795 SLIC_LF_ACTIVE_FWD :
1796 SLIC_LF_ACTIVE_REV;
1797 break;
1798 default:
1799 x = fxs->idletxhookstate;
1800 break;
1801 }
1802 break;
1803 case DAHDI_TXSIG_START:
1804 x = SLIC_LF_RINGING;
1805 break;
1806 case DAHDI_TXSIG_KEWL:
1807 x = SLIC_LF_OPEN;
1808 break;
1809 default:
1810 spin_unlock_irqrestore(&wc->reglock, flags);
1811 dev_notice(&wc->xb.pdev->dev,
1812 "Can't set tx state to %d\n", txsig);
1813 return;
1814 }
1815
1816 if (x != fxs->lasttxhook) {
1817 fxs->lasttxhook = x | SLIC_LF_OPPENDING;
1818 mod->sethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
1819 fxs->oppending_timeout = wc->framecount + 100;
1820 spin_unlock_irqrestore(&wc->reglock, flags);
1821
1822 if (debug & DEBUG_CARD) {
1823 dev_info(&wc->xb.pdev->dev,
1824 "Setting FXS hook state to %d (%02x) framecount=%ld\n",
1825 txsig, x, wc->framecount);
1826 }
1827 } else {
1828 spin_unlock_irqrestore(&wc->reglock, flags);
1829 }
1830 }
1831
1832 static void
1833 wcaxx_fxs_off_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
1834 {
1835 struct fxs *const fxs = &mod->mod.fxs;
1836
1837 if (debug & DEBUG_CARD) {
1838 dev_info(&wc->xb.pdev->dev,
1839 "fxs_off_hook: Card %d Going off hook\n", mod->card);
1840 }
1841 switch (fxs->lasttxhook) {
1842 case SLIC_LF_RINGING: /* Ringing */
1843 case SLIC_LF_OHTRAN_FWD: /* Forward On Hook Transfer */
1844 case SLIC_LF_OHTRAN_REV: /* Reverse On Hook Transfer */
1845 /* just detected OffHook, during Ringing or OnHookTransfer */
1846 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
1847 SLIC_LF_ACTIVE_REV :
1848 SLIC_LF_ACTIVE_FWD;
1849 break;
1850 }
1851 if ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
1852 wcaxx_fxs_hooksig(wc, mod, DAHDI_TXSIG_OFFHOOK);
1853 dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_OFFHOOK);
1854
1855 #ifdef DEBUG
1856 if (robust)
1857 wcaxx_init_proslic(wc, mod, 1, 0, 1);
1858 #endif
1859 }
1860
1861 /**
1862 * wcaxx_fxs_on_hook - Report on hook to DAHDI.
1863 * @wc: Board hosting the module.
1864 * @card: Index of the module / port to place on hook.
1865 *
1866 * If we are intentionally dropping battery to signal a forward
1867 * disconnect we do not want to place the line "On-Hook". In this
1868 * case, the core of DAHDI will place us on hook when one of the RBS
1869 * timers expires.
1870 *
1871 */
1872 static void
1873 wcaxx_fxs_on_hook(struct wcaxx *wc, struct wcaxx_module *const mod)
1874 {
1875 if (debug & DEBUG_CARD) {
1876 dev_info(&wc->xb.pdev->dev,
1877 "fxs_on_hook: Card %d Going on hook\n", mod->card);
1878 }
1879
1880 if ((mod->mod.fxs.lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)
1881 wcaxx_fxs_hooksig(wc, mod, DAHDI_TXSIG_ONHOOK);
1882 dahdi_hooksig(get_dahdi_chan(wc, mod), DAHDI_RXSIG_ONHOOK);
1883 }
1884
1885 static void
1886 wcaxx_isr_misc_fxs(struct wcaxx *wc, struct wcaxx_module *const mod)
1887 {
1888 struct fxs *const fxs = &mod->mod.fxs;
1889 unsigned long flags;
1890
1891 if (time_after(wc->framecount, fxs->check_alarm)) {
1892 /* Accept an alarm once per 10 seconds */
1893 fxs->check_alarm = wc->framecount + (1000*10);
1894 if (fxs->palarms)
1895 fxs->palarms--;
1896 }
1897
1898 if (fxs->off_hook && !(fxs->hook_state_shadow & 1)) {
1899 wcaxx_fxs_on_hook(wc, mod);
1900 fxs->off_hook = 0;
1901 } else if (!fxs->off_hook && (fxs->hook_state_shadow & 1)) {
1902 wcaxx_fxs_off_hook(wc, mod);
1903 fxs->off_hook = 1;
1904 }
1905
1906 wcaxx_proslic_check_oppending(wc, mod);
1907
1908 if (time_after(wc->framecount, fxs->check_proslic)) {
1909 fxs->check_proslic = wc->framecount + 250; /* every 250ms */
1910 wcaxx_proslic_recheck_sanity(wc, mod);
1911 }
1912
1913 if (SLIC_LF_RINGING == fxs->lasttxhook) {
1914 /* RINGing, prepare for OHT */
1915 fxs->ohttimer = wc->framecount + OHT_TIMER;
1916 /* OHT mode when idle */
1917 fxs->idletxhookstate = POLARITY_XOR(fxs) ? SLIC_LF_OHTRAN_REV :
1918 SLIC_LF_OHTRAN_FWD;
1919 } else if (fxs->oht_active) {
1920 /* check if still OnHook */
1921 if (!fxs->off_hook) {
1922 if (time_before(wc->framecount, fxs->ohttimer))
1923 return;
1924
1925 /* Switch to active */
1926 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
1927 SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
1928 spin_lock_irqsave(&wc->reglock, flags);
1929 if (SLIC_LF_OHTRAN_FWD == fxs->lasttxhook) {
1930 /* Apply the change if appropriate */
1931 fxs->lasttxhook = SLIC_LF_OPPENDING |
1932 SLIC_LF_ACTIVE_FWD;
1933 /* Data enqueued here */
1934 mod->sethook = CMD_WR(LINE_STATE,
1935 fxs->lasttxhook);
1936 if (debug & DEBUG_CARD) {
1937 dev_info(&wc->xb.pdev->dev,
1938 "Channel %d OnHookTransfer stop\n",
1939 mod->card);
1940 }
1941 } else if (SLIC_LF_OHTRAN_REV == fxs->lasttxhook) {
1942 /* Apply the change if appropriate */
1943 fxs->lasttxhook = SLIC_LF_OPPENDING |
1944 SLIC_LF_ACTIVE_REV;
1945 /* Data enqueued here */
1946 mod->sethook = CMD_WR(LINE_STATE,
1947 fxs->lasttxhook);
1948 if (debug & DEBUG_CARD) {
1949 dev_info(&wc->xb.pdev->dev,
1950 "Channel %d OnHookTransfer stop\n",
1951 mod->card);
1952 }
1953 }
1954 spin_unlock_irqrestore(&wc->reglock, flags);
1955 } else {
1956 fxs->oht_active = 0;
1957 /* Switch to active */
1958 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
1959 SLIC_LF_ACTIVE_REV : SLIC_LF_ACTIVE_FWD;
1960 if (debug & DEBUG_CARD) {
1961 dev_info(&wc->xb.pdev->dev,
1962 "Channel %d OnHookTransfer abort\n",
1963 mod->card);
1964 }
1965 }
1966
1967 }
1968 }
1969
1970 static void wcaxx_handle_receive(struct wcxb *xb, void *_frame)
1971 {
1972 int i, j;
1973 struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
1974 u8 *const frame = _frame;
1975
1976 wc->framecount++;
1977
1978 if (time_after(wc->framecount, wc->module_poll_time)) {
1979 for (i = 0; i < wc->mods_per_board; i++) {
1980 struct wcaxx_module *const mod = &wc->mods[i];
1981 if (mod->mod_poll) {
1982 wcxb_spi_async(mod->spi, &mod->mod_poll->m);
1983 mod->mod_poll = NULL;
1984 }
1985 }
1986 wc->module_poll_time = wc->framecount + MODULE_POLL_TIME_MS;
1987 }
1988
1989 /* TODO: This protection needs to be thought about. */
1990 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->span.flags))
1991 return;
1992
1993 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
1994 for (i = 0; i < wc->span.channels; i++) {
1995 wc->chans[i]->chan.readchunk[j] =
1996 frame[j*WCXB_DMA_CHAN_SIZE+(1+i*4)];
1997 }
1998 }
1999 for (i = 0; i < wc->span.channels; i++) {
2000 struct dahdi_chan *const c = wc->span.chans[i];
2001 __dahdi_ec_chunk(c, c->readchunk, c->readchunk, c->writechunk);
2002 }
2003 _dahdi_receive(&wc->span);
2004 return;
2005 }
2006
2007 static void wcaxx_handle_transmit(struct wcxb *xb, void *_frame)
2008 {
2009 int i, j;
2010 struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
2011 u8 *const frame = _frame;
2012
2013 wcxb_spi_handle_interrupt(wc->master);
2014
2015 /* TODO: This protection needs to be thought about. */
2016 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &wc->span.flags))
2017 return;
2018
2019 _dahdi_transmit(&wc->span);
2020 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
2021 for (i = 0; i < wc->span.channels; i++) {
2022 struct dahdi_chan *c = &wc->chans[i]->chan;
2023 frame[j*WCXB_DMA_CHAN_SIZE+(1+i*4)] = c->writechunk[j];
2024 }
2025 }
2026 return;
2027 }
2028
2029 static int wcaxx_voicedaa_insane(struct wcaxx *wc, struct wcaxx_module *mod)
2030 {
2031 int blah;
2032 blah = wcaxx_getreg(wc, mod, 2);
2033 if (blah != 0x3)
2034 return -2;
2035 blah = wcaxx_getreg(wc, mod, 11);
2036 if (debug & DEBUG_CARD) {
2037 dev_info(&wc->xb.pdev->dev,
2038 "VoiceDAA System: %02x\n", blah & 0xf);
2039 }
2040 return 0;
2041 }
2042
2043 static int
2044 wcaxx_proslic_insane(struct wcaxx *wc, struct wcaxx_module *const mod)
2045 {
2046 int blah, reg1, insane_report;
2047 insane_report = 0;
2048
2049 blah = wcaxx_getreg(wc, mod, 0);
2050 if (blah != 0xff && (debug & DEBUG_CARD)) {
2051 dev_info(&wc->xb.pdev->dev,
2052 "ProSLIC on module %d, product %d, "
2053 "version %d\n", mod->card, (blah & 0x30) >> 4,
2054 (blah & 0xf));
2055 }
2056
2057 #if 0
2058 if ((blah & 0x30) >> 4) {
2059 dev_info(&wc->xb.pdev->dev,
2060 "ProSLIC on module %d is not a 3210.\n", mod->card);
2061 return -1;
2062 }
2063 #endif
2064 if (((blah & 0xf) == 0) || ((blah & 0xf) == 0xf)) {
2065 /* SLIC not loaded */
2066 return -1;
2067 }
2068
2069 /* let's be really sure this is an FXS before we continue */
2070 reg1 = wcaxx_getreg(wc, mod, 1);
2071 if ((0x80 != (blah & 0xf0)) || (0x88 != reg1)) {
2072 if (debug & DEBUG_CARD) {
2073 dev_info(&wc->xb.pdev->dev,
2074 "DEBUG: not FXS b/c reg0=%x or "
2075 "reg1 != 0x88 (%x).\n", blah, reg1);
2076 }
2077 return -1;
2078 }
2079
2080 blah = wcaxx_getreg(wc, mod, 8);
2081 if (blah != 0x2) {
2082 dev_notice(&wc->xb.pdev->dev,
2083 "ProSLIC on module %d insane (1) %d should be 2\n",
2084 mod->card, blah);
2085 return -1;
2086 } else if (insane_report) {
2087 dev_notice(&wc->xb.pdev->dev,
2088 "ProSLIC on module %d Reg 8 Reads %d Expected "
2089 "is 0x2\n", mod->card, blah);
2090 }
2091
2092 blah = wcaxx_getreg(wc, mod, 64);
2093 if (blah != 0x0) {
2094 dev_notice(&wc->xb.pdev->dev,
2095 "ProSLIC on module %d insane (2)\n",
2096 mod->card);
2097 return -1;
2098 } else if (insane_report) {
2099 dev_notice(&wc->xb.pdev->dev,
2100 "ProSLIC on module %d Reg 64 Reads %d Expected "
2101 "is 0x0\n", mod->card, blah);
2102 }
2103
2104 blah = wcaxx_getreg(wc, mod, 11);
2105 if (blah != 0x33) {
2106 dev_notice(&wc->xb.pdev->dev,
2107 "ProSLIC on module %d insane (3)\n", mod->card);
2108 return -1;
2109 } else if (insane_report) {
2110 dev_notice(&wc->xb.pdev->dev,
2111 "ProSLIC on module %d Reg 11 Reads %d "
2112 "Expected is 0x33\n", mod->card, blah);
2113 }
2114
2115 /* Just be sure it's setup right. */
2116 wcaxx_setreg(wc, mod, 30, 0);
2117
2118 if (debug & DEBUG_CARD) {
2119 dev_info(&wc->xb.pdev->dev,
2120 "ProSLIC on module %d seems sane.\n", mod->card);
2121 }
2122 return 0;
2123 }
2124
2125 static int
2126 wcaxx_proslic_powerleak_test(struct wcaxx *wc,
2127 struct wcaxx_module *const mod)
2128 {
2129 unsigned long start;
2130 unsigned char vbat;
2131
2132 /* Turn off linefeed */
2133 wcaxx_setreg(wc, mod, LINE_STATE, 0);
2134
2135 /* Power down */
2136 wcaxx_setreg(wc, mod, 14, 0x10);
2137
2138 start = jiffies;
2139
2140 /* TODO: Why is this sleep necessary. Without it, the first read
2141 * comes back with a 0 value. */
2142 msleep(20);
2143
2144 while ((vbat = wcaxx_getreg(wc, mod, 82)) > 0x6) {
2145 if (time_after(jiffies, start + HZ/4))
2146 break;
2147 }
2148
2149 if (vbat < 0x06) {
2150 dev_notice(&wc->xb.pdev->dev,
2151 "Excessive leakage detected on module %d: %d "
2152 "volts (%02x) after %d ms\n", mod->card,
2153 376 * vbat / 1000, vbat,
2154 (int)((jiffies - start) * 1000 / HZ));
2155 return -1;
2156 } else if (debug & DEBUG_CARD) {
2157 dev_info(&wc->xb.pdev->dev,
2158 "Post-leakage voltage: %d volts\n", 376 * vbat / 1000);
2159 }
2160 return 0;
2161 }
2162
2163 static int wcaxx_powerup_proslic(struct wcaxx *wc,
2164 struct wcaxx_module *mod, int fast)
2165 {
2166 unsigned char vbat;
2167 unsigned long origjiffies;
2168 int lim;
2169
2170 /* Set period of DC-DC converter to 1/64 khz */
2171 wcaxx_setreg(wc, mod, 92, 0xc0 /* was 0xff */);
2172
2173 /* Wait for VBat to powerup */
2174 origjiffies = jiffies;
2175
2176 /* Disable powerdown */
2177 wcaxx_setreg(wc, mod, 14, 0);
2178
2179 /* If fast, don't bother checking anymore */
2180 if (fast)
2181 return 0;
2182
2183 while ((vbat = wcaxx_getreg(wc, mod, 82)) < 0xc0) {
2184 /* Wait no more than 500ms */
2185 if ((jiffies - origjiffies) > HZ/2)
2186 break;
2187 }
2188
2189 if (vbat < 0xc0) {
2190 dev_notice(&wc->xb.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",
2191 mod->card, (int)(((jiffies - origjiffies) * 1000 / HZ)),
2192 vbat * 375);
2193 return -1;
2194 } else if (debug & DEBUG_CARD) {
2195 dev_info(&wc->xb.pdev->dev,
2196 "ProSLIC on module %d powered up to -%d volts (%02x) "
2197 "in %d ms\n", mod->card, vbat * 376 / 1000, vbat,
2198 (int)(((jiffies - origjiffies) * 1000 / HZ)));
2199 }
2200
2201 /* Proslic max allowed loop current, reg 71 LOOP_I_LIMIT */
2202 /* If out of range, just set it to the default value */
2203 lim = (loopcurrent - 20) / 3;
2204 if (loopcurrent > 41) {
2205 lim = 0;
2206 if (debug & DEBUG_CARD) {
2207 dev_info(&wc->xb.pdev->dev,
2208 "Loop current out of range! Setting to default 20mA!\n");
2209 }
2210 } else if (debug & DEBUG_CARD) {
2211 dev_info(&wc->xb.pdev->dev,
2212 "Loop current set to %dmA!\n", (lim*3)+20);
2213 }
2214 wcaxx_setreg(wc, mod, LOOP_I_LIMIT, lim);
2215
2216 /* Engage DC-DC converter */
2217 wcaxx_setreg(wc, mod, 93, 0x19 /* was 0x19 */);
2218 return 0;
2219
2220 }
2221
2222 static int
2223 wcaxx_proslic_manual_calibrate(struct wcaxx *wc,
2224 struct wcaxx_module *const mod)
2225 {
2226 unsigned long origjiffies;
2227 unsigned char i;
2228
2229 /* Disable all interupts in DR21-23 */
2230 wcaxx_setreg(wc, mod, 21, 0);
2231 wcaxx_setreg(wc, mod, 22, 0);
2232 wcaxx_setreg(wc, mod, 23, 0);
2233
2234 wcaxx_setreg(wc, mod, 64, 0);
2235
2236 /* (0x18) Calibrations without the ADC and DAC offset and without
2237 * common mode calibration. */
2238 wcaxx_setreg(wc, mod, 97, 0x18);
2239
2240 /* (0x47) Calibrate common mode and differential DAC mode DAC + ILIM */
2241 wcaxx_setreg(wc, mod, 96, 0x47);
2242
2243 origjiffies = jiffies;
2244 while (wcaxx_getreg(wc, mod, 96) != 0) {
2245 if ((jiffies-origjiffies) > 80)
2246 return -1;
2247 }
2248 /* Initialized DR 98 and 99 to get consistant results. 98 and 99 are
2249 * the results registers and the search should have same intial
2250 * conditions.
2251 */
2252
2253 /******* The following is the manual gain mismatch calibration ********/
2254 /******* This is also available as a function *************************/
2255 msleep(20);
2256 wcaxx_proslic_setreg_indirect(wc, mod, 88, 0);
2257 wcaxx_proslic_setreg_indirect(wc, mod, 89, 0);
2258 wcaxx_proslic_setreg_indirect(wc, mod, 90, 0);
2259 wcaxx_proslic_setreg_indirect(wc, mod, 91, 0);
2260 wcaxx_proslic_setreg_indirect(wc, mod, 92, 0);
2261 wcaxx_proslic_setreg_indirect(wc, mod, 93, 0);
2262
2263 /* This is necessary if the calibration occurs other than at reset */
2264 wcaxx_setreg(wc, mod, 98, 0x10);
2265 wcaxx_setreg(wc, mod, 99, 0x10);
2266
2267 for (i = 0x1f; i > 0; i--) {
2268 wcaxx_setreg(wc, mod, 98, i);
2269 msleep(40);
2270 if ((wcaxx_getreg(wc, mod, 88)) == 0)
2271 break;
2272 }
2273
2274 for (i = 0x1f; i > 0; i--) {
2275 wcaxx_setreg(wc, mod, 99, i);
2276 msleep(40);
2277 if ((wcaxx_getreg(wc, mod, 89)) == 0)
2278 break;
2279 }
2280
2281 /******** The preceding is the manual gain mismatch calibration *******/
2282 /******** The following is the longitudinal Balance Cal ***************/
2283 wcaxx_setreg(wc, mod, 64, 1);
2284 msleep(100);
2285
2286 wcaxx_setreg(wc, mod, 64, 0);
2287
2288 /* enable interrupt for the balance Cal */
2289 wcaxx_setreg(wc, mod, 23, 0x4);
2290
2291 /* this is a singular calibration bit for longitudinal calibration */
2292 wcaxx_setreg(wc, mod, 97, 0x1);
2293 wcaxx_setreg(wc, mod, 96, 0x40);
2294
2295 wcaxx_getreg(wc, mod, 96); /* Read Reg 96 just cause */
2296
2297 wcaxx_setreg(wc, mod, 21, 0xFF);
2298 wcaxx_setreg(wc, mod, 22, 0xFF);
2299 wcaxx_setreg(wc, mod, 23, 0xFF);
2300
2301 /**The preceding is the longitudinal Balance Cal***/
2302 return 0;
2303
2304 }
2305
2306 static int
2307 wcaxx_proslic_calibrate(struct wcaxx *wc, struct wcaxx_module *mod)
2308 {
2309 unsigned long origjiffies;
2310 int x;
2311
2312 /* Perform all calibrations */
2313 wcaxx_setreg(wc, mod, 97, 0x1f);
2314
2315 /* Begin, no speedup */
2316 wcaxx_setreg(wc, mod, 96, 0x5f);
2317
2318 /* Wait for it to finish */
2319 origjiffies = jiffies;
2320 while (wcaxx_getreg(wc, mod, 96)) {
2321 if (time_after(jiffies, (origjiffies + (2*HZ)))) {
2322 dev_notice(&wc->xb.pdev->dev,
2323 "Timeout waiting for calibration of "
2324 "module %d\n", mod->card);
2325 return -1;
2326 }
2327 }
2328
2329 if (debug & DEBUG_CARD) {
2330 /* Print calibration parameters */
2331 dev_info(&wc->xb.pdev->dev,
2332 "Calibration Vector Regs 98 - 107:\n");
2333 for (x = 98; x < 108; x++) {
2334 dev_info(&wc->xb.pdev->dev,
2335 "%d: %02x\n", x, wcaxx_getreg(wc, mod, x));
2336 }
2337 }
2338 return 0;
2339 }
2340
2341 /*********************************************************************
2342 * Set the hwgain on the analog modules
2343 *
2344 * card = the card position for this module (0-23)
2345 * gain = gain in dB x10 (e.g. -3.5dB would be gain=-35)
2346 * tx = (0 for rx; 1 for tx)
2347 *
2348 *******************************************************************/
2349 static int
2350 wcaxx_set_hwgain(struct wcaxx *wc, struct wcaxx_module *mod,
2351 __s32 gain, __u32 tx)
2352 {
2353 if (mod->type != FXO) {
2354 dev_notice(&wc->xb.pdev->dev,
2355 "Cannot adjust gain. Unsupported module type!\n");
2356 return -1;
2357 }
2358
2359 if (tx) {
2360 if (debug) {
2361 dev_info(&wc->xb.pdev->dev,
2362 "setting FXO tx gain for card=%d to %d\n",
2363 mod->card, gain);
2364 }
2365 if (gain >= -150 && gain <= 0) {
2366 wcaxx_setreg(wc, mod, 38, 16 + (gain / -10));
2367 wcaxx_setreg(wc, mod, 40, 16 + (-gain % 10));
2368 } else if (gain <= 120 && gain > 0) {
2369 wcaxx_setreg(wc, mod, 38, gain/10);
2370 wcaxx_setreg(wc, mod, 40, (gain%10));
2371 } else {
2372 dev_notice(&wc->xb.pdev->dev,
2373 "FXO tx gain is out of range (%d)\n", gain);
2374 return -1;
2375 }
2376 } else { /* rx */
2377 if (debug) {
2378 dev_info(&wc->xb.pdev->dev,
2379 "setting FXO rx gain for card=%d to %d\n",
2380 mod->card, gain);
2381 }
2382 if (gain >= -150 && gain <= 0) {
2383 wcaxx_setreg(wc, mod, 39, 16 + (gain / -10));
2384 wcaxx_setreg(wc, mod, 41, 16 + (-gain % 10));
2385 } else if (gain <= 120 && gain > 0) {
2386 wcaxx_setreg(wc, mod, 39, gain/10);
2387 wcaxx_setreg(wc, mod, 41, (gain%10));
2388 } else {
2389 dev_notice(&wc->xb.pdev->dev,
2390 "FXO rx gain is out of range (%d)\n", gain);
2391 return -1;
2392 }
2393 }
2394
2395 return 0;
2396 }
2397
2398 static int set_lasttxhook_interruptible(struct wcaxx *wc, struct fxs *fxs,
2399 unsigned newval, int *psethook)
2400 {
2401 int res = 0;
2402 unsigned long flags;
2403 int timeout = 0;
2404
2405 do {
2406 spin_lock_irqsave(&wc->reglock, flags);
2407 if (SLIC_LF_OPPENDING & fxs->lasttxhook) {
2408 spin_unlock_irqrestore(&wc->reglock, flags);
2409 if (timeout++ > 100)
2410 return -1;
2411 msleep(100);
2412 } else {
2413 fxs->lasttxhook = (newval & SLIC_LF_SETMASK) |
2414 SLIC_LF_OPPENDING;
2415 *psethook = CMD_WR(LINE_STATE, fxs->lasttxhook);
2416 spin_unlock_irqrestore(&wc->reglock, flags);
2417 break;
2418 }
2419 } while (1);
2420
2421 return res;
2422 }
2423
2424 /* Must be called from within an interruptible context */
2425 static int set_vmwi(struct wcaxx *wc, struct wcaxx_module *const mod)
2426 {
2427 int x;
2428 struct fxs *const fxs = &mod->mod.fxs;
2429
2430 /* Presently only supports line reversal MWI */
2431 if ((fxs->vmwi_active_messages) &&
2432 (fxs->vmwisetting.vmwi_type & DAHDI_VMWI_LREV))
2433 fxs->vmwi_linereverse = 1;
2434 else
2435 fxs->vmwi_linereverse = 0;
2436
2437 /* Set line polarity for new VMWI state */
2438 if (POLARITY_XOR(fxs)) {
2439 fxs->idletxhookstate |= SLIC_LF_REVMASK;
2440 /* Do not set while currently ringing or open */
2441 if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
2442 ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
2443 x = fxs->lasttxhook;
2444 x |= SLIC_LF_REVMASK;
2445 set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
2446 }
2447 } else {
2448 fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
2449 /* Do not set while currently ringing or open */
2450 if (((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_RINGING) &&
2451 ((fxs->lasttxhook & SLIC_LF_SETMASK) != SLIC_LF_OPEN)) {
2452 x = fxs->lasttxhook;
2453 x &= ~SLIC_LF_REVMASK;
2454 set_lasttxhook_interruptible(wc, fxs, x, &mod->sethook);
2455 }
2456 }
2457 if (debug) {
2458 dev_info(&wc->xb.pdev->dev,
2459 "Setting VMWI on channel %d, messages=%d, lrev=%d\n",
2460 mod->card, fxs->vmwi_active_messages,
2461 fxs->vmwi_linereverse);
2462 }
2463 return 0;
2464 }
2465
2466 static void
2467 wcaxx_voicedaa_set_ts(struct wcaxx *wc, struct wcaxx_module *mod, int ts)
2468 {
2469 /* 34 bits from framesysc to the first channel, 8 bits in each ts * (th
2470 * e timeslot we're assigning + 1 to skip for VPMOCT issue on first
2471 * timeslot + 3 in that there are 4 bytes assigned for each timeslot on
2472 * framer which was copied to this card */
2473 /* 34 + 8 * (ts + 1 + 3) */
2474 wcaxx_setreg(wc, mod, 34, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
2475 wcaxx_setreg(wc, mod, 35, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
2476 wcaxx_setreg(wc, mod, 36, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
2477 wcaxx_setreg(wc, mod, 37, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
2478
2479 if (debug) {
2480 dev_info(&wc->xb.pdev->dev,
2481 "voicedaa: card %d new timeslot: %d\n",
2482 mod->card + 1, ts);
2483 }
2484 }
2485
2486 static int
2487 wcaxx_init_voicedaa(struct wcaxx *wc, struct wcaxx_module *mod,
2488 int fast, int manual, int sane)
2489 {
2490 unsigned char reg16 = 0, reg26 = 0, reg30 = 0, reg31 = 0;
2491 unsigned long flags;
2492 unsigned long newjiffies;
2493
2494 /* Send a short write to the device in order to reset the SPI state
2495 * machine. It may be out of sync since the driver was probing for an
2496 * FXS device on that chip select. */
2497 /* wcxb_spi_short_write(mod->spi); */
2498
2499 spin_lock_irqsave(&wc->reglock, flags);
2500 mod->type = FXO;
2501 spin_unlock_irqrestore(&wc->reglock, flags);
2502
2503 if (!sane && wcaxx_voicedaa_insane(wc, mod))
2504 return -2;
2505
2506 /* Software reset */
2507 wcaxx_setreg(wc, mod, 1, 0x80);
2508 msleep(100);
2509
2510 /* Set On-hook speed, Ringer impedence, and ringer threshold */
2511 reg16 |= (fxo_modes[_opermode].ohs << 6);
2512 reg16 |= (fxo_modes[_opermode].rz << 1);
2513 reg16 |= (fxo_modes[_opermode].rt);
2514 wcaxx_setreg(wc, mod, 16, reg16);
2515
2516 /* Enable ring detector full-wave rectifier mode */
2517 wcaxx_setreg(wc, mod, 18, 2);
2518 wcaxx_setreg(wc, mod, 24, 0);
2519
2520 /* Set DC Termination:
2521 Tip/Ring voltage adjust, minimum operational current, current
2522 limitation */
2523 reg26 |= (fxo_modes[_opermode].dcv << 6);
2524 reg26 |= (fxo_modes[_opermode].mini << 4);
2525 reg26 |= (fxo_modes[_opermode].ilim << 1);
2526 wcaxx_setreg(wc, mod, 26, reg26);
2527
2528 /* Set AC Impedence */
2529 reg30 = (fxo_modes[_opermode].acim);
2530 wcaxx_setreg(wc, mod, 30, reg30);
2531
2532 /* Misc. DAA parameters */
2533
2534 /* If fast pickup is set, then the off hook counter will be set to 8
2535 * ms, otherwise 128 ms. */
2536 reg31 = (fastpickup) ? 0xe3 : 0xa3;
2537
2538 reg31 |= (fxo_modes[_opermode].ohs2 << 3);
2539 wcaxx_setreg(wc, mod, 31, reg31);
2540
2541 wcaxx_voicedaa_set_ts(wc, mod, mod->card);
2542
2543 /* Enable ISO-Cap */
2544 wcaxx_setreg(wc, mod, 6, 0x00);
2545
2546 /* Turn off the calibration delay when fastpickup is enabled. */
2547 if (fastpickup)
2548 wcaxx_setreg(wc, mod, 17, wcaxx_getreg(wc, mod, 17) | 0x20);
2549
2550 /* Wait 2000ms for ISO-cap to come up */
2551 newjiffies = jiffies + msecs_to_jiffies(2000);
2552
2553 while (time_before(jiffies, newjiffies) &&
2554 !(wcaxx_getreg(wc, mod, 11) & 0xf0))
2555 msleep(100);
2556
2557 if (!(wcaxx_getreg(wc, mod, 11) & 0xf0)) {
2558 dev_notice(&wc->xb.pdev->dev, "VoiceDAA did not bring up ISO link properly!\n");
2559 return -1;
2560 }
2561
2562 if (debug & DEBUG_CARD) {
2563 dev_info(&wc->xb.pdev->dev, "ISO-Cap is now up, line side: %02x rev %02x\n",
2564 wcaxx_getreg(wc, mod, 11) >> 4,
2565 (wcaxx_getreg(wc, mod, 13) >> 2) & 0xf);
2566 }
2567
2568 /* Enable on-hook line monitor */
2569 wcaxx_setreg(wc, mod, 5, 0x08);
2570
2571 /* Take values for fxotxgain and fxorxgain and apply them to module */
2572 wcaxx_set_hwgain(wc, mod, fxotxgain, 1);
2573 wcaxx_set_hwgain(wc, mod, fxorxgain, 0);
2574
2575 #ifdef DEBUG
2576 if (digitalloopback) {
2577 dev_info(&wc->xb.pdev->dev,
2578 "Turning on digital loopback for port %d.\n",
2579 mod->card + 1);
2580 wcaxx_setreg(wc, mod, 10, 0x01);
2581 }
2582 #endif
2583
2584 if (debug) {
2585 dev_info(&wc->xb.pdev->dev,
2586 "DEBUG fxotxgain:%i.%i fxorxgain:%i.%i\n",
2587 (wcaxx_getreg(wc, mod, 38)/16) ?
2588 -(wcaxx_getreg(wc, mod, 38) - 16) :
2589 wcaxx_getreg(wc, mod, 38),
2590 (wcaxx_getreg(wc, mod, 40)/16) ?
2591 -(wcaxx_getreg(wc, mod, 40) - 16) :
2592 wcaxx_getreg(wc, mod, 40),
2593 (wcaxx_getreg(wc, mod, 39)/16) ?
2594 -(wcaxx_getreg(wc, mod, 39) - 16) :
2595 wcaxx_getreg(wc, mod, 39),
2596 (wcaxx_getreg(wc, mod, 41)/16) ?
2597 -(wcaxx_getreg(wc, mod, 41) - 16) :
2598 wcaxx_getreg(wc, mod, 41));
2599 }
2600
2601 return 0;
2602 }
2603
2604 static void
2605 wcaxx_proslic_set_ts(struct wcaxx *wc, struct wcaxx_module *mod, int ts)
2606 {
2607 /* Tx Start low byte 0 */
2608 wcaxx_setreg(wc, mod, 2, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
2609 /* Tx Start high byte 0 */
2610 wcaxx_setreg(wc, mod, 3, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
2611 /* Rx Start low byte 0 */
2612 wcaxx_setreg(wc, mod, 4, (ts * 8 + 42 + (ts * 3 * 8)) & 0xff);
2613 /* Rx Start high byte 0 */
2614 wcaxx_setreg(wc, mod, 5, (ts * 8 + 42 + (ts * 3 * 8)) >> 8);
2615
2616 if (debug) {
2617 dev_info(&wc->xb.pdev->dev,
2618 "proslic: card %d new timeslot: %d\n",
2619 mod->card + 1, ts);
2620 }
2621 }
2622
2623 static int
2624 wcaxx_init_proslic(struct wcaxx *wc, struct wcaxx_module *const mod,
2625 int fast, int manual, int sane)
2626 {
2627
2628 struct fxs *const fxs = &mod->mod.fxs;
2629 unsigned short tmp[5];
2630 unsigned long flags;
2631 unsigned char r19, r9;
2632 int x;
2633 int fxsmode = 0;
2634 int addresses[ARRAY_SIZE(fxs->calregs.vals)];
2635
2636 #if 0 /* TODO */
2637 if (wc->mods[mod->card & 0xfc].type == QRV)
2638 return -2;
2639 #endif
2640
2641 spin_lock_irqsave(&wc->reglock, flags);
2642 mod->type = FXS;
2643 spin_unlock_irqrestore(&wc->reglock, flags);
2644
2645 /* msleep(100); */
2646
2647 /* Sanity check the ProSLIC */
2648 if (!sane && wcaxx_proslic_insane(wc, mod))
2649 return -2;
2650
2651 /* Initialize VMWI settings */
2652 memset(&(fxs->vmwisetting), 0, sizeof(fxs->vmwisetting));
2653 fxs->vmwi_linereverse = 0;
2654
2655 /* By default, don't send on hook */
2656 if (!reversepolarity != !fxs->reversepolarity)
2657 fxs->idletxhookstate = SLIC_LF_ACTIVE_REV;
2658 else
2659 fxs->idletxhookstate = SLIC_LF_ACTIVE_FWD;
2660
2661 if (sane) {
2662 /* Make sure we turn off the DC->DC converter to prevent
2663 * anything from blowing up */
2664 wcaxx_setreg(wc, mod, 14, 0x10);
2665 }
2666
2667 if (wcaxx_proslic_init_indirect_regs(wc, mod)) {
2668 dev_info(&wc->xb.pdev->dev,
2669 "Indirect Registers failed to initialize on "
2670 "module %d.\n", mod->card);
2671 return -1;
2672 }
2673
2674 /* Clear scratch pad area */
2675 wcaxx_proslic_setreg_indirect(wc, mod, 97, 0);
2676
2677 /* Clear digital loopback */
2678 wcaxx_setreg(wc, mod, 8, 0);
2679
2680 /* Revision C optimization */
2681 wcaxx_setreg(wc, mod, 108, 0xeb);
2682
2683 /* Disable automatic VBat switching for safety to prevent
2684 * Q7 from accidently turning on and burning out.
2685 * If pulse dialing has trouble at high REN loads change this to 0x17 */
2686 wcaxx_setreg(wc, mod, 67, 0x07);
2687
2688 /* Turn off Q7 */
2689 wcaxx_setreg(wc, mod, 66, 1);
2690
2691 /* Flush ProSLIC digital filters by setting to clear, while
2692 saving old values */
2693 for (x = 0; x < 5; x++) {
2694 tmp[x] = wcaxx_proslic_getreg_indirect(wc, mod, x + 35);
2695 wcaxx_proslic_setreg_indirect(wc, mod, x + 35, 0x8000);
2696 }
2697
2698 /* Power up the DC-DC converter */
2699 if (wcaxx_powerup_proslic(wc, mod, fast)) {
2700 dev_notice(&wc->xb.pdev->dev,
2701 "Unable to do INITIAL ProSLIC powerup on "
2702 "module %d\n", mod->card);
2703 return -1;
2704 }
2705
2706 if (!fast) {
2707 /* Check for power leaks */
2708 if (wcaxx_proslic_powerleak_test(wc, mod)) {
2709 dev_notice(&wc->xb.pdev->dev,
2710 "ProSLIC module %d failed leakage test. "
2711 "Check for short circuit\n", mod->card);
2712 }
2713 /* Power up again */
2714 if (wcaxx_powerup_proslic(wc, mod, fast)) {
2715 dev_notice(&wc->xb.pdev->dev,
2716 "Unable to do FINAL ProSLIC powerup on "
2717 "module %d\n", mod->card);
2718 return -1;
2719 }
2720 #ifndef NO_CALIBRATION
2721 /* Perform calibration */
2722 if (manual) {
2723 if (wcaxx_proslic_manual_calibrate(wc, mod)) {
2724 dev_dbg(&wc->xb.pdev->dev,
2725 "Proslic failed on Manual Calibration\n");
2726 if (wcaxx_proslic_manual_calibrate(wc, mod)) {
2727 dev_notice(&wc->xb.pdev->dev,
2728 "Proslic Failed on Second Attempt to Calibrate Manually. (Try -DNO_CALIBRATION in Makefile)\n");
2729 return -1;
2730 }
2731 dev_info(&wc->xb.pdev->dev,
2732 "Proslic Passed Manual Calibration on Second Attempt\n");
2733 }
2734 } else {
2735 if (wcaxx_proslic_calibrate(wc, mod)) {
2736 dev_dbg(&wc->xb.pdev->dev,
2737 "ProSlic died on Auto Calibration.\n");
2738 if (wcaxx_proslic_calibrate(wc, mod)) {
2739 dev_notice(&wc->xb.pdev->dev,
2740 "Proslic Failed on Second Attempt to Auto Calibrate\n");
2741 return -1;
2742 }
2743 dev_info(&wc->xb.pdev->dev,
2744 "Proslic Passed Auto Calibration on Second Attempt\n");
2745 }
2746 }
2747 /* Perform DC-DC calibration */
2748 wcaxx_setreg(wc, mod, 93, 0x99);
2749 r19 = wcaxx_getreg(wc, mod, 107);
2750 if ((r19 < 0x2) || (r19 > 0xd)) {
2751 dev_notice(&wc->xb.pdev->dev,
2752 "DC-DC cal has a surprising direct 107 of 0x%02x!\n",
2753 r19);
2754 wcaxx_setreg(wc, mod, 107, 0x8);
2755 }
2756
2757 /* Save calibration vectors */
2758 for (x = 0; x < ARRAY_SIZE(addresses); x++)
2759 addresses[x] = 96 + x;
2760 wcaxx_getregs(wc, mod, addresses, ARRAY_SIZE(addresses));
2761 for (x = 0; x < ARRAY_SIZE(fxs->calregs.vals); x++)
2762 fxs->calregs.vals[x] = addresses[x];
2763 #endif
2764
2765 } else {
2766 /* Restore calibration registers */
2767 for (x = 0; x < ARRAY_SIZE(fxs->calregs.vals); x++)
2768 wcaxx_setreg(wc, mod, 96 + x, fxs->calregs.vals[x]);
2769 }
2770 /* Calibration complete, restore original values */
2771 for (x = 0; x < 5; x++)
2772 wcaxx_proslic_setreg_indirect(wc, mod, x + 35, tmp[x]);
2773
2774 if (wcaxx_proslic_verify_indirect_regs(wc, mod)) {
2775 dev_info(&wc->xb.pdev->dev, "Indirect Registers failed verification.\n");
2776 return -1;
2777 }
2778
2779 /* U-Law 8-bit interface */
2780 wcaxx_proslic_set_ts(wc, mod, mod->card);
2781
2782 wcaxx_setreg(wc, mod, 18, 0xff); /* clear all interrupt */
2783 wcaxx_setreg(wc, mod, 19, 0xff);
2784 wcaxx_setreg(wc, mod, 20, 0xff);
2785 wcaxx_setreg(wc, mod, 22, 0xff);
2786 wcaxx_setreg(wc, mod, 73, 0x04);
2787
2788 wcaxx_setreg(wc, mod, 69, 0x4);
2789
2790 if (fxshonormode) {
2791 static const int ACIM2TISS[16] = { 0x0, 0x1, 0x4, 0x5, 0x7,
2792 0x0, 0x0, 0x6, 0x0, 0x0,
2793 0x0, 0x2, 0x0, 0x3 };
2794 fxsmode = ACIM2TISS[fxo_modes[_opermode].acim];
2795 wcaxx_setreg(wc, mod, 10, 0x08 | fxsmode);
2796 if (fxo_modes[_opermode].ring_osc) {
2797 wcaxx_proslic_setreg_indirect(wc, mod, 20,
2798 fxo_modes[_opermode].ring_osc);
2799 }
2800 if (fxo_modes[_opermode].ring_x) {
2801 wcaxx_proslic_setreg_indirect(wc, mod, 21,
2802 fxo_modes[_opermode].ring_x);
2803 }
2804 }
2805 if (lowpower)
2806 wcaxx_setreg(wc, mod, 72, 0x10);
2807
2808 if (fastringer) {
2809 /* Speed up Ringer */
2810 wcaxx_proslic_setreg_indirect(wc, mod, 20, 0x7e6d);
2811 wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x01b9);
2812 /* Beef up Ringing voltage to 89V */
2813 if (boostringer) {
2814 wcaxx_setreg(wc, mod, 74, 0x3f);
2815 if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x247))
2816 return -1;
2817 dev_info(&wc->xb.pdev->dev,
2818 "Boosting fast ringer on slot %d (89V peak)\n",
2819 mod->card + 1);
2820 } else if (lowpower) {
2821 if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x14b))
2822 return -1;
2823 dev_info(&wc->xb.pdev->dev,
2824 "Reducing fast ring power on slot %d "
2825 "(50V peak)\n", mod->card + 1);
2826 } else
2827 dev_info(&wc->xb.pdev->dev,
2828 "Speeding up ringer on slot %d (25Hz)\n",
2829 mod->card + 1);
2830 } else {
2831 /* Beef up Ringing voltage to 89V */
2832 if (boostringer) {
2833 wcaxx_setreg(wc, mod, 74, 0x3f);
2834 if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x1d1))
2835 return -1;
2836 dev_info(&wc->xb.pdev->dev,
2837 "Boosting ringer on slot %d (89V peak)\n",
2838 mod->card + 1);
2839 } else if (lowpower) {
2840 if (wcaxx_proslic_setreg_indirect(wc, mod, 21, 0x108))
2841 return -1;
2842 dev_info(&wc->xb.pdev->dev,
2843 "Reducing ring power on slot %d "
2844 "(50V peak)\n", mod->card + 1);
2845 }
2846 }
2847
2848 if (fxstxgain || fxsrxgain) {
2849 r9 = wcaxx_getreg(wc, mod, 9);
2850 switch (fxstxgain) {
2851 case 35:
2852 r9 += 8;
2853 break;
2854 case -35:
2855 r9 += 4;
2856 break;
2857 case 0:
2858 break;
2859 }
2860
2861 switch (fxsrxgain) {
2862 case 35:
2863 r9 += 2;
2864 break;
2865 case -35:
2866 r9 += 1;
2867 break;
2868 case 0:
2869 break;
2870 }
2871 wcaxx_setreg(wc, mod, 9, r9);
2872 }
2873
2874 if (debug) {
2875 dev_info(&wc->xb.pdev->dev,
2876 "DEBUG: fxstxgain:%s fxsrxgain:%s\n",
2877 ((wcaxx_getreg(wc, mod, 9) / 8) == 1) ?
2878 "3.5" : ((wcaxx_getreg(wc, mod, 9) / 4) == 1) ?
2879 "-3.5" : "0.0",
2880 ((wcaxx_getreg(wc, mod, 9) / 2) == 1) ?
2881 "3.5" : ((wcaxx_getreg(wc, mod, 9) % 2) ?
2882 "-3.5" : "0.0"));
2883 }
2884
2885 fxs->lasttxhook = fxs->idletxhookstate;
2886 wcaxx_setreg(wc, mod, LINE_STATE, fxs->lasttxhook);
2887
2888 /* Preset the shadow register so that we won't get a power alarm when
2889 * we finish initialization, otherwise the line state register may not
2890 * have been read yet. */
2891 fxs->linefeed_control_shadow = fxs->lasttxhook;
2892 return 0;
2893 }
2894
2895 static void wcaxx_get_fxs_regs(struct wcaxx *wc, struct wcaxx_module *mod,
2896 struct wctdm_regs *regs)
2897 {
2898 int x;
2899
2900 for (x = 0; x < NUM_INDIRECT_REGS; x++)
2901 regs->indirect[x] = wcaxx_proslic_getreg_indirect(wc, mod, x);
2902
2903 for (x = 0; x < NUM_REGS; x++)
2904 regs->direct[x] = wcaxx_getreg(wc, mod, x);
2905 }
2906
2907 static void wcaxx_get_fxo_regs(struct wcaxx *wc, struct wcaxx_module *mod,
2908 struct wctdm_regs *regs)
2909 {
2910 const unsigned int NUM_FXO_REGS = 60;
2911 int x;
2912
2913 for (x = 0; x < NUM_FXO_REGS; x++)
2914 regs->direct[x] = wcaxx_getreg(wc, mod, x);
2915 }
2916
2917 static int
2918 wcaxx_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
2919 {
2920 struct wctdm_stats stats;
2921 struct wctdm_regop regop;
2922 struct wctdm_echo_coefs echoregs;
2923 struct dahdi_hwgain hwgain;
2924 struct wcaxx *wc = chan->pvt;
2925 int x;
2926 struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
2927 struct fxs *const fxs = &mod->mod.fxs;
2928
2929 switch (cmd) {
2930 case DAHDI_ONHOOKTRANSFER:
2931 if (mod->type != FXS)
2932 return -EINVAL;
2933 if (get_user(x, (__user int *) data))
2934 return -EFAULT;
2935
2936 /* Active mode when idle */
2937 fxs->idletxhookstate = POLARITY_XOR(fxs) ?
2938 SLIC_LF_ACTIVE_REV :
2939 SLIC_LF_ACTIVE_FWD;
2940
2941 if (fxs_lf(fxs, ACTIVE_FWD) || fxs_lf(fxs, ACTIVE_REV)) {
2942 int res;
2943
2944 res = set_lasttxhook_interruptible(wc, fxs,
2945 (POLARITY_XOR(fxs) ?
2946 SLIC_LF_OHTRAN_REV : SLIC_LF_OHTRAN_FWD),
2947 &mod->sethook);
2948
2949 if (debug & DEBUG_CARD) {
2950 if (res) {
2951 dev_info(&wc->xb.pdev->dev,
2952 "Channel %d TIMEOUT: "
2953 "OnHookTransfer start\n",
2954 chan->chanpos - 1);
2955 } else {
2956 dev_info(&wc->xb.pdev->dev,
2957 "Channel %d OnHookTransfer "
2958 "start\n", chan->chanpos - 1);
2959 }
2960 }
2961
2962 }
2963
2964 fxs->ohttimer = wc->framecount + x;
2965 fxs->oht_active = 1;
2966
2967 break;
2968 case DAHDI_VMWI_CONFIG:
2969 if (mod->type != FXS)
2970 return -EINVAL;
2971 if (copy_from_user(&(fxs->vmwisetting),
2972 (__user void *)data,
2973 sizeof(fxs->vmwisetting)))
2974 return -EFAULT;
2975 set_vmwi(wc, mod);
2976 break;
2977 case DAHDI_VMWI:
2978 if (mod->type != FXS)
2979 return -EINVAL;
2980 if (get_user(x, (__user int *) data))
2981 return -EFAULT;
2982 if (0 > x)
2983 return -EFAULT;
2984 fxs->vmwi_active_messages = x;
2985 set_vmwi(wc, mod);
2986 break;
2987 case WCTDM_GET_STATS:
2988 if (mod->type == FXS) {
2989 stats.tipvolt = wcaxx_getreg(wc, mod, 80) * -376;
2990 stats.ringvolt = wcaxx_getreg(wc, mod, 81) * -376;
2991 stats.batvolt = wcaxx_getreg(wc, mod, 82) * -376;
2992 } else if (mod->type == FXO) {
2993 stats.tipvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
2994 stats.ringvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
2995 stats.batvolt = (s8)wcaxx_getreg(wc, mod, 29) * 1000;
2996 } else
2997 return -EINVAL;
2998 if (copy_to_user((__user void *) data, &stats, sizeof(stats)))
2999 return -EFAULT;
3000 break;
3001 case WCTDM_GET_REGS:
3002 {
3003 struct wctdm_regs *regs = kzalloc(sizeof(*regs), GFP_KERNEL);
3004 if (!regs)
3005 return -ENOMEM;
3006
3007 if (mod->type == FXS)
3008 wcaxx_get_fxs_regs(wc, mod, regs);
3009 else
3010 wcaxx_get_fxo_regs(wc, mod, regs);
3011
3012 if (copy_to_user((__user void *)data, regs, sizeof(*regs))) {
3013 kfree(regs);
3014 return -EFAULT;
3015 }
3016
3017 kfree(regs);
3018 break;
3019 }
3020 case WCTDM_SET_REG:
3021 if (copy_from_user(&regop, (__user void *) data, sizeof(regop)))
3022 return -EFAULT;
3023 if (regop.indirect) {
3024 if (mod->type != FXS)
3025 return -EINVAL;
3026 dev_info(&wc->xb.pdev->dev,
3027 "Setting indirect %d to 0x%04x on %d\n",
3028 regop.reg, regop.val, chan->chanpos);
3029 wcaxx_proslic_setreg_indirect(wc, mod, regop.reg,
3030 regop.val);
3031 } else {
3032 regop.val &= 0xff;
3033 if (regop.reg == LINE_STATE) {
3034 /* Set feedback register to indicate the new
3035 * state that is being set */
3036 fxs->lasttxhook = (regop.val & 0x0f) |
3037 SLIC_LF_OPPENDING;
3038 }
3039 dev_info(&wc->xb.pdev->dev,
3040 "Setting direct %d to %04x on %d\n",
3041 regop.reg, regop.val, chan->chanpos);
3042 wcaxx_setreg(wc, mod, regop.reg, regop.val);
3043 }
3044 break;
3045 case WCTDM_SET_ECHOTUNE:
3046 dev_info(&wc->xb.pdev->dev, "-- Setting echo registers:\n");
3047 if (copy_from_user(&echoregs, (__user void *) data,
3048 sizeof(echoregs)))
3049 return -EFAULT;
3050
3051 if (mod->type == FXO) {
3052 /* Set the ACIM register */
3053 wcaxx_setreg(wc, mod, 30, echoregs.acim);
3054
3055 /* Set the digital echo canceller registers */
3056 wcaxx_setreg(wc, mod, 45, echoregs.coef1);
3057 wcaxx_setreg(wc, mod, 46, echoregs.coef2);
3058 wcaxx_setreg(wc, mod, 47, echoregs.coef3);
3059 wcaxx_setreg(wc, mod, 48, echoregs.coef4);
3060 wcaxx_setreg(wc, mod, 49, echoregs.coef5);
3061 wcaxx_setreg(wc, mod, 50, echoregs.coef6);
3062 wcaxx_setreg(wc, mod, 51, echoregs.coef7);
3063 wcaxx_setreg(wc, mod, 52, echoregs.coef8);
3064
3065 dev_info(&wc->xb.pdev->dev, "-- Set echo registers successfully\n");
3066
3067 break;
3068 } else {
3069 return -EINVAL;
3070
3071 }
3072 break;
3073 case DAHDI_SET_HWGAIN:
3074 if (copy_from_user(&hwgain, (__user void *) data,
3075 sizeof(hwgain)))
3076 return -EFAULT;
3077
3078 wcaxx_set_hwgain(wc, mod, hwgain.newgain, hwgain.tx);
3079
3080 if (debug) {
3081 dev_info(&wc->xb.pdev->dev,
3082 "Setting hwgain on channel %d to %d for %s direction\n",
3083 chan->chanpos-1, hwgain.newgain,
3084 ((hwgain.tx) ? "tx" : "rx"));
3085 }
3086 break;
3087 case DAHDI_TONEDETECT:
3088 /* Hardware DTMF detection is not supported. */
3089 return -ENOSYS;
3090 case DAHDI_SETPOLARITY:
3091 if (get_user(x, (__user int *) data))
3092 return -EFAULT;
3093 if (mod->type != FXS)
3094 return -EINVAL;
3095 /* Can't change polarity while ringing or when open */
3096 if (((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_RINGING) ||
3097 ((fxs->lasttxhook & SLIC_LF_SETMASK) == SLIC_LF_OPEN)) {
3098 if (debug & DEBUG_CARD) {
3099 dev_info(&wc->xb.pdev->dev,
3100 "Channel %d Unable to Set Polarity\n",
3101 chan->chanpos - 1);
3102 }
3103 return -EINVAL;
3104 }
3105
3106 fxs->reversepolarity = (x) ? 1 : 0;
3107
3108 if (POLARITY_XOR(fxs)) {
3109 fxs->idletxhookstate |= SLIC_LF_REVMASK;
3110 x = fxs->lasttxhook & SLIC_LF_SETMASK;
3111 x |= SLIC_LF_REVMASK;
3112 if (x != fxs->lasttxhook) {
3113 x = set_lasttxhook_interruptible(wc, fxs, x,
3114 &mod->sethook);
3115 if ((debug & DEBUG_CARD) && x) {
3116 dev_info(&wc->xb.pdev->dev,
3117 "Channel %d TIMEOUT: Set Reverse Polarity\n",
3118 chan->chanpos - 1);
3119 } else if (debug & DEBUG_CARD) {
3120 dev_info(&wc->xb.pdev->dev,
3121 "Channel %d Set Reverse Polarity\n",
3122 chan->chanpos - 1);
3123 }
3124 }
3125 } else {
3126 fxs->idletxhookstate &= ~SLIC_LF_REVMASK;
3127 x = fxs->lasttxhook & SLIC_LF_SETMASK;
3128 x &= ~SLIC_LF_REVMASK;
3129 if (x != fxs->lasttxhook) {
3130 x = set_lasttxhook_interruptible(wc, fxs, x,
3131 &mod->sethook);
3132 if ((debug & DEBUG_CARD) & x) {
3133 dev_info(&wc->xb.pdev->dev,
3134 "Channel %d TIMEOUT: Set Normal Polarity\n",
3135 chan->chanpos - 1);
3136 } else if (debug & DEBUG_CARD) {
3137 dev_info(&wc->xb.pdev->dev,
3138 "Channel %d Set Normal Polarity\n",
3139 chan->chanpos - 1);
3140 }
3141 }
3142 }
3143 break;
3144 default:
3145 return -ENOTTY;
3146 }
3147 return 0;
3148 }
3149
3150 static int wcaxx_open(struct dahdi_chan *chan)
3151 {
3152 struct wcaxx *const wc = chan->pvt;
3153 unsigned long flags;
3154 struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
3155
3156 #if 0
3157 if (wc->dead)
3158 return -ENODEV;
3159 #endif
3160 if (mod->type == FXO) {
3161 /* Reset the mwi indicators */
3162 spin_lock_irqsave(&wc->reglock, flags);
3163 mod->mod.fxo.neonmwi_debounce = 0;
3164 mod->mod.fxo.neonmwi_offcounter = 0;
3165 mod->mod.fxo.neonmwi_state = 0;
3166 spin_unlock_irqrestore(&wc->reglock, flags);
3167 }
3168
3169 return 0;
3170 }
3171
3172 static inline struct wcaxx *span_to_wcaxx(struct dahdi_span *span)
3173 {
3174 struct wcaxx *wc = container_of(span, struct wcaxx, span);
3175 return wc;
3176 }
3177
3178 static int wcaxx_watchdog(struct dahdi_span *span, int event)
3179 {
3180 struct wcaxx *wc = span_to_wcaxx(span);
3181 dev_info(&wc->xb.pdev->dev, "TDM: Called watchdog\n");
3182 return 0;
3183 }
3184
3185 static int wcaxx_close(struct dahdi_chan *chan)
3186 {
3187 struct wcaxx *wc;
3188 int x;
3189
3190 wc = chan->pvt;
3191 for (x = 0; x < wc->mods_per_board; x++) {
3192 struct wcaxx_module *const mod = &wc->mods[x];
3193 if (FXS == mod->type) {
3194 mod->mod.fxs.idletxhookstate =
3195 POLARITY_XOR(&mod->mod.fxs) ? SLIC_LF_ACTIVE_REV :
3196 SLIC_LF_ACTIVE_FWD;
3197 }
3198 }
3199
3200 return 0;
3201 }
3202
3203 static int wcaxx_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
3204 {
3205 struct wcaxx *wc = chan->pvt;
3206 struct wcaxx_module *const mod = &wc->mods[chan->chanpos - 1];
3207
3208 if (mod->type == FXO) {
3209 switch (txsig) {
3210 case DAHDI_TXSIG_START:
3211 case DAHDI_TXSIG_OFFHOOK:
3212 mod->mod.fxo.offhook = 1;
3213 mod->sethook = CMD_WR(5, 0x9);
3214 /* wcaxx_setreg(wc, chan->chanpos - 1, 5, 0x9); */
3215 break;
3216 case DAHDI_TXSIG_ONHOOK:
3217 mod->mod.fxo.offhook = 0;
3218 mod->sethook = CMD_WR(5, 0x8);
3219 /* wcaxx_setreg(wc, chan->chanpos - 1, 5, 0x8); */
3220 break;
3221 default:
3222 dev_notice(&wc->xb.pdev->dev,
3223 "Can't set tx state to %d\n", txsig);
3224 break;
3225 }
3226 } else if (mod->type == FXS) {
3227 wcaxx_fxs_hooksig(wc, mod, txsig);
3228 }
3229 return 0;
3230 }
3231
3232 static void wcaxx_dacs_connect(struct wcaxx *wc, int srccard, int dstcard)
3233 {
3234 struct wcaxx_module *const srcmod = &wc->mods[srccard];
3235 struct wcaxx_module *const dstmod = &wc->mods[dstcard];
3236 unsigned int type;
3237
3238 if (wc->mods[dstcard].dacssrc > -1) {
3239 dev_notice(&wc->xb.pdev->dev, "wcaxx_dacs_connect: Can't have double sourcing yet!\n");
3240 return;
3241 }
3242 type = wc->mods[srccard].type;
3243 if ((type == FXS) || (type == FXO)) {
3244 dev_notice(&wc->xb.pdev->dev,
3245 "wcaxx_dacs_connect: Unsupported modtype for "
3246 "card %d\n", srccard);
3247 return;
3248 }
3249 type = wc->mods[dstcard].type;
3250 if ((type != FXS) && (type != FXO)) {
3251 dev_notice(&wc->xb.pdev->dev,
3252 "wcaxx_dacs_connect: Unsupported modtype "
3253 "for card %d\n", dstcard);
3254 return;
3255 }
3256
3257 if (debug) {
3258 dev_info(&wc->xb.pdev->dev,
3259 "connect %d => %d\n", srccard, dstcard);
3260 }
3261
3262 dstmod->dacssrc = srccard;
3263
3264 /* make srccard transmit to srccard+24 on the TDM bus */
3265 if (srcmod->type == FXS) {
3266 /* proslic */
3267 wcaxx_setreg(wc, srcmod, PCM_XMIT_START_COUNT_LSB,
3268 ((srccard+24) * 8) & 0xff);
3269 wcaxx_setreg(wc, srcmod, PCM_XMIT_START_COUNT_MSB,
3270 ((srccard+24) * 8) >> 8);
3271 } else if (srcmod->type == FXO) {
3272 /* daa TX */
3273 wcaxx_setreg(wc, srcmod, 34, ((srccard+24) * 8) & 0xff);
3274 wcaxx_setreg(wc, srcmod, 35, ((srccard+24) * 8) >> 8);
3275 }
3276
3277 /* have dstcard receive from srccard+24 on the TDM bus */
3278 if (dstmod->type == FXS) {
3279 /* proslic */
3280 wcaxx_setreg(wc, dstmod, PCM_RCV_START_COUNT_LSB,
3281 ((srccard+24) * 8) & 0xff);
3282 wcaxx_setreg(wc, dstmod, PCM_RCV_START_COUNT_MSB,
3283 ((srccard+24) * 8) >> 8);
3284 } else if (dstmod->type == FXO) {
3285 /* daa RX */
3286 wcaxx_setreg(wc, dstmod, 36, ((srccard+24) * 8) & 0xff);
3287 wcaxx_setreg(wc, dstmod, 37, ((srccard+24) * 8) >> 8);
3288 }
3289 }
3290
3291 static void wcaxx_dacs_disconnect(struct wcaxx *wc, int card)
3292 {
3293 struct wcaxx_module *const mod = &wc->mods[card];
3294 struct wcaxx_module *dacssrc;
3295
3296 if (mod->dacssrc <= -1)
3297 return;
3298
3299 dacssrc = &wc->mods[mod->dacssrc];
3300
3301 if (debug) {
3302 dev_info(&wc->xb.pdev->dev,
3303 "wcaxx_dacs_disconnect: restoring TX for %d and RX for %d\n",
3304 mod->dacssrc, card);
3305 }
3306
3307 /* restore TX (source card) */
3308 if (dacssrc->type == FXS) {
3309 wcaxx_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_LSB,
3310 (mod->dacssrc * 8) & 0xff);
3311 wcaxx_setreg(wc, dacssrc, PCM_XMIT_START_COUNT_MSB,
3312 (mod->dacssrc * 8) >> 8);
3313 } else if (dacssrc->type == FXO) {
3314 wcaxx_setreg(wc, mod, 34, (card * 8) & 0xff);
3315 wcaxx_setreg(wc, mod, 35, (card * 8) >> 8);
3316 } else {
3317 dev_warn(&wc->xb.pdev->dev,
3318 "WARNING: wcaxx_dacs_disconnect() called "
3319 "on unsupported modtype\n");
3320 }
3321
3322 /* restore RX (this card) */
3323 if (FXS == mod->type) {
3324 wcaxx_setreg(wc, mod, PCM_RCV_START_COUNT_LSB,
3325 (card * 8) & 0xff);
3326 wcaxx_setreg(wc, mod, PCM_RCV_START_COUNT_MSB,
3327 (card * 8) >> 8);
3328 } else if (FXO == mod->type) {
3329 wcaxx_setreg(wc, mod, 36, (card * 8) & 0xff);
3330 wcaxx_setreg(wc, mod, 37, (card * 8) >> 8);
3331 } else {
3332 dev_warn(&wc->xb.pdev->dev,
3333 "WARNING: wcaxx_dacs_disconnect() called "
3334 "on unsupported modtype\n");
3335 }
3336
3337 mod->dacssrc = -1;
3338 }
3339
3340 static int wcaxx_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
3341 {
3342 struct wcaxx *wc;
3343
3344 if (!nativebridge)
3345 return 0; /* should this return -1 since unsuccessful? */
3346
3347 wc = dst->pvt;
3348
3349 if (src) {
3350 wcaxx_dacs_connect(wc, src->chanpos - 1, dst->chanpos - 1);
3351 if (debug) {
3352 dev_info(&wc->xb.pdev->dev,
3353 "dacs connecct: %d -> %d!\n\n",
3354 src->chanpos, dst->chanpos);
3355 }
3356 } else {
3357 wcaxx_dacs_disconnect(wc, dst->chanpos - 1);
3358 if (debug) {
3359 dev_info(&wc->xb.pdev->dev,
3360 "dacs disconnect: %d!\n", dst->chanpos);
3361 }
3362 }
3363 return 0;
3364 }
3365
3366 /**
3367 * wcaxx_chanconfig - Called when the channels are being configured.
3368 *
3369 * Ensure that the card is completely ready to go before we allow the channels
3370 * to be completely configured. This is to allow lengthy initialization
3371 * actions to take place in background on driver load and ensure we're synced
3372 * up by the time dahdi_cfg is run.
3373 *
3374 */
3375 static int
3376 wcaxx_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
3377 {
3378 struct wcaxx *wc = chan->pvt;
3379 if ((file->f_flags & O_NONBLOCK) && !is_initialized(wc))
3380 return -EAGAIN;
3381 return 0;
3382 }
3383
3384 /*
3385 * wcaxx_assigned - Called when span is assigned.
3386 * @span: The span that is now assigned.
3387 *
3388 * This function is called by the core of DAHDI after the span number and
3389 * channel numbers have been assigned.
3390 *
3391 */
3392 static void wcaxx_assigned(struct dahdi_span *span)
3393 {
3394 struct dahdi_span *s;
3395 struct dahdi_device *ddev = span->parent;
3396 struct wcaxx *wc = NULL;
3397
3398 list_for_each_entry(s, &ddev->spans, device_node) {
3399 wc = container_of(s, struct wcaxx, span);
3400 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &s->flags))
3401 return;
3402 }
3403 }
3404
3405 static const struct dahdi_span_ops wcaxx_span_ops = {
3406 .owner = THIS_MODULE,
3407 .hooksig = wcaxx_hooksig,
3408 .open = wcaxx_open,
3409 .close = wcaxx_close,
3410 .ioctl = wcaxx_ioctl,
3411 .watchdog = wcaxx_watchdog,
3412 .chanconfig = wcaxx_chanconfig,
3413 .dacs = wcaxx_dacs,
3414 .assigned = wcaxx_assigned,
3415 .echocan_create = wcaxx_echocan_create,
3416 .echocan_name = wcaxx_echocan_name,
3417 };
3418
3419 static struct wcaxx_chan *
3420 wcaxx_init_chan(struct wcaxx *wc, struct dahdi_span *s, int channo)
3421 {
3422 struct wcaxx_chan *c;
3423
3424 c = kzalloc(sizeof(*c), GFP_KERNEL);
3425 if (!c)
3426 return NULL;
3427
3428 snprintf(c->chan.name, sizeof(c->chan.name), "WCTDM/%d/%d",
3429 wc->num, channo);
3430 c->chan.chanpos = channo+1;
3431 c->chan.span = s;
3432 c->chan.pvt = wc;
3433 c->timeslot = channo;
3434 return c;
3435 }
3436
3437 static void wcaxx_init_span(struct wcaxx *wc)
3438 {
3439 int x;
3440 struct wcaxx_chan *c;
3441 struct dahdi_echocan_state *ec[NUM_MODULES] = {NULL, };
3442
3443 /* DAHDI stuff */
3444 wc->span.offset = 0;
3445
3446 sprintf(wc->span.name, "WCTDM/%d", wc->num);
3447
3448 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1,
3449 "%s", wc->desc->name);
3450
3451 if (wc->companding == DAHDI_LAW_DEFAULT) {
3452 wc->span.deflaw = DAHDI_LAW_MULAW;
3453 } else if (wc->companding == DAHDI_LAW_ALAW) {
3454 /* Force everything to alaw */
3455 wc->span.deflaw = DAHDI_LAW_ALAW;
3456 } else {
3457 /* Auto set to ulaw */
3458 wc->span.deflaw = DAHDI_LAW_MULAW;
3459 }
3460
3461 wc->span.ops = &wcaxx_span_ops;
3462 wc->span.flags = DAHDI_FLAG_RBS;
3463 wc->span.spantype = SPANTYPE_ANALOG_MIXED;
3464
3465 wc->span.chans = kmalloc(sizeof(wc->span.chans[0]) * wc->desc->ports,
3466 GFP_KERNEL);
3467 if (!wc->span.chans)
3468 return;
3469
3470 /* allocate channels for the span */
3471 for (x = 0; x < wc->desc->ports; x++) {
3472 c = wcaxx_init_chan(wc, &wc->span, x);
3473 if (!c)
3474 return;
3475 wc->chans[x] = c;
3476 wc->span.chans[x] = &c->chan;
3477
3478 /* TODO: Should echocan state hide under VPM_ENABLED or does
3479 * software ec use it? */
3480 ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
3481 }
3482
3483 wc->span.channels = wc->desc->ports;
3484 memcpy(wc->ec, ec, sizeof(wc->ec));
3485 memset(ec, 0, sizeof(ec));
3486 }
3487
3488 /**
3489 * should_set_alaw() - Should be called after all the spans are initialized.
3490 *
3491 * Returns true if the module companding should be set to alaw, otherwise
3492 * false.
3493 */
3494 static bool should_set_alaw(const struct wcaxx *wc)
3495 {
3496 if (DAHDI_LAW_ALAW == wc->companding)
3497 return true;
3498 else
3499 return false;
3500 }
3501
3502 static void wcaxx_fixup_span(struct wcaxx *wc)
3503 {
3504 struct dahdi_span *s;
3505 int x, y;
3506
3507 y = 0;
3508 s = &wc->span;
3509
3510 for (x = 0; x < wc->desc->ports; x++) {
3511 struct wcaxx_module *const mod = &wc->mods[x];
3512 if (debug) {
3513 dev_info(&wc->xb.pdev->dev,
3514 "fixup_analog: x=%d, y=%d modtype=%d, "
3515 "s->chans[%d]=%p\n", x, y, mod->type,
3516 y, s->chans[y]);
3517 }
3518 if (mod->type == FXO) {
3519 int val;
3520 s->chans[y++]->sigcap = DAHDI_SIG_FXSKS |
3521 DAHDI_SIG_FXSLS | DAHDI_SIG_SF |
3522 DAHDI_SIG_CLEAR;
3523 val = should_set_alaw(wc) ? 0x20 : 0x28;
3524 #ifdef DEBUG
3525 val = (digitalloopback) ? 0x30 : val;
3526 #endif
3527 wcaxx_setreg(wc, mod, 33, val);
3528 wcaxx_voicedaa_set_ts(wc, mod, wc->chans[x]->timeslot);
3529 } else if (mod->type == FXS) {
3530
3531 /* NOTE: Digital loopback does not work on the FXS
3532 * modules in the same way since the data is still
3533 * companded by the ProSLIC and doesn't appear to have
3534 * perfect symetry. */
3535
3536 s->chans[y++]->sigcap = DAHDI_SIG_FXOKS |
3537 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
3538 DAHDI_SIG_SF | DAHDI_SIG_EM | DAHDI_SIG_CLEAR;
3539 wcaxx_setreg(wc, mod, 1,
3540 (should_set_alaw(wc) ? 0x20 : 0x28));
3541 wcaxx_proslic_set_ts(wc, mod, wc->chans[x]->timeslot);
3542 } else {
3543 s->chans[y++]->sigcap = 0;
3544 }
3545 }
3546 }
3547
3548 static bool wcaxx_init_fxs_port(struct wcaxx *wc, struct wcaxx_module *mod)
3549 {
3550 u8 readi;
3551 enum {UNKNOWN = 0, SANE = 1};
3552 int ret = wcaxx_init_proslic(wc, mod, 0, 0, UNKNOWN);
3553 if (!ret) {
3554 if (debug & DEBUG_CARD) {
3555 readi = wcaxx_getreg(wc, mod, LOOP_I_LIMIT);
3556 dev_info(&wc->xb.pdev->dev,
3557 "Proslic module %d loop current is %dmA\n",
3558 mod->card, ((readi*3) + 20));
3559 }
3560 return true;
3561 }
3562
3563 if (ret != -2) {
3564 /* Init with Manual Calibration */
3565 if (!wcaxx_init_proslic(wc, mod, 0, 1, SANE)) {
3566 if (debug & DEBUG_CARD) {
3567 readi = wcaxx_getreg(wc, mod, LOOP_I_LIMIT);
3568 dev_info(&wc->xb.pdev->dev,
3569 "Proslic module %d loop current is %dmA\n",
3570 mod->card, ((readi*3)+20));
3571 }
3572 } else {
3573 dev_notice(&wc->xb.pdev->dev,
3574 "Port %d: FAILED FXS (%s)\n", mod->card + 1,
3575 fxshonormode ? fxo_modes[_opermode].name :
3576 "FCC");
3577 }
3578 return true;
3579 }
3580 return false;
3581 }
3582
3583 static void wcaxx_reset_module(struct wcaxx *wc, struct wcaxx_module *mod)
3584 {
3585
3586 u32 reg_val = (1UL << (mod->spi->chip_select + 12));
3587 wcxb_gpio_clear(&wc->xb, reg_val);
3588 udelay(500);
3589 wcxb_gpio_set(&wc->xb, reg_val);
3590 msleep(250); /* TODO: What should this value be? */
3591 }
3592
3593 static bool check_for_single_fxs(struct wcaxx *wc, unsigned int port)
3594 {
3595 bool result;
3596 struct wcaxx_module *mod = &wc->mods[port];
3597
3598 mod->spi = get_spi_device_for_port(wc, mod->card, false);
3599 mod->subaddr = 0;
3600 wcaxx_reset_module(wc, mod);
3601 wcaxx_fxsinit(mod->spi);
3602 result = wcaxx_init_fxs_port(wc, mod);
3603 if (!result)
3604 mod->type = NONE;
3605
3606 /* It is currently unclear why this read is necessary for some of the
3607 * S100M modules to properly function. */
3608 wcaxx_getreg(wc, mod, 0x00);
3609 return result;
3610 }
3611
3612 static bool check_for_single_fxo(struct wcaxx *wc, unsigned int port)
3613 {
3614 bool result;
3615 struct wcaxx_module *mod = &wc->mods[port];
3616 mod->spi = get_spi_device_for_port(wc, mod->card, false);
3617 mod->subaddr = 0;
3618 wcaxx_reset_module(wc, mod);
3619 result = (wcaxx_init_voicedaa(wc, mod, 0, 0, 0) == 0);
3620 if (!result)
3621 mod->type = NONE;
3622 return result;
3623 }
3624
3625 static bool check_for_quad_fxs(struct wcaxx *wc, unsigned int base_port)
3626 {
3627 int port;
3628 int offset;
3629 struct wcaxx_module *mod = &wc->mods[base_port + 1];
3630
3631 /* Cannot have quad port modules on the 4 port base cards. */
3632 if (is_four_port(wc))
3633 return false;
3634
3635 /* We can assume that the base port has already been configured as an
3636 * FXS port if we're even in this function */
3637 mod->spi = get_spi_device_for_port(wc, mod->card, true);
3638 mod->subaddr = offset = 1;
3639 if (wcaxx_init_fxs_port(wc, mod)) {
3640 /* This must be a 4 port FXS module... */
3641 for (port = base_port + 2; port < base_port+4; ++port) {
3642 mod = &wc->mods[port];
3643 mod->spi = get_spi_device_for_port(wc, mod->card, true);
3644 mod->subaddr = ++offset;
3645 if (!wcaxx_init_fxs_port(wc, mod)) {
3646 /* This means that a quad-module failed to
3647 * setup ports 3 or 4? */
3648 dev_err(&wc->xb.pdev->dev,
3649 "Quad-FXS at base %d failed initialization.\n",
3650 base_port);
3651 goto error_exit;
3652 }
3653 }
3654 return true;
3655 }
3656 error_exit:
3657 for (port = base_port + 1; port < base_port + 4; ++port) {
3658 mod = &wc->mods[port];
3659 mod->type = NONE;
3660 }
3661 return false;
3662 }
3663
3664 static bool check_for_quad_fxo(struct wcaxx *wc, unsigned int base_port)
3665 {
3666 int port;
3667 int offset;
3668 struct wcaxx_module *mod = &wc->mods[base_port + 1];
3669
3670 /* Cannot have quad port modules on the 4 port base cards. */
3671 if (is_four_port(wc))
3672 return false;
3673
3674 /* We can assume that the base port has already been configured as an
3675 * FXO port if we're even in this function */
3676 mod->spi = get_spi_device_for_port(wc, mod->card, true);
3677 mod->subaddr = offset = 1;
3678 if (!wcaxx_init_voicedaa(wc, mod, 0, 0, 0)) {
3679 /* This must be a 4 port FXO module. */
3680 for (port = base_port + 2; port < base_port + 4; ++port) {
3681 mod = &wc->mods[port];
3682 mod->spi = get_spi_device_for_port(wc, mod->card, true);
3683 mod->subaddr = ++offset;
3684 if (wcaxx_init_voicedaa(wc, mod, 0, 0, 0)) {
3685 dev_err(&wc->xb.pdev->dev,
3686 "Quad-FXO at base %d failed initialization.\n",
3687 base_port);
3688 goto error_exit;
3689 }
3690 }
3691 return true;
3692 }
3693 error_exit:
3694 for (port = base_port + 1; port < base_port + 4; ++port) {
3695 mod = &wc->mods[port];
3696 mod->type = NONE;
3697 }
3698 return false;
3699 }
3700
3701 static void __wcaxx_identify_four_port_module_group(struct wcaxx *wc)
3702 {
3703 int i;
3704 for (i = 0; i < wc->desc->ports; i++) {
3705 if (!check_for_single_fxs(wc, i))
3706 check_for_single_fxo(wc, i);
3707 }
3708 return;
3709 }
3710
3711 static void
3712 __wcaxx_identify_module_group(struct wcaxx *wc, unsigned long base)
3713 {
3714 if (check_for_single_fxs(wc, base)) {
3715 if (check_for_quad_fxs(wc, base)) {
3716 /* S400M installed */
3717 return;
3718 } else if (check_for_single_fxs(wc, base + 1)) {
3719 /* Two S110M installed */
3720 return;
3721 } else if (check_for_single_fxo(wc, base + 1)) {
3722 /* 1 S110M 1 X100M */
3723 return;
3724 } else {
3725 /* 1 S110M 1 Empty */
3726 return;
3727 }
3728 } else if (check_for_single_fxo(wc, base)) {
3729 if (check_for_quad_fxo(wc, base)) {
3730 /* X400M installed */
3731 return;
3732 } else if (check_for_single_fxo(wc, base + 1)) {
3733 /* Two X100M installed */
3734 return;
3735 } else if (check_for_single_fxs(wc, base + 1)) {
3736 /* 1 X100M 1 S100M installed */
3737 return;
3738 } else {
3739 /* 1 X100M 1 Empty */
3740 return;
3741 }
3742 } else if (check_for_single_fxs(wc, base + 1)) {
3743 /* 1 Empty 1 S110M installed */
3744 return;
3745 } else if (check_for_single_fxo(wc, base + 1)) {
3746 /* 1 Empty 1 X100M installed */
3747 return;
3748 }
3749 /* No module */
3750 return;
3751 }
3752
3753 /**
3754 * wcaxx_print_moule_configuration - Print the configuration to the kernel log
3755 * @wc: The card we're interested in.
3756 *
3757 * This is to ensure that the module configuration from each card shows up
3758 * sequentially in the kernel log, as opposed to interleaved with one another.
3759 *
3760 */
3761 static void wcaxx_print_module_configuration(const struct wcaxx *const wc)
3762 {
3763 int i;
3764 static DEFINE_MUTEX(print);
3765
3766 mutex_lock(&print);
3767 for (i = 0; i < wc->mods_per_board; ++i) {
3768 const struct wcaxx_module *const mod = &wc->mods[i];
3769
3770 switch (mod->type) {
3771 case FXO:
3772 dev_info(&wc->xb.pdev->dev,
3773 "Port %d: Installed -- AUTO FXO (%s mode)\n",
3774 i + 1, fxo_modes[_opermode].name);
3775 break;
3776 case FXS:
3777 dev_info(&wc->xb.pdev->dev,
3778 "Port %d: Installed -- AUTO FXS/DPO\n", i + 1);
3779 break;
3780 case NONE:
3781 dev_info(&wc->xb.pdev->dev,
3782 "Port %d: Not installed\n", i + 1);
3783 break;
3784 }
3785 }
3786 mutex_unlock(&print);
3787 }
3788
3789 static void wcaxx_identify_modules(struct wcaxx *wc)
3790 {
3791 int x;
3792 unsigned long flags;
3793
3794 /* A8A/A8B - Reset the modules. */
3795 wcxb_gpio_clear(&wc->xb, 0xf000);
3796 msleep(50); /* TODO: what should these values be? */
3797 wcxb_gpio_set(&wc->xb, 0xf000);
3798 msleep(250); /* TODO: What should these values be? */
3799
3800 /* Place all units in the daisy chain mode of operation. This allows
3801 * multiple devices to share a chip select (like on the X400 and S400
3802 * modules) */
3803 for (x = 0; x < ARRAY_SIZE(wc->spi_devices); ++x)
3804 wcaxx_fxsinit(wc->spi_devices[x]);
3805
3806 spin_lock_irqsave(&wc->reglock, flags);
3807 wc->mods_per_board = wc->desc->ports;
3808 spin_unlock_irqrestore(&wc->reglock, flags);
3809
3810 BUG_ON(wc->desc->ports % 4);
3811
3812 if (is_four_port(wc)) {
3813 __wcaxx_identify_four_port_module_group(wc);
3814 } else {
3815 for (x = 0; x < wc->desc->ports/4; x++)
3816 __wcaxx_identify_module_group(wc, x*4);
3817 }
3818
3819 wcaxx_print_module_configuration(wc);
3820 }
3821
3822 static struct pci_driver wcaxx_driver;
3823
3824 static void wcaxx_back_out_gracefully(struct wcaxx *wc)
3825 {
3826 int i;
3827 unsigned long flags;
3828
3829 clear_bit(INITIALIZED, &wc->bit_flags);
3830 smp_mb__after_atomic();
3831
3832 /* Make sure we're not on the card list anymore. */
3833 mutex_lock(&card_list_lock);
3834 list_del(&wc->card_node);
3835 mutex_unlock(&card_list_lock);
3836
3837 wcxb_release(&wc->xb);
3838
3839 for (i = 0; i < wc->mods_per_board; i++) {
3840 struct wcaxx_module *const mod = &wc->mods[i];
3841 kfree(mod->mod_poll);
3842 mod->mod_poll = NULL;
3843 }
3844
3845 kfree(wc->span.chans);
3846 wc->span.chans = NULL;
3847
3848 spin_lock_irqsave(&wc->reglock, flags);
3849 for (i = 0; i < wc->span.channels; ++i) {
3850 kfree(wc->chans[i]);
3851 kfree(wc->ec[i]);
3852 wc->chans[i] = NULL;
3853 wc->ec[i] = NULL;
3854 }
3855 spin_unlock_irqrestore(&wc->reglock, flags);
3856
3857 for (i = 0; i < ARRAY_SIZE(wc->spi_devices); i++)
3858 wcxb_spi_device_destroy(wc->spi_devices[i]);
3859 wcxb_spi_master_destroy(wc->master);
3860
3861 kfree(wc->board_name);
3862 if (wc->ddev) {
3863 kfree(wc->ddev->devicetype);
3864 kfree(wc->ddev->location);
3865 kfree(wc->ddev->hardware_id);
3866 dahdi_free_device(wc->ddev);
3867 }
3868 kfree(wc);
3869 }
3870
3871 static void wcaxx_handle_error(struct wcxb *xb)
3872 {
3873 struct wcaxx *wc = container_of(xb, struct wcaxx, xb);
3874 wc->ddev->irqmisses++;
3875 }
3876
3877 static const struct wcxb_operations wcxb_operations = {
3878 .handle_receive = wcaxx_handle_receive,
3879 .handle_transmit = wcaxx_handle_transmit,
3880 .handle_error = wcaxx_handle_error,
3881 };
3882
3883 struct cmd_results {
3884 u8 results[8];
3885 };
3886
3887 static int wcaxx_check_firmware(struct wcaxx *wc)
3888 {
3889 char *filename;
3890 u32 firmware_version;
3891 const bool force_firmware = false;
3892 const unsigned int A4A_VERSION = 0x0a0017;
3893 const unsigned int A4B_VERSION = 0x0d001e;
3894 const unsigned int A8A_VERSION = 0x1d0017;
3895 const unsigned int A8B_VERSION = 0x1f001e;
3896
3897 if (wc->desc == &device_a8a) {
3898 firmware_version = A8A_VERSION;
3899 filename = "dahdi-fw-a8a.bin";
3900 } else if (wc->desc == &device_a8b) {
3901 firmware_version = A8B_VERSION;
3902 filename = "dahdi-fw-a8b.bin";
3903 } else if (wc->desc == &device_a4a) {
3904 firmware_version = A4A_VERSION;
3905 filename = "dahdi-fw-a4a.bin";
3906 } else if (wc->desc == &device_a4b) {
3907 firmware_version = A4B_VERSION;
3908 filename = "dahdi-fw-a4b.bin";
3909 } else {
3910 /* This is a bug in the driver code */
3911 WARN_ON(1);
3912 return 0;
3913 }
3914
3915 return wcxb_check_firmware(&wc->xb, firmware_version,
3916 filename, force_firmware, WCXB_RESET_NOW);
3917 }
3918
3919 static void wcaxx_check_sethook(struct wcaxx *wc, struct wcaxx_module *mod)
3920 {
3921 if (mod->sethook) {
3922 wcaxx_setreg(wc, mod, ((mod->sethook >> 8) & 0xff),
3923 mod->sethook & 0xff);
3924 mod->sethook = 0;
3925 }
3926 }
3927
3928 static void wcaxx_poll_fxs_complete(void *arg)
3929 {
3930 struct wcaxx_mod_poll *poll_fxs = arg;
3931 struct wcaxx *wc = poll_fxs->wc;
3932 struct wcaxx_module *const mod = poll_fxs->mod;
3933
3934 if (!is_initialized(wc)) {
3935 kfree(poll_fxs);
3936 return;
3937 }
3938
3939 mod->mod.fxs.hook_state_shadow = poll_fxs->buffer[2];
3940 mod->mod.fxs.linefeed_control_shadow = poll_fxs->buffer[5];
3941 wcaxx_isr_misc_fxs(poll_fxs->wc, poll_fxs->mod);
3942 memcpy(poll_fxs->buffer, poll_fxs->master_buffer,
3943 sizeof(poll_fxs->buffer));
3944 wcaxx_check_sethook(poll_fxs->wc, poll_fxs->mod);
3945 mod->mod_poll = poll_fxs;
3946 }
3947
3948 /**
3949 * wcaxx_start_poll_fxs - Starts the interrupt polling loop for FXS modules.
3950 *
3951 * To stop the polling loop, clear the initialized bit and then flush the
3952 * pending wcxb_spi messages.
3953 *
3954 */
3955 static int wcaxx_start_poll_fxs(struct wcaxx *wc, struct wcaxx_module *mod)
3956 {
3957 struct wcaxx_mod_poll *mod_poll = kzalloc(sizeof(*mod_poll),
3958 GFP_KERNEL);
3959 struct wcxb_spi_message *m = &mod_poll->m;
3960 struct wcxb_spi_transfer *t = &mod_poll->t;
3961
3962 WARN_ON(!is_initialized(wc));
3963 if (!mod_poll)
3964 return -ENOMEM;
3965
3966 memset(t, 0, sizeof(*t));
3967 wcxb_spi_message_init(m);
3968
3969 t->tx_buf = t->rx_buf = mod_poll->buffer;
3970 t->len = sizeof(mod_poll->buffer);
3971 wcxb_spi_message_add_tail(t, m);
3972 mod_poll->wc = wc;
3973 mod_poll->mod = mod;
3974
3975 mod_poll->master_buffer[0] = 1 << mod_poll->mod->subaddr;
3976 mod_poll->master_buffer[1] = (LOOP_STAT | 0x80) & 0xff;
3977 mod_poll->master_buffer[2] = 0;
3978
3979 mod_poll->master_buffer[3] = mod_poll->master_buffer[0];
3980 mod_poll->master_buffer[4] = (LINE_STATE | 0x80) & 0xff;
3981 mod_poll->master_buffer[5] = 0;
3982
3983 memcpy(mod_poll->buffer, mod_poll->master_buffer,
3984 sizeof(mod_poll->buffer));
3985
3986 m->arg = mod_poll;
3987 m->complete = &wcaxx_poll_fxs_complete;
3988 wcxb_spi_async(mod->spi, m);
3989 return 0;
3990 }
3991
3992 static void wcaxx_poll_fxo_complete(void *arg)
3993 {
3994 struct wcaxx_mod_poll *poll_fxo = arg;
3995 struct wcaxx *wc = poll_fxo->wc;
3996 struct wcaxx_module *const mod = poll_fxo->mod;
3997
3998 if (!is_initialized(wc)) {
3999 kfree(poll_fxo);
4000 return;
4001 }
4002
4003 mod->mod.fxo.hook_ring_shadow = poll_fxo->buffer[2];
4004 mod->mod.fxo.line_voltage_status = poll_fxo->buffer[5];
4005 wcaxx_voicedaa_check_hook(poll_fxo->wc, poll_fxo->mod);
4006 memcpy(poll_fxo->buffer, poll_fxo->master_buffer,
4007 sizeof(poll_fxo->buffer));
4008 wcaxx_check_sethook(poll_fxo->wc, poll_fxo->mod);
4009 mod->mod_poll = poll_fxo;
4010 }
4011
4012 /**
4013 * wcaxx_start_poll_fxo - Starts the interrupt polling loop for FXS modules.
4014 *
4015 * To stop the polling loop, clear the initialized bit and then flush the
4016 * pending wcxb_spi messages.
4017 *
4018 */
4019 static int wcaxx_start_poll_fxo(struct wcaxx *wc, struct wcaxx_module *mod)
4020 {
4021 static const int ADDRS[4] = {0x00, 0x08, 0x04, 0x0c};
4022 struct wcaxx_mod_poll *poll_fxo = kzalloc(sizeof(*poll_fxo),
4023 GFP_KERNEL);
4024 struct wcxb_spi_message *m = &poll_fxo->m;
4025 struct wcxb_spi_transfer *t = &poll_fxo->t;
4026
4027 WARN_ON(!is_initialized(wc));
4028 if (!poll_fxo)
4029 return -ENOMEM;
4030
4031 memset(t, 0, sizeof(*t));
4032 wcxb_spi_message_init(m);
4033
4034 t->tx_buf = t->rx_buf = poll_fxo->buffer;
4035 t->len = sizeof(poll_fxo->buffer);
4036 wcxb_spi_message_add_tail(t, m);
4037 poll_fxo->wc = wc;
4038 poll_fxo->mod = mod;
4039
4040 poll_fxo->master_buffer[0] = 0x60 | ADDRS[poll_fxo->mod->subaddr];
4041 poll_fxo->master_buffer[1] = 5 & 0x7f; /* Hook / Ring State */
4042 poll_fxo->master_buffer[2] = 0;
4043
4044 poll_fxo->master_buffer[3] = poll_fxo->master_buffer[0];
4045 poll_fxo->master_buffer[4] = 29 & 0x7f; /* Battery */
4046 poll_fxo->master_buffer[5] = 0;
4047
4048 memcpy(poll_fxo->buffer, poll_fxo->master_buffer,
4049 sizeof(poll_fxo->buffer));
4050
4051 m->arg = poll_fxo;
4052 m->complete = &wcaxx_poll_fxo_complete;
4053 wcxb_spi_async(mod->spi, m);
4054 return 0;
4055 }
4056
4057 /**
4058 * wcaxx_read_serial - Returns the serial number of the board.
4059 * @wc: The board whos serial number we are reading.
4060 *
4061 * The buffer returned is dynamically allocated and must be kfree'd by the
4062 * caller. If memory could not be allocated, NULL is returned.
4063 *
4064 * Must be called in process context.
4065 *
4066 */
4067 static char *wcaxx_read_serial(struct wcaxx *wc)
4068 {
4069 int i;
4070 static const int MAX_SERIAL = 20*5;
4071 const unsigned int SERIAL_ADDRESS = 0x1f0000;
4072 unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
4073 struct wcxb const *xb = &wc->xb;
4074 struct wcxb_spi_master *flash_spi_master = NULL;
4075 struct wcxb_spi_device *flash_spi_device = NULL;
4076 const unsigned int FLASH_SPI_BASE = 0x200;
4077
4078 if (!serial)
4079 return NULL;
4080
4081 flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
4082 xb->membase + FLASH_SPI_BASE,
4083 false);
4084 if (!flash_spi_master)
4085 return NULL;
4086
4087 flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
4088 if (!flash_spi_device)
4089 goto error_exit;
4090
4091 wcxb_flash_read(flash_spi_device, SERIAL_ADDRESS,
4092 serial, MAX_SERIAL);
4093
4094 for (i = 0; i < MAX_SERIAL; ++i) {
4095 if ((serial[i] < 0x20) || (serial[i] > 0x7e)) {
4096 serial[i] = '\0';
4097 break;
4098 }
4099 }
4100
4101 if (!i) {
4102 kfree(serial);
4103 serial = NULL;
4104 } else {
4105 /* Limit the size of the buffer to just what is needed to
4106 * actually hold the serial number. */
4107 unsigned char *new_serial;
4108 new_serial = kasprintf(GFP_KERNEL, "%s", serial);
4109 kfree(serial);
4110 serial = new_serial;
4111 }
4112
4113 error_exit:
4114 wcxb_spi_device_destroy(flash_spi_device);
4115 wcxb_spi_master_destroy(flash_spi_master);
4116 return serial;
4117 }
4118
4119 static void wcaxx_start_module_polling(struct wcaxx *wc)
4120 {
4121 int x;
4122 WARN_ON(!is_initialized(wc));
4123 for (x = 0; x < wc->mods_per_board; x++) {
4124 struct wcaxx_module *const mod = &wc->mods[x];
4125 switch (mod->type) {
4126 case FXO:
4127 wcaxx_start_poll_fxo(wc, mod);
4128 break;
4129 case FXS:
4130 wcaxx_start_poll_fxs(wc, mod);
4131 break;
4132 case NONE:
4133 break;
4134 }
4135 }
4136 wc->module_poll_time = wc->framecount + MODULE_POLL_TIME_MS;
4137 }
4138
4139 /**
4140 * t43x_assign_num - Assign wc->num a unique value and place on card_list
4141 *
4142 */
4143 static void wcaxx_assign_num(struct wcaxx *wc)
4144 {
4145 mutex_lock(&card_list_lock);
4146 if (list_empty(&card_list)) {
4147 wc->num = 0;
4148 list_add(&wc->card_node, &card_list);
4149 } else {
4150 struct wcaxx *cur;
4151 struct list_head *insert_pos;
4152 int new_num = 0;
4153
4154 insert_pos = &card_list;
4155 list_for_each_entry(cur, &card_list, card_node) {
4156 if (new_num != cur->num)
4157 break;
4158 new_num++;
4159 insert_pos = &cur->card_node;
4160 }
4161
4162 wc->num = new_num;
4163 list_add_tail(&wc->card_node, insert_pos);
4164 }
4165 mutex_unlock(&card_list_lock);
4166 }
4167
4168 #ifdef USE_ASYNC_INIT
4169 struct async_data {
4170 struct pci_dev *pdev;
4171 const struct pci_device_id *ent;
4172 };
4173 static int __devinit
4174 __wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent,
4175 async_cookie_t cookie)
4176 #else
4177 static int __devinit
4178 __wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4179 #endif
4180 {
4181 struct wcaxx *wc;
4182 int i, ret;
4183 int curchan;
4184
4185 neonmwi_offlimit_cycles = neonmwi_offlimit / MS_PER_HOOKCHECK;
4186
4187 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
4188 if (!wc)
4189 return -ENOMEM;
4190
4191 wcaxx_assign_num(wc);
4192
4193 wc->desc = (struct _device_desc *)ent->driver_data;
4194
4195 spin_lock_init(&wc->reglock);
4196
4197 wc->board_name = kasprintf(GFP_KERNEL, "%s%d",
4198 wcaxx_driver.name, wc->num);
4199 if (!wc->board_name) {
4200 wcaxx_back_out_gracefully(wc);
4201 return -ENOMEM;
4202 }
4203
4204 #ifdef CONFIG_VOICEBUS_DISABLE_ASPM
4205 if (is_pcie(wc)) {
4206 pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
4207 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
4208 };
4209 #endif
4210
4211 pci_set_drvdata(pdev, wc);
4212 wc->xb.ops = &wcxb_operations;
4213 wc->xb.pdev = pdev;
4214 wc->xb.debug = &debug;
4215
4216 ret = wcxb_init(&wc->xb, wc->board_name, int_mode);
4217 if (ret) {
4218 wcaxx_back_out_gracefully(wc);
4219 return ret;
4220 }
4221
4222 wcxb_set_minlatency(&wc->xb, latency);
4223 wcxb_set_maxlatency(&wc->xb, max_latency);
4224
4225 ret = wcaxx_check_firmware(wc);
4226 if (ret) {
4227 wcaxx_back_out_gracefully(wc);
4228 return ret;
4229 }
4230
4231 wcxb_lock_latency(&wc->xb);
4232
4233 wc->mods_per_board = NUM_MODULES;
4234
4235 if (alawoverride) {
4236 companding = "alaw";
4237 dev_info(&wc->xb.pdev->dev,
4238 "The module parameter alawoverride has been deprecated. Please use the parameter companding=alaw instead");
4239 }
4240
4241 if (!strcasecmp(companding, "alaw"))
4242 /* Force this card's companding to alaw */
4243 wc->companding = DAHDI_LAW_ALAW;
4244 else if (!strcasecmp(companding, "ulaw"))
4245 /* Force this card's companding to ulaw */
4246 wc->companding = DAHDI_LAW_MULAW;
4247 else
4248 /* Auto detect this card's companding */
4249 wc->companding = DAHDI_LAW_DEFAULT;
4250
4251 wc->master = wcxb_spi_master_create(&pdev->dev,
4252 wc->xb.membase + 0x280, true);
4253 for (i = 0; i < ARRAY_SIZE(wc->spi_devices); i++)
4254 wc->spi_devices[i] = wcxb_spi_device_create(wc->master, 3-i);
4255
4256 for (i = 0; i < ARRAY_SIZE(wc->mods); i++) {
4257 struct wcaxx_module *const mod = &wc->mods[i];
4258 mod->dacssrc = -1;
4259 mod->card = i;
4260 mod->spi = NULL;
4261 mod->subaddr = 0;
4262 mod->type = NONE;
4263 }
4264
4265 ret = wcaxx_vpm_init(wc);
4266 if (!ret)
4267 wcxb_enable_echocan(&wc->xb);
4268
4269 /* Now track down what modules are installed */
4270 wcaxx_identify_modules(wc);
4271
4272 /* Start the hardware processing. */
4273 if (wcxb_start(&wc->xb)) {
4274 WARN_ON(1);
4275 return -EIO;
4276 }
4277
4278 if (fatal_signal_pending(current)) {
4279 wcaxx_back_out_gracefully(wc);
4280 return -EINTR;
4281 }
4282
4283 curchan = 0;
4284 wcaxx_init_span(wc);
4285 wcaxx_fixup_span(wc);
4286 curchan += wc->desc->ports;
4287
4288 #ifdef USE_ASYNC_INIT
4289 async_synchronize_cookie(cookie);
4290 #endif
4291 wc->ddev = dahdi_create_device();
4292 if (!wc->ddev) {
4293 wcaxx_back_out_gracefully(wc);
4294 return -ENOMEM;
4295 }
4296 wc->ddev->manufacturer = "Digium";
4297 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
4298 pdev->bus->number,
4299 PCI_SLOT(pdev->devfn) + 1);
4300 if (!wc->ddev->location) {
4301 wcaxx_back_out_gracefully(wc);
4302 return -ENOMEM;
4303 }
4304
4305 if (wc->vpm)
4306 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s (%s)",
4307 wc->desc->name, "VPMOCT032");
4308 else
4309 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s",
4310 wc->desc->name);
4311
4312 if (!wc->ddev->devicetype) {
4313 wcaxx_back_out_gracefully(wc);
4314 return -ENOMEM;
4315 }
4316
4317 wc->ddev->hardware_id = wcaxx_read_serial(wc);
4318
4319 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
4320
4321 if (dahdi_register_device(wc->ddev, &wc->xb.pdev->dev)) {
4322 dev_notice(&wc->xb.pdev->dev, "Unable to register device with DAHDI\n");
4323 wcaxx_back_out_gracefully(wc);
4324 return -1;
4325 }
4326
4327 dev_info(&wc->xb.pdev->dev, "Found a %s (SN: %s)\n",
4328 wc->desc->name, wc->ddev->hardware_id);
4329
4330 set_bit(INITIALIZED, &wc->bit_flags);
4331 wcaxx_start_module_polling(wc);
4332 wcxb_unlock_latency(&wc->xb);
4333 return 0;
4334 }
4335
4336 #ifdef USE_ASYNC_INIT
4337 static __devinit void
4338 wcaxx_init_one_async(void *data, async_cookie_t cookie)
4339 {
4340 struct async_data *dat = data;
4341 __wcaxx_init_one(dat->pdev, dat->ent, cookie);
4342 kfree(dat);
4343 }
4344
4345 static int __devinit
4346 wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4347 {
4348 struct async_data *dat;
4349
4350 dat = kmalloc(sizeof(*dat), GFP_KERNEL);
4351 /* If we can't allocate the memory for the async_data, odds are we won't
4352 * be able to initialize the device either, but let's try synchronously
4353 * anyway... */
4354 if (!dat)
4355 return __wcaxx_init_one(pdev, ent, 0);
4356
4357 dat->pdev = pdev;
4358 dat->ent = ent;
4359 async_schedule(wcaxx_init_one_async, dat);
4360 return 0;
4361 }
4362 #else
4363 static int __devinit
4364 wcaxx_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4365 {
4366 return __wcaxx_init_one(pdev, ent);
4367 }
4368 #endif
4369
4370 static void wcaxx_release(struct wcaxx *wc)
4371 {
4372 if (is_initialized(wc))
4373 dahdi_unregister_device(wc->ddev);
4374
4375 wcaxx_back_out_gracefully(wc);
4376 }
4377
4378 static void __devexit wcaxx_remove_one(struct pci_dev *pdev)
4379 {
4380 struct wcaxx *wc = pci_get_drvdata(pdev);
4381
4382 if (!wc)
4383 return;
4384
4385 dev_info(&wc->xb.pdev->dev, "Removing a %s.\n", wc->desc->name);
4386
4387 flush_scheduled_work();
4388 wcxb_stop(&wc->xb);
4389
4390 if (wc->vpm)
4391 release_vpm450m(wc->vpm);
4392 wc->vpm = NULL;
4393
4394 wcaxx_release(wc);
4395 }
4396
4397 static DEFINE_PCI_DEVICE_TABLE(wcaxx_pci_tbl) = {
4398 { 0xd161, 0x800d, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
4399 (unsigned long) &device_a8b
4400 },
4401 { 0xd161, 0x800c, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
4402 (unsigned long) &device_a8a
4403 },
4404 { 0xd161, 0x8010, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
4405 (unsigned long) &device_a4b
4406 },
4407 { 0xd161, 0x800f, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
4408 (unsigned long) &device_a4a
4409 },
4410 { 0 }
4411 };
4412
4413 MODULE_DEVICE_TABLE(pci, wcaxx_pci_tbl);
4414
4415 static void wcaxx_shutdown(struct pci_dev *pdev)
4416 {
4417 struct wcaxx *wc = pci_get_drvdata(pdev);
4418 wcxb_stop(&wc->xb);
4419 }
4420
4421 static int wcaxx_suspend(struct pci_dev *pdev, pm_message_t state)
4422 {
4423 return -ENOSYS;
4424 }
4425
4426 static struct pci_driver wcaxx_driver = {
4427 .name = "wcaxx",
4428 .probe = wcaxx_init_one,
4429 .remove = __devexit_p(wcaxx_remove_one),
4430 .shutdown = wcaxx_shutdown,
4431 .suspend = wcaxx_suspend,
4432 .id_table = wcaxx_pci_tbl,
4433 };
4434
4435 static int __init wcaxx_init(void)
4436 {
4437 int res;
4438 int x;
4439
4440 for (x = 0; x < ARRAY_SIZE(fxo_modes); x++) {
4441 if (!strcmp(fxo_modes[x].name, opermode))
4442 break;
4443 }
4444 if (x < ARRAY_SIZE(fxo_modes)) {
4445 _opermode = x;
4446 } else {
4447 pr_notice("Invalid/unknown operating mode '%s' specified. Please choose one of:\n",
4448 opermode);
4449 for (x = 0; x < ARRAY_SIZE(fxo_modes); x++)
4450 pr_notice(" %s\n", fxo_modes[x].name);
4451 pr_notice("Note this option is CASE SENSITIVE!\n");
4452 return -ENODEV;
4453 }
4454
4455 if (!strcmp(opermode, "AUSTRALIA")) {
4456 boostringer = 1;
4457 fxshonormode = 1;
4458 }
4459
4460 if (-1 == fastpickup) {
4461 if (!strcmp(opermode, "JAPAN"))
4462 fastpickup = 1;
4463 else
4464 fastpickup = 0;
4465 }
4466
4467 /* for the voicedaa_check_hook defaults, if the user has not
4468 * overridden them by specifying them as module parameters, then get
4469 * the values from the selected operating mode */
4470 if (!battdebounce)
4471 battdebounce = fxo_modes[_opermode].battdebounce;
4472 if (!battalarm)
4473 battalarm = fxo_modes[_opermode].battalarm;
4474 if (!battthresh)
4475 battthresh = fxo_modes[_opermode].battthresh;
4476
4477 res = dahdi_pci_module(&wcaxx_driver);
4478 if (res)
4479 return -ENODEV;
4480
4481 #ifdef USE_ASYNC_INIT
4482 async_synchronize_full();
4483 #endif
4484 return 0;
4485 }
4486
4487 static void __exit wcaxx_cleanup(void)
4488 {
4489 pci_unregister_driver(&wcaxx_driver);
4490 }
4491
4492
4493 module_param(debug, int, 0600);
4494 module_param(int_mode, int, 0400);
4495 MODULE_PARM_DESC(int_mode,
4496 "0 = Use MSI interrupt if available. 1 = Legacy interrupt only.\n");
4497 module_param(fastpickup, int, 0400);
4498 MODULE_PARM_DESC(fastpickup,
4499 "Set to 1 to shorten the calibration delay when taking an FXO port off "
4500 "hook. This can be required for Type-II CID. If -1 the calibration "
4501 "delay will depend on the current opermode.\n");
4502 module_param(fxovoltage, int, 0600);
4503 module_param(loopcurrent, int, 0600);
4504 module_param(reversepolarity, int, 0600);
4505 #ifdef DEBUG
4506 module_param(robust, int, 0600);
4507 module_param(digitalloopback, int, 0400);
4508 MODULE_PARM_DESC(digitalloopback,
4509 "Set to 1 to place FXO modules into loopback mode for troubleshooting.");
4510 #endif
4511 module_param(opermode, charp, 0600);
4512 module_param(lowpower, int, 0600);
4513 module_param(boostringer, int, 0600);
4514 module_param(fastringer, int, 0600);
4515 module_param(fxshonormode, int, 0600);
4516 module_param(battdebounce, uint, 0600);
4517 module_param(battalarm, uint, 0600);
4518 module_param(battthresh, uint, 0600);
4519 module_param(nativebridge, int, 0600);
4520 module_param(fxotxgain, int, 0600);
4521 module_param(fxorxgain, int, 0600);
4522 module_param(fxstxgain, int, 0600);
4523 module_param(fxsrxgain, int, 0600);
4524 module_param(ringdebounce, int, 0600);
4525 module_param(latency, int, 0400);
4526 module_param(max_latency, int, 0400);
4527 module_param(neonmwi_monitor, int, 0600);
4528 module_param(neonmwi_level, int, 0600);
4529 module_param(neonmwi_envelope, int, 0600);
4530 module_param(neonmwi_offlimit, int, 0600);
4531 module_param(vpmsupport, int, 0400);
4532
4533 module_param(forceload, int, 0600);
4534 MODULE_PARM_DESC(forceload,
4535 "Set to 1 in order to force an FPGA reload after power on.");
4536
4537 module_param(companding, charp, 0400);
4538 MODULE_PARM_DESC(companding,
4539 "Change the companding to \"auto\" or \"alaw\" or \"ulaw\". Auto "
4540 "(default) will set everything to ulaw unless a BRI module is "
4541 "installed. It will use alaw in that case.");
4542
4543 MODULE_DESCRIPTION("A4A,A4B,A8A,A8B Driver for Analog Telephony Cards");
4544 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
4545 MODULE_LICENSE("GPL v2");
4546
4547 module_init(wcaxx_init);
4548 module_exit(wcaxx_cleanup);