Debian Codegraph @

drivers/dahdi/wcaxx-base.c

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